waterline-mssql

1.0.6 • Public • Published

Microsoft SQL Server adapter for waterline. Built for Microsoft SQL Server 2012 and newer.

Installing

$ npm install waterline-mssql --save

Usage

Configuration and initialization

var adapter = require('waterline-mssql');
var Waterline = require('waterline');

var waterline = new Waterline();

var config = {

  adapters: {
    default: adapter
  },

  connections: {
    default: {
      adapter: 'default',
      database: 'database_name',
      host: 'addrestohost',
      port: 1433,
      user: 'dbo',
      persistent: true,
      password: 'superlongpassword'
    }
  },

  defaults: {
    migrate: 'create'
  }

};

waterline.initialize(config, function (err, data) {
  if (err) {
    throw err;
  }

  var collections = data.collections;
  var connections = data.connections;
});

Collection/Model definition

var bcrypt = require('bcrypt');

var userModel = {

  attributes: {

    name: {
      type: 'string',
      required: true
    },

    email: {
      type: 'string',
      required: true,
      unique: true,
      size: 255 // defaults to 'max'
    },

    password: {
      type: 'string',
      required: true
    },

    role: {
      model: 'role'
    }

  },

  beforeCreate: function (values, next) {
    bcrypt.hash(values.password, 8, function (err, hash) {
      if (err) {
        return next(err);
      }

      values.password = hash;

      next();
    });
  }

};

Collection/Model Transation

waterline.initialize(config, function (err, data) {
  if (err) {
    throw err;
  }

  var collections = data.collections;
  var connections = data.connections;

  // start transaction
  collections.usermodel.transaction.start(function(err) {
     // commit transaction
    collections.userModel.transaction.commit(function(err) {

    });

    // rollback transaction
    collections.usermodel.transaction.rollback(function(err) {

    });
  })
});

Package Sidebar

Install

npm i waterline-mssql

Weekly Downloads

8

Version

1.0.6

License

MIT

Last publish

Collaborators

  • libinqi