import { PersistentRow, Row_Sample as Row } from "jm-castle-ac-dc-types"; import { InsertResponse, SelectResponse, UpdateResponse, } from "jm-castle-types"; import { without } from "../../../utils/Basic.js"; import { MariaDbClient } from "../MariaDb.mjs"; import { TableSample } from "../tables/Sample.js"; import { Filter_NameLike, valuesClause } from "./QueryUtils.mjs"; const table = TableSample; export const insert = async ( values: Row & PersistentRow, client: MariaDbClient ): Promise> => { try { const cmd = `INSERT INTO ${table.id} SET${valuesClause(values)}`; const response: any = await client.getDatabasePool().query(cmd); const { affectedRows } = response || {}; return { result: { cmd, affectedRows, data: values } }; } catch (error) { return { error: error.toString() }; } }; export const update = async ( values: Row & PersistentRow, client: MariaDbClient ): Promise> => { try { const { sample_id, dataset_version } = values; const valuesToUpdate = without({ ...values }, "sample_id"); const cmd = `UPDATE ${table.id} SET${valuesClause( valuesToUpdate )} WHERE sample_id = '${sample_id}' AND dataset_version = ${dataset_version}`; const response: any = await client.getDatabasePool().query(cmd); const { affectedRows } = response || {}; if (affectedRows === 1) { return { result: { cmd, affectedRows, data: { ...values, ...valuesToUpdate } }, }; } else { const { result, error } = await selectByKey(sample_id, client); const { rows } = result || {}; if (rows && rows.length === 1) { const existingRow = rows[0]; return { error: `The current dataset_version of sample (${sample_id}) is ${existingRow.dataset_version}. You tried to update with dataset_version ${dataset_version}. Refresh your data first.`, }; } if (rows && rows.length === 0) { return { error: `The sample (${sample_id}) was not found.` }; } if (error) { return { error: `Sample (${sample_id}) was not updated. Received error when checking for reason: ${error}`, }; } return { error: `Fatal error when updating sample (${sample_id}). Sample was not updated.`, }; } } catch (error) { return { error: error.toString() }; } }; export const selectByKey = async ( sampleId: string, client: MariaDbClient ): Promise> => { try { const cmd = `SELECT * FROM ${table.id} WHERE sample_id = '${sampleId}'`; const queryResult = await client.getDatabasePool().query(cmd); const rows: Row[] = []; queryResult.forEach((r: Row) => rows.push(r)); return { result: { cmd, rows } }; } catch (error) { return { error: error.toString() }; } }; export const select = async ( filter: Filter_NameLike, client: MariaDbClient ): Promise> => { try { const { name } = filter; const cmd = `SELECT * FROM ${table.id} WHERE name LIKE '${name}'`; const queryResult = await client.getDatabasePool().query(cmd); const rows: Row[] = []; queryResult.forEach((r: Row) => rows.push(r)); return { result: { cmd, rows } }; } catch (error) { return { error: error.toString() }; } }; export const all = async ( client: MariaDbClient ): Promise> => { try { const cmd = `SELECT * FROM ${table.id}`; const queryResult = await client.getDatabasePool().query(cmd); const rows: Row[] = []; queryResult.forEach((r: Row) => rows.push(r)); return { result: { cmd, rows } }; } catch (error) { return { error: error.toString() }; } };