pqb 0.70.0 → 0.71.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +29 -17
- package/dist/index.js +41 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +41 -19
- package/dist/index.mjs.map +1 -1
- package/dist/internal.d.ts +23 -15
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -147,7 +147,7 @@ const toPascalCase = (str) => {
|
|
|
147
147
|
* @param str - string to translate
|
|
148
148
|
*/
|
|
149
149
|
const toSnakeCase = (str) => {
|
|
150
|
-
return str.replace(/[A-Z]/g, (a) =>
|
|
150
|
+
return str.replace(/[A-Z]/g, (a, i) => `${i ? "_" : ""}${a.toLowerCase()}`);
|
|
151
151
|
};
|
|
152
152
|
/**
|
|
153
153
|
* Compare two values deeply.
|
|
@@ -6602,12 +6602,12 @@ const processJoinItem = (ctx, table, query, args, quotedAs) => {
|
|
|
6602
6602
|
ctx.aliasValue = aliasValue;
|
|
6603
6603
|
} else if ("j" in args) {
|
|
6604
6604
|
const { j, s, r } = args;
|
|
6605
|
-
const
|
|
6606
|
-
const joinTable = requireTableOrStringFrom(j);
|
|
6605
|
+
const joinTable = j.q.nameInDb || requireTableOrStringFrom(j);
|
|
6607
6606
|
target = quoteFromWithSchema(getQuerySchema$1(j), joinTable);
|
|
6608
6607
|
const as = j.q.as;
|
|
6609
6608
|
const joinAs = `"${as}"`;
|
|
6610
|
-
|
|
6609
|
+
const alias = getQueryRelationAliasForAs(j, as);
|
|
6610
|
+
if (alias) target += ` ${alias}`;
|
|
6611
6611
|
if (r && s) target = subJoinToSql(ctx, j, `"${joinTable}"`, !forbidLateral, joinAs, true);
|
|
6612
6612
|
else on = whereToSql(ctx, j, j.q, joinAs);
|
|
6613
6613
|
} else if ("w" in args) {
|
|
@@ -6665,16 +6665,16 @@ const getArgQueryTarget = (ctx, first, lateral, joinSubQuery, cloned) => {
|
|
|
6665
6665
|
const quotedFrom = typeof joinQuery.from === "string" ? `"${joinQuery.from}"` : void 0;
|
|
6666
6666
|
let joinAs = quotedFrom || `"${first.table}"`;
|
|
6667
6667
|
const qAs = joinQuery.as ? `"${joinQuery.as}"` : void 0;
|
|
6668
|
-
const
|
|
6668
|
+
const aliasToAdd = getQueryRelationAliasForAs(first, joinQuery.as);
|
|
6669
6669
|
if (joinSubQuery) return {
|
|
6670
6670
|
target: subJoinToSql(ctx, first, joinAs, lateral, qAs, cloned),
|
|
6671
|
-
joinAs:
|
|
6671
|
+
joinAs: qAs || joinAs
|
|
6672
6672
|
};
|
|
6673
6673
|
else {
|
|
6674
6674
|
let target = quotedFrom || quoteTableWithSchema(first);
|
|
6675
|
-
if (
|
|
6676
|
-
joinAs =
|
|
6677
|
-
target += ` ${
|
|
6675
|
+
if (aliasToAdd) {
|
|
6676
|
+
joinAs = aliasToAdd;
|
|
6677
|
+
target += ` ${aliasToAdd}`;
|
|
6678
6678
|
}
|
|
6679
6679
|
return {
|
|
6680
6680
|
target,
|
|
@@ -7432,14 +7432,18 @@ const makeInsertSql = (ctx, q, query, quotedAs, isSubSql) => {
|
|
|
7432
7432
|
}
|
|
7433
7433
|
}
|
|
7434
7434
|
const hasNonSelect = ctx.hasNonSelect;
|
|
7435
|
+
const returningQuotedAs = query.as ? `"${query.as}"` : quotedAs;
|
|
7436
|
+
const insertAlias = getQueryRelationAliasForAs(q, query.as);
|
|
7437
|
+
quotedAs = insertAlias || quotedAs;
|
|
7435
7438
|
const sqlState = {
|
|
7436
7439
|
ctx,
|
|
7437
7440
|
q,
|
|
7438
7441
|
query,
|
|
7439
7442
|
quotedAs,
|
|
7443
|
+
returningQuotedAs,
|
|
7440
7444
|
isSubSql,
|
|
7441
7445
|
returningPos: 0,
|
|
7442
|
-
insertSql: `INSERT INTO ${quoteTableWithSchema(q)}${quotedColumns.length ? "(" + quotedColumns.join(", ") + ")" : ""}`
|
|
7446
|
+
insertSql: `INSERT INTO ${quoteTableWithSchema(q)}${insertAlias ? ` AS ${insertAlias}` : ""}${quotedColumns.length ? "(" + quotedColumns.join(", ") + ")" : ""}`
|
|
7443
7447
|
};
|
|
7444
7448
|
ctx.sql.push(null, null);
|
|
7445
7449
|
const hasOnConflictWhere = pushOnConflictSql(ctx, q, query, quotedAs, columns, quotedColumns, runtimeDefaultColumns);
|
|
@@ -7552,7 +7556,7 @@ const applySqlState = (sqlState) => {
|
|
|
7552
7556
|
const insertSql = sqlState.selectFromSql ? sqlState.insertSql + sqlState.selectFromSql : sqlState.insertSql;
|
|
7553
7557
|
const wrapInCte = getShouldWrapMainQueryInCte(ctx, sqlState.query, "insert", sqlState.isSubSql);
|
|
7554
7558
|
if ("valuesSql" in sqlState) ctx.sql[1] = sqlState.valuesPrepend + sqlState.valuesSql.join(", ") + sqlState.valuesAppend;
|
|
7555
|
-
const returning = makeReturningSql(ctx, sqlState.q, sqlState.query, sqlState.
|
|
7559
|
+
const returning = makeReturningSql(ctx, sqlState.q, sqlState.query, sqlState.returningQuotedAs, sqlState.relationSelectState, "Create", void 0, sqlState.isSubSql || !!ctx.topCtx.cteHooks);
|
|
7556
7560
|
if (returning) ctx.sql[sqlState.returningPos] = "RETURNING " + returning;
|
|
7557
7561
|
ctx.sql[0] = insertSql;
|
|
7558
7562
|
if (wrapInCte) wrapMainQueryInCte(ctx, sqlState.query);
|
|
@@ -7713,8 +7717,9 @@ const pushUpdateSql = (ctx, query, q, quotedAs, isSubSql) => {
|
|
|
7713
7717
|
}
|
|
7714
7718
|
};
|
|
7715
7719
|
const pushUpdateSqlWithoutValuesJoinedAs = (ctx, query, q, quotedAs, isSubSql) => {
|
|
7716
|
-
const quotedTable = `"${query.table || q.from}"`;
|
|
7717
7720
|
const from = quoteTableWithSchema(query);
|
|
7721
|
+
const alias = getQueryRelationAliasForAs(query, q.as);
|
|
7722
|
+
quotedAs = alias || quotedAs;
|
|
7718
7723
|
const set = [];
|
|
7719
7724
|
const hookSet = q.hookUpdateSet ? Object.fromEntries(q.hookUpdateSet.flatMap((item) => Object.entries(item))) : emptyObject;
|
|
7720
7725
|
const relationSelectState = newMutativeQueriesSelectRelationsSqlState(query);
|
|
@@ -7729,7 +7734,7 @@ const pushUpdateSqlWithoutValuesJoinedAs = (ctx, query, q, quotedAs, isSubSql) =
|
|
|
7729
7734
|
if (!set.length) pushSelectForEmptySet(ctx, query, q, quotedAs, from, isSubSql, updateManyValuesSql, relationSelectState);
|
|
7730
7735
|
else {
|
|
7731
7736
|
ctx.sql.push(`UPDATE ${from}`);
|
|
7732
|
-
if (
|
|
7737
|
+
if (alias) ctx.sql.push(alias);
|
|
7733
7738
|
ctx.sql.push("SET", set.join(", "));
|
|
7734
7739
|
let fromWhereSql;
|
|
7735
7740
|
if (updateManyValuesSql) {
|
|
@@ -7746,6 +7751,8 @@ const pushSelectForEmptySet = (ctx, query, q, quotedAs, from, isSubSql, updateMa
|
|
|
7746
7751
|
if (!q.select) q.select = countSelect;
|
|
7747
7752
|
pushUpdateReturning(ctx, query, q, quotedAs, "SELECT", relationSelectState, isSubSql);
|
|
7748
7753
|
let fromSql = `FROM ${from}`;
|
|
7754
|
+
const alias = getQueryRelationAliasForAs(query, q.as);
|
|
7755
|
+
if (alias) fromSql += ` ${alias}`;
|
|
7749
7756
|
if (updateManyValuesSql) fromSql += `, ${updateManyValuesSql}`;
|
|
7750
7757
|
ctx.sql.push(fromSql);
|
|
7751
7758
|
pushWhereStatementSql(ctx, query, q, quotedAs);
|
|
@@ -7860,7 +7867,8 @@ const throwOnDifferentColumns = (query, keysSet, row, i) => {
|
|
|
7860
7867
|
const pushDeleteSql = (ctx, query, q, quotedAs, isSubSql) => {
|
|
7861
7868
|
const from = quoteTableWithSchema(query);
|
|
7862
7869
|
ctx.sql.push(`DELETE FROM ${from}`);
|
|
7863
|
-
|
|
7870
|
+
const alias = getQueryRelationAliasForAs(query, q.as);
|
|
7871
|
+
if (alias) ctx.sql.push(alias);
|
|
7864
7872
|
const relationSelectState = newMutativeQueriesSelectRelationsSqlState(query);
|
|
7865
7873
|
let returning = makeReturningSql(ctx, query, q, quotedAs, relationSelectState, "Delete", void 0, isSubSql);
|
|
7866
7874
|
const selectRelations = handleDeleteSelectRelationsSqlState(ctx, query, relationSelectState);
|
|
@@ -9445,7 +9453,8 @@ const getFrom = (ctx, query, data, quotedAs) => {
|
|
|
9445
9453
|
return fromToSql(ctx, query, data, from, quotedAs);
|
|
9446
9454
|
}
|
|
9447
9455
|
let sql = quoteTableWithSchema(query);
|
|
9448
|
-
|
|
9456
|
+
const alias = getQueryRelationAliasForAs(query, data.as);
|
|
9457
|
+
if (alias) sql += ` ${alias}`;
|
|
9449
9458
|
if (data.only) sql = `ONLY ${sql}`;
|
|
9450
9459
|
return sql;
|
|
9451
9460
|
};
|
|
@@ -9457,7 +9466,7 @@ const fromToSql = (ctx, query, data, from, quotedAs) => {
|
|
|
9457
9466
|
only = from.q.only;
|
|
9458
9467
|
if (!from.table) sql = `(${moveMutativeQueryToCte(ctx, from)})`;
|
|
9459
9468
|
else if (!checkIfASimpleQuery(from)) sql = `(${moveMutativeQueryToCte(ctx, from)}) ${quotedAs || `"${getQueryAs(from)}"`}`;
|
|
9460
|
-
else sql =
|
|
9469
|
+
else sql = quoteTableWithSchemaAndAlias(from);
|
|
9461
9470
|
fromQuery = from;
|
|
9462
9471
|
}
|
|
9463
9472
|
else sql = quoteFromWithSchema(getQuerySchema$1(query), from);
|
|
@@ -9758,7 +9767,18 @@ const requireTableOrStringFrom = (query) => {
|
|
|
9758
9767
|
if (!table) throw new OrchidOrmInternalError(query, "The query object does not have a table and doesn't define a `from` string");
|
|
9759
9768
|
return table;
|
|
9760
9769
|
};
|
|
9761
|
-
const
|
|
9770
|
+
const getQueryRelationAlias = (query) => query.table && query.q.nameInDb && query.table !== query.q.nameInDb ? `"${query.table}"` : void 0;
|
|
9771
|
+
const getQueryRelationAliasForAs = (query, as) => {
|
|
9772
|
+
if (!as) return getQueryRelationAlias(query);
|
|
9773
|
+
const nameInDb = query.q.nameInDb || requireTableOrStringFrom(query);
|
|
9774
|
+
return nameInDb && as !== nameInDb ? `"${as}"` : void 0;
|
|
9775
|
+
};
|
|
9776
|
+
const quoteTableWithSchema = (query) => quoteFromWithSchema(getQuerySchema$1(query), query.q.nameInDb || requireTableOrStringFrom(query));
|
|
9777
|
+
const quoteTableWithSchemaAndAlias = (query) => {
|
|
9778
|
+
const table = quoteTableWithSchema(query);
|
|
9779
|
+
const alias = getQueryRelationAlias(query);
|
|
9780
|
+
return alias ? `${table} ${alias}` : table;
|
|
9781
|
+
};
|
|
9762
9782
|
const quoteFromWithSchema = (schema, table) => {
|
|
9763
9783
|
const s = typeof schema === "function" ? schema() : schema;
|
|
9764
9784
|
return s ? `"${s}"."${table}"` : `"${table}"`;
|
|
@@ -10026,7 +10046,7 @@ const _joinLateral = (self, type, joinQuery, as, innerJoinLateral) => {
|
|
|
10026
10046
|
const joinedAs = getQueryAs(query);
|
|
10027
10047
|
setObjectValueImmutable(joinQuery.q, "joinedShapes", joinedAs, query.q.selectShape);
|
|
10028
10048
|
}
|
|
10029
|
-
const shape = getShapeFromSelect(joinQuery, true);
|
|
10049
|
+
const shape = joinQuery.table && joinQuery.q.joinedShapes?.[joinQuery.table] || joinQuery.q.joinedShapes?.[joinAs] || getShapeFromSelect(joinQuery, true);
|
|
10030
10050
|
setObjectValueImmutable(query.q, "joinedShapes", joinAs, shape);
|
|
10031
10051
|
if (joinValue) setObjectValueImmutable(query.q, "valuesJoinedAs", joinAs, joinValueAs);
|
|
10032
10052
|
setObjectValueImmutable(query.q, "joinedParsers", joinValueAs || joinAs, getQueryParsers(joinQuery));
|
|
@@ -13872,6 +13892,7 @@ const performQuery = async (q, args, method) => {
|
|
|
13872
13892
|
throw err;
|
|
13873
13893
|
}
|
|
13874
13894
|
};
|
|
13895
|
+
const getTableNameInDb = (table, nameInDb, snakeCase) => table && (nameInDb || (snakeCase ? toSnakeCase(table) : table));
|
|
13875
13896
|
var Db = class extends QueryMethods {
|
|
13876
13897
|
constructor(adapterNotInTransaction, qb, table = void 0, shape, columnTypes, asyncStorage, options, tableData = {}, viewData) {
|
|
13877
13898
|
super();
|
|
@@ -13949,6 +13970,7 @@ var Db = class extends QueryMethods {
|
|
|
13949
13970
|
this.q = {
|
|
13950
13971
|
adapter: adapterNotInTransaction,
|
|
13951
13972
|
selectShape: shape,
|
|
13973
|
+
nameInDb: getTableNameInDb(table, options.nameInDb, snakeCase),
|
|
13952
13974
|
handleResult,
|
|
13953
13975
|
logger,
|
|
13954
13976
|
log: logParamToLogObject(logger, options.log),
|
|
@@ -14285,7 +14307,7 @@ const _initQueryBuilder = (adapter, columnTypes, asyncStorage, commonOptions, op
|
|
|
14285
14307
|
const makeColumnInfoSql = (query, column) => {
|
|
14286
14308
|
const values = [];
|
|
14287
14309
|
const schema = getQuerySchema(query);
|
|
14288
|
-
let text = `SELECT * FROM information_schema.columns WHERE table_name = ${addValue(values, query.table)} AND table_catalog = current_database() AND table_schema = ${schema ? addValue(values, schema) : "current_schema()"}`;
|
|
14310
|
+
let text = `SELECT * FROM information_schema.columns WHERE table_name = ${addValue(values, query.q.nameInDb || query.table)} AND table_catalog = current_database() AND table_schema = ${schema ? addValue(values, schema) : "current_schema()"}`;
|
|
14289
14311
|
if (column) text += ` AND column_name = ${addValue(values, query.shape[column]?.data.name || column)}`;
|
|
14290
14312
|
return {
|
|
14291
14313
|
text,
|