Set the knex.raw queries to use insert parameter (#56)
* Set the knex.raw queries to use insert parameter * changed knex method to use whereIn istead of raw, added rewrite table names for all tables with - in the name. * fixed minor bug with interiors insert
This commit is contained in:
+85
-7
@@ -8,14 +8,92 @@ export const dbConfig = {
|
|||||||
stringcase: [
|
stringcase: [
|
||||||
'snakecase',
|
'snakecase',
|
||||||
(output) => {
|
(output) => {
|
||||||
if (output === 'product_products') {
|
switch (output) {
|
||||||
return 'product-products';
|
case 'delivery_methods':
|
||||||
} else if (output === 'order_rows_fields') {
|
return 'delivery-methods';
|
||||||
return 'order-rows_fields';
|
case 'delivery_methods_prices':
|
||||||
} else if (output === 'product_products_products') {
|
return 'delivery-methods_prices';
|
||||||
return 'product-products_products';
|
case 'designers_blocked':
|
||||||
|
return 'designers-blocked';
|
||||||
|
case 'i18n_currencies':
|
||||||
|
return 'i18n-currencies';
|
||||||
|
case 'i18n_currencies_names':
|
||||||
|
return 'i18n-currencies_names';
|
||||||
|
case 'i18n_currencies_territories':
|
||||||
|
return 'i18n-currencies_territories';
|
||||||
|
case 'i18n_languages':
|
||||||
|
return 'i18n-languages';
|
||||||
|
case 'i18n_languages_names':
|
||||||
|
return 'i18n-languages_names';
|
||||||
|
case 'i18n_languages_territories':
|
||||||
|
return 'i18n-languages_territories';
|
||||||
|
case 'i18n_territories':
|
||||||
|
return 'i18n-territories';
|
||||||
|
case 'i18n_territories_names':
|
||||||
|
return 'i18n-territories_names';
|
||||||
|
case 'i18n_texts':
|
||||||
|
return 'i18n-texts';
|
||||||
|
case 'i18n_texts_data':
|
||||||
|
return 'i18n-texts_data';
|
||||||
|
case 'order_fields':
|
||||||
|
return 'order-fields';
|
||||||
|
case 'order_orders':
|
||||||
|
return 'order-orders';
|
||||||
|
case 'order_orders_fields':
|
||||||
|
return 'order-orders_fields';
|
||||||
|
case 'order_row_fields':
|
||||||
|
return 'order-row_fields';
|
||||||
|
case 'order_rows':
|
||||||
|
return 'order-rows';
|
||||||
|
case 'order_rows_details':
|
||||||
|
return 'order-rows_details';
|
||||||
|
case 'product_currencies':
|
||||||
|
return 'product-currencies';
|
||||||
|
case 'product_fields':
|
||||||
|
return 'product-fields';
|
||||||
|
case 'product_groups':
|
||||||
|
return 'product-groups';
|
||||||
|
case 'product_materials':
|
||||||
|
return 'product-materials';
|
||||||
|
case 'product_printprices':
|
||||||
|
return 'product-printprices';
|
||||||
|
case 'product_printproducts':
|
||||||
|
return 'product-printproducts';
|
||||||
|
case 'product_printproducts_materials':
|
||||||
|
return 'product-printproducts_materials';
|
||||||
|
case 'product_products':
|
||||||
|
return 'product-products';
|
||||||
|
case 'product_products_fields':
|
||||||
|
return 'product-products_fields';
|
||||||
|
case 'product_products_products':
|
||||||
|
return 'product-products_products';
|
||||||
|
case 'product_status_history':
|
||||||
|
return 'product-status-history';
|
||||||
|
case 'product_status_history_lookup':
|
||||||
|
return 'product-status-history_lookup';
|
||||||
|
case 'product_stockprices':
|
||||||
|
return 'product-stockprices';
|
||||||
|
case 'product_stockproducts':
|
||||||
|
return 'product-stockproducts';
|
||||||
|
case 'product_types':
|
||||||
|
return 'product-types';
|
||||||
|
case 'stock_items':
|
||||||
|
return 'stock-items';
|
||||||
|
case 'stock_items_data':
|
||||||
|
return 'stock-items_data';
|
||||||
|
case 'stock_items_data_fields':
|
||||||
|
return 'stock-items_data_fields';
|
||||||
|
case 'stock_places':
|
||||||
|
return 'stock-places';
|
||||||
|
case 'stock_stock':
|
||||||
|
return 'stock-stock';
|
||||||
|
case 'stock_stock_data':
|
||||||
|
return 'stock-stock_data';
|
||||||
|
case 'stock_stock_data_fields':
|
||||||
|
return 'stock-stock_data_fields';
|
||||||
|
default:
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
return output;
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -351,19 +351,19 @@ export class ProductAPI extends BaseSQLDataSource {
|
|||||||
// DELETE
|
// DELETE
|
||||||
if (shouldDelete.length > 0) {
|
if (shouldDelete.length > 0) {
|
||||||
// Delete from product-printproducts_materials
|
// Delete from product-printproducts_materials
|
||||||
const printProductIds = shouldDelete
|
const printProductIds = shouldDelete.map((item) => item.printid);
|
||||||
.map((item) => item.printid)
|
|
||||||
.join(',');
|
|
||||||
|
|
||||||
// Delete from materials first, must be in sequence since we have constraint in materials to printproducts
|
// Delete from materials first, must be in sequence since we have constraint in materials to printproducts
|
||||||
await this.knex.raw(
|
await this.knex
|
||||||
`DELETE FROM "product-printproducts_materials" WHERE printid IN (${printProductIds})`,
|
.from('product-printproducts_materials')
|
||||||
);
|
.whereIn('printid', printProductIds)
|
||||||
|
.del();
|
||||||
|
|
||||||
// Delete from product-printproducts
|
// Delete from product-printproducts
|
||||||
await this.knex.raw(
|
await this.knex
|
||||||
`DELETE FROM "product-printproducts" WHERE printid IN (${printProductIds})`,
|
.from('product-printproducts')
|
||||||
);
|
.whereIn('printid', printProductIds)
|
||||||
|
.del();
|
||||||
}
|
}
|
||||||
|
|
||||||
// INSERT
|
// INSERT
|
||||||
@@ -388,16 +388,16 @@ export class ProductAPI extends BaseSQLDataSource {
|
|||||||
await Promise.all(insertionProducts);
|
await Promise.all(insertionProducts);
|
||||||
|
|
||||||
// Fetch the new printids to be able to insert new materials
|
// Fetch the new printids to be able to insert new materials
|
||||||
const newPrintIds = await this.knex.raw(
|
const newPrintIds = await this.knex
|
||||||
`SELECT printid, groupid FROM "product-printproducts" WHERE productid = ${productId} AND groupid IN (${shouldAdd.join(
|
.from('product-printproducts')
|
||||||
',',
|
.select('printid', 'groupid')
|
||||||
)})`,
|
.where('productid', productId)
|
||||||
);
|
.whereIn('groupid', shouldAdd);
|
||||||
|
|
||||||
// Insert printIds into product-printproducts_materials
|
// Insert printIds into product-printproducts_materials
|
||||||
const insertMaterials = [];
|
const insertMaterials = [];
|
||||||
const insertMaterialQuery = `INSERT INTO "product-printproducts_materials" (printid, materialid) VALUES (?, ?)`;
|
const insertMaterialQuery = `INSERT INTO "product-printproducts_materials" (printid, materialid) VALUES (?, ?)`;
|
||||||
newPrintIds.rows.forEach(({ printid, groupid }) => {
|
newPrintIds.forEach(({ printid, groupid }) => {
|
||||||
switch (groupid) {
|
switch (groupid) {
|
||||||
case ProductGroup.PHOTO_WALLPAPER:
|
case ProductGroup.PHOTO_WALLPAPER:
|
||||||
case ProductGroup.WALLPAPER:
|
case ProductGroup.WALLPAPER:
|
||||||
@@ -470,6 +470,9 @@ export class ProductAPI extends BaseSQLDataSource {
|
|||||||
const insertRegistry = uris.map((uri, index) => {
|
const insertRegistry = uris.map((uri, index) => {
|
||||||
let id = null;
|
let id = null;
|
||||||
let roomName = null;
|
let roomName = null;
|
||||||
|
if (uri.indexOf('/') !== 0) {
|
||||||
|
uri = '/' + uri;
|
||||||
|
}
|
||||||
if (uri.indexOf('/interiors/') > -1) {
|
if (uri.indexOf('/interiors/') > -1) {
|
||||||
const spl = uri.split('/');
|
const spl = uri.split('/');
|
||||||
roomName = `${spl[5].split('.')[0]}_${spl[4]}_${spl[3]}`;
|
roomName = `${spl[5].split('.')[0]}_${spl[4]}_${spl[3]}`;
|
||||||
@@ -530,13 +533,21 @@ export class ProductAPI extends BaseSQLDataSource {
|
|||||||
// Insert room-ids, printids and position into "interiors" table.
|
// Insert room-ids, printids and position into "interiors" table.
|
||||||
const insertPromises = [];
|
const insertPromises = [];
|
||||||
insertRegistry.forEach((item) => {
|
insertRegistry.forEach((item) => {
|
||||||
let sql;
|
|
||||||
if (item.roomName) {
|
if (item.roomName) {
|
||||||
sql = `INSERT INTO interiors (print_id, room_id, position) VALUES (${insertPrintId}, (SELECT id FROM rooms WHERE name = '${item.roomName}'), ${item.position})`;
|
insertPromises.push(
|
||||||
|
this.knex.raw(
|
||||||
|
`INSERT INTO interiors (print_id, room_id, position) VALUES (?, (SELECT id FROM rooms WHERE name = ?), ?)`,
|
||||||
|
[insertPrintId, item.roomName, item.position],
|
||||||
|
),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
sql = `INSERT INTO interiors (id, print_id, position) VALUES (${item.id}, ${insertPrintId}, ${item.position})`;
|
insertPromises.push(
|
||||||
|
this.knex.raw(
|
||||||
|
`INSERT INTO interiors (id, print_id, position) VALUES (?, ?, ?)`,
|
||||||
|
[item.id, insertPrintId, item.position],
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
insertPromises.push(this.knex.raw(sql));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return Promise.all(insertPromises);
|
return Promise.all(insertPromises);
|
||||||
|
|||||||
@@ -49,3 +49,22 @@ export function roundOrDefaultTo(
|
|||||||
const number = parseFloat(v);
|
const number = parseFloat(v);
|
||||||
return roundToDecimals(number, decimalPlaces);
|
return roundToDecimals(number, decimalPlaces);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @function createQuestionMarkFromList
|
||||||
|
*
|
||||||
|
* @description For knex.raw queries the input of a list should be split into input to the knex.raw function
|
||||||
|
* So for the example below we need to split input into questionmarks.
|
||||||
|
* const list = [1,2,3];
|
||||||
|
* this.knex.raw(`SELECT * FROM table WHERE id IN (${createQuestMarksFromList(list)})`, [...list]);
|
||||||
|
* will result in
|
||||||
|
* this.knex.raw(`SELECT * FROM table WHERE id IN (?,?,?)`, [1,2,3]);
|
||||||
|
* Read more: https://knexjs.org/#Raw
|
||||||
|
*
|
||||||
|
* @param {unknown[]} arr
|
||||||
|
*
|
||||||
|
* @return {string} "?,?,?"
|
||||||
|
*/
|
||||||
|
export const createQuestionMarksFromList = (arr: unknown[]): string => {
|
||||||
|
return '?,'.repeat(arr.length).slice(0, -1);
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user