Memory Plugin
The Memory Plugin provides fast, in-memory data storage for your Routier application.
Quick Navigation
- Overview
- Installation
- Basic Usage
- Configuration
- Performance Characteristics
- Use Cases
- API Reference
- Next Steps
Overview
The Memory Plugin is the fastest storage option in Routier, storing all data in RAM for instant access. It's perfect for development, testing, and high-performance applications.
Installation
npm install @routier/memory-pluginBasic Usage
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).proxy().create();
constructor() {
super(new MemoryPlugin("app"));
}
}
const ctx = new Ctx();
await ctx.users.addAsync({ name: "Ada", email: "[email protected]" });
await ctx.saveChangesAsync();Configuration
Constructor Parameters
import { MemoryPlugin } from "@routier/memory-plugin";
const memoryPlugin = new MemoryPlugin(
"my-app" // Database name (required)
);Database Name
The database name is used to namespace your data and should be unique within your application:
import { DataStore } from "@routier/datastore";
import { MemoryPlugin } from "@routier/memory-plugin";
// Different contexts can use different names
const userContext = new DataStore(new MemoryPlugin("users"));
const orderContext = new DataStore(new MemoryPlugin("orders"));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 { MemoryPlugin } from "@routier/memory-plugin";
// Perfect for unit tests
class TestContext extends DataStore {
constructor() {
super(new MemoryPlugin("test"));
}
}High-Performance Applications
import { DataStore } from "@routier/datastore";
import { MemoryPlugin } from "@routier/memory-plugin";
// For applications requiring maximum speed
class PerformanceContext extends DataStore {
constructor() {
super(new MemoryPlugin("performance"));
}
}Offline-First with Sync
import { MemoryPlugin } from "@routier/memory-plugin";
import { PouchDbPlugin } from "@routier/pouchdb-plugin";
import { ReplicationDbPlugin } from "@routier/core/plugins";
const memoryPlugin = new MemoryPlugin("offline");
const pouchDbPlugin = new PouchDbPlugin("remote");
const replicationPlugin = new ReplicationDbPlugin({
replicas: [memoryPlugin],
source: pouchDbPlugin,
read: memoryPlugin,
});API Reference
Constructor
import { MemoryPlugin } from "@routier/memory-plugin";
new MemoryPlugin(databaseName: string)
// Constructor
// @param databaseName - Unique name for the database instanceProperties
databaseName- The name of the database
Methods
The Memory Plugin implements all standard plugin methods:
add()- Add entities to collectionsupdate()- Update existing entitiesremove()- Remove entitiesquery()- Query collectionsdestroy()- Clear the named database
Shared Named Databases
The plugin keeps one database per NAME, shared by every MemoryPlugin instance in the process. Two new MemoryPlugin("app") instances read and write the same records, which is what lets a multi-store test behave like several connections to one database.
This has one consequence to plan for: destroy() clears the named database for every user of that name, not only for the instance you call it on. A test that destroys its store empties the database out from under any other store that named it.
Give each test its own database name when the tests run in one process:
const store = new AppStore(new MemoryPlugin(`test-${crypto.randomUUID()}`));Next Steps
- Local Storage Plugin - Browser storage plugin
- File System Plugin - Node.js file storage
- Plugin Architecture - Creating custom plugins