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 install objection knex
yarn add objection knex
pnpm add objection knex
Import
import { Model } from 'objection';Quick Example
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
| Package | objection |
| Category | Database |
| Weekly Downloads | 150K+ |
| License | MIT |
| Install | npm install objection knex |
Related Packages
Knex.js is a batteries-included SQL query builder for Node.js that supports PostgreSQL, MySQL, Maria…
Bookshelf is a JavaScript ORM for Node.js built on top of the Knex.js query builder, providing an Ac…
Prisma is a next-generation Node.js and TypeScript ORM that provides a declarative data modeling lan…
Sequelize is a promise-based Node.js ORM that supports PostgreSQL, MySQL, MariaDB, SQLite, and Micro…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.