@abedmurrar/objection

中文文档
4.2.1发布于 2026/11/04MITTypeScriptESM + CJS构建来源已验证近 30 天下载 3587

A fast, strictly typed SQL ORM for Node.js and TypeScript, built on knex: models, relations, eager loading, graph upserts and transactions on Postgres, MySQL, MariaDB and SQLite. A maintained fork of objection.js.

原始使用文档
文档目录7 节
  1. Why this one
  2. A quick look
  3. Requirements
  4. Coming from objection.js
  5. Documentation
  6. The approach
  7. Credits

Tests Docs npm

@abedmurrar/objection

A fast, strictly typed SQL ORM for Node.js, built on knex. Define your models and relations once, then query them with the full power of SQL: eager loading, graph inserts and upserts, transactions, JSON columns and expressions, with types that know what each query returns.

It started as a fork of objection.js 3.1.5 and keeps its API, so objection.js code runs on it apart from the breaking changes, most of which fix queries that read or changed the wrong rows.

npm install @abedmurrar/objection knex

Why this one

  • Types that know what you loaded. Relations are inferred from relationMappings, query results only contain the relations the query fetched, and relation expressions, modifier names and Model.ref() columns are checked at compile time. See TypeScript.
  • Fast. objection's own work for a query takes 13% to 19% less time than in objection.js 3.1.5, toJSON() 44% less, validation 55% less, and a validated insert in a new transaction 91% less, with the same SQL and results. See Performance.
  • MySQL and MariaDB as first-class databases, not only postgres: JSON field expressions, the whereJson* methods, batch inserts and an fn.* helper for every MySQL 8.4 function. MariaDB 10.6 and 11.4 are tested in CI. See MySQL and MariaDB.
  • Expressions without raw SQL, like Pet.ref('age').gte(3), fn.count().filter(...), fn.case() and window functions. See Expressions.
  • Safer. JSON field keys are escaped (in objection.js 3.1.5 they allowed SQL injection), an orWhere() can no longer match the rows of other relation owners, and for() is no longer silently ignored. See Breaking changes.
  • Modern. Node.js 24+, ES modules and CommonJS from one package, knex 3.3, the knex methods objection didn't have, no lodash and no bluebird, and npm audit reports nothing. The source is written in strict TypeScript and compiled with TypeScript 7.

A quick look

import { Model, defineRelations } from '@abedmurrar/objection';
import { knex } from 'knex';

Model.knex(knex({ client: 'pg', connection: process.env.DATABASE_URL }));

class Pet extends Model {
  static tableName = 'pets';

  id!: number;
  name!: string;
  ownerId!: number;
}

class Person extends Model {
  static tableName = 'persons';

  id!: number;
  firstName!: string;

  static relationMappings = defineRelations(() => ({
    pets: {
      relation: Model.HasManyRelation,
      modelClass: Pet,
      join: { from: 'persons.id', to: 'pets.ownerId' },
    },
  }));
}

const people = await Person.query().where('firstName', 'like', 'J%').withGraphFetched('pets');

people[0].pets[0].name; // `pets` is in the type because the query loaded it
people[0].firstName;

// A person and their pets in one insert.
await Person.query().insertGraph({
  firstName: 'Jennifer',
  pets: [{ name: 'Doggo' }],
});

Requirements

DependencyVersion
Node.js24 or newer (24 and 26 are tested)
knex3.3 or newer
MySQL8.0.17 or newer (tested on 8.4 with mysql2)
MariaDB10.6 or newer (tested on 10.6 and 11.4 with mariadb)
SQLite3.35 or newer for batch inserts

Postgres is tested on the current release.

Coming from objection.js

Install the package under the name objection, and existing import { Model } from 'objection' code keeps working:

npm install objection@npm:@abedmurrar/objection

Then read the breaking changes and the migration guide. Most changes make queries do what they look like they do. The stricter types can flag code that only compiled because the old types were loose; the TypeScript page lists an opt-out for every check.

Documentation

The approach

Like objection.js, this is less a classic ORM than a relational query builder: you work with queries, not with an object graph that hides the SQL.

  • You get a declarative way to define models and their relations, the full power of SQL to fetch and change rows, eager loading, graph inserts and upserts, transactions, optional JSON schema validation, and a way to store documents in JSON columns.
  • You don't get a query language of its own (the query builder is knex, and raw() is there for anything it can't express), or a schema generated from the models: knex migrations do that job.

Credits

This project is built on objection.js by Sami Koskimäki and its many contributors, maintained at Vincit. Its design, its API, most of its code and much of the documentation come from there. Thank you.

Released under the MIT license, like objection.js.

版本与发布记录

@abedmurrar/objection 共发布 27 个版本,首次发布于 2026/10/20;过去 12 个月发布了 27 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.2.1latest2026/11/042 天前
4.2.02026/11/042 天前
4.1.42026/11/042 天前
4.1.32026/11/042 天前
4.1.22026/11/042 天前
4.1.12026/11/042 天前

其他发布通道:next → 4.0.0-beta.11。

关于 @abedmurrar/objection 的常见问题

@abedmurrar/objection 是什么?

A fast, strictly typed SQL ORM for Node.js and TypeScript, built on knex: models, relations, eager loading, graph upserts and transactions on Postgres, MySQL, MariaDB and SQLite. A maintained fork of objection.js.

如何安装 @abedmurrar/objection?

在项目目录运行 npm install @abedmurrar/objection,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:knex(>=3.3.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@abedmurrar/objection 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@abedmurrar/objection 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@abedmurrar/objection 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@abedmurrar/objection 可以商用吗?

@abedmurrar/objection 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@abedmurrar/objection 的下载量有多少?

根据 npm 官方下载统计,@abedmurrar/objection 在 2026/10/05 – 2026/11/03 这 30 天内被下载 3,587 次,日均约 120 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@abedmurrar/objection 还在维护吗?

最新版本 4.2.1 发布于 2026/11/04(2 天前);至今共发布 27 个版本,其中过去 12 个月发布了 27 个;首次发布于 2026/10/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。