diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/bridge/index.js | 12 | ||||
| -rw-r--r-- | lib/db/index.js | 22 | ||||
| -rw-r--r-- | lib/server/index.js | 61 | ||||
| -rw-r--r-- | lib/worker/index.js | 3 |
4 files changed, 88 insertions, 10 deletions
diff --git a/lib/bridge/index.js b/lib/bridge/index.js index 096c3b9..199dfb3 100644 --- a/lib/bridge/index.js +++ b/lib/bridge/index.js @@ -43,16 +43,16 @@ ipc.serve( () => { console.log('>>> worker connected') ipc.server.emit(socket, 'message', true) }) -// ipc.server.on('message', (data, socket) => { -// ipc.log('got a message : '.debug, data); + ipc.server.on('message', (data, socket) => { + ipc.log('got a message : '.debug, data); // ipc.server.emit( // socket, // 'message', // data+' world!' // ) -// }) -// ipc.server.on( 'socket.disconnected', (socket, destroyedSocketID) => { -// ipc.log('client ' + destroyedSocketID + ' has disconnected!'); -// }) + }) + ipc.server.on( 'socket.disconnected', (socket, destroyedSocketID) => { + ipc.log('client ' + destroyedSocketID + ' has disconnected!'); + }) }) ipc.server.start() diff --git a/lib/db/index.js b/lib/db/index.js index 2e63f74..1c7cc15 100644 --- a/lib/db/index.js +++ b/lib/db/index.js @@ -13,7 +13,7 @@ var File = db.File = bookshelf.Model.extend({ hasTimestamps: true, }) var Job = db.Job = bookshelf.Model.extend({ - tableName: 'job', + tableName: 'jobs', hasTimestamps: true, }) var Task = db.Task = bookshelf.Model.extend({ @@ -37,12 +37,19 @@ db.crud = function(model) { qb.orderBy("id", "desc") if (limit) qb.limit( limit ) if (offset) qb.offset( offset ) + // console.log(qb) return qb }).fetchAll() }, show: (id) => { return new model({'id': id}).fetch() }, + show_ids: (ids) => { + return model.query( (qb) => { + qb.whereIn('id', ids) + return qb + }).fetchAll() + }, create: (data) => { return new model(data).save() }, @@ -54,3 +61,16 @@ db.crud = function(model) { }, } } + +function memoize (f) { + const o = {} + return (model) => { + console.log(model) + t = model.toString() + if (o[t]) { + return o[t] + } + o[t] = f(model) + return o[t] + } +}
\ No newline at end of file diff --git a/lib/server/index.js b/lib/server/index.js index 5069478..5b1150c 100644 --- a/lib/server/index.js +++ b/lib/server/index.js @@ -35,7 +35,9 @@ site.init = function(bridge){ }) const api_files = crud(app, 'file', db.File) const api_jobs = crud(app, 'job', db.Job) - const api_tasks = crud(app, 'task', db.Task) + const api_tasks = crud(app, 'task', db.Task, { + hasOne: { content_file_id: db.File, style_file_id: db.File } + }) app.get('/devices', (req, res) => { res.json( bridge.devices ) @@ -91,7 +93,41 @@ site.init = function(bridge){ app.get(type, (req, res) => { console.log('index', type) crud.index(req.query).then( (data) => { - res.json(data ? data.toJSON() : []) + + if (! callbacks.hasOne) { + res.json(data ? data.toJSON() : []) + } + else { + let recs = data.toJSON() + const loader = new Loader () + loader.onReady( () => { + console.log(type, 'ready') + res.json(recs) + }) + console.log('hasOne') + loader.register('hasOne') + Object.keys(callbacks.hasOne).forEach( (key,i) => { + loader.register(key) + console.log('key') + const type = callbacks.hasOne[key] + const id_lookup = {} + recs.forEach(r => { + const id = r[key] + id_lookup[id] = id_lookup[id] || [] + id_lookup[id].push(r) + }) + console.log(key, recs.length, Object.keys(id_lookup).length) + db.crud(type).show_ids(Object.keys(id_lookup)).then( (sub_recs) => { + console.log(key, 'sub_recs', sub_recs) + const short_key = key.replace('_id','') + sub_recs.toJSON().forEach(rec => { + id_lookup[rec.id].forEach( parent_rec => parent_rec[short_key] = rec ) + }) + loader.ready(key) + }) + }) + loader.ready('hasOne') + } }) // }).catch( () => res.sendStatus(500) ) }) @@ -99,7 +135,26 @@ site.init = function(bridge){ app.get(type_id, (req, res) => { console.log('show', type, req.params.id) crud.show(req.params.id).then( (data) => { - res.json(data.toJSON()) + if (! callbacks.hasOne) { + res.json(data.toJSON()) + } + else { + rec = data.toJSON() + const loader = new Loader () + loader.onReady( () => { + res.json(rec) + }) + loader.register('hasOne') + Object.keys(callbacks.hasOne).forEach( (key,i) => { + loader.register(key) + const type = callbacks.hasOne[key] + db.crud(type).show(rec[key]).then( (sub_rec) => { + rec[key] = sub_rec + loader.ready(key) + }) + }) + loader.ready('hasOne') + } }) // .catch( (err) => res.sendStatus(500) ) }) diff --git a/lib/worker/index.js b/lib/worker/index.js index 604756b..52f12e2 100644 --- a/lib/worker/index.js +++ b/lib/worker/index.js @@ -29,4 +29,7 @@ ipc.connectTo('cortex', () => { ipc.of.cortex.on('job', (data) => { ipc.log('received job: '.debug, data); }) + ipc.of.cortex.on('task', (data) => { + ipc.log('received task: '.debug, data); + }) }) |
