← Complete Projects

阅读中文版本

Build a Contact Book REST API with SQLite

A small REST API for contacts backed by SQLite, with validated creation, case-insensitive search, pagination, and a migration step that creates the schema on first boot.

intermediate~6 hnodejssqliterestapi

Project goal

Build a REST API that stores contacts in SQLite, validates every write, supports search across name and email, paginates list results, and applies schema migrations automatically on startup.

Final features

  • Create, read, update, and delete contacts with server-side validation.
  • Search contacts by a case-insensitive substring across name and email.
  • Paginate list results with limit and offset and return total counts.
  • Run schema migrations automatically the first time the server boots.
  • Return consistent JSON error envelopes with meaningful status codes.

Architecture

An HTTP layer maps routes to handlers; a repository encapsulates all SQL so handlers never build queries; a validation module normalises and checks input; and a migration runner applies ordered SQL statements tracked in a migrations table. The database connection is opened once and shared.

Prerequisites

  • Node.js 22 LTS (for the built-in node:sqlite module)
  • Understanding of HTTP methods and status codes
  • Basic SQL knowledge

Dependencies

  • node >=22 — Built-in node:sqlite and the HTTP server
  • vitest ^2.1.0 — Repository and API endpoint tests

Directory structure

contact-api/
├── src/
│   ├── db.js
│   ├── migrations.js
│   ├── contacts.repo.js
│   ├── validation.js
│   ├── routes.js
│   └── server.js
├── test/
│   ├── repo.test.js
│   └── api.test.js
└── package.json

Phases

1. Database connection

Open a SQLite database from a configurable path, enable foreign keys, and expose a thin query helper.

Files: src/db.js

import { DatabaseSync } from 'node:sqlite';

export function openDatabase(path = ':memory:') {
  const db = new DatabaseSync(path);
  db.exec('PRAGMA foreign_keys = ON;');
  return {
    run: (sql, params = []) => db.prepare(sql).run(...params),
    all: (sql, params = []) => db.prepare(sql).all(...params),
    get: (sql, params = []) => db.prepare(sql).get(...params),
    close: () => db.close(),
  };
}

Expected behavior: Opening an in-memory database and running a trivial query works, and opening a file path creates the file on first use.

2. Migrations

Apply ordered migrations exactly once, tracking which have run in a migrations table so boot is idempotent.

Files: src/migrations.js

const MIGRATIONS = [
  {
    id: 1,
    up: `CREATE TABLE contacts (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      name TEXT NOT NULL,
      email TEXT NOT NULL UNIQUE,
      phone TEXT,
      created_at TEXT NOT NULL
    );`,
  },
];

export function migrate(db) {
  db.run('CREATE TABLE IF NOT EXISTS migrations (id INTEGER PRIMARY KEY, applied_at TEXT)');
  const applied = new Set(db.all('SELECT id FROM migrations').map((r) => r.id));
  for (const m of MIGRATIONS) {
    if (applied.has(m.id)) continue;
    db.run(m.up);
    db.run('INSERT INTO migrations (id, applied_at) VALUES (?, ?)', [m.id, new Date().toISOString()]);
  }
  return MIGRATIONS.length;
}

Expected behavior: Calling migrate twice applies each migration only once, and a fresh database ends up with both the migrations table and the contacts table.

3. Contacts repository

Implement CRUD and search as parameterised SQL inside one module so handlers never construct queries.

Files: src/contacts.repo.js

export function createContactsRepo(db) {
  return {
    create(c) {
      const info = db.run(
        'INSERT INTO contacts (name, email, phone, created_at) VALUES (?, ?, ?, ?)',
        [c.name, c.email, c.phone ?? null, new Date().toISOString()],
      );
      return this.find(info.lastInsertRowid);
    },
    find(id) {
      return db.get('SELECT * FROM contacts WHERE id = ?', [id]) ?? null;
    },
    search(q, { limit = 20, offset = 0 } = {}) {
      const like = '%' + q.toLowerCase() + '%';
      return db.all(
        'SELECT * FROM contacts WHERE lower(name) LIKE ? OR lower(email) LIKE ? ORDER BY id LIMIT ? OFFSET ?',
        [like, like, limit, offset],
      );
    },
    remove(id) {
      return db.run('DELETE FROM contacts WHERE id = ?', [id]).changes;
    },
  };
}

