api.setTokens = function(tokens) { if (tokens.refresh_token) { this.refreshToken = tokens.refresh_token; } this.tokenPromise = q.fulfill(tokens); };
Model.prototype.entityImport = function(entity){ if (!entity) return Q.fulfill(entity); if (_.isArray(entity)) return Q.all(_.map(entity, this.entityImport.bind(this))); // -> entities // Convert var self = this; return Q() // beforeImport .then(function(){ return self.hooks.beforeImport(entity); }) // convertEntity .then(function(){ entity = self.converter.convertEntity('load', entity); }) // entityPrototype .then(function(){ if (self.options.entityPrototype) entity.__proto__ = self.options.entityPrototype; }) // afterImport .then(function(){ return self.hooks.afterImport(entity); }) // return .then(function(){ return entity; }); };
commits.then( function( commits ) { var addCommit = function( spec ) { return utils.addCommit.bind( null, pathToRepo, pathToExercise, spec ) } if ( commits ) { return commits.map( function( commit ) { return addCommit( commit ) }).reduce( q.when, q.fulfill() ) } else { return utils.git( pathToRepo, 'commit', [ '-m', 'Initial commit' ] ) } })
return function(entities){ // Do nothing when there's nothing to do :) if (!self._queryWith || !self._queryWith.rel || _.isEmpty(entities)) return Q.fulfill(entities); // Call every function with the stashed arguments return _.map(self._queryWith.rel, function(args){ return function(){ return self.doRelated.apply(self, [act, entities].concat(args)); }; }).reduce(Q.when, Q(1)) .thenResolve(entities); // -> entities };
Model.prototype.remove = function(entities, options){ if (!_.isObject(entities)) throw new errors.MissyModelError(this, 'Can only remove object or array'); var self = this; // Empty if (_.isEmpty(entities)) return Q.fulfill(entities); // Params var ctx = this._initContext({ entities: [].concat(entities), options: options || {} }); return Q() // Convert .then(function(){ // don't do entityExport() here, as the entity can be partial return _.map(ctx.entities, function(entity){ return self.converter.convertEntity('save', entity); }); }) // beforeRemove .then(function(entities){ ctx.entities = entities; return self.hooks.beforeRemove(ctx.entities, ctx); // -> [entities, ctx] }) // removeRelated .get(0) .then(self._applyQueryWith_rel('remove')) // -> entities // remove .then(function(){ return self.schema.driver.remove(self, ctx.entities, ctx.options); // -> entities }) // entityImport .then(function(entities){ ctx.entities = entities; return self.entityImport(ctx.entities); // -> entities }) // afterRemove .then(function(entities){ ctx.entities = entities; return self.hooks.afterRemove(ctx.entities, ctx); // -> [entities, ctx] }) // Return .spread(function(ret){ return _.isArray(entities)? ret : ret[0]; // -> entities || entity }); };
Model.prototype.save = function(entities, options){ if (!_.isObject(entities)) throw new errors.MissyModelError(this, 'Can only upsert object or array'); var self = this; // Empty if (_.isEmpty(entities)) return Q.fulfill(entities); // Params var ctx = this._initContext({ entities: [].concat(entities), options: options || {} }); return Q() // entityExport .then(function(){ return self.entityExport(ctx.entities); // -> entities }) // beforeSave .then(function(entities){ ctx.entities = entities; return self.hooks.beforeSave(ctx.entities, ctx); // -> [entities, ctx] }) // save .then(function(){ return self.schema.driver.save(self, self._withoutRelated(ctx.entities), ctx.options); // -> entities }) // saveRelated .then(function(entities){ return self._applyQueryWith_rel('save')(ctx.entities) .thenResolve(entities); // -> entities }) // entityImport .then(function(entities){ ctx.entities = entities; return self.entityImport(ctx.entities); // -> entities }) // afterSave .then(function(entities){ ctx.entities = entities; return self.hooks.afterSave(ctx.entities, ctx); // -> [entities, ctx] }) // Return .spread(function(ret){ return _.isArray(entities)? ret : ret[0]; // -> entities || entity }); };
exports.connect = function(options) { if (typeof options != 'object') return Q.reject("options must be an object"); if (!options.type) return Q.fulfill(null); switch (options.type) { case 'sqlite': return sqlite.connect(options); case 'mysql': return mysql.connect(options); default: return Q.reject(new Error('Unsupported data base type: ' + options.type)); } };
connect = function(){ return Q.fulfill(); };