MasterRecord ORM

1.0.4 · active · verified Wed Apr 22

MasterRecord is a lightweight, code-first Object-Relational Mapper (ORM) for Node.js, currently at version 1.0.4. It emphasizes a fluent, lambda-based query API, a comprehensive CLI-driven migration system, and out-of-the-box support for multiple relational databases including MySQL (5.7+/8.0+), PostgreSQL (9.6+/12+), and SQLite (3.x). The ORM operates on an Active Record pattern, providing entity serialization, lifecycle hooks, and built-in validation. A key differentiator is its "ESM only" nature, requiring Node.js 20+ and a host project configured as a module, with no CommonJS build available. It offers features like query result caching, bulk operations, and robust SQL injection protection through parameterized queries. The project seems to follow a stable release cadence with its initial major v1.0 release.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure MasterRecord with an SQLite database, define a `User` entity, sync the schema, and perform basic CRUD operations (create, save, query) on entity instances.

import { MasterRecord } from 'masterrecord';

// 1. Define an entity extending MasterRecord.Entity
class User extends MasterRecord.Entity {
  id!: number;
  name!: string;
  email!: string;
  age?: number;

  static tableName = 'users'; // Explicitly define the table name

  static fields = {
    id: { type: 'number', primaryKey: true, autoIncrement: true },
    name: { type: 'string', required: true, length: { min: 3, max: 255 } },
    email: { type: 'string', required: true, pattern: /^\S+@\S+\.\S+$/, unique: true },
    age: { type: 'number', required: false, min: 18 }
  };
}

async function runMasterRecord() {
  // 2. Configure MasterRecord for SQLite database
  await MasterRecord.configure({
    connection: {
      client: 'sqlite',
      filename: './quickstart.sqlite'
    },
    entities: [User],
    debug: process.env.NODE_ENV !== 'production' // Enable logging in development
  });

  // 3. Sync schema (for development; use migrations in production)
  await MasterRecord.syncSchema();
  console.log('Database schema synced successfully for User entity.');

  // 4. Create a new user instance using the factory method
  const newUser = MasterRecord.create(User, { 
    name: 'Alice Wonderland',
    email: 'alice@example.com',
    age: 28 
  });
  await newUser.save();
  console.log('Created new user:', newUser.toObject());

  // 5. Find a user by email
  const foundUser = await MasterRecord.query(User).where(u => u.email === 'alice@example.com').first();
  if (foundUser) {
    console.log('Found user:', foundUser.toObject());
    // 6. Update the user
    foundUser.age = 29;
    await foundUser.save();
    console.log('Updated user age:', foundUser.toObject());
  }

  // 7. Disconnect from the database
  await MasterRecord.disconnect();
  console.log('MasterRecord disconnected.');
}

runMasterRecord().catch(console.error);

view raw JSON →