Which is Better Node.js ORM Libraries?
knex vs waterline

1 Year
knexwaterline
What's Node.js ORM Libraries?

Node.js ORM, or Object-Relational Mapping, is a programming technique that simplifies database interaction in Node.js applications. It enables developers to work with databases using JavaScript or TypeScript objects instead of raw SQL queries. This approach abstracts the complexities of database operations, allowing for a more object-oriented coding style. With features like cross-database compatibility, schema management, query building, and middleware support, Node.js ORM libraries, such as Mongoose, Prisma, Knex, Sequelize, TypeORM, Objection, Bookshelf, and Waterline, provide a convenient and consistent way to model, query, and manage data in various database systems, catering to different project needs and preferences in the Node.js ecosystem.

NPM Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Weekly Downloads
Github Stars
Open Issues
Last Commit
License
knex1,679,90918,6851,0842 days agoMIT License
waterline23,0215,41732a year agoMIT License
Feature Comparison
Featuresknexwaterline
Size
Knex is lightweight and focused on query building, making it versatile and efficient for smaller projects.
Waterline is lightweight, designed to be a flexible data abstraction layer with a smaller footprint.
Consistency
Knex provides flexibility, allowing developers to define and alter schemas as needed.
Waterline provides a schema-agnostic approach, allowing dynamic data models for NoSQL databases.
Mutability
Knex allows for dynamic schema changes, providing flexibility for evolving projects.
Waterline allows dynamic schema changes, making it suitable for projects with changing data structures.
Localization
Knex does not have built-in localization features but allows developers to implement custom solutions.
Waterline does not natively support localization but can be extended using custom code.
Extensibility
Knex is extensible, enabling developers to create custom query builders and plugins.
Waterline is extensible, allowing developers to create custom adapters and extend core functionality.
Maintenance
Knex is actively maintained, providing continuous improvements and bug fixes.
Waterline is maintained, with updates occurring to address issues and improve compatibility.
Popular
Knex is popular for its simplicity and flexibility, especially in smaller projects.
Waterline is known in certain communities and is chosen for its simplicity and flexibility.
Learning Curve
Knex has a low learning curve, making it easy for developers to get started quickly.
Waterline has a low to moderate learning curve, suitable for developers aiming for simplicity.
Ecosystem
Knex has a modest ecosystem, with some plugins and community support.
Waterline has a niche ecosystem with support for various databases and community-contributed adapters.
NPM Package Introudction

knex.js

npm version npm downloads Coverage Status Dependencies Status Gitter chat

A SQL query builder that is flexible, portable, and fun to use!

A batteries-included, multi-dialect (PostgreSQL, MySQL, CockroachDB, MSSQL, SQLite3, Oracle (including Oracle Wallet Authentication)) query builder for Node.js, featuring:

Node.js versions 12+ are supported.

You can report bugs and discuss features on the GitHub issues page or send tweets to @kibertoad.

For support and questions, join our Gitter channel.

For knex-based Object Relational Mapper, see:

To see the SQL that Knex will generate for a given query, you can use Knex Query Lab

Examples

We have several examples on the website. Here is the first one to get you started:

const knex = require('knex')({
  client: 'sqlite3',
  connection: {
    filename: './data.db',
  },
});

try {
  // Create a table
  await knex.schema
    .createTable('users', (table) => {
      table.increments('id');
      table.string('user_name');
    })
    // ...and another
    .createTable('accounts', (table) => {
      table.increments('id');
      table.string('account_name');
      table.integer('user_id').unsigned().references('users.id');
    });

  // Then query the table...
  const insertedRows = await knex('users').insert({ user_name: 'Tim' });

  // ...and using the insert id, insert into the other table.
  await knex('accounts').insert({
    account_name: 'knex',
    user_id: insertedRows[0],
  });

  // Query both of the rows.
  const selectedRows = await knex('users')
    .join('accounts', 'users.id', 'accounts.user_id')
    .select('users.user_name as user', 'accounts.account_name as account');

  // map over the results
  const enrichedRows = selectedRows.map((row) => ({ ...row, active: true }));

  // Finally, add a catch statement
} catch (e) {
  console.error(e);
}

TypeScript example

import { Knex, knex } from 'knex';

interface User {
  id: number;
  age: number;
  name: string;
  active: boolean;
  departmentId: number;
}

const config: Knex.Config = {
  client: 'sqlite3',
  connection: {
    filename: './data.db',
  },
};

const knexInstance = knex(config);

try {
  const users = await knex<User>('users').select('id', 'age');
} catch (err) {
  // error handling
}

Usage as ESM module

If you are launching your Node application with --experimental-modules, knex.mjs should be picked up automatically and named ESM import should work out-of-the-box. Otherwise, if you want to use named imports, you'll have to import knex like this:

import { knex } from 'knex/knex.mjs';

You can also just do the default import:

import knex from 'knex';

If you are not using TypeScript and would like the IntelliSense of your IDE to work correctly, it is recommended to set the type explicitly:

/**
 * @type {Knex}
 */
const database = knex({
  client: 'mysql',
  connection: {
    host: '127.0.0.1',
    user: 'your_database_user',
    password: 'your_database_password',
    database: 'myapp_test',
  },
});
database.migrate.latest();