npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@simplysm/orm-common

v13.0.16

Published

심플리즘 패키지 - ORM 모듈 (common)

Downloads

4,892

Readme

@simplysm/orm-common

The common module of Simplysm ORM, providing core ORM functionality including type-safe query builders, schema definitions, and SQL expressions. It generates JSON AST instead of SQL strings, which are then converted to SQL for each DBMS (MySQL, MSSQL, PostgreSQL).

Installation

npm install @simplysm/orm-common
# or
pnpm add @simplysm/orm-common

Dependencies

| Package | Description | |--------|------| | @simplysm/core-common | Common utilities, DateTime, DateOnly, Time, Uuid types |

Supported Databases

| Database | Dialect | Minimum Version | |-------------|---------|----------| | MySQL | mysql | 8.0.14+ | | SQL Server | mssql | 2012+ | | PostgreSQL | postgresql | 9.0+ |

Core Modules

Schema Definition

See docs/schema.md for full documentation.

Query Execution

See docs/queries.md for full documentation.

SQL Expressions

See docs/expressions.md for full documentation.

  • Comparison Expressions - expr.eq(), expr.gt(), expr.lt(), expr.between(), expr.in(), expr.like(), expr.regexp(), expr.exists()
  • Logical Expressions - expr.and(), expr.or(), expr.not()
  • String Expressions - expr.concat(), expr.trim(), expr.substring(), expr.upper(), expr.lower(), expr.length()
  • Numeric Expressions - expr.abs(), expr.round(), expr.ceil(), expr.floor()
  • Date Expressions - expr.year(), expr.month(), expr.day(), expr.dateDiff(), expr.dateAdd(), expr.formatDate()
  • Conditional Expressions - expr.ifNull(), expr.nullIf(), expr.if(), expr.switch()
  • Aggregate Expressions - expr.count(), expr.sum(), expr.avg(), expr.max(), expr.min(), expr.greatest(), expr.least()
  • Window Functions - expr.rowNumber(), expr.rank(), expr.denseRank(), expr.lag(), expr.lead(), expr.sumOver(), expr.avgOver()
  • Other Expressions - expr.val(), expr.raw(), expr.cast(), expr.subquery(), expr.random()

DbContext API

Functional API (Recommended)

The functional API uses defineDbContext + createDbContext for better type safety and composability.

import { defineDbContext, createDbContext, createColumnFactory } from "@simplysm/orm-common";

// Step 1: Define DbContext schema
const MyDbDef = defineDbContext({
  tables: { user: User, post: Post },
  views: { activeUsers: ActiveUsers },
  procedures: { getUserById: GetUserById },
  migrations: [
    {
      name: "20260101_add_status",
      up: async (db) => {
        const c = createColumnFactory();
        await db.addColumn(
          { database: "mydb", name: "User" },
          "status",
          c.varchar(20).nullable(),
        );
      },
    },
  ],
});

// Step 2: Create instance with executor
const db = createDbContext(MyDbDef, executor, { database: "mydb" });

// Use queryable accessors
await db.connect(async () => {
  const users = await db.user().result();
  const posts = await db.post().result();
});

Type Definitions

| Type | Description | |------|-------------| | DbContextDef<TTables, TViews, TProcedures> | DbContext definition (schema blueprint) | | DbContextInstance<TDef> | Full DbContext instance with queryable accessors and DDL methods | | DbContextBase | Core interface used internally (status, executeDefs, etc.) |

Class-based API (Removed)

The old class-based API has been removed. You must migrate to the functional API for better type safety and composability.

// Old (no longer available):
// import { DbContext, queryable } from "@simplysm/orm-common";
// class MyDb extends DbContext { ... }  // This is no longer supported

// New (required):
const MyDbDef = defineDbContext({
  tables: { user: User },
  migrations: [...],
});
const db = createDbContext(MyDbDef, executor, { database: "mydb" });

Migration Guide

To migrate from class-based to functional API:

Step 1: Replace class definition

// Before:
class MyDb extends DbContext {
  readonly user = queryable(this, User);
  readonly post = queryable(this, Post);
  readonly getUserById = executable(this, GetUserById);

  readonly migrations = [
    { name: "...", up: async (db: MyDb) => { ... } }
  ];
}

// After:
const MyDbDef = defineDbContext({
  tables: { user: User, post: Post },
  procedures: { getUserById: GetUserById },
  migrations: [
    { name: "...", up: async (db) => { ... } }
  ],
});

Step 2: Replace instantiation

// Before:
const db = new MyDb(executor, { database: "mydb" });

// After:
const db = createDbContext(MyDbDef, executor, { database: "mydb" });

Step 3: Update usage (no changes needed)

// Both APIs use the same queryable accessors:
await db.connect(async () => {
  const users = await db.user().result();  // Same syntax
});

Type inference:

// Extract instance type:
type MyDb = DbContextInstance<typeof MyDbDef>;

// Use in function parameters:
async function doSomething(db: MyDb) {
  await db.user().result();
}

Security Notes

orm-common uses enhanced string escaping instead of parameter binding due to its dynamic query nature. Always perform input validation at the application level.

// Bad: Direct user input
const userInput = req.query.name; // e.g. malicious SQL payload
await db.user().where((u) => [expr.eq(u.name, userInput)]).result();

// Good: Validate before use
const userName = validateUserName(req.query.name);
await db.user().where((u) => [expr.eq(u.name, userName)]).result();

// Better: Type coercion
const userId = Number(req.query.id);
if (Number.isNaN(userId)) throw new Error("Invalid ID");
await db.user().where((u) => [expr.eq(u.id, userId)]).result();

Quick Start

import { Table, defineDbContext, createDbContext, expr, DateTime } from "@simplysm/orm-common";

// Define table schema
const User = Table("User")
  .database("mydb")
  .columns((c) => ({
    id: c.bigint().autoIncrement(),
    name: c.varchar(100),
    email: c.varchar(200).nullable(),
    createdAt: c.datetime(),
  }))
  .primaryKey("id");

// Define DbContext
const MyDbDef = defineDbContext({
  tables: { user: User },
});

// Create DbContext instance with executor (from orm-node package)
const db = createDbContext(MyDbDef, executor, { database: "mydb" });

// Execute queries
await db.connect(async () => {
  // INSERT
  await db.user().insert([
    { name: "John", createdAt: DateTime.now() }
  ]);

  // SELECT
  const users = await db.user()
    .where((u) => [expr.eq(u.email, "[email protected]")])
    .result();

  // UPDATE
  await db.user()
    .where((u) => [expr.eq(u.id, 1)])
    .update(() => ({ name: expr.val("string", "Jane") }));
});

License

Apache-2.0