Skip to content

Instantly share code, notes, and snippets.

@jstrimpel
Last active August 29, 2015 14:09
Show Gist options
  • Save jstrimpel/1d76b55b06494a5e7468 to your computer and use it in GitHub Desktop.
Save jstrimpel/1d76b55b06494a5e7468 to your computer and use it in GitHub Desktop.
mongodb collection syncher example
// app/server/synchers/base.js
define(['lazoSyncher'], function (LazoSyncher) {
'use strict';
// set up mongo connection here; not sure of best practices for establishing connections
// https://www.npmjs.org/package/mongodb
return LazoSyncer.extend({
fetch: function (options) {
// get data from mongo
// pass the document name down; could come from parameters; doesn't really matter
var collection = db.collection(options.document);
collection.find({}).toArray(function (err, docs) {
if (err) {
return options.error(err);
}
// filter results
options.success(docs);
});
}
});
});
// models/model_name/server/syncher.js
define(['app/server/synchers/base'], function (BaseSyncher) {
'use strict';
return BaseSyncher.extend({
// override the base if needed
fetch: function (options) {
// syncher specific code, e.g. document name, set up filters
BaseSyncher.prototype.fetch.call(this, options);
}
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment