-
Do you have any examples on how to implement multiple collections? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Implement multiple collections with rxdb should be really straight forward. You just have to make sure that each signaldb collection uses an own persistence adapter with a dedicated rxdb collection. async function getCollectionByName(name: string) {
// return the cached rxdb collection object for the given name or create it if it does not exist
}
function createPersistenceAdapterForRxdbCollection(collectionName: string) {
const persistence: PersistenceAdapter<{ id: string, text: string, completed: boolean }, string> = {
register: async (onChange) => {
const collection = await getCollectionByName(collectionName)
collection.postInsert(() => onChange(), false)
collection.postRemove(() => onChange(), false)
collection.postSave(() => onChange(), false)
},
save: async (_items, changes) => {
const collection = await getCollectionByName(collectionName)
await Promise.all([
...changes.added.map(item => collection.insert(item)),
...changes.modified.map(async (item) => {
const doc = await collection.findOne({ selector: { id: item.id } }).exec()
if (doc) await doc.update({ $set: item })
}),
...changes.removed.map(async (item) => {
const doc = await collection.findOne({ selector: { id: item.id } }).exec()
if (doc) await doc.remove()
}),
])
},
load: async () => {
const collection = await getCollectionByName(collectionName)
const items = await collection.find().exec()
return { items: items.map(item => item.toMutableJSON()) }
},
}
return persistence
} based on the example code |
Beta Was this translation helpful? Give feedback.
-
I encountered an error when attempting to create a collection following the provided example in this repo. by the way i'm using meteor js |
Beta Was this translation helpful? Give feedback.
Implement multiple collections with rxdb should be really straight forward. You just have to make sure that each signaldb collection uses an own persistence adapter with a dedicated rxdb collection.
One solution would be to create a factory function to create persistence adapters for different rxdb collections.