pqb 0.40.7 → 0.40.9
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 +133 -11
- package/dist/index.js +79 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +79 -51
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -384,6 +384,8 @@ interface CommonQueryData {
|
|
|
384
384
|
outerJoinOverrides?: JoinOverrides;
|
|
385
385
|
schema?: string;
|
|
386
386
|
select?: SelectItem[];
|
|
387
|
+
selectAllColumns?: string[];
|
|
388
|
+
selectAllKeys?: RecordUnknown;
|
|
387
389
|
/**
|
|
388
390
|
* column type for query with 'value' or 'valueOrThrow' return type
|
|
389
391
|
* Is needed in {@link getShapeFromSelect} to get shape of sub-select that returns a single value.
|
|
@@ -3345,16 +3347,15 @@ type DbTableOptionScopes<Table extends string | undefined, Shape extends QueryCo
|
|
|
3345
3347
|
[K in Keys]: (q: ScopeArgumentQuery<Table, Shape>) => QueryBase;
|
|
3346
3348
|
};
|
|
3347
3349
|
type QueryDefaultReturnData<Shape extends QueryColumnsInit> = {
|
|
3348
|
-
[K in DefaultSelectColumns<Shape>
|
|
3350
|
+
[K in DefaultSelectColumns<Shape>]: Shape[K]['outputType'];
|
|
3349
3351
|
}[];
|
|
3350
3352
|
declare const anyShape: QueryColumnsInit;
|
|
3351
3353
|
interface Db<Table extends string | undefined = undefined, Shape extends QueryColumnsInit = QueryColumnsInit, PrimaryKeys = never, UniqueColumns = never, UniqueColumnTuples = never, UniqueConstraints = never, ColumnTypes = DefaultColumnTypes<ColumnSchemaConfig>, ShapeWithComputed extends QueryColumnsInit = Shape, Scopes extends RecordUnknown | undefined = EmptyObject> extends DbBase<Adapter, Table, Shape, ColumnTypes, ShapeWithComputed>, QueryMethods<ColumnTypes>, QueryBase {
|
|
3352
|
-
result: Pick<Shape, DefaultSelectColumns<Shape
|
|
3354
|
+
result: Pick<Shape, DefaultSelectColumns<Shape>>;
|
|
3353
3355
|
queryBuilder: Db;
|
|
3354
3356
|
returnType: undefined;
|
|
3355
3357
|
then: QueryThen<QueryDefaultReturnData<Shape>>;
|
|
3356
3358
|
windows: Query['windows'];
|
|
3357
|
-
defaultSelectColumns: DefaultSelectColumns<Shape>;
|
|
3358
3359
|
relations: EmptyObject;
|
|
3359
3360
|
withData: EmptyObject;
|
|
3360
3361
|
error: new (message: string, length: number, name: QueryErrorName) => QueryError<this>;
|
|
@@ -3367,6 +3368,7 @@ interface Db<Table extends string | undefined = undefined, Shape extends QueryCo
|
|
|
3367
3368
|
[K in keyof Scopes]: true;
|
|
3368
3369
|
};
|
|
3369
3370
|
selectable: SelectableFromShape<ShapeWithComputed, Table>;
|
|
3371
|
+
defaultSelect: DefaultSelectColumns<Shape>;
|
|
3370
3372
|
};
|
|
3371
3373
|
internal: QueryInternal<{
|
|
3372
3374
|
[K in keyof PrimaryKeys]: (keyof PrimaryKeys extends K ? never : keyof PrimaryKeys) extends never ? PrimaryKeys[K] : never;
|
|
@@ -4764,7 +4766,7 @@ type FromResult<T extends FromQuerySelf, Arg extends MaybeArray<FromArg<T>>> = A
|
|
|
4764
4766
|
} : K extends 'result' ? T['withData'][Arg]['shape'] : K extends 'then' ? QueryThen<GetQueryResult<T, T['withData'][Arg]['shape']>> : T[K];
|
|
4765
4767
|
} : SetQueryTableAlias<T, Arg> : Arg extends PickQueryTableMetaResultInputType ? {
|
|
4766
4768
|
[K in keyof T]: K extends 'meta' ? {
|
|
4767
|
-
[K in keyof T['meta']]: K extends 'as' ? AliasOrTable<Arg> : K extends 'selectable' ? SelectableFromShape<Arg['result'], AliasOrTable<Arg>> : K extends 'kind' ? 'select' : T['meta'][K];
|
|
4769
|
+
[K in keyof T['meta']]: K extends 'as' ? AliasOrTable<Arg> : K extends 'selectable' ? SelectableFromShape<Arg['result'], AliasOrTable<Arg>> : K extends 'kind' ? 'select' : K extends 'defaultSelect' ? keyof Arg['result'] : T['meta'][K];
|
|
4768
4770
|
} : K extends 'result' ? Arg['result'] : K extends 'shape' ? Arg['result'] : K extends 'inputType' ? Arg['inputType'] : K extends 'then' ? QueryThen<GetQueryResult<T, Arg['result']>> : T[K];
|
|
4769
4771
|
} : Arg extends (infer A)[] ? {
|
|
4770
4772
|
[K in keyof T]: K extends 'meta' ? {
|
|
@@ -5022,8 +5024,129 @@ declare class AfterCommitError extends OrchidOrmError {
|
|
|
5022
5024
|
}
|
|
5023
5025
|
declare const _afterCommitError: (result: unknown, hookResults: AfterCommitErrorResult[], catchAfterCommitError: ((error: AfterCommitError) => void) | undefined) => void;
|
|
5024
5026
|
declare class Transaction {
|
|
5025
|
-
|
|
5026
|
-
|
|
5027
|
+
/**
|
|
5028
|
+
* In Orchid ORM the method is `$transaction`, when using `pqb` on its own it is `transaction`.
|
|
5029
|
+
*
|
|
5030
|
+
* `COMMIT` happens automatically after the callback was successfully resolved, and `ROLLBACK` is done automatically if the callback fails.
|
|
5031
|
+
*
|
|
5032
|
+
* Let's consider the case of transferring money from one user to another:
|
|
5033
|
+
*
|
|
5034
|
+
* ```ts
|
|
5035
|
+
* export const transferMoney = async (
|
|
5036
|
+
* fromId: number,
|
|
5037
|
+
* toId: number,
|
|
5038
|
+
* amount: number,
|
|
5039
|
+
* ) => {
|
|
5040
|
+
* try {
|
|
5041
|
+
* // db.$transaction returns data that is returned from the callback
|
|
5042
|
+
* // result here is senderRemainder
|
|
5043
|
+
* const result = await db.$transaction(async () => {
|
|
5044
|
+
* const sender = await db.user.find(fromId);
|
|
5045
|
+
* const senderRemainder = sender.balance - amount;
|
|
5046
|
+
* if (senderRemainder < 0) {
|
|
5047
|
+
* throw new Error('Sender does not have enough money');
|
|
5048
|
+
* }
|
|
5049
|
+
*
|
|
5050
|
+
* await db.user.find(fromId).decrement({
|
|
5051
|
+
* balance: amount,
|
|
5052
|
+
* });
|
|
5053
|
+
* await db.user.find(toId).increment({
|
|
5054
|
+
* balance: amount,
|
|
5055
|
+
* });
|
|
5056
|
+
*
|
|
5057
|
+
* return senderRemainder;
|
|
5058
|
+
* });
|
|
5059
|
+
* } catch (error) {
|
|
5060
|
+
* // handle transaction error
|
|
5061
|
+
* }
|
|
5062
|
+
* };
|
|
5063
|
+
* ```
|
|
5064
|
+
*
|
|
5065
|
+
* It performs 3 queries in a single transaction: load sender record, decrement sender's balance, increment receiver's balance.
|
|
5066
|
+
*
|
|
5067
|
+
* If sender or receiver record doesn't exist, it will throw `NotFound` error, and there is an error thrown when sender's balance is too low.
|
|
5068
|
+
* In such case, the transaction will be rolled back and no changes will be applied to the database.
|
|
5069
|
+
*
|
|
5070
|
+
* Internally, ORM relies on [AsyncLocalStorage](https://nodejs.org/api/async_context.html#class-asynclocalstorage) feature of node.js,
|
|
5071
|
+
* it allows passing the transaction object implicitly. So that any query that is done inside of callback, will run inside a transaction.
|
|
5072
|
+
*
|
|
5073
|
+
* ## nested transactions
|
|
5074
|
+
*
|
|
5075
|
+
* Transactions can be nested one in another.
|
|
5076
|
+
* The top level transaction is the real one,
|
|
5077
|
+
* and the nested ones are emulated with [savepoint](https://www.postgresql.org/docs/current/sql-savepoint.html) instead of `BEGIN`
|
|
5078
|
+
* and [release savepoint](https://www.postgresql.org/docs/current/sql-release-savepoint.html) instead of `COMMIT`.
|
|
5079
|
+
*
|
|
5080
|
+
* Use [ensureTransaction](#ensuretransaction) to run all queries in a single transaction.
|
|
5081
|
+
*
|
|
5082
|
+
* ```ts
|
|
5083
|
+
* const result = await db.$transaction(async () => {
|
|
5084
|
+
* await db.table.create(...one);
|
|
5085
|
+
*
|
|
5086
|
+
* const result = await db.$transaction(async () => {
|
|
5087
|
+
* await db.table.create(...two);
|
|
5088
|
+
* return 123;
|
|
5089
|
+
* });
|
|
5090
|
+
*
|
|
5091
|
+
* await db.table.create(...three);
|
|
5092
|
+
*
|
|
5093
|
+
* return result;
|
|
5094
|
+
* });
|
|
5095
|
+
*
|
|
5096
|
+
* // result is returned from the inner transaction
|
|
5097
|
+
* result === 123;
|
|
5098
|
+
* ```
|
|
5099
|
+
*
|
|
5100
|
+
* If the inner transaction throws an error, and it is caught by `try/catch` of outer transaction,
|
|
5101
|
+
* it performs [rollback to savepoint](https://www.postgresql.org/docs/current/sql-rollback-to.html)
|
|
5102
|
+
* and the outer transaction can continue:
|
|
5103
|
+
*
|
|
5104
|
+
* ```ts
|
|
5105
|
+
* class CustomError extends Error {}
|
|
5106
|
+
*
|
|
5107
|
+
* await db.$transaction(async () => {
|
|
5108
|
+
* try {
|
|
5109
|
+
* await db.$transaction(async () => {
|
|
5110
|
+
* throw new CustomError();
|
|
5111
|
+
* });
|
|
5112
|
+
* } catch (err) {
|
|
5113
|
+
* if (err instanceof CustomError) {
|
|
5114
|
+
* // ignore this error
|
|
5115
|
+
* return;
|
|
5116
|
+
* }
|
|
5117
|
+
* throw err;
|
|
5118
|
+
* }
|
|
5119
|
+
*
|
|
5120
|
+
* // this transaction can continue
|
|
5121
|
+
* await db.table.create(...data);
|
|
5122
|
+
* });
|
|
5123
|
+
* ```
|
|
5124
|
+
*
|
|
5125
|
+
* If the error in the inner transaction is not caught, all nested transactions are rolled back and aborted.
|
|
5126
|
+
*/
|
|
5127
|
+
transaction<Result>(this: PickQueryQAndInternal, cb: () => Promise<Result>): Promise<Result>;
|
|
5128
|
+
transaction<Result>(this: PickQueryQAndInternal, options: IsolationLevel | TransactionOptions, cb: () => Promise<Result>): Promise<Result>;
|
|
5129
|
+
/**
|
|
5130
|
+
* Use the `$ensureTransaction` when you want to ensure the sequence of queries is running in a transaction, but there is no need for Postgres [savepoints](https://www.postgresql.org/docs/current/sql-savepoint.html).
|
|
5131
|
+
*
|
|
5132
|
+
* ```ts
|
|
5133
|
+
* async function updateUserBalance(userId: string, amount: number) {
|
|
5134
|
+
* await db.$ensureTransaction(async () => {
|
|
5135
|
+
* await db.transfer.create({ userId, amount })
|
|
5136
|
+
* await db.user.find(userId).increment({ balance: amount })
|
|
5137
|
+
* })
|
|
5138
|
+
* }
|
|
5139
|
+
*
|
|
5140
|
+
* async function saveDeposit(userId: string, deposit: { ... }) {
|
|
5141
|
+
* await db.$ensureTransaction(async () => {
|
|
5142
|
+
* await db.deposit.create(deposit)
|
|
5143
|
+
* // transaction in updateUserBalance won't be started
|
|
5144
|
+
* await updateUserBalance(userId, deposit.amount)
|
|
5145
|
+
* })
|
|
5146
|
+
* }
|
|
5147
|
+
* ```
|
|
5148
|
+
*/
|
|
5149
|
+
ensureTransaction<Result>(this: PickQueryQAndInternal, cb: () => Promise<Result>): Promise<Result>;
|
|
5027
5150
|
}
|
|
5028
5151
|
|
|
5029
5152
|
type AfterHook<Select extends PropertyKey[], Shape extends QueryColumns> = QueryAfterHook<{
|
|
@@ -5243,11 +5366,11 @@ interface SelectAsCheckReturnTypes {
|
|
|
5243
5366
|
}
|
|
5244
5367
|
type SelectResult<T extends SelectSelf, Columns extends PropertyKey[]> = {
|
|
5245
5368
|
[K in keyof T]: K extends 'result' ? ('*' extends Columns[number] ? {
|
|
5246
|
-
[K in Columns[number] |
|
|
5369
|
+
[K in Columns[number] | T['meta']['defaultSelect'] as K extends '*' ? never : T['meta']['selectable'][K]['as']]: T['meta']['selectable'][K]['column'];
|
|
5247
5370
|
} : {
|
|
5248
5371
|
[K in Columns[number] as T['meta']['selectable'][K]['as']]: T['meta']['selectable'][K]['column'];
|
|
5249
5372
|
}) & (T['meta']['hasSelect'] extends true ? Omit<T['result'], Columns[number]> : unknown) : K extends 'then' ? QueryThen<GetQueryResult<T, ('*' extends Columns[number] ? {
|
|
5250
|
-
[K in Exclude<Columns[number], '*'> |
|
|
5373
|
+
[K in Exclude<Columns[number], '*'> | T['meta']['defaultSelect'] as T['meta']['selectable'][K]['as']]: T['meta']['selectable'][K]['column'];
|
|
5251
5374
|
} : {
|
|
5252
5375
|
[K in Columns[number] as T['meta']['selectable'][K]['as']]: T['meta']['selectable'][K]['column'];
|
|
5253
5376
|
}) & (T['meta']['hasSelect'] extends true ? Omit<T['result'], Columns[number]> : unknown)>> : T[K];
|
|
@@ -5270,9 +5393,9 @@ type SelectResultColumnsAndObj<T extends SelectSelf, Columns extends PropertyKey
|
|
|
5270
5393
|
selectable: SelectAsSelectable<Obj>;
|
|
5271
5394
|
} : K extends 'result' ? // Combine previously selected items, all columns if * was provided,
|
|
5272
5395
|
{
|
|
5273
|
-
[K in ('*' extends Columns[number] ? Exclude<Columns[number], '*'> |
|
|
5396
|
+
[K in ('*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['meta']['defaultSelect'] : Columns[number]) | keyof Obj as K extends Columns[number] ? T['meta']['selectable'][K]['as'] : K]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : T['meta']['selectable'][K]['column'];
|
|
5274
5397
|
} & (T['meta']['hasSelect'] extends true ? Omit<T['result'], Columns[number]> : unknown) : K extends 'then' ? QueryThen<GetQueryResult<T, {
|
|
5275
|
-
[K in ('*' extends Columns[number] ? Exclude<Columns[number], '*'> |
|
|
5398
|
+
[K in ('*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['meta']['defaultSelect'] : Columns[number]) | keyof Obj as K extends Columns[number] ? T['meta']['selectable'][K]['as'] : K]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : T['meta']['selectable'][K]['column'];
|
|
5276
5399
|
} & (T['meta']['hasSelect'] extends true ? Omit<T['result'], Columns[number]> : unknown)>> : T[K];
|
|
5277
5400
|
};
|
|
5278
5401
|
type SelectAsSelectable<Arg> = {
|
|
@@ -7479,7 +7602,6 @@ interface Query extends QueryBase, QueryMethods<unknown> {
|
|
|
7479
7602
|
then: QueryThen<unknown>;
|
|
7480
7603
|
catch: QueryCatch;
|
|
7481
7604
|
windows: EmptyObject;
|
|
7482
|
-
defaultSelectColumns: string[];
|
|
7483
7605
|
relations: RelationsBase;
|
|
7484
7606
|
error: new (message: string, length: number, name: QueryErrorName) => QueryError;
|
|
7485
7607
|
}
|
package/dist/index.js
CHANGED
|
@@ -805,8 +805,8 @@ const columnCode = (type, ctx, key, code, data = type.data, skip) => {
|
|
|
805
805
|
orchidCore.addCode(code, part);
|
|
806
806
|
}
|
|
807
807
|
}
|
|
808
|
-
if (data.
|
|
809
|
-
orchidCore.addCode(code, ".
|
|
808
|
+
if (data.explicitSelect)
|
|
809
|
+
orchidCore.addCode(code, ".select(false)");
|
|
810
810
|
if (data.isNullable)
|
|
811
811
|
orchidCore.addCode(code, ".nullable()");
|
|
812
812
|
if (type.encodeFn && type.encodeFn !== (skip == null ? void 0 : skip.encodeFn))
|
|
@@ -2358,7 +2358,7 @@ const getArgQueryTarget = (ctx, first, joinSubQuery, cloned) => {
|
|
|
2358
2358
|
}
|
|
2359
2359
|
};
|
|
2360
2360
|
const subJoinToSql = (ctx, jq, innerAs, outerAs, cloned) => {
|
|
2361
|
-
if (!jq.q.select && jq.
|
|
2361
|
+
if (!jq.q.select && jq.q.selectAllColumns) {
|
|
2362
2362
|
if (!cloned)
|
|
2363
2363
|
jq = jq.clone();
|
|
2364
2364
|
jq.q.select = [new RawSQL(`${innerAs}.*`)];
|
|
@@ -2695,7 +2695,7 @@ const _join = (query, require2, type, first, args) => {
|
|
|
2695
2695
|
);
|
|
2696
2696
|
if (joinKey && "s" in joinArgs && joinArgs.s) {
|
|
2697
2697
|
const j = "j" in joinArgs ? (_b = joinArgs.r) != null ? _b : joinArgs.j : "r" in joinArgs ? joinArgs.r : joinArgs.q;
|
|
2698
|
-
if (j.q.select || !j.
|
|
2698
|
+
if (j.q.select || !j.q.selectAllColumns) {
|
|
2699
2699
|
const shape2 = getShapeFromSelect(j, true);
|
|
2700
2700
|
setQueryObjectValue(
|
|
2701
2701
|
query,
|
|
@@ -3397,6 +3397,32 @@ class Transaction {
|
|
|
3397
3397
|
}
|
|
3398
3398
|
}
|
|
3399
3399
|
}
|
|
3400
|
+
/**
|
|
3401
|
+
* Use the `$ensureTransaction` when you want to ensure the sequence of queries is running in a transaction, but there is no need for Postgres [savepoints](https://www.postgresql.org/docs/current/sql-savepoint.html).
|
|
3402
|
+
*
|
|
3403
|
+
* ```ts
|
|
3404
|
+
* async function updateUserBalance(userId: string, amount: number) {
|
|
3405
|
+
* await db.$ensureTransaction(async () => {
|
|
3406
|
+
* await db.transfer.create({ userId, amount })
|
|
3407
|
+
* await db.user.find(userId).increment({ balance: amount })
|
|
3408
|
+
* })
|
|
3409
|
+
* }
|
|
3410
|
+
*
|
|
3411
|
+
* async function saveDeposit(userId: string, deposit: { ... }) {
|
|
3412
|
+
* await db.$ensureTransaction(async () => {
|
|
3413
|
+
* await db.deposit.create(deposit)
|
|
3414
|
+
* // transaction in updateUserBalance won't be started
|
|
3415
|
+
* await updateUserBalance(userId, deposit.amount)
|
|
3416
|
+
* })
|
|
3417
|
+
* }
|
|
3418
|
+
* ```
|
|
3419
|
+
*/
|
|
3420
|
+
ensureTransaction(cb) {
|
|
3421
|
+
const trx = this.internal.transactionStorage.getStore();
|
|
3422
|
+
if (trx)
|
|
3423
|
+
return cb();
|
|
3424
|
+
return Transaction.prototype.transaction.call(this, cb);
|
|
3425
|
+
}
|
|
3400
3426
|
}
|
|
3401
3427
|
const runAfterCommit = async (afterCommit, result) => {
|
|
3402
3428
|
if (afterCommit) {
|
|
@@ -4689,7 +4715,7 @@ class SelectItemExpression extends orchidCore.Expression {
|
|
|
4689
4715
|
}
|
|
4690
4716
|
// `makeSQL` acts similarly to how select args are handled
|
|
4691
4717
|
makeSQL(ctx, quotedAs) {
|
|
4692
|
-
return typeof this.item === "string" ? this.item === "*" ? selectAllSql(this.
|
|
4718
|
+
return typeof this.item === "string" ? this.item === "*" ? selectAllSql(this.q, quotedAs) : columnToSql(ctx, this.q, this.q.shape, this.item, quotedAs, true) : this.item.toSQL(ctx, quotedAs);
|
|
4693
4719
|
}
|
|
4694
4720
|
}
|
|
4695
4721
|
|
|
@@ -4823,12 +4849,14 @@ function queryFrom(self, arg) {
|
|
|
4823
4849
|
data.batchParsers = q.q.batchParsers;
|
|
4824
4850
|
}
|
|
4825
4851
|
data.from = arg;
|
|
4852
|
+
data.selectAllColumns = data.selectAllKeys = void 0;
|
|
4826
4853
|
return self;
|
|
4827
4854
|
}
|
|
4828
4855
|
function queryFromSql(self, args) {
|
|
4829
4856
|
const data = self.q;
|
|
4830
4857
|
data.as || (data.as = "t");
|
|
4831
4858
|
data.from = sqlQueryArgsToExpression(args);
|
|
4859
|
+
data.selectAllColumns = data.selectAllKeys = void 0;
|
|
4832
4860
|
return self;
|
|
4833
4861
|
}
|
|
4834
4862
|
class FromMethods {
|
|
@@ -4943,11 +4971,11 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
|
|
|
4943
4971
|
if (item === "*") {
|
|
4944
4972
|
if (hookSelect) {
|
|
4945
4973
|
selected != null ? selected : selected = {};
|
|
4946
|
-
for (const key in
|
|
4974
|
+
for (const key in query.selectAllKeys || query.shape) {
|
|
4947
4975
|
selected[key] = quotedAs;
|
|
4948
4976
|
}
|
|
4949
4977
|
}
|
|
4950
|
-
sql = selectAllSql(
|
|
4978
|
+
sql = selectAllSql(query, quotedAs);
|
|
4951
4979
|
} else {
|
|
4952
4980
|
const index = item.indexOf(".");
|
|
4953
4981
|
if (index !== -1) {
|
|
@@ -5049,15 +5077,15 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
|
|
|
5049
5077
|
list.push(sql);
|
|
5050
5078
|
}
|
|
5051
5079
|
}
|
|
5052
|
-
return list.length ? list.join(", ") : query.select ? "" : selectAllSql(
|
|
5080
|
+
return list.length ? list.join(", ") : query.select ? "" : selectAllSql(query, quotedAs);
|
|
5053
5081
|
};
|
|
5054
5082
|
function selectedObjectToSQL(ctx, quotedAs, item) {
|
|
5055
5083
|
const sql = item.toSQL(ctx, quotedAs);
|
|
5056
5084
|
return ctx.aliasValue ? `${sql} r` : sql;
|
|
5057
5085
|
}
|
|
5058
|
-
const selectAllSql = (
|
|
5086
|
+
const selectAllSql = (query, quotedAs) => {
|
|
5059
5087
|
var _a, _b, _c;
|
|
5060
|
-
return ((_a = query.join) == null ? void 0 : _a.length) ? ((_b =
|
|
5088
|
+
return ((_a = query.join) == null ? void 0 : _a.length) ? ((_b = query.selectAllColumns) == null ? void 0 : _b.map((item) => `${quotedAs}.${item}`).join(", ")) || `${quotedAs}.*` : ((_c = query.selectAllColumns) == null ? void 0 : _c.join(", ")) || "*";
|
|
5061
5089
|
};
|
|
5062
5090
|
const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
|
|
5063
5091
|
var _a, _b, _c;
|
|
@@ -5251,7 +5279,7 @@ const pushWithSql = (ctx, items) => {
|
|
|
5251
5279
|
|
|
5252
5280
|
const checkIfASimpleQuery = (q) => {
|
|
5253
5281
|
var _a, _b;
|
|
5254
|
-
if (q.q.returnType && q.q.returnType !== "all" || q.
|
|
5282
|
+
if (q.q.returnType && q.q.returnType !== "all" || q.q.selectAllColumns || ((_a = q.q.and) == null ? void 0 : _a.length) || ((_b = q.q.or) == null ? void 0 : _b.length) || q.q.scopes)
|
|
5255
5283
|
return false;
|
|
5256
5284
|
const keys = Object.keys(q.q);
|
|
5257
5285
|
return !keys.some((key) => queryKeysOfNotSimpleQuery.includes(key));
|
|
@@ -5279,7 +5307,7 @@ const pushFromAndAs = (ctx, table, data, quotedAs) => {
|
|
|
5279
5307
|
const from = getFrom(ctx, table, data, quotedAs);
|
|
5280
5308
|
sql += from;
|
|
5281
5309
|
if (data.as && quotedAs && quotedAs !== from) {
|
|
5282
|
-
sql += `
|
|
5310
|
+
sql += ` ${quotedAs}`;
|
|
5283
5311
|
}
|
|
5284
5312
|
for (const as in data.sources) {
|
|
5285
5313
|
const source = data.sources[as];
|
|
@@ -9413,16 +9441,17 @@ var __spreadValues$5 = (a, b) => {
|
|
|
9413
9441
|
}
|
|
9414
9442
|
return a;
|
|
9415
9443
|
};
|
|
9416
|
-
const mergableObjects =
|
|
9417
|
-
shape
|
|
9418
|
-
withShapes
|
|
9419
|
-
parsers
|
|
9420
|
-
defaults
|
|
9421
|
-
joinedShapes
|
|
9422
|
-
joinedParsers
|
|
9423
|
-
joinedBatchParsers
|
|
9424
|
-
selectedComputeds
|
|
9425
|
-
|
|
9444
|
+
const mergableObjects = /* @__PURE__ */ new Set([
|
|
9445
|
+
"shape",
|
|
9446
|
+
"withShapes",
|
|
9447
|
+
"parsers",
|
|
9448
|
+
"defaults",
|
|
9449
|
+
"joinedShapes",
|
|
9450
|
+
"joinedParsers",
|
|
9451
|
+
"joinedBatchParsers",
|
|
9452
|
+
"selectedComputeds"
|
|
9453
|
+
]);
|
|
9454
|
+
const dontMergeArrays = /* @__PURE__ */ new Set(["selectAllColumns", "selectAllKeys"]);
|
|
9426
9455
|
class MergeQueryMethods {
|
|
9427
9456
|
merge(q) {
|
|
9428
9457
|
const query = this.clone();
|
|
@@ -9438,8 +9467,10 @@ class MergeQueryMethods {
|
|
|
9438
9467
|
break;
|
|
9439
9468
|
case "object":
|
|
9440
9469
|
if (Array.isArray(value)) {
|
|
9441
|
-
|
|
9442
|
-
|
|
9470
|
+
if (!dontMergeArrays.has(key)) {
|
|
9471
|
+
a[key] = a[key] ? [...a[key], ...value] : value;
|
|
9472
|
+
}
|
|
9473
|
+
} else if (mergableObjects.has(key)) {
|
|
9443
9474
|
a[key] = a[key] ? __spreadValues$5(__spreadValues$5({}, a[key]), value) : value;
|
|
9444
9475
|
} else if (key === "union") {
|
|
9445
9476
|
a[key] = a[key] ? {
|
|
@@ -11359,7 +11390,11 @@ class QueryUpsertOrCreate {
|
|
|
11359
11390
|
if (!orchidCore.isObjectEmpty(updateData)) {
|
|
11360
11391
|
_queryUpdate(q, updateData);
|
|
11361
11392
|
}
|
|
11362
|
-
|
|
11393
|
+
const c = orCreate(q, data.create, updateData, mergeData);
|
|
11394
|
+
if (!c.q.select) {
|
|
11395
|
+
c.q.returnType = "void";
|
|
11396
|
+
}
|
|
11397
|
+
return c;
|
|
11363
11398
|
}
|
|
11364
11399
|
/**
|
|
11365
11400
|
* `orCreate` creates a record only if it was not found by conditions.
|
|
@@ -12660,7 +12695,7 @@ class Db {
|
|
|
12660
12695
|
const parsers = {};
|
|
12661
12696
|
let hasParsers = false;
|
|
12662
12697
|
let modifyQuery = void 0;
|
|
12663
|
-
let
|
|
12698
|
+
let prepareSelectAll = false;
|
|
12664
12699
|
const { snakeCase } = options;
|
|
12665
12700
|
for (const key in shape) {
|
|
12666
12701
|
const column = shape[key];
|
|
@@ -12670,14 +12705,17 @@ class Db {
|
|
|
12670
12705
|
parsers[key] = column.parseFn;
|
|
12671
12706
|
}
|
|
12672
12707
|
if (column.data.name) {
|
|
12673
|
-
|
|
12708
|
+
prepareSelectAll = true;
|
|
12674
12709
|
} else if (snakeCase) {
|
|
12675
12710
|
const snakeName = orchidCore.toSnakeCase(key);
|
|
12676
12711
|
if (snakeName !== key) {
|
|
12677
|
-
|
|
12712
|
+
prepareSelectAll = true;
|
|
12678
12713
|
column.data.name = snakeName;
|
|
12679
12714
|
}
|
|
12680
12715
|
}
|
|
12716
|
+
if (column.data.explicitSelect) {
|
|
12717
|
+
prepareSelectAll = true;
|
|
12718
|
+
}
|
|
12681
12719
|
const { modifyQuery: mq } = column.data;
|
|
12682
12720
|
if (mq) {
|
|
12683
12721
|
modifyQuery = orchidCore.pushOrNewArray(modifyQuery, (q) => mq(q, column));
|
|
@@ -12697,17 +12735,6 @@ class Db {
|
|
|
12697
12735
|
}
|
|
12698
12736
|
}
|
|
12699
12737
|
}
|
|
12700
|
-
if (hasCustomName) {
|
|
12701
|
-
const list = [];
|
|
12702
|
-
for (const key in shape) {
|
|
12703
|
-
const column = shape[key];
|
|
12704
|
-
list.push(
|
|
12705
|
-
column.data.name ? `"${column.data.name}" AS "${key}"` : `"${key}"`
|
|
12706
|
-
);
|
|
12707
|
-
}
|
|
12708
|
-
this.internal.columnsForSelectAll = list;
|
|
12709
|
-
this.internal.columnsKeysForSelectAll = __spreadValues({}, shape);
|
|
12710
|
-
}
|
|
12711
12738
|
this.q = {
|
|
12712
12739
|
adapter,
|
|
12713
12740
|
shape,
|
|
@@ -12740,21 +12767,22 @@ class Db {
|
|
|
12740
12767
|
const columns = Object.keys(
|
|
12741
12768
|
shape
|
|
12742
12769
|
);
|
|
12743
|
-
const { toSQL } = this;
|
|
12744
12770
|
this.columns = columns;
|
|
12745
|
-
this.defaultSelectColumns = columns.filter(
|
|
12746
|
-
(column) => !shape[column].data.isHidden
|
|
12747
|
-
);
|
|
12748
12771
|
if (options.computed)
|
|
12749
12772
|
applyComputedColumns(this, options.computed);
|
|
12750
|
-
|
|
12751
|
-
|
|
12752
|
-
const
|
|
12753
|
-
|
|
12754
|
-
|
|
12755
|
-
|
|
12756
|
-
|
|
12757
|
-
|
|
12773
|
+
if (prepareSelectAll) {
|
|
12774
|
+
const list = [];
|
|
12775
|
+
for (const key in shape) {
|
|
12776
|
+
const column = shape[key];
|
|
12777
|
+
if (!column.data.explicitSelect) {
|
|
12778
|
+
list.push(
|
|
12779
|
+
column.data.name ? `"${column.data.name}" AS "${key}"` : `"${key}"`
|
|
12780
|
+
);
|
|
12781
|
+
}
|
|
12782
|
+
}
|
|
12783
|
+
this.q.selectAllColumns = list;
|
|
12784
|
+
this.q.selectAllKeys = __spreadValues({}, shape);
|
|
12785
|
+
}
|
|
12758
12786
|
if (modifyQuery) {
|
|
12759
12787
|
for (const cb of modifyQuery) {
|
|
12760
12788
|
cb(this);
|