🗄️
Database150K+/wkMIT

objection

Objection.js is an SQL-friendly ORM for Node.js built on Knex.js that provides a powerful relation mapping system, eager loading, graph inserts/upserts, and JSO

Installation

npm
npm install objection knex
yarn
yarn add objection knex
pnpm
pnpm add objection knex

Import

ESM
import { Model } from 'objection';

Quick Example

usage
import { Model } from 'objection';

class User extends Model {
  static tableName = 'users';
  static relationMappings = {
    posts: {
      relation: Model.HasManyRelation,
      modelClass: Post,
      join: { from: 'users.id', to: 'posts.userId' },
    },
  };
}

const users = await User.query().withGraphFetched('posts');

About objection

Objection.js is an SQL-friendly ORM for Node.js built on Knex.js that provides a powerful relation mapping system, eager loading, graph inserts/upserts, and JSON schema validation while staying close to SQL rather than hiding it. Objection uses class-based models with a static relationMappings property to define relations (HasOneRelation, HasManyRelation, ManyToManyRelation, HasOneThroughRelation, BelongsToOneRelation) between models. The library's eager loading system uses a declarative syntax: Person.query().withGraphFetched('[pets, parent.[pets, children]]') loads nested relations efficiently with minimal queries. Graph operations allow inserting, upserting, and deleting entire object graphs with relations in a single operation. Objection provides a query builder that extends Knex's with model-aware features including relation-based filtering (whereExists with JoinEager), JSON column operations, raw expressions, and result mapping. The library supports JSON Schema validation on models, lifecycle hooks (beforeInsert, afterFind, etc.), mixins for reusable model behavior, and custom query builders. Objection strikes a balance between the simplicity of a query builder and the convenience of a full ORM, making it popular for applications where SQL knowledge is valued and query control is important.

Quick Facts

Packageobjection
CategoryDatabase
Weekly Downloads150K+
LicenseMIT
Installnpm install objection knex

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.