Table.prototype.insert = function(docs, options, query) { options = options || {}; try { util.assertType(docs, ['OBJECT', 'ARRAY'], query); } catch(err) { typeValue = util.getType(docs); throw new Error.ReqlRuntimeError("Cannot convert "+typeValue+" to SEQUENCE", this.frames) } var result = util.writeResult(); var generatedKeys = []; var newDoc; if (docs instanceof Sequence) { for(var i=0; i<docs.length; i++) { newResult = this._singleInsert(docs.get(i), options, query); util.mergeWriteResult(result, newResult); } } else { newResult = this._singleInsert(docs, options, query); util.mergeWriteResult(result, newResult); } return result; }
Selection.prototype.delete = function() { var result = helper.writeResult(); for(var i=0; i<this.selection.length; i++) { result.deleted += this.selection[i].delete().deleted; } return result; }
Selection.prototype.delete = function(options, query) { var result = util.writeResult(); for(var i=0; i<this.selection.length; i++) { util.mergeWriteResult(result, this.selection[i].delete(options, query)); } return result; }
Sequence.prototype.forEach = function(fn, query, internalOptions) { var result = util.writeResult(); for(var i=0; i<this.sequence.length; i++) { if (util.isFunction(fn)) { var varId = util.getVarId(fn); query.context[varId] = this.sequence[i]; writeResult = query.evaluate(fn, this, internalOptions); util.assertType(writeResult, 'DATUM', query); util.mergeWriteResult(result, writeResult); delete query.context[varId]; } else { writeResult = query.evaluate(fn, this, internalOptions); if (typeof writeResult === 'function') { try { writeResult = writeResult(util.toDatum(this.sequence[i])); } catch(error) { query.frames.push(1); throw new Error.ReqlRuntimeError(error.toString(), query.frames) } util.mergeWriteResult(result, writeResult); } else { util.mergeWriteResult(result, writeResult); } } } return result; }
}, {concurrency: 1}).then(function(mergedResults) { var result = util.writeResult(); for(var i=0; i<mergedResults.length; i++) { util.mergeWriteResult(result, mergedResults[i]); } return result; });
Selection.prototype.update = function(rawUpdate, options, query) { var result = util.writeResult(); var primaryKey = this.table.options.primaryKey; var updateValue; var varId; for(var i=0; i<this.selection.length; i++) { util.mergeWriteResult(result, this.selection[i].update(rawUpdate, options, query)) } return result; }
Selection.prototype.replace = function(newValue, options, query) { options = options || {}; var result = helper.writeResult(); var primaryKey = this.table.options.primaryKey; var replaceValue; for(var i=0; i<this.selection.length; i++) { helper.mergeWriteResult(result, this.selection[i].replace(newValue, options, query)) } return result; }
Selection.prototype.replace = function(rawUpdate, options, query) { var result = util.writeResult(); var primaryKey = this.table.options.primaryKey; var replaceValue; for(var i=0; i<this.selection.length; i++) { var temp = this.selection[i].replace(rawUpdate, options, query); util.mergeWriteResult(result, temp) //util.mergeWriteResult(result, this.selection[i].replace(rawUpdate, options, query)) } return result; }
Selection.prototype.update = function(rawUpdate, options, query) { options = options || {}; var result = helper.writeResult(); var primaryKey = this.table.options.primaryKey; var updateValue; for(var i=0; i<this.selection.length; i++) { //TODO Set context for function updateValue = query.evaluate(rawUpdate); helper.mergeWriteResult(result, this.selection[i].update(updateValue, options, query)) } return result; }
Table.prototype._singleInsert = function(doc, options, query) { var self = this; var result = util.writeResult(); doc = new Document(doc, this); // Forward events doc.on('change', function(change) { self.emit('change', change); }); var pk = this.options.primaryKey; if (doc.doc[pk] === undefined) { // Keep generating a uuid until one is free... var uuid; while (true) { uuid = util.uuid(); if (this.documents[uuid] === undefined) { doc.doc[pk] = uuid; this.documents[util.makeInternalPk(uuid)] = doc; this.emit('change', {new_val: doc.doc, old_val: null}) if (!util.isSequence(result.generated_keys)) { result.generated_keys = new Sequence(); } result.generated_keys.push(uuid); result.inserted++; } } } else { // Can throw, `insert` will catch it var internalPk = util.makeInternalPk(doc.doc[pk]); if (this.documents[internalPk] === undefined) { this.documents[internalPk] = doc; this.emit('change', {new_val: doc.doc, old_val: null}) result.inserted++; if (options.return_changes === true) { result.changes = new Sequence([{ new_val: util.deepCopy(doc.doc), old_val: null }]) } } else { if (options.conflict === 'replace') { var oldValue = util.deepCopy(this.documents[internalPk].doc) this.documents[internalPk] = doc; result.replaced++; if (options.return_changes === true) { result.changes = new Sequence([{ new_val: util.deepCopy(doc.doc), old_val: oldValue }]) } this.emit('change', {new_val: doc.doc, old_val: oldValue}) } else if (options.conflict === 'update') { var oldValue = util.deepCopy(this.documents[internalPk].doc) var changed = util._merge(this.documents[internalPk].doc, doc.doc); if (changed === true) { result.replaced++; this.emit('change', {new_val: this.documents[internalPk].doc, old_val: oldValue}) if (options.return_changes === true) { result.changes = new Sequence([{ new_val: util.deepCopy(this.documents[internalPk].doc), old_val: oldValue }]) } } } else { result.first_error = 'Duplicate primary key `'+pk+'`:'+JSON.stringify(this.documents[internalPk].toDatum(), null, 4) result.errors++; if (options.return_changes === true) { result.changes = new Sequence([{ new_val: util.deepCopy(this.documents[internalPk].doc), old_val: util.deepCopy(this.documents[internalPk].doc) }]) } } } } if (result.errors === 0) { this._saveOriginal(doc.doc[pk], this.documents[internalPk].doc); } return result; }
Document.prototype.update = function(newValue, options, query, internalOptions) { var result = util.writeResult(); var primaryKey = this.table.options.primaryKey; // Update context var updateValue; if (util.isFunction(newValue)) { var varId = util.getVarId(newValue); query.context[varId] = this; if (options.non_atomic === true) { try { updateValue = query.evaluate(newValue, internalOptions) } catch(err) { result.errors++; result.first_error = err.message+'.'; // WHAT?!? return result; } } else { try { updateValue = query.evaluate(newValue, {deterministic: true}) } catch(err) { if (err.message.match(/^Could not prove function deterministic. Maybe you want to use the non_atomic flag?/)) { throw err; } else { result.errors++; result.first_error = err.message+'.'; return result; } } } delete query.context[varId] } else { if (options.non_atomic === true) { try { updateValue = query.evaluate(newValue, internalOptions) } catch(err) { result.errors++; result.first_error = err.message+'.'; return result; } } else { try { updateValue = query.evaluate(newValue, {deterministic: true}) } catch(err) { if (err.message.match(/^Could not prove function deterministic. Maybe you want to use the non_atomic flag?/)) { throw err; } result.errors++; result.first_error = err.message+'.'; return result; } } if (typeof updateValue === 'function') { try { updateValue = updateValue(util.toDatum(this)); util.assertJavaScriptResult(updateValue, query); } catch(err) { result.errors++; result.first_error = err.message+'.'; return result; } updateValue = util.revertDatum(updateValue); } } try { util.assertType(updateValue, 'DATUM', this); } catch(err) { result.errors++; result.first_error = err.message; return result; } if ((updateValue[primaryKey] !== undefined) && (this.doc[primaryKey] !== updateValue[primaryKey])) { result.errors++; result.first_error = "Primary key `id` cannot be changed(`"+ JSON.stringify(util.toDatum(updateValue), null, 2)+"` -> `"+ JSON.stringify(this.doc, null, 2)+"`)" } else { var old_val = util.deepCopy(this.doc); if (updateValue instanceof Document) { updateValue = updateValue.doc; } var changed = util._merge(this.doc, updateValue); if (changed === true) { result.replaced++; this.emit('change', {new_val: this.doc, old_val: old_val}); if (options.return_changes === true) { result.changes = new Sequence([{ new_val: util.deepCopy(this.doc), old_val: old_val }]) } } else { result.unchanged++; } } if (result.errors === 0) { this.table._saveOriginal(this.doc[primaryKey], util.deepCopy(this.doc)); } return result; }