-
import { uuidv7 } from 'uuidv7'
import { PersistentCollection, Collection, createFilesystemAdapter, createIndex } from 'signaldb'
const users = new Collection({
persistence: createFilesystemAdapter('./users.json'),
indices: [createIndex('id')],
})
users.on('persistence.error', (...argv) => {
console.log('error:', argv)
})
users.on('added', (...argv) => {
console.log('added:', argv)
})
users.insert({id: uuidv7(), name: 'qwerty', age: 1})
users.insert({id: uuidv7(), name: 'qwerty', age: 2})
users.insert({id: uuidv7(), name: 'qwerty', age: 3}) how do I save a collection? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
The save itself is done automatically in the background. The |
Beta Was this translation helpful? Give feedback.
-
Please run my code and check it. |
Beta Was this translation helpful? Give feedback.
-
The file is created when adding await setTimeout(1000). import { uuidv7 } from 'uuidv7'
import { PersistentCollection, Collection, createFilesystemAdapter, createIndex } from 'signaldb'
import { setTimeout } from 'node:timers/promises'
const users = new Collection({
persistence: createFilesystemAdapter('./users.json'),
indices: [createIndex('id')],
})
users.on('persistence.error', (...argv) => {
console.log('error:', argv)
})
users.on('added', (...argv) => {
console.log('added:', argv)
})
users.insert({id: uuidv7(), name: 'qwerty', age: 1})
users.insert({id: uuidv7(), name: 'qwerty', age: 2})
users.insert({id: uuidv7(), name: 'qwerty', age: 3})
await setTimeout(1000)
process.exit() |
Beta Was this translation helpful? Give feedback.
-
Working version import { uuidv7 } from 'uuidv7'
import { PersistentCollection, Collection, createFilesystemAdapter, createIndex } from 'signaldb'
import { setTimeout } from 'node:timers/promises'
const users = new Collection({
persistence: createFilesystemAdapter('./users.json'),
indices: [createIndex('id')],
})
users.on('persistence.error', (e) => console.log('error:', e))
let flag = false
users.once('persistence.init', () => flag = true)
users.on('added', (data) => console.log('added:', data))
while (true) {
await setTimeout(1000)
if (flag) break
}
users.insert({id: uuidv7(), name: 'qwerty', age: 1})
users.insert({id: uuidv7(), name: 'qwerty', age: 2})
users.insert({id: uuidv7(), name: 'qwerty', age: 3})
const cursor = users.find({})
console.log(cursor.fetch())
await setTimeout(1000)
process.exit() |
Beta Was this translation helpful? Give feedback.
-
v0.8.3 |
Beta Was this translation helpful? Give feedback.
Please try again with version
v0.8.3
. It should be also no more be necessary to wait untilpersistence.init
is emitted