UNPKG

sails-arangojs

Version:

A sails-arangojs adapter for Sails / Waterline

251 lines (212 loc) 10.9 kB
// ██╗ ██╗██████╗ ██████╗ █████╗ ████████╗███████╗ █████╗ ██████╗████████╗██╗ ██████╗ ███╗ ██╗ // ██║ ██║██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝ ██╔══██╗██╔════╝╚══██╔══╝██║██╔═══██╗████╗ ██║ // ██║ ██║██████╔╝██║ ██║███████║ ██║ █████╗ ███████║██║ ██║ ██║██║ ██║██╔██╗ ██║ // ██║ ██║██╔═══╝ ██║ ██║██╔══██║ ██║ ██╔══╝ ██╔══██║██║ ██║ ██║██║ ██║██║╚██╗██║ // ╚██████╔╝██║ ██████╔╝██║ ██║ ██║ ███████╗ ██║ ██║╚██████╗ ██║ ██║╚██████╔╝██║ ╚████║ // ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ // module.exports = require('machine').build({ friendlyName: 'Upsert', description: 'Upsert record(s) in the database based on a query criteria.', inputs: { datastore: { description: 'The datastore to use for connections.', extendedDescription: 'Datastores represent the config and manager required to obtain an active database connection.', required: true, readOnly: true, example: '===', }, models: { description: 'An object containing all of the model definitions that have been registered.', required: true, example: '===', }, query: { description: 'A valid stage three Waterline query.', required: true, example: '===', }, }, exits: { success: { description: 'The records were successfully upsertd.', outputVariableName: 'records', outputType: 'ref', }, invalidDatastore: { description: 'The datastore used is invalid. It is missing key pieces.', }, badConnection: { friendlyName: 'Bad connection', description: 'A connection either could not be obtained or there was an error using the connection.', }, notUnique: { friendlyName: 'Not Unique', outputType: 'ref', }, }, fn: async function upsert(inputs, exits) { // Dependencies const _ = require('@sailshq/lodash'); const Helpers = require('./private'); const flaverr = require('flaverr'); const { query, models } = inputs; // Store the Query input for easier access query.meta = query.meta || {}; // Find the model definition const WLModel = models[query.using]; if (!WLModel) { return exits.invalidDatastore(); } // Grab the pk column name (for use below) let pkColumnName; try { pkColumnName = WLModel.attributes[WLModel.primaryKey].columnName; } catch (e) { return exits.error(e); } // Set a flag to determine if records are being returned let fetchRecords = false; let trx; let mergeObjects = false; // ╔═╗╦═╗╔═╗ ╔═╗╦═╗╔═╗╔═╗╔═╗╔═╗╔═╗ ┬─┐┌─┐┌─┐┌─┐┬─┐┌┬┐┌─┐ // ╠═╝╠╦╝║╣───╠═╝╠╦╝║ ║║ ║╣ ╚═╗╚═╗ ├┬┘├┤ │ │ │├┬┘ ││└─┐ // ╩ ╩╚═╚═╝ ╩ ╩╚═╚═╝╚═╝╚═╝╚═╝╚═╝ ┴└─└─┘└─┘└─┘┴└──┴┘└─┘ // Process each record to normalize output try { Helpers.query.preProcessRecord({ records: [query.valuesToSet], identity: WLModel.identity, model: WLModel, }); delete query.valuesToSet[pkColumnName]; } catch (e) { return exits.error(e); } // ╔═╗╔═╗╔╗╔╦ ╦╔═╗╦═╗╔╦╗ ┌┬┐┌─┐ ┌─┐┌┬┐┌─┐┌┬┐┌─┐┌┬┐┌─┐┌┐┌┌┬┐ // ║ ║ ║║║║╚╗╔╝║╣ ╠╦╝ ║ │ │ │ └─┐ │ ├─┤ │ ├┤ │││├┤ │││ │ // ╚═╝╚═╝╝╚╝ ╚╝ ╚═╝╩╚═ ╩ ┴ └─┘ └─┘ ┴ ┴ ┴ ┴ └─┘┴ ┴└─┘┘└┘ ┴ // Convert the Waterline criteria into a Waterline Query Statement. This // turns it into something that is declarative and can be easily used to // build a SQL query. // See: https://github.com/treelinehq/waterline-query-docs for more info // on Waterline Query Statements. let statement; try { statement = Helpers.query.compileStatement({ pkColumnName, model: query.using, method: 'upsert', criteria: query.criteria, values: query.valuesToSet, }); } catch (e) { return exits.error(e); } // ╔╦╗╔═╗╔╦╗╔═╗╦═╗╔╦╗╦╔╗╔╔═╗ ┬ ┬┬ ┬┬┌─┐┬ ┬ ┬ ┬┌─┐┬ ┬ ┬┌─┐┌─┐ // ║║║╣ ║ ║╣ ╠╦╝║║║║║║║║╣ │││├─┤││ ├─┤ └┐┌┘├─┤│ │ │├┤ └─┐ // ═╩╝╚═╝ ╩ ╚═╝╩╚═╩ ╩╩╝╚╝╚═╝ └┴┘┴ ┴┴└─┘┴ ┴ └┘ ┴ ┴┴─┘└─┘└─┘└─┘ // ┌┬┐┌─┐ ┬─┐┌─┐┌┬┐┬ ┬┬─┐┌┐┌ // │ │ │ ├┬┘├┤ │ │ │├┬┘│││ // ┴ └─┘ ┴└─└─┘ ┴ └─┘┴└─┘└┘ if (_.has(query.meta, 'fetch') && query.meta.fetch) { fetchRecords = true; } if (_.has(query.meta, 'trx') && query.meta.trx) { trx = query.meta.trx; } if (_.has(query.meta, 'mergeObjects') && !query.meta.mergeObjects) { mergeObjects = false; } // ╔═╗╔═╗╔═╗╦ ╦╔╗╔ ┌─┐┌─┐┌┐┌┌┐┌┌─┐┌─┐┌┬┐┬┌─┐┌┐┌ // ╚═╗╠═╝╠═╣║║║║║║ │ │ │││││││├┤ │ │ ││ ││││ // ╚═╝╩ ╩ ╩╚╩╝╝╚╝ └─┘└─┘┘└┘┘└┘└─┘└─┘ ┴ ┴└─┘┘└┘ // ┌─┐┬─┐ ┬ ┬┌─┐┌─┐ ┬ ┌─┐┌─┐┌─┐┌─┐┌┬┐ ┌─┐┌─┐┌┐┌┌┐┌┌─┐┌─┐┌┬┐┬┌─┐┌┐┌ // │ │├┬┘ │ │└─┐├┤ │ ├┤ ├─┤└─┐├┤ ││ │ │ │││││││├┤ │ │ ││ ││││ // └─┘┴└─ └─┘└─┘└─┘ ┴─┘└─┘┴ ┴└─┘└─┘─┴┘ └─┘└─┘┘└┘┘└┘└─┘└─┘ ┴ ┴└─┘┘└┘ // Spawn a new connection for running queries on. const { dbConnection, dsName } = Helpers.connection.getConnection( inputs.datastore, query.meta ); let session; let result; let updatedRecords = []; try { // ╦═╗╦ ╦╔╗╔ ┬ ┬┌─┐┌┬┐┌─┐┌┬┐┌─┐ ┌─┐ ┬ ┬┌─┐┬─┐┬ ┬ // ╠╦╝║ ║║║║ │ │├─┘ ││├─┤ │ ├┤ │─┼┐│ │├┤ ├┬┘└┬┘ // ╩╚═╚═╝╝╚╝ └─┘┴ ─┴┘┴ ┴ ┴ └─┘ └─┘└└─┘└─┘┴└─ ┴ const updatecriteria = statement.criteria; const upsertvalues = `${statement.values}`.replace(/OLD\./g, 'record.'); const insetvalues = `${statement.insertvalues}`.replace( /OLD\./g, 'record.' ); let sql = ` UPSERT ${updatecriteria} INSERT ${insertvalues} UPDATE ${upsertvalues} IN ${statement.tableName}`; sql = `${sql} OPTIONS { ignoreRevs: false, mergeObjects: ${ mergeObjects ? 'true' : 'false' } }`; if (fetchRecords) { sql = `${sql} RETURN {new: NEW, old: OLD}`; } let cursor; if (trx) { cursor = await trx.step(() => dbConnection.query(sql)); } else { cursor = await dbConnection.query(sql); } if (fetchRecords) { updatedRecords = await cursor.map((r) => global[`${WLModel.globalId}Object`].initialize(r.new, dsName) ); } } catch (error) { if (dbConnection) { Helpers.connection.releaseConnection(dbConnection); } if (error.code === 409) { return exits.notUnique( flaverr( { name: 'upsert error', code: 'E_UNIQUE', }, error ) ); } return exits.badConnection(error); } // If `fetch` is NOT enabled, we're done. if (!fetchRecords) { Helpers.connection.releaseConnection(dbConnection); return exits.success(); } // -• // Otherwise, IWMIH we'll be sending back records: // ============================================ // ╔═╗╦═╗╔═╗╔═╗╔═╗╔═╗╔═╗ ┌┐┌┌─┐┌┬┐┬┬ ┬┌─┐ ┬─┐┌─┐┌─┐┌─┐┬─┐┌┬┐┌─┌─┐─┐ // ╠═╝╠╦╝║ ║║ ║╣ ╚═╗╚═╗ │││├─┤ │ │└┐┌┘├┤ ├┬┘├┤ │ │ │├┬┘ │││ └─┐ │ // ╩ ╩╚═╚═╝╚═╝╚═╝╚═╝╚═╝ ┘└┘┴ ┴ ┴ ┴ └┘ └─┘ ┴└─└─┘└─┘└─┘┴└──┴┘└─└─┘─┘ // Process record(s) (mutate in-place) to wash away adapter-specific eccentricities. try { await Helpers.connection.releaseConnection(dbConnection); const newrecords = updatedRecords.map((record) => Helpers.query.processNativeRecord(record, WLModel, query.meta) ); // Helpers.query.processNativeRecord(record.new, WLModel, query.meta); // Helpers.query.processNativeRecord(record.old, WLModel, query.meta); return exits.success({ records: newrecords }); } catch (e) { if (session) { await Helpers.connection.releaseConnection(session); } return exits.error(e); } }, });