Expected behavior: Insert then find returns the same record, search matches on a case-insensitive substring, and deleting a missing id reports zero changes.

4. Validation

Normalise incoming contact payloads, reject missing or malformed fields with a structured error, and enforce a valid email shape.

Files: src/validation.js

const EMAIL = /^[^@\s]+@[^@\s]+\.[^@\s]+$/;

export function validateContact(input) {
  const errors = {};
  const name = typeof input.name === 'string' ? input.name.trim() : '';
  const email = typeof input.email === 'string' ? input.email.trim().toLowerCase() : '';
  const phone = typeof input.phone === 'string' ? input.phone.trim() : undefined;
  if (name.length < 2) errors.name = 'name must be at least 2 characters';
  if (!EMAIL.test(email)) errors.email = 'email is not valid';
  if (Object.keys(errors).length) return { ok: false, errors };
  return { ok: true, value: { name, email, phone } };
}

Expected behavior: A payload missing a name returns ok false with a name error and no email error, and a valid payload normalises the email to lowercase.

5. Routes and error envelope

Map HTTP methods and paths to handlers and return one consistent JSON error shape with correct status codes.

Files: src/routes.js

import { validateContact } from './validation.js';

export const routes = [
  {
    method: 'POST',
    pattern: /^\/contacts$/,
    async handler({ repo, body }) {
      const result = validateContact(body ?? {});
      if (!result.ok) return { status: 422, body: { error: 'validation_failed', fields: result.errors } };
      try {
        return { status: 201, body: await repo.create(result.value) };
      } catch (err) {
        if (String(err.message).includes('UNIQUE')) {
          return { status: 409, body: { error: 'email_exists' } };
        }
        throw err;
      }
    },
  },
  {
    method: 'GET',
    pattern: /^\/contacts$/,
    async handler({ repo, query }) {
      const q = query.get('q') ?? '';
      const limit = Math.min(100, Number(query.get('limit') ?? 20));
      const offset = Number(query.get('offset') ?? 0);
      return { status: 200, body: { items: repo.search(q, { limit, offset }) } };
    },
  },
];

Expected behavior: A duplicate email returns 409, an invalid payload returns 422 with field errors, and a valid create returns 201 with the stored record.

6. Server wiring and tests

Boot the server, run migrations, wire routes, and test the whole API against a temporary database.

Files: src/server.js, test/api.test.js

import { openDatabase } from './db.js';
import { migrate } from './migrations.js';
import { createContactsRepo } from './contacts.repo.js';
import { createApi } from './api.js';

const db = openDatabase(process.env.DB_PATH ?? './contacts.db');
migrate(db);
const repo = createContactsRepo(db);
const server = createApi({ repo });

server.listen(process.env.PORT ?? 3000, () => {
  console.log('listening on ' + (process.env.PORT ?? 3000));
});

export { server };

Expected behavior: Starting the server with DB_PATH pointed at a temporary file boots cleanly, applies migrations, and serves a 201 on the first contact create.

Testing

Run npx vitest. Repository tests open an in-memory database, apply migrations, and assert CRUD and search behaviour directly against SQL. API tests start the server on an ephemeral port, exercise each route including error cases, and assert both status codes and JSON bodies.

Troubleshooting

The server crashes on boot when the database file already exists

Use CREATE TABLE IF NOT EXISTS and a migrations table so re-running migrate is a no-op; boot must be idempotent because every restart re-enters the same code path.

Search returns nothing for an uppercase query

Lowercase both the column and the search term in SQL (or store a normalised column) so matching does not depend on the caller’s casing.

Tests interfere with each other through a shared database file

Open a fresh in-memory database per test with DB_PATH unset, and never point tests at the development file; isolation is cheaper than cleanup.

Deployment

Run node src/server.js behind a reverse proxy with DB_PATH on a mounted volume. Because migrations run on boot, deploying a new version that adds a column is a restart rather than a manual migration step.

Known limitations

  • SQLite serialises writes, so the API is best for a single writer process or low write volume.
  • There is no authentication in this project; exposing it publicly requires an auth layer in front.

Project overview

A contact book is small enough to build in an afternoon and complete enough to demonstrate every decision a real API makes: validation, persistence, search, pagination, and consistent errors. SQLite keeps the persistence real without asking the reader to run a database server.

