Memory Collections
Memory collections provide fast, in-memory data storage for your Routier application.
Overview
Memory collections are the fastest storage option in Routier, storing all data in RAM for instant access. They’re perfect for:
- Development and testing
- Temporary data storage
- High-performance applications
- Offline-first applications with sync capabilities
Creating Memory Collections
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();Performance Characteristics
Advantages
- Instant access - No I/O delays
- High throughput - Can handle thousands of operations per second
- Low latency - Sub-millisecond response times
- No serialization overhead - Data stays in memory
Limitations
- Memory usage - All data must fit in RAM
- No persistence - Data is lost when application restarts
- No sharing - Data is isolated to the current process
Use Cases
Development and Testing
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();High-Performance Applications
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();Offline-First with Sync
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();Next Steps
- Change Tracking - Understanding how changes are tracked
- Entity Management - Managing entities in collections
- Memory Plugin - Detailed plugin documentation