Skip to content
Tyler Swayne edited this page Jul 1, 2020 · 1 revision

Your models are an object representation of your data model and an object oriented means of interacting with your persisted data. Nexi uses waterline as it's orm. Waterline models expose methods to query and manipulate data within your database in a way that abstracts the underlying query language.

Location and filename

Models must reside in the src/app/models directory. The filename must match the model.

Structure

The file must export an object that describes your model. The object must at least specifiy:

  • identity - the model name. Must match the database table name.
  • datastore - set to 'default'
  • attributes - object, your model's fields, corresponding to your table's columns.

For detailed documentation on attribute schema and additional model fields, see the waterline docs

// src/app/models/User.js
module.exports = {
  identity: 'user',
  datastore: 'default',
  attributes: {
    name: {
      type: 'string',
      required: true,
    },
  }
}

Usage

All models defined in the models directory are attached to the application context, which is injected into all nexi components such as controllers, middleware, initialzers, or decorators.

The waterline model interface exposes methods to query and manipulate data within your database in a way that abstracts the underlying query language. Example:

const User = context.user
const user = await User.create({ name: "Baloo") // Create a new user named "Baloo"
const baloos = await User.find({ name: "Baloo") // Find all users named "Baloo"
await User.update({ id: user.id }, { name: "Baloo III" }) // Update our new user's name

For the extensive list of model methods, see the waterline model docs.