Skip to content

Instantly share code, notes, and snippets.

@shanelau
Created November 24, 2014 09:32
Show Gist options
  • Save shanelau/ea51ab034acc96fc1d2e to your computer and use it in GitHub Desktop.
Save shanelau/ea51ab034acc96fc1d2e to your computer and use it in GitHub Desktop.
waterline demo 不使用sails, 连接数据库 without sails
// Build A Config Object
mysqlAdapter = require('sails-mysql');
module.exports = {
// Setup Adapters
// Creates named adapters that have have been required
adapters: {
'default': mysqlAdapter,
//disk: diskAdapter,
mysql: mysqlAdapter
},
// Build Connections Config
// Setup connections using the named adapter configs
connections: {
myLocalMySql: {
adapter: 'mysql',
host: '127.0.0.1',
port: 3306,
database: 'baidu-sails',
user: 'liux',
password: 'liux'
}
},
defaults: {
migrate: 'alter'
}
};
/**
* Copyright (c) 2014 Meizu bigertech, All rights reserved.
* http://www.bigertech.com/
* @author liuxing
* @date 14-11-24
* @description
*
*/
var User = require('./User');
var config = require('../config/db'),
Waterline = require('waterline'),
models,
connections;
var orm = new Waterline();
orm.loadCollection(User);
function init(callbcak) {
// Start Waterline passing adapters in
orm.initialize(config, function(err, models) {
if(err) {
console.error(err);
throw err;
}
models = models.collections;
connections = models.connections;
console.log('DB is ready!');
callbcak(models);
});
}
module.exports = {
init: init,
models: models,
connections: connections
}
/**
* Copyright (c) 2014 Meizu bigertech, All rights reserved.
* http://www.bigertech.com/
* @author liuxing
* @date 14-11-24
* @description
*
*/
var db = require('../model');
var models;
var should = require('should');
describe('DB', function() {
before(function (done) {
db.init(function (data) {
models = data;
done();
});
});
describe('User', function(){
describe('#create()', function(){
it('should return -1 when the value is not present',function(done){
var data = {
name: 'ak'
};
models.user.create(data).exec(function(err, user) {
console.log(user);
user.id.should.be.ok;
done();
});
});
});
});
});
/**
* Copyright (c) 2014 Meizu bigertech, All rights reserved.
* http://www.bigertech.com/
* @author liuxing
* @date 14-11-24
* @description
*
*/
var Waterline = require('waterline');
// Instantiate a new instance of the ORM
var User = Waterline.Collection.extend({
identity: "user",
connection: 'myLocalMySql',
attributes: {
name: {
type: 'string',
required: true
},
email: {
type: 'string',
}
}
});
module.exports = User;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment