123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- // ██████╗ ██████╗ ██████╗ ██████╗
- // ██╔══██╗██╔══██╗██╔═══██╗██╔══██╗
- // ██║ ██║██████╔╝██║ ██║██████╔╝
- // ██║ ██║██╔══██╗██║ ██║██╔═══╝
- // ██████╔╝██║ ██║╚██████╔╝██║
- // ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
- //
- module.exports = require('machine').build({
- friendlyName: 'Drop',
- description: 'Remove a table from the database.',
- 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,
- example: '==='
- },
- tableName: {
- description: 'The name of the table to destroy.',
- required: true,
- example: 'users'
- },
- meta: {
- friendlyName: 'Meta (custom)',
- description: 'Additional stuff to pass to the driver.',
- extendedDescription: 'This is reserved for custom driver-specific extensions.',
- example: '==='
- }
- },
- exits: {
- success: {
- description: 'The table was destroyed successfully.'
- },
- badConnection: {
- friendlyName: 'Bad connection',
- description: 'A connection either could not be obtained or there was an error using the connection.'
- }
- },
- fn: function drop(inputs, exits) {
- // Dependencies
- var _ = require('@sailshq/lodash');
- var Helpers = require('./private');
- // Set a flag if a leased connection from outside the adapter was used or not.
- var leased = _.has(inputs.meta, 'leasedConnection');
- // ╔═╗╔═╗╔═╗╦ ╦╔╗╔ ┌─┐┌─┐┌┐┌┌┐┌┌─┐┌─┐┌┬┐┬┌─┐┌┐┌
- // ╚═╗╠═╝╠═╣║║║║║║ │ │ │││││││├┤ │ │ ││ ││││
- // ╚═╝╩ ╩ ╩╚╩╝╝╚╝ └─┘└─┘┘└┘┘└┘└─┘└─┘ ┴ ┴└─┘┘└┘
- // Spawn a new connection to run the queries on.
- Helpers.connection.spawnOrLeaseConnection(inputs.datastore, inputs.meta, function spawnConnectionCb(err, connection) {
- if (err) {
- return exits.badConnection(err);
- }
- // ╔═╗╔═╗╔═╗╔═╗╔═╗╔═╗ ┌┬┐┌─┐┌┐ ┬ ┌─┐ ┌┐┌┌─┐┌┬┐┌─┐
- // ║╣ ╚═╗║ ╠═╣╠═╝║╣ │ ├─┤├┴┐│ ├┤ │││├─┤│││├┤
- // ╚═╝╚═╝╚═╝╩ ╩╩ ╚═╝ ┴ ┴ ┴└─┘┴─┘└─┘ ┘└┘┴ ┴┴ ┴└─┘
- var tableName;
- try {
- tableName = Helpers.schema.escapeTableName(inputs.tableName);
- } catch (e) {
- // Release the connection on error
- Helpers.connection.releaseConnection(connection, leased, function releaseConnectionCb() {
- return exits.error(e);
- });
- return;
- }
- // Build native query
- var query = 'DROP TABLE IF EXISTS ' + tableName + ';';
- // ╦═╗╦ ╦╔╗╔ ┌┬┐┬─┐┌─┐┌─┐ ┌─┐ ┬ ┬┌─┐┬─┐┬ ┬
- // ╠╦╝║ ║║║║ ││├┬┘│ │├─┘ │─┼┐│ │├┤ ├┬┘└┬┘
- // ╩╚═╚═╝╝╚╝ ─┴┘┴└─└─┘┴ └─┘└└─┘└─┘┴└─ ┴
- Helpers.query.runNativeQuery(connection, query, [], undefined, function runNativeQueryCb(err) {
- // Always release the connection back to the pool
- Helpers.connection.releaseConnection(connection, leased, function releaseConnectionCb() {
- if (err) {
- return exits.error(err);
- }
- return exits.success();
- }); // </ releaseConnection >
- }); // </ runNativeQuery >
- }); // </ spawnConnection >
- }
- });
|