objection.js
An SQL-friendly ORM for Node.js
A Node.js library for working with SQL databases that keeps SQL visible and central while making common operations like fetching related records and running transactions straightforward in JavaScript.
Objection.js is a library for Node.js that makes it easier to work with SQL databases. Node.js is a JavaScript runtime used to build server-side applications, and SQL is the language most relational databases use to store and retrieve data. Objection.js sits between your application code and your database, letting you write queries in JavaScript rather than raw SQL strings, while still giving you full access to the underlying SQL if you need it.
The library describes itself less as a traditional ORM and more as a relational query builder. An ORM, or object-relational mapper, typically tries to hide the database behind an object-oriented layer. Objection takes a different approach: it keeps SQL visible and central, but gives you tools to make common operations like fetching related records, inserting nested data, and running transactions feel straightforward. You define your data models and describe how they relate to each other, and the library handles building the correct SQL queries.
It supports SQLite, Postgres, and MySQL, all of which are thoroughly tested. It includes TypeScript support, optional JSON schema validation for validating data before it reaches the database, and the ability to store complex nested documents as a single database row.
What the library deliberately does not include is automatic schema creation from model definitions, a custom query language that replaces SQL, or a fully object-oriented abstraction over every database concept. The README is candid that those approaches add complexity without proportional benefit. For database migrations, the project recommends using a separate tool called knex, which Objection.js is built on top of.
The best starting point, according to the documentation, is to clone one of the provided example projects and experiment from there.
Where it fits
- Build a REST API that reads and writes data to a PostgreSQL or MySQL database without writing raw SQL strings.
- Fetch related records across multiple database tables in a single JavaScript query with nested data support.
- Validate data against a JSON schema before inserting it into the database.