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

npm install @routier/memory-plugin

Basic 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).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 { DbPluginReplicator } from "@routier/core/plugins";

const memoryPlugin = new MemoryPlugin("offline");
const pouchDbPlugin = new PouchDbPlugin("remote");

const replicationPlugin = DbPluginReplicator.create({
  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 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() - Clean up resources

Next Steps