Skip to content

Collection

Lucas A. Ouverney edited this page Jan 16, 2025 · 4 revisions

Collection

Collections in Jason provide a simple CRUD API with built-in caching and optional schema validation.

Create

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"
})

Features:

  • Automatic UUID generation for the id field if not provided
  • Optional schema validation (when schema is provided to db.collection)
  • Returns the created document
  • Creates _metadata.json if metadata tracking is enabled

Read

Reading documents utilizes the built-in caching system for improved performance.

const urser = await userCollection.read('id1');

Features:

  • 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

Schema Validation

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
})

Cache Behavior

The read operation:

  • Checks the internal cache first
  • If not found in cache, searches the collection file
  • Returns null if document isn't found in either location

Clone this wiki locally