What is objection?
Objection.js is an ORM (Object-Relational Mapper) for Node.js, built on top of the SQL query builder Knex.js. It aims to provide a powerful and flexible way to interact with SQL databases using JavaScript, while maintaining a clean and intuitive API.
What are objection's main functionalities?
Model Definition
Defines a model class that maps to a database table. In this example, the `Person` class represents the `persons` table in the database.
const { Model } = require('objection');
class Person extends Model {
static get tableName() {
return 'persons';
}
}
Querying
Performs a query to fetch all persons older than 30 years. Objection.js provides a fluent API for building SQL queries.
const people = await Person.query().where('age', '>', 30);
Relations
Defines relationships between models. In this example, a `Person` can have many `Animal` pets, establishing a one-to-many relationship.
class Person extends Model {
static get tableName() {
return 'persons';
}
static get relationMappings() {
const Animal = require('./Animal');
return {
pets: {
relation: Model.HasManyRelation,
modelClass: Animal,
join: {
from: 'persons.id',
to: 'animals.ownerId'
}
}
};
}
}
Eager Loading
Fetches related data in a single query. This example fetches persons along with their pets using eager loading.
const peopleWithPets = await Person.query().withGraphFetched('pets');
Validation
Defines a JSON schema for model validation. This example ensures that `firstName` and `lastName` are required fields and must be strings of a certain length.
class Person extends Model {
static get jsonSchema() {
return {
type: 'object',
required: ['firstName', 'lastName'],
properties: {
id: { type: 'integer' },
firstName: { type: 'string', minLength: 1, maxLength: 255 },
lastName: { type: 'string', minLength: 1, maxLength: 255 }
}
};
}
}
Other packages similar to objection
sequelize
Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication, and more. Compared to Objection.js, Sequelize has a more extensive feature set but can be more complex to use.
typeorm
TypeORM is an ORM for TypeScript and JavaScript (ES7, ES6, ES5). It supports many database systems including MySQL, MariaDB, PostgreSQL, SQLite, and more. TypeORM is known for its strong TypeScript support and decorators, making it a good choice for TypeScript projects. It offers similar functionalities to Objection.js but with a different API style.
bookshelf
Bookshelf.js is a JavaScript ORM for Node.js, built on top of the Knex.js SQL query builder. It provides a simple and straightforward way to interact with SQL databases. Bookshelf.js is similar to Objection.js in that both are built on Knex.js, but Bookshelf.js has a more minimalistic approach.
Objection.js is an ORM for Node.js that aims to stay out of your way and make it as easy as possible to use the full power of SQL and the underlying database engine while still making the common stuff easy and enjoyable.
Even though ORM is the best commonly known acronym to describe objection, a more accurate description is to call it a relational query builder. You get all the benefits of an SQL query builder but also a powerful set of tools for working with relations.
Objection.js is built on an SQL query builder called knex. All databases supported by knex are supported by objection.js. SQLite3, Postgres and MySQL are thoroughly tested.
What objection.js gives you:
What objection.js doesn't give you:
- A fully object oriented view of your database
With objection you don't work with entities. You work with queries. Objection doesn't try to wrap every concept with an
object oriented equivalent. The best attempt to do that (IMO) is Hibernate, which is excellent, but it has 800k lines
of code and a lot more concepts to learn than SQL itself. The point is, writing a good traditional ORM is borderline
impossible. Objection attempts to provide a completely different way of working with SQL.
- A custom query DSL. SQL is used as a query language.
This doesn't mean you have to write SQL strings though. A query builder based on knex is
used to build the SQL. However, if the query builder fails you for some reason, raw SQL strings can be easily
written using the raw helper function.
- Automatic database schema creation and migration from model definitions.
For simple things it is useful that the database schema is automatically generated from the model definitions,
but usually just gets in your way when doing anything non-trivial. Objection.js leaves the schema related things
to you. knex has a great migration tool that we recommend for this job. Check
out the example project.
The best way to get started is to clone our example project and start playing with it. There's also a typescript version available.
Check out this issue to see who is using objection and what they think about it.
Shortcuts: