Last active
June 13, 2016 05:33
-
-
Save lvegerano/f45fe5f510f3e386d7f6d9da03b17b13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Modules | |
const EventsEmitter = require('events'); | |
// Libs | |
const _ = require('lodash'); | |
const Winston = require('winston'); | |
const Pluralize = require('pluralize'); | |
const Promise = require('bluebird'); | |
const bookshelf = require('./pg'); | |
function Collection(dbSettings) { | |
const defaults = { | |
tableName: 'default' | |
}; | |
this.settings = Object.assign({}, defaults, dbSettings); | |
this.tableName = this.settings.tableName; | |
this.model = null; | |
delete this.settings.tableName; | |
this.orm = bookshelf(this.settings); | |
} | |
const proto = { | |
setModel: function(name, model) { | |
const defaults = { | |
tableName: this.tableName, | |
}; | |
const modelData = Object.assign({}, defaults, model); | |
const baseModel = this.orm.Model.extend.call(this.orm.Model, modelData); | |
this.model = this.orm.model(name, baseModel); | |
}, | |
create(record, options) { | |
}, | |
read(record, options) { | |
}, | |
update(search, updateValues, options) { | |
}, | |
delete(record, options) { | |
}, | |
}; | |
Collection.prototype = proto; | |
module.exports = Collection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Winston = require('winston'); | |
const _ = require('lodash'); | |
const Collection = require('./collection'); | |
class Rest { | |
constructor(server, pgSettings) { | |
this.server = server; | |
this.db = new Collection(pgSettings); | |
this.route = {}; | |
this.route.before = []; | |
this.route.after = []; | |
} | |
} | |
module.exports = Rest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const connection = { | |
tableName: 'user', | |
connection: { | |
host: '192.168.99.100', | |
port: 5432, | |
user: 'pgUser', | |
password: 'password', | |
database: 'local' | |
} | |
}; | |
const User = new Rest({}, connection); | |
connection.tableName = 'comments'; | |
const Comments = new Rest({}, connection); | |
Comments.db.setModel('Comments', { | |
user: function() { | |
return this.belongsTo('User'); | |
} | |
}); | |
User.db.setModel({ | |
firstName: function() { | |
return this.get('first') + ' ' + this.get('last'); | |
}, | |
comments: function() { | |
return this.hasMany('Comments'); | |
} | |
}); | |
User.db.model.forge().fetch({ withRelated: ['comments']}).then((user) => { | |
console.log('user comments', user.related('comments')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment