feathersjs-ecosystem / feathers-waterline

A Feathers adapter for the Waterline ORM
MIT License
21 stars 5 forks source link

feathers-waterline

Important: This project is no longer maintained.

Greenkeeper badge Maintenance

Build Status Code Climate Test Coverage Dependency Status Download Status Slack Status

A database adapter for the Waterline ORM, the ORM used by SailsJS. For detailed Waterline documentation, see the waterline-docs repository. Currently Waterline supports the following data stores:

Status

This module will continue to work as is but needs a maintainer for future feature and dependency updates. Create an issue if you are interested.

Installation

npm install feathers-waterline --save

ProTip: You also need to install the waterline database adapter for the DB you want to use.

Getting Started

feathers-waterline hooks a Waterline Model up to a configured data store as a feathers service.

const Message = require('./models/message');
const config = require('./config/waterline');
const Waterline = require('waterline');
const service = require('feathers-waterline');

const ORM = new Waterline();

ORM.loadCollection(Message);
ORM.initialize(config, function(error, data) {
    app.use('/messages', waterlineService({
      Model: data.collections.message
    }));
});

Options

Creating a new Waterline service currently offers the following options:

Complete Example

Here is an example of a Feathers server with a messages Waterline Model using the Disk store:

$ npm install feathers feathers-rest feathers-socketio body-parser waterline sails-disk feathers-waterline
const feathers = require('feathers');
const rest = require('feathers-rest');
const socketio = require('feathers-socketio');
const bodyParser = require('body-parser');
const Waterline = require('waterline');
const diskAdapter = require('sails-disk');
const service = require('feathers-waterline');

const ORM = new Waterline();
const config = {
  adapters: {
    'default': diskAdapter,
    disk: diskAdapter
  },
  connections: {
    myLocalDisk: {
      adapter: 'disk'
    }
  },
  defaults: {
    migrate: 'alter'
  }
};
const Message = Waterline.Collection.extend({
  identity: 'message',
  schema: true,
  connection: 'myLocalDisk',
  attributes: {
    text: {
      type: 'string',
      required: true
    },

    complete: {
      type: 'boolean'
    }
  }
});

// Create a feathers instance.
const app = feathers()
  // Enable REST services
  .configure(rest())
  // Enable Socket.io services
  .configure(socketio())
  // Turn on JSON parser for REST services
  .use(bodyParser.json())
  // Turn on URL-encoded parser for REST services
  .use(bodyParser.urlencoded({ extended: true }));

ORM.loadCollection(Message);
ORM.initialize(config, (error, data) => {
  if (error) {
    console.error(error);
  }

  // Create a Waterline Feathers service with a default page size of 2 items
  // and a maximum size of 4
  app.use('/messages', service({
    Model: data.collections.message,
    paginate: {
      default: 2,
      max: 4
    }
  }));

  app.use(function(error, req, res, next){
    res.json(error);
  });

  // Create a dummy Message
  app.service('messages').create({
    text: 'Server message',
    complete: false
  }).then(function(message) {
    console.log('Created message', message.toJSON());
  });

  // Start the server
  const server = app.listen(3030);
  server.on('listening', function() {
    console.log('Feathers Message waterline service running on 127.0.0.1:3030');
    resolve(server);
  });
});

License

Copyright (c) 2016

Licensed under the MIT license.