-
-
Notifications
You must be signed in to change notification settings - Fork 7
Collection
Lucas A. Ouverney edited this page Jan 16, 2025
·
4 revisions
Collections in Jason provide a simple CRUD API with built-in caching and optional schema validation.
After setting up your collection (see Wiki Home), you can start storing documents. The create operation ensures the collection file exists, creating it if necessary.
await userCollection.create({
name: "John Smith",
email: "john@example.com"
})- Automatic UUID generation for the
idfield if not provided - Optional schema validation (when schema is provided to
db.collection) - Returns the created document
- Creates
_metadata.jsonif metadata tracking is enabled
Reading documents utilizes the built-in caching system for improved performance.
const urser = await userCollection.read('id1');- Returns cached document if available
- Falls back to collection file if not in cache
- Returns null if document doesn't exist
- Type-safe return values based on your interfaces
If you provided a schema during collection creation, the create operation validates your input:
const userCollection = await db.collection('user', {
schema: userSchema
})
// This will throw an error if it doesn't match the schema
await userCollection.create({
name: "John Smith",
email: "invalid-email" // throws validation error
})The read operation:
- Checks the internal cache first
- If not found in cache, searches the collection file
- Returns
nullif document isn't found in either location