fh-wfm-sync
Advanced tools
Comparing version 0.3.3 to 1.0.0-RC1
@@ -22,2 +22,4 @@ var _ = require('lodash'); | ||
dataSetData = _.compact(dataSetData); | ||
//Sorting the data set by their IDs | ||
@@ -24,0 +26,0 @@ return _.sortBy(dataSetData, function(singleDataSetItem) { |
@@ -22,3 +22,4 @@ module.exports = { | ||
FORCE_SYNC: "force_sync" | ||
} | ||
}, | ||
FH_MBAAS_API_VERSION_RANGE: "^7.0.0" | ||
}; |
@@ -7,71 +7,102 @@ 'use strict'; | ||
var debug = require('./utils/logger')(__filename); | ||
var CONSTANTS = require('./constants'); | ||
var buildQuery = require('./db/query'); | ||
var shortid = require('shortid'); | ||
function initSync(mediator, mbaasApi, datasetId, syncOptions) { | ||
var semver = require('semver'); | ||
/** | ||
* @param {*} store | ||
* store.db - mongoConnection | ||
* store.dataListHandler etc. override for custom data handling | ||
*/ | ||
function initSync(mediator, mbaasApi, datasetId, syncOptions, store) { | ||
syncOptions = syncOptions || defaultConfig.syncOptions; | ||
debug('Sync init'); | ||
var dataListHandler = function(datasetId, queryParams, cb) { | ||
//start the sync service | ||
var deferred = q.defer(); | ||
//Checking the version of fh-mbaas-api passed. | ||
if (!semver.valid(mbaasApi.getVersion(), CONSTANTS.FH_MBAAS_API_VERSION_RANGE)) { | ||
deferred.reject(new Error("Invalid fh-mbaas-api version . Expected " + CONSTANTS.FH_MBAAS_API_VERSION_RANGE + " but got " + mbaasApi.getVersion())); | ||
return deferred.promise; | ||
} | ||
var dataListHandler = function(datasetId, queryParams, cb, metadata) { | ||
if (store.dataListHandler) { | ||
return store.dataListHandler(datasetId, queryParams, cb, metadata); | ||
} | ||
queryParams = queryParams || {}; | ||
var uid = shortid.generate(); | ||
queryParams.topicUid = uid; | ||
var query = buildQuery(queryParams.filter); | ||
var resultPromise = store.db.collection(datasetId).find(query); | ||
mediator.request('wfm:cloud:' + datasetId + ':list', queryParams, {uid: uid}) | ||
.then(function(data) { | ||
var syncData = {}; | ||
data.forEach(function(object) { | ||
syncData[object.id] = object; | ||
}); | ||
cb(null, syncData); | ||
}, function(error) { | ||
debug('Sync error: init:', datasetId, error); | ||
cb(error); | ||
}); | ||
if (queryParams.sort && typeof queryParams.sort === 'object') { | ||
resultPromise = resultPromise.sort(queryParams.sort); | ||
} | ||
return resultPromise.toArray().then(function(list) { | ||
return cb(null, list); | ||
}).catch(function(err) { | ||
return cb(err); | ||
}); | ||
}; | ||
var dataCreateHandler = function(datasetId, data, cb) { | ||
var ts = new Date().getTime(); // TODO: replace this with a proper uniqe (eg. a cuid) | ||
mediator.request('wfm:cloud:' + datasetId + ':create', [data, ts], {uid: ts}) | ||
.then(function(object) { | ||
var res = { | ||
"uid": object.id, | ||
"data": object | ||
}; | ||
cb(null, res); | ||
}, function(error) { | ||
debug('Sync error: init:', datasetId, error); | ||
cb(error); | ||
}); | ||
var dataCreateHandler = function(datasetId, data, cb, metadata) { | ||
if (store.dataCreateHandler) { | ||
return store.dataCreateHandler(datasetId, data, cb, metadata); | ||
} | ||
store.db.collection(datasetId).insertOne(data).then(function() { | ||
return cb(null, data); | ||
}).catch(function(err) { | ||
return cb(err); | ||
}); | ||
}; | ||
var dataSaveHandler = function(datasetId, uid, data, cb) { | ||
mediator.request('wfm:cloud:' + datasetId + ':update', data, {uid: uid}) | ||
.then(function(object) { | ||
cb(null, object); | ||
}, function(error) { | ||
debug('Sync error: init:', datasetId, error); | ||
cb(error); | ||
}); | ||
var dataSaveHandler = function(datasetId, uid, data, cb, metadata) { | ||
if (store.dataSaveHandler) { | ||
return store.dataSaveHandler(datasetId, uid, data, cb, metadata); | ||
} | ||
var query; | ||
if (!_.isObject(data)) { | ||
return cb("Expected an object to update"); | ||
} | ||
if (data._id) { | ||
delete data._id; | ||
} | ||
if (data.id) { | ||
query = {id: data.id}; | ||
} else if (data._localuid) { | ||
query = {_localuid: data._localuid}; | ||
} else { | ||
return cb("Expected the object to have either an id or _localuid field"); | ||
} | ||
return store.db.collection(datasetId).replaceOne(query, data).then(function() { | ||
return cb(null, data); | ||
}).catch(function(err) { | ||
return cb(err); | ||
}); | ||
}; | ||
var dataGetHandler = function(datasetId, uid, cb) { | ||
mediator.request('wfm:cloud:' + datasetId + ':read', uid) | ||
.then(function(object) { | ||
cb(null, object); | ||
}, function(error) { | ||
debug('Sync error: init:', datasetId, error); | ||
cb(error); | ||
var dataGetHandler = function(datasetId, uid, cb, metadata) { | ||
if (store.dataGetHandler) { | ||
return store.dataGetHandler(datasetId, uid, cb, metadata); | ||
} | ||
store.db.collection(datasetId).findOne({id: uid}, {_id: 0}) | ||
.then(function(result) { | ||
if (!result) { | ||
return cb("Missing result"); | ||
} | ||
return cb(null, result); | ||
}).catch(function(err) { | ||
return cb(err); | ||
}); | ||
}; | ||
var dataDeleteHandler = function(datasetId, uid, cb) { | ||
mediator.request('wfm:cloud:' + datasetId + ':delete', uid) | ||
.then(function(message) { | ||
cb(null, message); | ||
}, function(error) { | ||
debug('Sync error: init:', datasetId, error); | ||
cb(error); | ||
}); | ||
var dataDeleteHandler = function(datasetId, uid, cb, metadata) { | ||
if (store.dataDeleteHandler) { | ||
return store.dataDeleteHandler(datasetId, uid, cb, metadata); | ||
} | ||
store.db.collection(datasetId).deleteOne({id: uid}).then(function(object) { | ||
return cb(null, object); | ||
}).catch(function(err) { | ||
return cb(err); | ||
}); | ||
}; | ||
@@ -81,4 +112,2 @@ | ||
//start the sync service | ||
var deferred = q.defer(); | ||
mbaasApi.sync.init(datasetId, syncOptions, function(err) { | ||
@@ -102,3 +131,3 @@ if (err) { | ||
if (_.isFunction(syncOptions.hashFunction)) { | ||
mbaasApi.sync.handleHash(datasetId, syncOptions.hashFunction); | ||
mbaasApi.sync.setRecordHashFn(datasetId, syncOptions.hashFunction); | ||
} | ||
@@ -105,0 +134,0 @@ |
{ | ||
"name": "fh-wfm-sync", | ||
"version": "0.3.3", | ||
"version": "1.0.0-RC1", | ||
"description": "An sync module for WFM", | ||
@@ -29,3 +29,4 @@ "main": "lib/angular/sync-ng.js", | ||
"rx": "4.1.0", | ||
"shortid": "2.2.8" | ||
"semver": "5.3.0", | ||
"shortid": "^2.2.8" | ||
}, | ||
@@ -42,3 +43,2 @@ "devDependencies": { | ||
"express": "4.13.4", | ||
"fh-mbaas-api": "6.1.6", | ||
"grunt": "1.0.1", | ||
@@ -51,3 +51,2 @@ "grunt-cli": "1.2.0", | ||
"nodemon": "1.9.1", | ||
"shortid": "^2.2.6", | ||
"should": "8.3.0", | ||
@@ -54,0 +53,0 @@ "sinon": "1.17.6", |
@@ -9,2 +9,7 @@ # FeedHenry WFM sync | ||
## Notice | ||
Version `1.x` of this module requires `fh-mbaas-api 7.x`. | ||
Version `0.x` of this module supports older versions of `fh-mbaas-api`. | ||
## Client-side usage | ||
@@ -11,0 +16,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
933070
19
50
25055
548
7
6
+ Addedsemver@5.3.0
+ Addednanoid@2.1.11(transitive)
+ Addedsemver@5.3.0(transitive)
+ Addedshortid@2.2.16(transitive)
- Removedshortid@2.2.8(transitive)
Updatedshortid@^2.2.8