Basic Example
This guide shows a complete working example of using Routier for a simple user management system.
Complete Example
import { DataStore } from "@routier/datastore";
import { s } from "@routier/core/schema";
import { MemoryPlugin } from "@routier/memory-plugin";
const userSchema = s
.define("users", {
id: s.string().key().identity(),
email: s.string().distinct(),
name: s.string(),
createdAt: s.date().default(() => new Date()),
})
.compile();
class Ctx extends DataStore {
users = this.collection(userSchema).create();
constructor() {
super(new MemoryPlugin("app"));
}
}
const ctx = new Ctx();
await ctx.users.addAsync({ name: "Ada", email: "[email protected]" });
await ctx.saveChangesAsync();What This Example Shows
- Schema definition with various property types
- Context class extending DataStore
- Collection creation and data addition
- Change tracking and saving
- Querying with filters and sorting
- Async operations throughout
Next Steps
- Configuration - Advanced configuration options
- Schema Guide - Detailed schema creation
- Query Guide - Advanced querying techniques