Configuration

This guide covers the various configuration options available in Routier.

Quick Navigation

Plugin Configuration

Memory Plugin

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();

Local Storage Plugin

import { DataStore } from "@routier/datastore";
import { s } from "@routier/core/schema";
import { BrowserStoragePlugin } from "@routier/browser-storage-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 BrowserStoragePlugin("app", window.localStorage));
  }
}

const ctx = new Ctx();
await ctx.users.addAsync({ name: "Ada", email: "[email protected]" });
await ctx.saveChangesAsync();

File System Plugin

import { DataStore } from "@routier/datastore";
import { s } from "@routier/core/schema";
import { FileSystemPlugin } from "@routier/file-system-plugin";
import path from "path";

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 FileSystemPlugin(path.join(__dirname, "./data"), "app"));
    }
}

const ctx = new Ctx();
await ctx.users.addAsync({ name: "Ada", email: "[email protected]" });
await ctx.saveChangesAsync();

PouchDB Plugin

import { DataStore } from "@routier/datastore";
import { s } from "@routier/core/schema";
import { PouchDbPlugin } from "@routier/pouchdb-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 PouchDbPlugin("myapp"));
    }
}

const ctx = new Ctx();
await ctx.users.addAsync({ name: "Ada", email: "[email protected]" });
await ctx.saveChangesAsync();

Dexie Plugin

import { DataStore } from "@routier/datastore";
import { s } from "@routier/core/schema";
import { DexiePlugin } from "@routier/dexie-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 DexiePlugin("app"));
    }
}

const ctx = new Ctx();
await ctx.users.addAsync({ name: "Ada", email: "[email protected]" });
await ctx.saveChangesAsync();

Advanced Configuration

Plugin Composition

import { ReplicationDbPlugin } from "@routier/core/plugins";
import { MemoryPlugin } from "@routier/memory-plugin";
import { PouchDbPlugin } from "@routier/pouchdb-plugin";

// Create base plugins
const memoryPlugin = new MemoryPlugin("offline");
const pouchDbPlugin = new PouchDbPlugin("remote");

// Create replication between plugins
const replicationPlugin = ReplicationDbPlugin.create({
  replicas: [memoryPlugin],
  source: pouchDbPlugin,
  read: memoryPlugin,
});

Custom Context Configuration

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

class AppContext extends DataStore {
  constructor() {
    // Pass primary plugin to super constructor
    super(new MemoryPlugin("my-app"));

    // Additional configuration can go here
  }
}

Environment-Specific Configuration

Development

import { DataStore } from "@routier/datastore";
import { MemoryPlugin } from "@routier/memory-plugin";
import { PouchDbPlugin } from "@routier/pouchdb-plugin";

const isDevelopment = process.env.NODE_ENV === "development";
const plugin = isDevelopment ? new MemoryPlugin("dev-app") : new PouchDbPlugin("prod-database");


class MyContext extends DataStore {
  constructor() {
    super(plugin);
  }
}

Testing

For tests, prefer the Memory plugin or mocks/stubs around persistence. The internal testing plugin is not part of the public distribution.

Next Steps