Scope a collection (single physical store)
For single-table/collection backends (e.g., PouchDB, Local Storage), scope each collection so queries only return documents for that logical collection.
Quick Navigation
Steps
- Add a tracked computed discriminator to your schema that stores the logical collection name (for example,
documentType). - Apply a global scope when creating the collection so all queries are constrained to that discriminator.
import { s } from "@routier/core/schema";
import { DataStore } from "@routier/datastore";
// Define a discriminator that tags each record with its logical collection name
export const productsSchema = s
.define("products", {
id: s.string().key().identity(),
name: s.string(),
})
.modify((x) => ({
// Persist the logical collection name for single-table/collection backends
documentType: x.computed((_, collectionName) => collectionName).tracked(),
}))
.compile();
// Apply a global scope so all queries are constrained to this collection
export class AppDataStore extends DataStore {
products = this.collection(productsSchema)
.scope(([e, p]) => e.documentType === p.collectionName, { ...productsSchema })
.create();
}Why
This prevents cross-type collisions when multiple entity types share a single physical table/collection.
Related
- Concepts: Data Collections
- Integration: PouchDB Plugin