Updates
This commit is contained in:
parent
82152082f3
commit
60cccc9d9f
@ -5,8 +5,9 @@ interface SQLDeleteGenReturn {
|
||||
/**
|
||||
* # SQL Delete Generator
|
||||
*/
|
||||
export default function sqlDeleteGenerator({ tableName, data, }: {
|
||||
export default function sqlDeleteGenerator({ tableName, data, dbFullName, }: {
|
||||
data: any;
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLDeleteGenReturn | undefined;
|
||||
export {};
|
||||
|
@ -4,9 +4,10 @@ exports.default = sqlDeleteGenerator;
|
||||
/**
|
||||
* # SQL Delete Generator
|
||||
*/
|
||||
function sqlDeleteGenerator({ tableName, data, }) {
|
||||
function sqlDeleteGenerator({ tableName, data, dbFullName, }) {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
try {
|
||||
let queryStr = `DELETE FROM ${tableName}`;
|
||||
let queryStr = `DELETE FROM ${finalDbName}${tableName}`;
|
||||
let deleteBatch = [];
|
||||
let queryArr = [];
|
||||
Object.keys(data).forEach((ky) => {
|
||||
|
@ -2,7 +2,7 @@ import { ServerQueryParam } from "../../../types";
|
||||
type Param = {
|
||||
genObject?: ServerQueryParam;
|
||||
tableName: string;
|
||||
dbFullName: string;
|
||||
dbFullName?: string;
|
||||
};
|
||||
type Return = {
|
||||
string: string;
|
||||
|
@ -11,16 +11,17 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
const finalQuery = genObject.query ? genObject.query : undefined;
|
||||
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
|
||||
const sqlSearhValues = [];
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
/**
|
||||
* # Generate Query
|
||||
*/
|
||||
function genSqlSrchStr({ queryObj, join, field, }) {
|
||||
const finalFieldName = (() => {
|
||||
if (queryObj === null || queryObj === void 0 ? void 0 : queryObj.tableName) {
|
||||
return `${dbFullName}.${queryObj.tableName}.${field}`;
|
||||
return `${finalDbName}${queryObj.tableName}.${field}`;
|
||||
}
|
||||
if (join) {
|
||||
return `${dbFullName}.${tableName}.${field}`;
|
||||
return `${finalDbName}${tableName}.${field}`;
|
||||
}
|
||||
return field;
|
||||
})();
|
||||
@ -87,18 +88,18 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
function generateJoinStr(
|
||||
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch,
|
||||
/** @type {import("../../../types").ServerQueryParamsJoin} */ join) {
|
||||
return `${dbFullName}.${typeof mtch.source == "object" ? mtch.source.tableName : tableName}.${typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source}=${(() => {
|
||||
return `${finalDbName}${typeof mtch.source == "object" ? mtch.source.tableName : tableName}.${typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source}=${(() => {
|
||||
if (mtch.targetLiteral) {
|
||||
return `'${mtch.targetLiteral}'`;
|
||||
}
|
||||
if (join.alias) {
|
||||
return `${dbFullName}.${typeof mtch.target == "object"
|
||||
return `${finalDbName}${typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.alias}.${typeof mtch.target == "object"
|
||||
? mtch.target.fieldName
|
||||
: mtch.target}`;
|
||||
}
|
||||
return `${dbFullName}.${typeof mtch.target == "object"
|
||||
return `${finalDbName}${typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.tableName}.${typeof mtch.target == "object"
|
||||
? mtch.target.fieldName
|
||||
@ -110,7 +111,7 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
let str = "SELECT";
|
||||
if ((_a = genObject.selectFields) === null || _a === void 0 ? void 0 : _a[0]) {
|
||||
if (genObject.join) {
|
||||
str += ` ${(_b = genObject.selectFields) === null || _b === void 0 ? void 0 : _b.map((fld) => `${dbFullName}.${tableName}.${fld}`).join(",")}`;
|
||||
str += ` ${(_b = genObject.selectFields) === null || _b === void 0 ? void 0 : _b.map((fld) => `${finalDbName}${tableName}.${fld}`).join(",")}`;
|
||||
}
|
||||
else {
|
||||
str += ` ${(_c = genObject.selectFields) === null || _c === void 0 ? void 0 : _c.join(",")}`;
|
||||
@ -118,7 +119,7 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
}
|
||||
else {
|
||||
if (genObject.join) {
|
||||
str += ` ${dbFullName}.${tableName}.*`;
|
||||
str += ` ${finalDbName}${tableName}.*`;
|
||||
}
|
||||
else {
|
||||
str += " *";
|
||||
@ -141,12 +142,12 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
return joinObj.selectFields
|
||||
.map((selectField) => {
|
||||
if (typeof selectField == "string") {
|
||||
return `${dbFullName}.${joinTableName}.${selectField}`;
|
||||
return `${finalDbName}${joinTableName}.${selectField}`;
|
||||
}
|
||||
else if (typeof selectField == "object") {
|
||||
let aliasSelectField = selectField.count
|
||||
? `COUNT(${dbFullName}.${joinTableName}.${selectField.field})`
|
||||
: `${dbFullName}.${joinTableName}.${selectField.field}`;
|
||||
? `COUNT(${finalDbName}${joinTableName}.${selectField.field})`
|
||||
: `${finalDbName}${joinTableName}.${selectField.field}`;
|
||||
if (selectField.alias)
|
||||
aliasSelectField += ` AS ${selectField.alias}`;
|
||||
return aliasSelectField;
|
||||
@ -155,13 +156,13 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
.join(",");
|
||||
}
|
||||
else {
|
||||
return `${dbFullName}.${joinTableName}.*`;
|
||||
return `${finalDbName}${joinTableName}.*`;
|
||||
}
|
||||
})
|
||||
.filter((_) => Boolean(_))
|
||||
.join(",");
|
||||
}
|
||||
str += ` FROM ${dbFullName}.${tableName}`;
|
||||
str += ` FROM ${finalDbName}${tableName}`;
|
||||
if (genObject.join) {
|
||||
str +=
|
||||
" " +
|
||||
@ -170,10 +171,10 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
return (join.joinType +
|
||||
" " +
|
||||
(join.alias
|
||||
? `${dbFullName}.${join.tableName}` +
|
||||
? `${finalDbName}${join.tableName}` +
|
||||
" " +
|
||||
join.alias
|
||||
: `${dbFullName}.${join.tableName}`) +
|
||||
: `${finalDbName}${join.tableName}`) +
|
||||
" ON " +
|
||||
(() => {
|
||||
if (Array.isArray(join.match)) {
|
||||
@ -200,7 +201,7 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
}
|
||||
if (genObject.order)
|
||||
queryString += ` ORDER BY ${genObject.join
|
||||
? `${dbFullName}.${tableName}.${genObject.order.field}`
|
||||
? `${finalDbName}${tableName}.${genObject.order.field}`
|
||||
: genObject.order.field} ${genObject.order.strategy}`;
|
||||
if (genObject.limit)
|
||||
queryString += ` LIMIT ${genObject.limit}`;
|
||||
|
@ -5,8 +5,9 @@ interface SQLInsertGenReturn {
|
||||
/**
|
||||
* # SQL Insert Generator
|
||||
*/
|
||||
export default function sqlInsertGenerator({ tableName, data, }: {
|
||||
export default function sqlInsertGenerator({ tableName, data, dbFullName, }: {
|
||||
data: any[];
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLInsertGenReturn | undefined;
|
||||
export {};
|
||||
|
@ -5,10 +5,10 @@ exports.default = sqlInsertGenerator;
|
||||
/**
|
||||
* # SQL Insert Generator
|
||||
*/
|
||||
function sqlInsertGenerator({ tableName, data, }) {
|
||||
function sqlInsertGenerator({ tableName, data, dbFullName, }) {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
try {
|
||||
if (Array.isArray(data) && (data === null || data === void 0 ? void 0 : data[0])) {
|
||||
/** @type {string[]} */
|
||||
let insertKeys = [];
|
||||
data.forEach((dt) => {
|
||||
const kys = Object.keys(dt);
|
||||
@ -33,7 +33,7 @@ function sqlInsertGenerator({ tableName, data, }) {
|
||||
})
|
||||
.join(",")})`);
|
||||
});
|
||||
let query = `INSERT INTO ${tableName} (${insertKeys.join(",")}) VALUES ${queryBatches.join(",")}`;
|
||||
let query = `INSERT INTO ${finalDbName}${tableName} (${insertKeys.join(",")}) VALUES ${queryBatches.join(",")}`;
|
||||
return {
|
||||
query: query,
|
||||
values: queryValues,
|
||||
|
@ -9,12 +9,16 @@ interface SQLDeleteGenReturn {
|
||||
export default function sqlDeleteGenerator({
|
||||
tableName,
|
||||
data,
|
||||
dbFullName,
|
||||
}: {
|
||||
data: any;
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLDeleteGenReturn | undefined {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
|
||||
try {
|
||||
let queryStr = `DELETE FROM ${tableName}`;
|
||||
let queryStr = `DELETE FROM ${finalDbName}${tableName}`;
|
||||
|
||||
let deleteBatch: string[] = [];
|
||||
let queryArr: string[] = [];
|
||||
|
@ -7,7 +7,7 @@ import {
|
||||
type Param = {
|
||||
genObject?: ServerQueryParam;
|
||||
tableName: string;
|
||||
dbFullName: string;
|
||||
dbFullName?: string;
|
||||
};
|
||||
|
||||
type Return =
|
||||
@ -34,6 +34,8 @@ export default function sqlGenerator({
|
||||
|
||||
const sqlSearhValues: string[] = [];
|
||||
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
|
||||
/**
|
||||
* # Generate Query
|
||||
*/
|
||||
@ -48,10 +50,10 @@ export default function sqlGenerator({
|
||||
}) {
|
||||
const finalFieldName = (() => {
|
||||
if (queryObj?.tableName) {
|
||||
return `${dbFullName}.${queryObj.tableName}.${field}`;
|
||||
return `${finalDbName}${queryObj.tableName}.${field}`;
|
||||
}
|
||||
if (join) {
|
||||
return `${dbFullName}.${tableName}.${field}`;
|
||||
return `${finalDbName}${tableName}.${field}`;
|
||||
}
|
||||
return field;
|
||||
})();
|
||||
@ -133,7 +135,7 @@ export default function sqlGenerator({
|
||||
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch: import("../../../types").ServerQueryParamsJoinMatchObject,
|
||||
/** @type {import("../../../types").ServerQueryParamsJoin} */ join: import("../../../types").ServerQueryParamsJoin
|
||||
) {
|
||||
return `${dbFullName}.${
|
||||
return `${finalDbName}${
|
||||
typeof mtch.source == "object" ? mtch.source.tableName : tableName
|
||||
}.${
|
||||
typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source
|
||||
@ -143,7 +145,7 @@ export default function sqlGenerator({
|
||||
}
|
||||
|
||||
if (join.alias) {
|
||||
return `${dbFullName}.${
|
||||
return `${finalDbName}${
|
||||
typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.alias
|
||||
@ -154,7 +156,7 @@ export default function sqlGenerator({
|
||||
}`;
|
||||
}
|
||||
|
||||
return `${dbFullName}.${
|
||||
return `${finalDbName}${
|
||||
typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.tableName
|
||||
@ -171,14 +173,14 @@ export default function sqlGenerator({
|
||||
if (genObject.selectFields?.[0]) {
|
||||
if (genObject.join) {
|
||||
str += ` ${genObject.selectFields
|
||||
?.map((fld) => `${dbFullName}.${tableName}.${fld}`)
|
||||
?.map((fld) => `${finalDbName}${tableName}.${fld}`)
|
||||
.join(",")}`;
|
||||
} else {
|
||||
str += ` ${genObject.selectFields?.join(",")}`;
|
||||
}
|
||||
} else {
|
||||
if (genObject.join) {
|
||||
str += ` ${dbFullName}.${tableName}.*`;
|
||||
str += ` ${finalDbName}${tableName}.*`;
|
||||
} else {
|
||||
str += " *";
|
||||
}
|
||||
@ -204,11 +206,11 @@ export default function sqlGenerator({
|
||||
return joinObj.selectFields
|
||||
.map((selectField) => {
|
||||
if (typeof selectField == "string") {
|
||||
return `${dbFullName}.${joinTableName}.${selectField}`;
|
||||
return `${finalDbName}${joinTableName}.${selectField}`;
|
||||
} else if (typeof selectField == "object") {
|
||||
let aliasSelectField = selectField.count
|
||||
? `COUNT(${dbFullName}.${joinTableName}.${selectField.field})`
|
||||
: `${dbFullName}.${joinTableName}.${selectField.field}`;
|
||||
? `COUNT(${finalDbName}${joinTableName}.${selectField.field})`
|
||||
: `${finalDbName}${joinTableName}.${selectField.field}`;
|
||||
if (selectField.alias)
|
||||
aliasSelectField += ` AS ${selectField.alias}`;
|
||||
return aliasSelectField;
|
||||
@ -216,14 +218,14 @@ export default function sqlGenerator({
|
||||
})
|
||||
.join(",");
|
||||
} else {
|
||||
return `${dbFullName}.${joinTableName}.*`;
|
||||
return `${finalDbName}${joinTableName}.*`;
|
||||
}
|
||||
})
|
||||
.filter((_) => Boolean(_))
|
||||
.join(",");
|
||||
}
|
||||
|
||||
str += ` FROM ${dbFullName}.${tableName}`;
|
||||
str += ` FROM ${finalDbName}${tableName}`;
|
||||
|
||||
if (genObject.join) {
|
||||
str +=
|
||||
@ -234,10 +236,10 @@ export default function sqlGenerator({
|
||||
join.joinType +
|
||||
" " +
|
||||
(join.alias
|
||||
? `${dbFullName}.${join.tableName}` +
|
||||
? `${finalDbName}${join.tableName}` +
|
||||
" " +
|
||||
join.alias
|
||||
: `${dbFullName}.${join.tableName}`) +
|
||||
: `${finalDbName}${join.tableName}`) +
|
||||
" ON " +
|
||||
(() => {
|
||||
if (Array.isArray(join.match)) {
|
||||
@ -274,7 +276,7 @@ export default function sqlGenerator({
|
||||
if (genObject.order)
|
||||
queryString += ` ORDER BY ${
|
||||
genObject.join
|
||||
? `${dbFullName}.${tableName}.${genObject.order.field}`
|
||||
? `${finalDbName}${tableName}.${genObject.order.field}`
|
||||
: genObject.order.field
|
||||
} ${genObject.order.strategy}`;
|
||||
|
||||
|
@ -11,13 +11,16 @@ interface SQLInsertGenReturn {
|
||||
export default function sqlInsertGenerator({
|
||||
tableName,
|
||||
data,
|
||||
dbFullName,
|
||||
}: {
|
||||
data: any[];
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLInsertGenReturn | undefined {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
|
||||
try {
|
||||
if (Array.isArray(data) && data?.[0]) {
|
||||
/** @type {string[]} */
|
||||
let insertKeys: string[] = [];
|
||||
|
||||
data.forEach((dt) => {
|
||||
@ -48,7 +51,7 @@ export default function sqlInsertGenerator({
|
||||
.join(",")})`
|
||||
);
|
||||
});
|
||||
let query = `INSERT INTO ${tableName} (${insertKeys.join(
|
||||
let query = `INSERT INTO ${finalDbName}${tableName} (${insertKeys.join(
|
||||
","
|
||||
)}) VALUES ${queryBatches.join(",")}`;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@moduletrace/datasquirel",
|
||||
"version": "3.6.0",
|
||||
"version": "3.6.1",
|
||||
"description": "Cloud-based SQL data management tool",
|
||||
"main": "dist/index.js",
|
||||
"bin": {
|
||||
|
Loading…
Reference in New Issue
Block a user