Waterline ORM

0.15.2 · active · verified Sun Apr 19

Waterline is an Object-Relational Mapper (ORM) for Node.js, designed to provide a uniform API for interacting with various data stores such as MySQL, PostgreSQL, MongoDB, Redis, and more, through a pluggable adapter system. While it is the default ORM within the Sails.js framework, it can also be used as a standalone library. The current stable version is 0.15.2, although its last publish date was over three years ago. A significant architectural shift occurred from v0.13 onwards, transitioning from callback-based APIs to fully embracing ECMAScript's `async/await` syntax for all query operations. Key differentiators include its consistent interface across diverse data stores, a strong emphasis on modularity and testability, and an ActiveRecord-inspired pattern tailored for modern JavaScript development, simplifying data persistence by abstracting database specifics behind declarative model definitions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use Waterline standalone with a `sails-disk` adapter. It covers initializing Waterline, defining a simple model, performing basic CRUD operations using `async/await` syntax, and tearing down the ORM gracefully. This setup is typical for standalone Waterline applications or for testing environments.

import { Waterline } from 'waterline';
import DiskAdapter from 'sails-disk'; // Example adapter, install with `npm i sails-disk`

// 1. Initialize Waterline
const waterline = new Waterline();

// 2. Define a Collection (Model)
const UserCollection = Waterline.Collection.extend({
  identity: 'user',
  datastore: 'default',
  primaryKey: 'id',

  attributes: {
    id: { type: 'number', autoIncrement: true },
    name: { type: 'string', required: true },
    email: { type: 'string', unique: true },
    age: { type: 'number', defaultsTo: 18 },
  },
});

// 3. Register the Collection
waterline.registerModel(UserCollection);

// 4. Configure Waterline
const config = {
  datastores: {
    default: {
      adapter: 'sails-disk',
    },
  },
  models: {
    migrate: 'alter', // 'safe', 'alter', 'drop'
  }
};

async function runWaterlineExample() {
  try {
    // 5. Initialize the ORM
    const { collections, connections } = await waterline.initialize(config);

    // Access the User model
    const User = collections.user;

    // Create a new user
    const newUser = await User.create({ name: 'Alice', email: 'alice@example.com' }).fetch();
    console.log('Created user:', newUser);

    // Find all users
    const allUsers = await User.find();
    console.log('All users:', allUsers);

    // Update a user
    const updatedUser = await User.updateOne({ id: newUser.id })
                                  .set({ age: 30 })
                                  .fetch();
    console.log('Updated user:', updatedUser);

    // Clean up (release connections)
    await waterline.teardown();
  } catch (err) {
    console.error('Waterline error:', err);
  }
}

runWaterlineExample();

view raw JSON →