Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
type: R.prop(findDataType(cacheData.dataType), sqlService.TYPES),
options
})
}
return params
}
/** SQL Service **/
const sqlService = {
// SQL type-mapping adapter. Add new types as required.
TYPES: {
BigInt: mssql.BigInt,
Bit: mssql.Bit,
Char: mssql.Char,
DateTimeOffset: mssql.DateTimeOffset,
DateTime: mssql.DateTime,
DateTime2: mssql.DateTime2,
Decimal: mssql.Decimal,
Float: mssql.Float,
Int: mssql.Int,
Numeric: mssql.Numeric,
NVarChar: mssql.NVarChar,
Real: mssql.Real,
SmallInt: mssql.SmallInt,
UniqueIdentifier: mssql.UniqueIdentifier
}
}
// Name of the admin database
sqlService.adminSchema = '[mtc_admin]'
sqlService.initPool = async () => {
// mark migration as applied in the database
if (_tableExists || !options.preview)
{
var request = tran.request();
var sql;
if (exists)
{
sql = 'UPDATE [' + migrationsTable + '] SET [Hash] = @hash, [ExecutionDate] = @execDate, [Duration] = @duration WHERE [Filename] = @script';
}
else
{
sql = 'INSERT [' + migrationsTable + '] ([Hash], [ExecutionDate], [Duration], [Filename]) VALUES(@hash, @execDate, @duration, @script)';
}
request.input('hash', MsSql.NVarChar(40), script.hash);
request.input('execDate', MsSql.DateTime, new Date());
request.input('duration', MsSql.Int, result.runtime);
request.input('script', MsSql.NVarChar(260), script.name);
request.query(sql, cb);
}
else
{
cb();
}
},
function (cb)
.then(function (bulkInsertResponse) {
logger.debug({
commandName: commandName,
message: 'Bulk insert operation complete',
bulkInsertResponse
});
if (bulkInsertResponse !== 'noIncrementalInventory') {
logger.debug({
message: 'Will delete the inserted/updated inventory from Azure SQL'
});
return sqlPool.request()
.input('inventory_per_page', sql.Int, INVENTORY_PER_PAGE)
.input('transfer_success_state', sql.Int, 1)
.input('transfer_time', sql.DateTime, new Date())
.query('UPDATE TOP (@inventory_per_page) ' + INVENTORY_SUM_TABLE +
' SET STOCKUPTRANSFER = @transfer_success_state, STOCKUPTRANSFERTIME = @transfer_time ' +
' WHERE %%physloc%% IN (' + rowIds + ')');
}
else {
return Promise.resolve('noIncrementalInventory');
}
})
.then(function (result) {
logger.debug({
message: 'Inserted/updated categories in products in DB',
result: {
upserted: result.nUpserted,
inserted: result.nInserted
},
commandName
});
logger.debug({
message: 'Will delete the inserted/updated products from Azure SQL',
commandName
});
return sqlPool.request()
.input('products_per_page', sql.Int, PRODUCTS_PER_PAGE)
.input('transfer_success_state', sql.Int, 1)
.input('transfer_time', sql.DateTime, new Date())
.query('UPDATE TOP (@products_per_page) ' + PRODUCT_TABLE +
' SET STOCKUPTRANSFER = @transfer_success_state, STOCKUPTRANSFERTIME = @transfer_time' +
' WHERE %%physloc%% IN (' + rowIds + ')');
})
.then(function (result) {
commandName: commandName,
message: 'Bulk insert operation complete'
});
logger.debug({
message: 'Inserted/updated sales in DB',
bulkInsertResponse,
commandName
});
logger.debug({
message: 'Will delete the inserted/updated sales from Azure SQL',
commandName
});
return sqlPool.request()
.input('sales_per_page', sql.Int, SALES_PER_PAGE)
.input('transfer_success_state', sql.Int, 1)
.input('transfer_time', sql.DateTime, new Date())
.query('UPDATE TOP (@sales_per_page) ' + SALES_TABLE +
' SET STOCKUPTRANSFER = @transfer_success_state, STOCKUPTRANSFERTIME = @transfer_time' +
' WHERE %%physloc%% IN (' + rowIds + ')');
})
.then(function (result) {
.then(function (result) {
logger.debug({
message: 'Inserted/updated products in DB',
result: {
upserted: result.nUpserted,
inserted: result.nInserted
}
});
logger.debug({
message: 'Will delete the inserted/updated products from Azure SQL',
commandName
});
return sqlPool.request()
.input('products_per_page', sql.Int, PRODUCTS_PER_PAGE)
.input('transfer_success_state', sql.Int, 1)
.input('transfer_time', sql.DateTime, new Date())
.query('UPDATE TOP (@products_per_page) ' + PRODUCT_TABLE +
' SET STOCKUPTRANSFER = @transfer_success_state, STOCKUPTRANSFERTIME = @transfer_time' +
' WHERE %%physloc%% IN (' + rowIds + ')');
})
.then(function (result) {
request.input(param.name, database.BigInt, value);
break;
case 'smallint':
case 'byte':
request.input(param.name, database.SmallInt, value);
break;
case 'string':
case 'nvarchar':
request.input(param.name, database.NVarChar, value);
break;
case 'boolean':
case 'bit':
request.input(param.name, database.Bit, value);
break;
case 'datetime':
request.input(param.name, database.DateTime, value);
break;
case 'smalldatetime':
request.input(param.name, database.SmallDateTime, value);
break;
case 'binary':
request.input(param.name, database.Binary, value);
break;
case 'image':
request.input(param.name, database.Image, value);
break;
case 'varbinary':
request.input(param.name, database.VarBinary, value);
break;
case 'varchar':
request.input(param.name, database.VarChar, value);
break;
Object.keys(schema.fields).forEach(function (code) {
const value = schema.fields[code];
const mssql = value.mssql || {};
const column = mssql.column || code;
let type = mssql.type, options = mssql.options;
if (!type) {
const jstype = _.keys(value) && _.keys(value).length > 0 ? value.type : value;
switch (jstype) {
case String:
type = sql.NVarChar(255);
break;
case Number:
type = sql.NVarChar(255);
break;
case Date:
type = sql.DateTime;
break;
case Boolean:
type = sql.Bit;
break;
default:
type = sql.NVarChar(255);
break;
}
if (['textarea', 'editor'].indexOf(value.ctrltype) != -1) type = sql.NVarChar(4000);
}
if (!options || !_.keys(options) || _.keys(options).length == 0) options = {nullable: (value.required ? false : true)};
table.columns.add(column, type, options);
});
let primaryKey = '';