The API opens one connection, applies migrations on boot, and keeps all SQL inside a repository so handlers stay thin. Validation happens before the repository is ever called.

The HTTP layer stays a handful of lines because route matching and handlers are the only thing it owns:

import { createServer } from 'node:http';
import { routes } from './routes.js';

export function createApi({ repo }) {
  return createServer(async (req, res) => {
    const url = new URL(req.url, 'http://localhost');
    const route = routes.find(
      (r) => r.method === req.method && r.pattern.test(url.pathname),
    );
    if (!route) return send(res, 404, { error: 'not found' });
    const params = url.pathname.match(route.pattern).slice(1);
    const body = await readJson(req);
    const result = await route.handler({ repo, params, query: url.searchParams, body });
    return send(res, result.status ?? 200, result.body);
  });
}

Returning a result object from each handler rather than writing to the response keeps the handlers testable without a socket.

Setup

This project targets Node 22 because node:sqlite ships in the standard library, which removes the only common native dependency from the stack.

Dependency Version Why it is here
node >=22 Built-in node:sqlite and the HTTP server
vitest ^2.1.0 Repository and API endpoint tests

Create the project and install:

mkdir contact-api && cd contact-api
npm init -y
npm install -D vitest@^2.1.0

Configure package.json with the server entry and the test runner:

{
  "name": "contact-api",
  "type": "module",
  "scripts": {
    "start": "node src/server.js",
    "test": "vitest run"
  }
}

The database file path comes from an environment variable so tests open an in-memory database and never touch your development data.

Phase 1: Database connection

Open a SQLite database from a configurable path, enable foreign keys, and expose a thin query helper. Scoped this tightly, the phase ends with a runnable artifact and a test that proves it, so the next one starts from a known-good base.

import { DatabaseSync } from 'node:sqlite';

export function openDatabase(path = ':memory:') {
  const db = new DatabaseSync(path);
  db.exec('PRAGMA foreign_keys = ON;');
  return {
    run: (sql, params = []) => db.prepare(sql).run(...params),
    all: (sql, params = []) => db.prepare(sql).all(...params),
    get: (sql, params = []) => db.prepare(sql).get(...params),
    close: () => db.close(),
  };
}

Acceptance for this phase: Opening an in-memory database and running a trivial query works, and opening a file path creates the file on first use.

Phase 2: Migrations

Apply ordered migrations exactly once, tracking which have run in a migrations table so boot is idempotent. Scoped this tightly, the phase ends with a runnable artifact and a test that proves it, so the next one starts from a known-good base.

const MIGRATIONS = [
  {
    id: 1,
    up: `CREATE TABLE contacts (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      name TEXT NOT NULL,
      email TEXT NOT NULL UNIQUE,
      phone TEXT,
      created_at TEXT NOT NULL
    );`,
  },
];

export function migrate(db) {
  db.run('CREATE TABLE IF NOT EXISTS migrations (id INTEGER PRIMARY KEY, applied_at TEXT)');
  const applied = new Set(db.all('SELECT id FROM migrations').map((r) => r.id));
  for (const m of MIGRATIONS) {
    if (applied.has(m.id)) continue;
    db.run(m.up);
    db.run('INSERT INTO migrations (id, applied_at) VALUES (?, ?)', [m.id, new Date().toISOString()]);
  }
  return MIGRATIONS.length;
}

Acceptance for this phase: Calling migrate twice applies each migration only once, and a fresh database ends up with both the migrations table and the contacts table.

Phase 3: Contacts repository

Implement CRUD and search as parameterised SQL inside one module so handlers never construct queries. Scoped this tightly, the phase ends with a runnable artifact and a test that proves it, so the next one starts from a known-good base.

export function createContactsRepo(db) {
  return {
    create(c) {
      const info = db.run(
        'INSERT INTO contacts (name, email, phone, created_at) VALUES (?, ?, ?, ?)',
        [c.name, c.email, c.phone ?? null, new Date().toISOString()],
      );
      return this.find(info.lastInsertRowid);
    },
    find(id) {
      return db.get('SELECT * FROM contacts WHERE id = ?', [id]) ?? null;
    },
    search(q, { limit = 20, offset = 0 } = {}) {
      const like = '%' + q.toLowerCase() + '%';
      return db.all(
        'SELECT * FROM contacts WHERE lower(name) LIKE ? OR lower(email) LIKE ? ORDER BY id LIMIT ? OFFSET ?',
        [like, like, limit, offset],
      );
    },
    remove(id) {
      return db.run('DELETE FROM contacts WHERE id = ?', [id]).changes;
    },
  };
}

