Created
November 9, 2015 19:47
-
-
Save randomnerd/8cf6539b5e28838cb72e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import dbmon from 'dbmon'; | |
import EventEmitter from 'events'; | |
export default function(name, schema) { | |
return { | |
setup(app) { | |
this.db = app.db; | |
this.model = this.db.define(name, schema, { freezeTableName: true }); | |
this.model.sync(); | |
let emitter = new EventEmitter; | |
let channel = dbmon.channel({ | |
driver: 'postgresql', | |
driverOpts: { | |
// postgresql: { cli: app.pgcli } | |
postgresql: { connStr: 'tcp://127.0.0.1:5432/lbtest' } | |
}, | |
table: name, | |
monitor: 'all', | |
keyfld: { name: 'id', type: 'integer' }, | |
transports: 'console,eventemitter', | |
transportsOpts: { | |
eventEmitter: { eventEmitter: emitter} | |
} | |
}); | |
emitter.on('insert', (row) => { | |
console.log('insert', row); | |
this.model.findOne({where: {id: row.k}}).then((obj) => { | |
console.log('created', JSON.stringify(obj)); | |
this.emit('created', obj); | |
}) | |
}); | |
}, | |
find(params, callback) { | |
this.model.findAll().then((models) => { | |
callback(null, models); | |
}).error(callback); | |
}, | |
create(data, params, callback) { | |
this.model.create(data).then((model) => { | |
callback(null, model); | |
}).error(callback); | |
}, | |
get(id, params, callback) { | |
this.model.findOne({id}).then((model) => { | |
callback(null, model); | |
}).error(callback); | |
}, | |
update(id, data, params, callback) { | |
this.model.findOne({id}).then((model) => { | |
model.update(data).then((model) => { | |
callback(null, model); | |
}).error(callback); | |
}).error(callback); | |
}, | |
patch(id, data, params, callback) { | |
this.update(id, data, params, callback); | |
}, | |
remove(id, params, callback) { | |
this.model.findOne({id}).then((model) => { | |
model.destroy().then(() => { | |
callback(null, model); | |
}).error(callback); | |
}).error(callback); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment