techfort / LokiJS

javascript embeddable / in-memory database
http:/techfort.github.io/LokiJS
MIT License
6.73k stars 482 forks source link

No way of persist new collections #841

Closed Vancete closed 4 years ago

Vancete commented 4 years ago

Hi there,

I'm getting in troubles persisting collections. The first time I initialize de db it works correctly, but if I try to add new collections on subsequents initializations, it never get saved to files.

Any idea on this? The collections are marked with autoupdate.

Thanks in advance

sittingbool commented 4 years ago

Hi, could you describe your setup a bit ore detailed? Maybe show some code snippets? I just checked the latest version against one of my applications using persistence. It worked. Which persistence system are you using? Disk persisting? It would really help to have more details and code ;)

Vancete commented 4 years ago

Sure, first time:

`let loki = {}

let adapter = new lfsa() loki.db = new lokijs('socks.db', { adapter : adapter, autoload: true, autoloadCallback : loki.databaseInitialize, autosave: true, persistenceMethod: 'adapter', autosaveInterval: 4000 })

loki.databaseInitialize = function() {

let users = loki.db.getCollection('users')

if (users === null) {
  loki.db.addCollection('users', { indices: ['user'], unique: ['user'], autoupdate: true })
}

}`

Then I add a new collection, it won't be saved ever. `let loki = {}

let adapter = new lfsa() loki.db = new lokijs('socks.db', { adapter : adapter, autoload: true, autoloadCallback : loki.databaseInitialize, autosave: true, persistenceMethod: 'adapter', autosaveInterval: 4000 })

loki.databaseInitialize = function() {

let users = loki.db.getCollection('users')
let roles = loki.db.getCollection('roles')

if (users === null) {
  loki.db.addCollection('users', { indices: ['user'], unique: ['user'], autoupdate: true })
}

if (roles === null) {
  loki.db.addCollection('roles', { indices: ['name'], unique: ['name'], autoupdate: true })
  roles = loki.db.getCollection('roles')
  roles.insert({ name: 'admin', description: 'Highest permission on the system', removable: false })
}

}`

sittingbool commented 4 years ago

I don't really find it in the documentation but I believe if you are using persistenceMethod: 'adapter' and adapter: adapter then you are responsible to provide the logic of saving your file in that given adapter. https://github.com/techfort/LokiJS/wiki/LokiJS-persistence-and-adapters. What happens if you use it without those two lines?

Vancete commented 4 years ago

Sorry, didn't provided that part of code, but was this:

const lfsa = require('lokijs/src/loki-fs-structured-adapter.js') let adapter = new lfsa()

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.