@minatojs/sql-utils
Advanced tools
Comparing version 3.0.1 to 4.0.0
@@ -19,3 +19,3 @@ import { Dict } from 'cosmokit'; | ||
export declare class Builder { | ||
tables: Dict<Model>; | ||
tables?: Dict<Model<any>, string> | undefined; | ||
protected escapeMap: {}; | ||
@@ -27,3 +27,3 @@ protected escapeRegExp?: RegExp; | ||
protected evalOperators: EvalOperators; | ||
constructor(tables: Dict<Model>); | ||
constructor(tables?: Dict<Model<any>, string> | undefined); | ||
protected createNullQuery(key: string, value: boolean): string; | ||
@@ -42,6 +42,7 @@ protected createMemberQuery(key: string, value: any[], notStr?: string): string; | ||
private parseAggr; | ||
private transformKey; | ||
private getRecursive; | ||
parseEval(expr: any): string; | ||
suffix(modifier: Modifier): string; | ||
get(sel: Selection.Immutable): any; | ||
get(sel: Selection.Immutable, inline?: boolean): any; | ||
define<S, T>(converter: Transformer<S, T>): void; | ||
@@ -48,0 +49,0 @@ dump(model: Model, obj: any): any; |
@@ -21,3 +21,3 @@ "use strict"; | ||
// minato/packages/sql-utils/src/index.ts | ||
// packages/sql-utils/src/index.ts | ||
var src_exports = {}; | ||
@@ -30,2 +30,3 @@ __export(src_exports, { | ||
var import_cosmokit = require("cosmokit"); | ||
var import_core = require("@minatojs/core"); | ||
function escapeId(value) { | ||
@@ -191,15 +192,21 @@ return "`" + value + "`"; | ||
} | ||
transformKey(key, fields, prefix) { | ||
if (key in fields || !key.includes(".")) | ||
return prefix + escapeId(key); | ||
const field = Object.keys(fields).find((k) => key.startsWith(k + ".")) || key.split(".")[0]; | ||
const rest = key.slice(field.length + 1).split("."); | ||
return `json_unquote(json_extract(${escapeId(prefix + field)}, '$${rest.map((key2) => `."${key2}"`).join("")}'))`; | ||
} | ||
getRecursive(args) { | ||
var _a; | ||
var _a, _b, _c, _d; | ||
if (typeof args === "string") { | ||
return this.getRecursive(["_", args]); | ||
} else { | ||
const [table, key] = args; | ||
const fields = ((_a = this.tables[table]) == null ? void 0 : _a.fields) || {}; | ||
if (key in fields || !key.includes(".")) | ||
return escapeId(key); | ||
const field = Object.keys(fields).find((k) => key.startsWith(k + ".")) || key.split(".")[0]; | ||
const rest = key.slice(field.length + 1).split("."); | ||
return `json_unquote(json_extract(${escapeId(field)}, '$${rest.map((key2) => `."${key2}"`).join("")}'))`; | ||
} | ||
const [table, key] = args; | ||
const fields = ((_b = (_a = this.tables) == null ? void 0 : _a[table]) == null ? void 0 : _b.fields) || {}; | ||
if ((_c = fields[key]) == null ? void 0 : _c.expr) { | ||
return this.parseEvalExpr((_d = fields[key]) == null ? void 0 : _d.expr); | ||
} | ||
const prefix = !this.tables || key in fields ? "" : `${escapeId(table)}.`; | ||
return this.transformKey(key, fields, prefix); | ||
} | ||
@@ -232,9 +239,10 @@ parseEval(expr) { | ||
} | ||
get(sel) { | ||
const { args, table, query, ref } = sel; | ||
get(sel, inline = false) { | ||
var _a; | ||
const { args, table, query, ref, model } = sel; | ||
const filter = this.parseQuery(query); | ||
if (filter === "0") | ||
return; | ||
const { fields } = args[0]; | ||
const keys = !fields ? "*" : Object.entries(fields).map(([key, value]) => { | ||
const fields = (_a = args[0].fields) != null ? _a : Object.fromEntries(Object.keys(model.fields).map((k) => [k, { $: [ref, k] }])); | ||
const keys = Object.entries(fields).map(([key, value]) => { | ||
key = escapeId(key); | ||
@@ -244,3 +252,21 @@ value = this.parseEval(value); | ||
}).join(", "); | ||
let prefix = `SELECT ${keys} FROM `; | ||
let prefix; | ||
if (typeof table === "string") { | ||
prefix = escapeId(table); | ||
} else if (table instanceof import_core.Selection) { | ||
prefix = this.get(table, true); | ||
if (!prefix) | ||
return; | ||
} else { | ||
prefix = Object.entries(table).map(([key, table2]) => { | ||
if (typeof table2 !== "string") { | ||
return `${this.get(table2, true)} AS ${escapeId(key)}`; | ||
} else { | ||
return key === table2 ? escapeId(table2) : `${escapeId(table2)} AS ${escapeId(key)}`; | ||
} | ||
}).join(" JOIN "); | ||
const filter2 = this.parseEval(args[0].having); | ||
if (filter2 !== "1") | ||
prefix += ` ON ${filter2}`; | ||
} | ||
let suffix = this.suffix(args[0]); | ||
@@ -250,13 +276,9 @@ if (filter !== "1") { | ||
} | ||
if (typeof table === "string") { | ||
prefix += escapeId(table); | ||
} else { | ||
const inner = this.get(table); | ||
if (!inner) | ||
return; | ||
if (!fields && !suffix) | ||
return inner; | ||
prefix += `(${inner})`; | ||
if (!prefix.includes(" ") || prefix.startsWith("(")) { | ||
suffix = ` ${ref}` + suffix; | ||
} | ||
return `${prefix} ${ref}${suffix}`; | ||
if (inline && !args[0].fields && !suffix) | ||
return prefix; | ||
const result = `SELECT ${keys} FROM ${prefix}${suffix}`; | ||
return inline ? `(${result})` : result; | ||
} | ||
@@ -263,0 +285,0 @@ define(converter) { |
{ | ||
"name": "@minatojs/sql-utils", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "SQL Utilities for Minato", | ||
@@ -32,3 +32,3 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@minatojs/core": "^2.1.2" | ||
"@minatojs/core": "^2.2.0" | ||
}, | ||
@@ -35,0 +35,0 @@ "dependencies": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44715
394