Last active
December 26, 2015 22:39
-
-
Save ritch/7224836 to your computer and use it in GitHub Desktop.
How to implement adapter pattern inside a LoopBack connector
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
var adapters = { | |
redis: RedisJobQueue, | |
foo: FooJobQueue, | |
bar: BarJobQueue | |
}; | |
function JobQueueConnector(settings) { | |
this.DataAccessObject = adapters[settings.type]; | |
} | |
// legacy api for jugglingdb compatibility | |
JobQueueConnector.initialize = function(dataSource, callback) { | |
var connector = dataSource.connector = new JobQueueConnector(dataSource.settings); | |
callback(); | |
} | |
function RedisJobQueue() { | |
} | |
RedisJobQueue.scheduleJob = function (id, options) { | |
// approximation | |
require('magic-redis-module').put(id, serialize(options)); | |
} | |
function BarJobQueue(id, options) { | |
} | |
BarJobQueue.scheduleJob = function (id, options) { | |
require('bar-module').schedule(it, options); | |
} | |
// ----- end connector ----- | |
// use with a model | |
var lb = require('loopback'); | |
var Job = lb.Model.extend('job'); | |
Job.attachTo(lb.createDataSource({connector: JobQueueConnector, type: 'redis'})); | |
// actual usage | |
Job.scheduleJob('some job', {...}); | |
// add to app | |
app.model(Job); | |
// use over rest | |
POST /jobs | |
{...options...} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment