Skip to content

Memory Plugin

The Memory Plugin provides fast, in-memory data storage for your Routier application.

Quick Navigation

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

bash
npm install @routier/memory-plugin

Basic Usage

ts
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

ts
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:

ts
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

ts
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

ts
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

ts
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

ts
import { MemoryPlugin } from "@routier/memory-plugin";

new MemoryPlugin(databaseName: string)

// Constructor
// @param databaseName - Unique name for the database instance

Properties

  • databaseName - The name of the database

Methods

The Memory Plugin implements all standard plugin methods:

  • add() - Add entities to collections
  • update() - Update existing entities
  • remove() - Remove entities
  • query() - Query collections
  • destroy() - 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:

ts
const store = new AppStore(new MemoryPlugin(`test-${crypto.randomUUID()}`));

Next Steps

Released under the MIT License.