Primate Logo Primate
Guides Databases

Use SQLite

Add SQLite as a database with the @primate/sqlite module. Configure it in config/db; Primate connects to it and provides a unified API.

SQLite is file-based or in-memory, no server needed.

1) Install

Install the Primate SQLite package.

$ npm install @primate/sqlite

2) Configure

Create a database configuration file.

TypeScriptconfig/db/index.ts
import sqlite from "@primate/sqlite";

export default sqlite({
  database: "/tmp/app.db", // or ":memory:" for in-memory
});

3) Create a store

SQLite stores abstract a table. Give the store a name and wire the database.

TypeScriptstores/User.ts
import p from "pema";
import key from "primate/orm/key";
import store from "primate/orm/store";
import db from "../config/db/index.ts";

export default store({
  name: "user",
  db,
  schema: {
    id: key.primary(p.uuid),
    name: p.string,
    email: p.string.email(),
  },
});

4) Use the store

Use the store in routes.

TypeScriptroutes/users.ts
import User from "#store/User";
import route from "primate/route";

route.get(async () => {
  const users = await User.find({});
  return users;
});

route.post(async request => {
  const user = await User.insert(request.body);
  return user;
});