Updates
This commit is contained in:
parent
80df059135
commit
a90706de5d
@ -2,12 +2,20 @@ interface SQLInsertGenReturn {
|
||||
query: string;
|
||||
values: string[];
|
||||
}
|
||||
type DataFn = () => {
|
||||
placeholder: string;
|
||||
value: string | number | Float32Array<ArrayBuffer>;
|
||||
};
|
||||
type DataType = {
|
||||
[k: string]: string | number | DataFn | undefined | null;
|
||||
};
|
||||
type Params = {
|
||||
data: DataType[];
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
};
|
||||
/**
|
||||
* # SQL Insert Generator
|
||||
*/
|
||||
export default function sqlInsertGenerator({ tableName, data, dbFullName, }: {
|
||||
data: any[];
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLInsertGenReturn | undefined;
|
||||
export default function sqlInsertGenerator({ tableName, data, dbFullName, }: Params): SQLInsertGenReturn | undefined;
|
||||
export {};
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
"use strict";
|
||||
// @ts-check
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = sqlInsertGenerator;
|
||||
/**
|
||||
@ -18,19 +17,28 @@ function sqlInsertGenerator({ tableName, data, dbFullName, }) {
|
||||
}
|
||||
});
|
||||
});
|
||||
/** @type {string[]} */
|
||||
let queryBatches = [];
|
||||
/** @type {string[]} */
|
||||
let queryValues = [];
|
||||
data.forEach((item) => {
|
||||
queryBatches.push(`(${insertKeys
|
||||
.map((ky) => {
|
||||
var _a, _b;
|
||||
queryValues.push(((_b = (_a = item[ky]) === null || _a === void 0 ? void 0 : _a.toString()) === null || _b === void 0 ? void 0 : _b.match(/./))
|
||||
? item[ky]
|
||||
: null);
|
||||
return "?";
|
||||
const value = item[ky];
|
||||
const finalValue = typeof value == "string" ||
|
||||
typeof value == "number"
|
||||
? String(value)
|
||||
: value
|
||||
? String(value().value)
|
||||
: null;
|
||||
if (!finalValue) {
|
||||
return undefined;
|
||||
}
|
||||
queryValues.push(finalValue);
|
||||
const placeholder = typeof value == "function"
|
||||
? value().placeholder
|
||||
: "?";
|
||||
return placeholder;
|
||||
})
|
||||
.filter((k) => Boolean(k))
|
||||
.join(",")})`);
|
||||
});
|
||||
let query = `INSERT INTO ${finalDbName}${tableName} (${insertKeys.join(",")}) VALUES ${queryBatches.join(",")}`;
|
||||
|
||||
8
dist/package-shared/types/index.d.ts
vendored
8
dist/package-shared/types/index.d.ts
vendored
@ -959,7 +959,13 @@ export type ServerQueryObject<T extends object = {
|
||||
__query?: {
|
||||
[key in keyof T]: Omit<ServerQueryObject<T>, "__query">;
|
||||
};
|
||||
vector?: Float32Array<ArrayBuffer>;
|
||||
vector?: boolean;
|
||||
/**
|
||||
* ### The Function to be used to generate the vector.
|
||||
* Eg. `vec_f32`. This will come out as `vec_f32(?)`
|
||||
* instead of just `?`
|
||||
*/
|
||||
vectorFunction?: string;
|
||||
};
|
||||
export type ServerQueryQueryObject<T extends object = {
|
||||
[key: string]: any;
|
||||
|
||||
@ -17,7 +17,7 @@ import grabParsedValue from "./grab-parsed-value";
|
||||
*/
|
||||
export default async function addDbEntry<
|
||||
T extends { [k: string]: any } = any,
|
||||
K extends string = string
|
||||
K extends string = string,
|
||||
>({
|
||||
dbContext,
|
||||
paradigm,
|
||||
@ -109,7 +109,7 @@ export default async function addDbEntry<
|
||||
|
||||
const targetFieldSchema = tableSchema
|
||||
? tableSchema?.fields?.find(
|
||||
(field) => field.fieldName === dataKey
|
||||
(field) => field.fieldName === dataKey,
|
||||
)
|
||||
: null;
|
||||
|
||||
@ -135,7 +135,7 @@ export default async function addDbEntry<
|
||||
} catch (error: any) {
|
||||
console.log(
|
||||
"DSQL: Error in parsing data keys =>",
|
||||
error.message
|
||||
error.message,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
@ -164,6 +164,7 @@ export default async function addDbEntry<
|
||||
if (v?.toString().match(/VEC_FromText/i)) {
|
||||
return v;
|
||||
}
|
||||
|
||||
return "?";
|
||||
})
|
||||
.join(",");
|
||||
@ -182,7 +183,7 @@ export default async function addDbEntry<
|
||||
let query = `INSERT INTO ${
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` (${insertKeysArray.join(
|
||||
","
|
||||
",",
|
||||
)}) VALUES (${grabQueryValuesString(insertValuesArray)})`;
|
||||
|
||||
const finalQueryValues = grabFinalQueryValuesArr(queryValuesArray);
|
||||
@ -230,13 +231,13 @@ export default async function addDbEntry<
|
||||
let query = `INSERT INTO ${
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` (${batchInsertKeysArray?.join(
|
||||
","
|
||||
",",
|
||||
)}) VALUES ${batchInsertValuesArray
|
||||
.map((vl) => `(${grabQueryValuesString(vl)})`)
|
||||
.join(",")}`;
|
||||
|
||||
const finalQueryValues = grabFinalQueryValuesArr(
|
||||
batchQueryValuesArray.flat()
|
||||
batchQueryValuesArray.flat(),
|
||||
);
|
||||
|
||||
if (onDuplicate) {
|
||||
|
||||
@ -1,10 +1,21 @@
|
||||
// @ts-check
|
||||
|
||||
interface SQLInsertGenReturn {
|
||||
query: string;
|
||||
values: string[];
|
||||
}
|
||||
|
||||
type DataFn = () => {
|
||||
placeholder: string;
|
||||
value: string | number | Float32Array<ArrayBuffer>;
|
||||
};
|
||||
|
||||
type DataType = { [k: string]: string | number | DataFn | undefined | null };
|
||||
|
||||
type Params = {
|
||||
data: DataType[];
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* # SQL Insert Generator
|
||||
*/
|
||||
@ -12,11 +23,7 @@ export default function sqlInsertGenerator({
|
||||
tableName,
|
||||
data,
|
||||
dbFullName,
|
||||
}: {
|
||||
data: any[];
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLInsertGenReturn | undefined {
|
||||
}: Params): SQLInsertGenReturn | undefined {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
|
||||
try {
|
||||
@ -32,27 +39,42 @@ export default function sqlInsertGenerator({
|
||||
});
|
||||
});
|
||||
|
||||
/** @type {string[]} */
|
||||
let queryBatches: string[] = [];
|
||||
/** @type {string[]} */
|
||||
let queryValues: string[] = [];
|
||||
|
||||
data.forEach((item) => {
|
||||
queryBatches.push(
|
||||
`(${insertKeys
|
||||
.map((ky) => {
|
||||
queryValues.push(
|
||||
item[ky]?.toString()?.match(/./)
|
||||
? item[ky]
|
||||
: null
|
||||
);
|
||||
return "?";
|
||||
const value = item[ky];
|
||||
|
||||
const finalValue =
|
||||
typeof value == "string" ||
|
||||
typeof value == "number"
|
||||
? String(value)
|
||||
: value
|
||||
? String(value().value)
|
||||
: null;
|
||||
|
||||
if (!finalValue) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
queryValues.push(finalValue);
|
||||
|
||||
const placeholder =
|
||||
typeof value == "function"
|
||||
? value().placeholder
|
||||
: "?";
|
||||
|
||||
return placeholder;
|
||||
})
|
||||
.join(",")})`
|
||||
.filter((k) => Boolean(k))
|
||||
.join(",")})`,
|
||||
);
|
||||
});
|
||||
let query = `INSERT INTO ${finalDbName}${tableName} (${insertKeys.join(
|
||||
","
|
||||
",",
|
||||
)}) VALUES ${queryBatches.join(",")}`;
|
||||
|
||||
return {
|
||||
|
||||
@ -1161,7 +1161,13 @@ export type ServerQueryObject<
|
||||
__query?: {
|
||||
[key in keyof T]: Omit<ServerQueryObject<T>, "__query">;
|
||||
};
|
||||
vector?: Float32Array<ArrayBuffer>;
|
||||
vector?: boolean;
|
||||
/**
|
||||
* ### The Function to be used to generate the vector.
|
||||
* Eg. `vec_f32`. This will come out as `vec_f32(?)`
|
||||
* instead of just `?`
|
||||
*/
|
||||
vectorFunction?: string;
|
||||
};
|
||||
|
||||
export type ServerQueryQueryObject<
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@moduletrace/datasquirel",
|
||||
"version": "5.7.39",
|
||||
"version": "5.7.40",
|
||||
"description": "Cloud-based SQL data management tool",
|
||||
"main": "dist/index.js",
|
||||
"bin": {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user