Updates
This commit is contained in:
@@ -89,6 +89,7 @@ export default async function apiPost({
|
||||
delete clonedTargetTable.childrenTables;
|
||||
delete clonedTargetTable.updateData;
|
||||
delete clonedTargetTable.indexes;
|
||||
delete clonedTargetTable.uniqueConstraints;
|
||||
|
||||
tableSchema = clonedTargetTable;
|
||||
}
|
||||
|
||||
@@ -3,41 +3,14 @@ import updateDbEntry from "./updateDbEntry";
|
||||
import _ from "lodash";
|
||||
import connDbHandler from "../../../utils/db/conn-db-handler";
|
||||
import checkIfIsMaster from "../../../utils/check-if-is-master";
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
import debugLog from "../../../utils/logging/debug-log";
|
||||
import {
|
||||
AddDbEntryParam,
|
||||
APIResponseObject,
|
||||
DSQL_TableSchemaType,
|
||||
PostInsertReturn,
|
||||
} from "../../../types";
|
||||
import purgeDefaultFields from "../../../utils/purge-default-fields";
|
||||
import grabParsedValue from "./grab-parsed-value";
|
||||
import { ConnectionConfig } from "mariadb";
|
||||
|
||||
export type AddDbEntryParam<
|
||||
T extends { [k: string]: any } = any,
|
||||
K extends string = string
|
||||
> = {
|
||||
dbContext?: (typeof DbContextsArray)[number];
|
||||
paradigm?: "Read Only" | "Full Access";
|
||||
dbFullName?: string;
|
||||
tableName: K;
|
||||
data?: T;
|
||||
batchData?: T[];
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
duplicateColumnName?: keyof T;
|
||||
duplicateColumnValue?: string | number;
|
||||
/**
|
||||
* Update Entry if a duplicate is found.
|
||||
* Requires `duplicateColumnName` and `duplicateColumnValue` parameters
|
||||
*/
|
||||
update?: boolean;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
forceLocal?: boolean;
|
||||
debug?: boolean;
|
||||
dbConfig?: ConnectionConfig;
|
||||
};
|
||||
|
||||
/**
|
||||
* Add a db Entry Function
|
||||
@@ -61,6 +34,7 @@ export default async function addDbEntry<
|
||||
forceLocal,
|
||||
debug,
|
||||
dbConfig,
|
||||
onDuplicate,
|
||||
}: AddDbEntryParam<T, K>): Promise<APIResponseObject<PostInsertReturn>> {
|
||||
const isMaster = forceLocal
|
||||
? true
|
||||
@@ -205,7 +179,7 @@ export default async function addDbEntry<
|
||||
const { insertKeysArray, insertValuesArray, queryValuesArray } =
|
||||
generateQuery(newData);
|
||||
|
||||
const query = `INSERT INTO ${
|
||||
let query = `INSERT INTO ${
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` (${insertKeysArray.join(
|
||||
","
|
||||
@@ -213,6 +187,13 @@ export default async function addDbEntry<
|
||||
|
||||
const finalQueryValues = grabFinalQueryValuesArr(queryValuesArray);
|
||||
|
||||
if (onDuplicate) {
|
||||
query += ` ON DUPLICATE KEY ${onDuplicate.sql}`;
|
||||
if (onDuplicate.values) {
|
||||
finalQueryValues.push(...onDuplicate.values);
|
||||
}
|
||||
}
|
||||
|
||||
const newInsert = await connDbHandler({
|
||||
query,
|
||||
values: finalQueryValues,
|
||||
@@ -246,7 +227,7 @@ export default async function addDbEntry<
|
||||
batchQueryValuesArray.push(queryValuesArray);
|
||||
}
|
||||
|
||||
const query = `INSERT INTO ${
|
||||
let query = `INSERT INTO ${
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` (${batchInsertKeysArray?.join(
|
||||
","
|
||||
@@ -258,6 +239,13 @@ export default async function addDbEntry<
|
||||
batchQueryValuesArray.flat()
|
||||
);
|
||||
|
||||
if (onDuplicate) {
|
||||
query += ` ON DUPLICATE KEY ${onDuplicate.sql}`;
|
||||
if (onDuplicate.values) {
|
||||
finalQueryValues.push(...onDuplicate.values);
|
||||
}
|
||||
}
|
||||
|
||||
const newInsert = await connDbHandler({
|
||||
query,
|
||||
values: finalQueryValues,
|
||||
|
||||
Reference in New Issue
Block a user