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