Acceptance for this phase: Insert then find returns the same record, search matches on a case-insensitive substring, and deleting a missing id reports zero changes.

Phase 4: Validation

Normalise incoming contact payloads, reject missing or malformed fields with a structured error, and enforce a valid email shape. Scoped this tightly, the phase ends with a runnable artifact and a test that proves it, so the next one starts from a known-good base.

const EMAIL = /^[^@\s]+@[^@\s]+\.[^@\s]+$/;

export function validateContact(input) {
  const errors = {};
  const name = typeof input.name === 'string' ? input.name.trim() : '';
  const email = typeof input.email === 'string' ? input.email.trim().toLowerCase() : '';
  const phone = typeof input.phone === 'string' ? input.phone.trim() : undefined;
  if (name.length < 2) errors.name = 'name must be at least 2 characters';
  if (!EMAIL.test(email)) errors.email = 'email is not valid';
  if (Object.keys(errors).length) return { ok: false, errors };
  return { ok: true, value: { name, email, phone } };
}

Acceptance for this phase: A payload missing a name returns ok false with a name error and no email error, and a valid payload normalises the email to lowercase.

Phase 5: Routes and error envelope

Map HTTP methods and paths to handlers and return one consistent JSON error shape with correct status codes. Scoped this tightly, the phase ends with a runnable artifact and a test that proves it, so the next one starts from a known-good base.

import { validateContact } from './validation.js';

export const routes = [
  {
    method: 'POST',
    pattern: /^\/contacts$/,
    async handler({ repo, body }) {
      const result = validateContact(body ?? {});
      if (!result.ok) return { status: 422, body: { error: 'validation_failed', fields: result.errors } };
      try {
        return { status: 201, body: await repo.create(result.value) };
      } catch (err) {
        if (String(err.message).includes('UNIQUE')) {
          return { status: 409, body: { error: 'email_exists' } };
        }
        throw err;
      }
    },
  },
  {
    method: 'GET',
    pattern: /^\/contacts$/,
    async handler({ repo, query }) {
      const q = query.get('q') ?? '';
      const limit = Math.min(100, Number(query.get('limit') ?? 20));
      const offset = Number(query.get('offset') ?? 0);
      return { status: 200, body: { items: repo.search(q, { limit, offset }) } };
    },
  },
];

Acceptance for this phase: A duplicate email returns 409, an invalid payload returns 422 with field errors, and a valid create returns 201 with the stored record.

Phase 6: Server wiring and tests

Boot the server, run migrations, wire routes, and test the whole API against a temporary database. Scoped this tightly, the phase ends with a runnable artifact and a test that proves it, so the next one starts from a known-good base.

import { openDatabase } from './db.js';
import { migrate } from './migrations.js';
import { createContactsRepo } from './contacts.repo.js';
import { createApi } from './api.js';

const db = openDatabase(process.env.DB_PATH ?? './contacts.db');
migrate(db);
const repo = createContactsRepo(db);
const server = createApi({ repo });

server.listen(process.env.PORT ?? 3000, () => {
  console.log('listening on ' + (process.env.PORT ?? 3000));
});

export { server };

Acceptance for this phase: Starting the server with DB_PATH pointed at a temporary file boots cleanly, applies migrations, and serves a 201 on the first contact create.

Testing strategy

Run npx vitest. Repository tests open an in-memory database, apply migrations, and assert CRUD and search behaviour directly against SQL. API tests start the server on an ephemeral port, exercise each route including error cases, and assert both status codes and JSON bodies.

Search and pagination get explicit tests because they are the two endpoints most likely to return subtly wrong results that pass a naive happy-path check.

Extending the project

Adding a field is a migration plus a validation rule; adding an endpoint is a route plus a repository method. If the app ever needs a second writer process, the SQLite file is the shared point and a server-side queue is where write serialisation would move.