Updates
This commit is contained in:
@@ -118,13 +118,19 @@ export default async function addDbEntry<
|
||||
const dataKeys = Object.keys(data);
|
||||
|
||||
let insertKeysArray = [];
|
||||
let insertValuesArray = [];
|
||||
let insertValuesArray: (string | number)[] = [];
|
||||
|
||||
for (let i = 0; i < dataKeys.length; i++) {
|
||||
try {
|
||||
const dataKey = dataKeys[i];
|
||||
let value = data[dataKey];
|
||||
|
||||
const targetFieldSchema = tableSchema
|
||||
? tableSchema?.fields?.find(
|
||||
(field) => field.fieldName === dataKey
|
||||
)
|
||||
: null;
|
||||
|
||||
const parsedValue = grabParsedValue({
|
||||
dataKey,
|
||||
encryptionKey,
|
||||
@@ -137,7 +143,9 @@ export default async function addDbEntry<
|
||||
|
||||
insertKeysArray.push("`" + dataKey + "`");
|
||||
|
||||
if (typeof parsedValue == "number") {
|
||||
if (targetFieldSchema?.dataType?.match(/vector/i)) {
|
||||
insertValuesArray.push(`VEC_FromText('${parsedValue}')`);
|
||||
} else if (typeof parsedValue == "number") {
|
||||
insertValuesArray.push(String(parsedValue));
|
||||
} else {
|
||||
insertValuesArray.push(parsedValue);
|
||||
@@ -163,11 +171,28 @@ export default async function addDbEntry<
|
||||
insertKeysArray.push("`date_updated_code`");
|
||||
insertValuesArray.push(Date.now());
|
||||
|
||||
const queryValuesArray = insertValuesArray;
|
||||
const queryValuesArray = insertValuesArray as (string | number)[];
|
||||
|
||||
return { queryValuesArray, insertValuesArray, insertKeysArray };
|
||||
}
|
||||
|
||||
function grabQueryValuesString(arr: (string | number)[]) {
|
||||
return arr
|
||||
.map((v, i) => {
|
||||
if (v.toString().match(/VEC_FromText/i)) {
|
||||
return v;
|
||||
}
|
||||
return "?";
|
||||
})
|
||||
.join(",");
|
||||
}
|
||||
|
||||
function grabFinalQueryValuesArr(arr: (string | number)[]) {
|
||||
return arr
|
||||
.filter((v) => !v.toString().match(/VEC_FromText/i))
|
||||
.map((v) => String(v));
|
||||
}
|
||||
|
||||
if (newData) {
|
||||
const { insertKeysArray, insertValuesArray, queryValuesArray } =
|
||||
generateQuery(newData);
|
||||
@@ -176,12 +201,14 @@ export default async function addDbEntry<
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` (${insertKeysArray.join(
|
||||
","
|
||||
)}) VALUES (${insertValuesArray.map(() => "?").join(",")})`;
|
||||
)}) VALUES (${grabQueryValuesString(insertValuesArray)})`;
|
||||
|
||||
const finalQueryValues = grabFinalQueryValuesArr(queryValuesArray);
|
||||
|
||||
const newInsert = await connDbHandler(
|
||||
null,
|
||||
query,
|
||||
queryValuesArray,
|
||||
finalQueryValues,
|
||||
debug
|
||||
);
|
||||
|
||||
@@ -190,7 +217,7 @@ export default async function addDbEntry<
|
||||
payload: newInsert,
|
||||
queryObject: {
|
||||
sql: query,
|
||||
params: queryValuesArray,
|
||||
params: finalQueryValues,
|
||||
},
|
||||
};
|
||||
} else if (newBatchData) {
|
||||
@@ -216,16 +243,17 @@ export default async function addDbEntry<
|
||||
}\`${tableName}\` (${batchInsertKeysArray?.join(
|
||||
","
|
||||
)}) VALUES ${batchInsertValuesArray
|
||||
.map((vl) => `(${vl.map(() => "?").join(",")})`)
|
||||
.map((vl) => `(${grabQueryValuesString(vl)})`)
|
||||
.join(",")}`;
|
||||
|
||||
console.log("query", query);
|
||||
console.log("batchQueryValuesArray", batchQueryValuesArray);
|
||||
const finalQueryValues = grabFinalQueryValuesArr(
|
||||
batchQueryValuesArray.flat()
|
||||
);
|
||||
|
||||
const newInsert = await connDbHandler(
|
||||
null,
|
||||
query,
|
||||
batchQueryValuesArray.flat(),
|
||||
finalQueryValues,
|
||||
debug
|
||||
);
|
||||
|
||||
@@ -242,7 +270,7 @@ export default async function addDbEntry<
|
||||
payload: newInsert,
|
||||
queryObject: {
|
||||
sql: query,
|
||||
params: batchQueryValuesArray.flat(),
|
||||
params: finalQueryValues,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
|
||||
@@ -25,13 +25,9 @@ export default function grabParsedValue({
|
||||
}: Param): any {
|
||||
let newValue = value;
|
||||
|
||||
const targetFieldSchemaArray = tableSchema
|
||||
? tableSchema?.fields?.filter((field) => field.fieldName === dataKey)
|
||||
const targetFieldSchema = tableSchema
|
||||
? tableSchema?.fields?.find((field) => field.fieldName === dataKey)
|
||||
: null;
|
||||
const targetFieldSchema =
|
||||
targetFieldSchemaArray && targetFieldSchemaArray[0]
|
||||
? targetFieldSchemaArray[0]
|
||||
: null;
|
||||
|
||||
if (typeof newValue == "undefined") return;
|
||||
if (typeof newValue == "object" && !newValue) newValue = null;
|
||||
|
||||
@@ -80,6 +80,12 @@ export default async function updateDbEntry<
|
||||
const dataKey = dataKeys[i];
|
||||
let value = newData[dataKey];
|
||||
|
||||
const targetFieldSchema = tableSchema
|
||||
? tableSchema?.fields?.find(
|
||||
(field) => field.fieldName === dataKey
|
||||
)
|
||||
: null;
|
||||
|
||||
const parsedValue = grabParsedValue({
|
||||
dataKey,
|
||||
encryptionKey,
|
||||
@@ -90,7 +96,11 @@ export default async function updateDbEntry<
|
||||
|
||||
if (typeof parsedValue == "undefined") continue;
|
||||
|
||||
updateKeyValueArray.push(`\`${dataKey}\`=?`);
|
||||
if (targetFieldSchema?.dataType?.match(/vector/i)) {
|
||||
updateKeyValueArray.push(`\`${dataKey}\`=VEC_FromText(?)`);
|
||||
} else {
|
||||
updateKeyValueArray.push(`\`${dataKey}\`=?`);
|
||||
}
|
||||
|
||||
if (typeof parsedValue == "number") {
|
||||
updateValues.push(String(parsedValue));
|
||||
|
||||
Reference in New Issue
Block a user