sails-mongo-cloud
Version:
Mongo DB adapter for Sails.js/Waterline. Forked from sails-mongo and updated with Mongodb Driver v4.9.1
129 lines (95 loc) • 5.99 kB
JavaScript
module.exports = {
friendlyName: 'Create each (record)',
description: 'Insert multiple records into a collection in the database.',
inputs: {
query: require('../constants/query.input'),
connection: require('../constants/connection.input'),
dryOrm: require('../constants/dry-orm.input'),
},
exits: {
success: {
outputFriendlyName: 'Records (maybe)',
outputDescription: 'Either `null` or (if `fetch:true`) an array of new physical records that were created.',
outputExample: '==='
},
notUnique: require('../constants/not-unique.exit'),
},
fn: function (inputs, exits) {
// Dependencies
var _ = require('@sailshq/lodash');
var processNativeRecord = require('./private/process-native-record');
var processNativeError = require('./private/process-native-error');
var reifyValuesToSet = require('./private/reify-values-to-set');
// Local var for the stage 3 query, for easier access.
var s3q = inputs.query;
if (s3q.meta && s3q.meta.logMongoS3Qs) {
console.log('* * * * * *\nADAPTER (CREATE EACH RECORD):',require('util').inspect(s3q,{depth:5}),'\n');
}
// Local var for the `tableName`, for clarity.
var tableName = s3q.using;
// Grab the model definition
var WLModel = _.find(inputs.dryOrm.models, {tableName: tableName});
if (!WLModel) {
return exits.error(new Error('No model with that tableName (`'+tableName+'`) has been registered with this adapter. Were any unexpected modifications made to the stage 3 query? Could the adapter\'s internal state have been corrupted? (This error is usually due to a bug in this adapter\'s implementation.)'));
}//-•
// ╦═╗╔═╗╦╔═╗╦ ╦ ┌─┐┌─┐┌─┐┬ ┬ ┌┐┌┌─┐┬ ┬ ┬─┐┌─┐┌─┐┌─┐┬─┐┌┬┐
// ╠╦╝║╣ ║╠╣ ╚╦╝ ├┤ ├─┤│ ├─┤ │││├┤ │││ ├┬┘├┤ │ │ │├┬┘ ││
// ╩╚═╚═╝╩╚ ╩ └─┘┴ ┴└─┘┴ ┴ ┘└┘└─┘└┴┘ ┴└─└─┘└─┘└─┘┴└──┴┘
try {
_.each(s3q.newRecords, function (newRecord){
reifyValuesToSet(newRecord, WLModel, s3q.meta);
});
} catch (e) { return exits.error(e); }
// ╔╦╗╔═╗╔╦╗╔═╗╦═╗╔╦╗╦╔╗╔╔═╗ ┬ ┬┬ ┬┌─┐┌┬┐┬ ┬┌─┐┬─┐ ┌┬┐┌─┐ ╔═╗╔═╗╔╦╗╔═╗╦ ╦ ┌─┐┬─┐ ┌┐┌┌─┐┌┬┐
// ║║║╣ ║ ║╣ ╠╦╝║║║║║║║║╣ │││├─┤├┤ │ ├─┤├┤ ├┬┘ │ │ │ ╠╣ ║╣ ║ ║ ╠═╣ │ │├┬┘ ││││ │ │
// ═╩╝╚═╝ ╩ ╚═╝╩╚═╩ ╩╩╝╚╝╚═╝ └┴┘┴ ┴└─┘ ┴ ┴ ┴└─┘┴└─ ┴ └─┘ ╚ ╚═╝ ╩ ╚═╝╩ ╩ └─┘┴└─ ┘└┘└─┘ ┴
var isFetchEnabled;
if (s3q.meta && s3q.meta.fetch) { isFetchEnabled = true; }
else { isFetchEnabled = false; }
// ╔═╗╔═╗╔╦╗╔╦╗╦ ╦╔╗╔╦╔═╗╔═╗╔╦╗╔═╗ ┬ ┬┬┌┬┐┬ ┬ ┌┬┐┌┐
// ║ ║ ║║║║║║║║ ║║║║║║ ╠═╣ ║ ║╣ ││││ │ ├─┤ ││├┴┐
// ╚═╝╚═╝╩ ╩╩ ╩╚═╝╝╚╝╩╚═╝╩ ╩ ╩ ╚═╝ └┴┘┴ ┴ ┴ ┴ ─┴┘└─┘
// Create these new records in the database by inserting documents in the appropriate Mongo collection.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// FUTURE: Carry through the `fetch: false` optimization all the way to Mongo here,
// if possible (e.g. using Mongo's projections API)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
var db = inputs.connection;
var mongoCollection = db.collection(tableName);
// if (s3q.meta && s3q.meta.logMongoS3Qs) {
// console.log('- - - - - - - - - -CREATE EACH: s3q.newRecords:',require('util').inspect(s3q.newRecords,{depth:5}),'\n');
// }
mongoCollection.insertMany(s3q.newRecords, function (err, nativeResult) {
if (err) {
err = processNativeError(err);
if (err.footprint && err.footprint.identity === 'notUnique') {
return exits.notUnique(err);
}
return exits.error(err);
}//-•
// If `fetch` is NOT enabled, we're done.
if (!isFetchEnabled) {
return exits.success();
}//-•
// Otherwise, IWMIH we'll be sending back records:
// ============================================
var index = 0;
_.each(s3q.newRecords, function (record){
record.id = nativeResult.insertedIds[index];
++index;
});
nativeResult.ops = s3q.newRecords;
// ╔═╗╦═╗╔═╗╔═╗╔═╗╔═╗╔═╗ ┌┐┌┌─┐┌┬┐┬┬ ┬┌─┐ ┬─┐┌─┐┌─┐┌─┐┬─┐┌┬┐┌─┌─┐─┐
// ╠═╝╠╦╝║ ║║ ║╣ ╚═╗╚═╗ │││├─┤ │ │└┐┌┘├┤ ├┬┘├┤ │ │ │├┬┘ │││ └─┐ │
// ╩ ╩╚═╚═╝╚═╝╚═╝╚═╝╚═╝ ┘└┘┴ ┴ ┴ ┴ └┘ └─┘ ┴└─└─┘└─┘└─┘┴└──┴┘└─└─┘─┘
// Process record(s) (mutate in-place) to wash away adapter-specific eccentricities.
var phRecords = nativeResult.ops;
try {
_.each(phRecords, function (phRecord){
processNativeRecord(phRecord, WLModel, s3q.meta);
});
} catch (e) { return exits.error(e); }
return exits.success(phRecords);
}); // </ mongoCollection.insertMany() >
}
};