parse-osmos-driver
Version:
A Parse SDK Driver for the OSMOS ODM
237 lines (213 loc) • 7.02 kB
JavaScript
(function () {
'use strict';
var _ = require('lodash');
function ParseDriver(Parse) {
this.Parse = Parse;
}
/**
* completes the repetivate query task
* @param {Parse.Query} q The query object to do it on
* @param {Object} query Query object, equal to the q._where varaible
*/
function doQuery(q, query) {
var keys = _.keys(query);
q._include.concat(keys);
keys.forEach(function (key) {
q._where[key] = _.clone(query[key], true);
});
return q;
}
ParseDriver.prototype = {
/**
* Creates a new document
* @param {Osmos.Model} model Osmos Model instance
* @param {Function} cb success or error callback
* @return {void}
*/
create: function (model, cb) {
cb(null, {});
},
/**
* Retrieves a document from the store. A document-not-found error should
* be passed to the callback as (null, null).
* @param {Osmos.Model} model Osmos Model instance
* @param {String} key the key to find
* @param {Function} cb success or error callback
* @return {void}
*/
get: function (model, key, cb) {
new this.Parse.Query(this.Parse.Object.extend(model.bucket))
.get(key)
.then(function (doc) {
if (doc) {
cb(null, doc.toJSON());
} else {
cb(null, null);
}
}, function (error) {
cb(error);
});
},
/**
* Inserts a new document into the data store. The data represents the
* serialized document and, under most circumstances, can be inserted
* directly into the data store. The document argument, which references
* the Osmos document that is requesting the insertion operation, is also
* passed along for convenience.
* @param {Osmos.Document} document The cument that is requestiong the insert
* operation
* @param {Object} data The data that needs to be inserted in the databse.
* @param {Function} cb The callback
* @return {void}
*/
post: function (document, data, cb) {
var newDoc = new this.Parse.Object(document.model.bucket);
if (typeof data === 'object') {
Object.keys(data)
.forEach(function (key) {
newDoc.set(key, data[key]);
});
newDoc.save()
.then(function (doc) {
document.primaryKey = doc.id;
cb(null, doc.toJSON());
}, function (e) {
cb(e);
});
} else {
cb(new Error("data needs to be an object!"));
}
},
/**
* Similar to post, but updates an already existing document.
* @param {Osmos.Document} document The document which to update
* @param {Object} data The data to insert
* @param {Function} cb Callback
*/
put: function (document, data, cb) {
var self = this;
process.nextTick(function () {
if (!document.model.schema.primaryKey || !document.primaryKey) {
throw new OsmosError('You cannot put a document without a primary key');
}
var primaryKey = document.__originalData__[document.schema.primaryKey];
new self.Parse.Query(self.Parse.Object.extend(document.model.bucket))
.get(primaryKey)
.then(function (doc) {
if (typeof data === 'object' && data.constructor.name === 'Object') {
Object.keys(data)
.forEach(function (key) {
doc.set(key, data);
});
return doc.save();
} else {
cb(new Error("data needs to be and object"));
}
})
.then(function () {
cb(null);
}, function (er) {
cb(er);
});
});
},
/**
* Deletes a document with a specific key.
* @param {Osmos.Model} model The model on which to destroy the data from
* @param {String} key the object to destroy
* @param {Function} cb [description]
* @return {[type]} [description]
*/
del: function (model, key, cb) {
if (typeof key === 'object') {
key = key[Object.keys(key)[0]];
}
new this.Parse.Query(this.Parse.Object.extend(model.bucket))
.get(key)
.then(function (doc) {
doc.destroy({
success: function () {
cb(null);
},
error: function (err) {
cb(err, null); // nothig to destroy
}
});
}, function (err) {
cb(new Error(err.message + ' key was ' + key));
});
},
/**
* Retrieves at most one Parse.Object that satisfies this query. Either
* options.success or options.error is called when it completes. success
* is passed the object if there is one. otherwise, undefined.
* @param {Osmos.Model} model The model on to which to do the query
* @param {Object} query The query object
* @param {Function} cb callback
*/
findOne: function (model, query, cb) {
var q = new this.Parse.Query(this.Parse.Object.extend(model.bucket));
doQuery(q, query);
q.first({
success: function (doc) {
if (doc) {
cb(null, doc.toJSON());
} else {
cb(null, null);
}
},
error: function (er) {
cb(er);
}
});
},
/**
* Retrieves a list of ParseObjects that satisfy this query.
* Either options.success or options.error is called when the
* find completes.
* @param {Osmos.Model} model Model to do the find queries on
* @param {Object} query Similar to the Parse.Query.prototype._where Object
* @param {Function} cb callback
*/
find: function (model, query, cb) {
var q = new this.Parse.Query(this.Parse.Object.extend(model.bucket));
q = doQuery(q, query);
q.find({
success: function (docs) {
if (docs) {
var ds = [];
docs.forEach(function (doc) {
ds.push(doc.toJSON());
});
cb(null, ds);
} else {
cb(null, null);
}
},
error: function (er) {
cb(er);
}
});
},
/**
* [findLimit description]
* @param {[type]} model [description]
* @param {[type]} query [description]
* @param {[type]} start [description]
* @param {[type]} limit [description]
* @param {Function} cb [description]
*/
findLimit: function (model, query, start, limit, cb) {
},
/**
* [count description]
* @param {[type]} model [description]
* @param {[type]} query [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
count: function (model, query, cb) {
}
};
module.exports = ParseDriver;
})();