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:
@@ -351,19 +351,19 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
// DELETE
|
||||
if (shouldDelete.length > 0) {
|
||||
// Delete from product-printproducts_materials
|
||||
const printProductIds = shouldDelete
|
||||
.map((item) => item.printid)
|
||||
.join(',');
|
||||
const printProductIds = shouldDelete.map((item) => item.printid);
|
||||
|
||||
// Delete from materials first, must be in sequence since we have constraint in materials to printproducts
|
||||
await this.knex.raw(
|
||||
`DELETE FROM "product-printproducts_materials" WHERE printid IN (${printProductIds})`,
|
||||
);
|
||||
await this.knex
|
||||
.from('product-printproducts_materials')
|
||||
.whereIn('printid', printProductIds)
|
||||
.del();
|
||||
|
||||
// Delete from product-printproducts
|
||||
await this.knex.raw(
|
||||
`DELETE FROM "product-printproducts" WHERE printid IN (${printProductIds})`,
|
||||
);
|
||||
await this.knex
|
||||
.from('product-printproducts')
|
||||
.whereIn('printid', printProductIds)
|
||||
.del();
|
||||
}
|
||||
|
||||
// INSERT
|
||||
@@ -388,16 +388,16 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
await Promise.all(insertionProducts);
|
||||
|
||||
// Fetch the new printids to be able to insert new materials
|
||||
const newPrintIds = await this.knex.raw(
|
||||
`SELECT printid, groupid FROM "product-printproducts" WHERE productid = ${productId} AND groupid IN (${shouldAdd.join(
|
||||
',',
|
||||
)})`,
|
||||
);
|
||||
const newPrintIds = await this.knex
|
||||
.from('product-printproducts')
|
||||
.select('printid', 'groupid')
|
||||
.where('productid', productId)
|
||||
.whereIn('groupid', shouldAdd);
|
||||
|
||||
// Insert printIds into product-printproducts_materials
|
||||
const insertMaterials = [];
|
||||
const insertMaterialQuery = `INSERT INTO "product-printproducts_materials" (printid, materialid) VALUES (?, ?)`;
|
||||
newPrintIds.rows.forEach(({ printid, groupid }) => {
|
||||
newPrintIds.forEach(({ printid, groupid }) => {
|
||||
switch (groupid) {
|
||||
case ProductGroup.PHOTO_WALLPAPER:
|
||||
case ProductGroup.WALLPAPER:
|
||||
@@ -470,6 +470,9 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
const insertRegistry = uris.map((uri, index) => {
|
||||
let id = null;
|
||||
let roomName = null;
|
||||
if (uri.indexOf('/') !== 0) {
|
||||
uri = '/' + uri;
|
||||
}
|
||||
if (uri.indexOf('/interiors/') > -1) {
|
||||
const spl = uri.split('/');
|
||||
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.
|
||||
const insertPromises = [];
|
||||
insertRegistry.forEach((item) => {
|
||||
let sql;
|
||||
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 {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user