pqb 0.11.22 → 0.11.24
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 +36 -29
- package/dist/index.js +47 -24
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +46 -24
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -336,8 +336,8 @@ type CommonQueryData = {
|
|
|
336
336
|
parsers?: ColumnsParsers;
|
|
337
337
|
notFoundDefault?: unknown;
|
|
338
338
|
defaults?: Record<string, unknown>;
|
|
339
|
-
beforeQuery?:
|
|
340
|
-
afterQuery?:
|
|
339
|
+
beforeQuery?: BeforeHook[];
|
|
340
|
+
afterQuery?: AfterHook[];
|
|
341
341
|
log?: QueryLogObject;
|
|
342
342
|
logger: QueryLogger;
|
|
343
343
|
autoPreparedStatements?: boolean;
|
|
@@ -385,8 +385,8 @@ type InsertQueryData = CommonQueryData & {
|
|
|
385
385
|
expr?: OnConflictItem;
|
|
386
386
|
update?: OnConflictMergeUpdate;
|
|
387
387
|
};
|
|
388
|
-
beforeCreate?:
|
|
389
|
-
afterCreate?:
|
|
388
|
+
beforeCreate?: BeforeHook[];
|
|
389
|
+
afterCreate?: AfterHook[];
|
|
390
390
|
};
|
|
391
391
|
type UpdateQueryDataObject = Record<string, RawExpression | {
|
|
392
392
|
op: string;
|
|
@@ -397,14 +397,14 @@ type UpdateQueryDataItem = UpdateQueryDataObject | RawExpression | UpdatedAtData
|
|
|
397
397
|
type UpdateQueryData = CommonQueryData & {
|
|
398
398
|
type: 'update';
|
|
399
399
|
updateData: UpdateQueryDataItem[];
|
|
400
|
-
beforeUpdate?:
|
|
401
|
-
afterUpdate?:
|
|
400
|
+
beforeUpdate?: BeforeHook[];
|
|
401
|
+
afterUpdate?: AfterHook[];
|
|
402
402
|
};
|
|
403
403
|
type DeleteQueryData = CommonQueryData & {
|
|
404
404
|
type: 'delete';
|
|
405
405
|
join?: JoinItem[];
|
|
406
|
-
beforeDelete?:
|
|
407
|
-
afterDelete?:
|
|
406
|
+
beforeDelete?: BeforeHook[];
|
|
407
|
+
afterDelete?: AfterHook[];
|
|
408
408
|
};
|
|
409
409
|
type TruncateQueryData = CommonQueryData & {
|
|
410
410
|
type: 'truncate';
|
|
@@ -1320,25 +1320,32 @@ declare class QueryLog {
|
|
|
1320
1320
|
_log<T extends Query>(this: T, log?: boolean): T;
|
|
1321
1321
|
}
|
|
1322
1322
|
|
|
1323
|
-
type
|
|
1324
|
-
type
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1323
|
+
type BeforeHook<T extends Query = Query> = (query: T) => void | Promise<void>;
|
|
1324
|
+
type AfterHook<T extends Query = Query> = (query: T, data: unknown) => void | Promise<void>;
|
|
1325
|
+
type BeforeHookKey = 'beforeQuery' | 'beforeCreate' | 'beforeUpdate' | 'beforeSave' | 'beforeDelete';
|
|
1326
|
+
type AfterHookKey = 'afterQuery' | 'afterCreate' | 'afterUpdate' | 'afterSave' | 'afterDelete';
|
|
1327
|
+
declare const addQueryHook: <T extends Query>(q: T, ...[key, cb]: [BeforeHookKey, BeforeHook<T>] | [AfterHookKey, AfterHook<T>]) => T;
|
|
1328
|
+
declare class QueryHooks {
|
|
1329
|
+
beforeQuery<T extends Query>(this: T, cb: BeforeHook<T>): T;
|
|
1330
|
+
_beforeQuery<T extends Query>(this: T, cb: BeforeHook<T>): T;
|
|
1331
|
+
afterQuery<T extends Query>(this: T, cb: AfterHook<T>): T;
|
|
1332
|
+
_afterQuery<T extends Query>(this: T, cb: AfterHook<T>): T;
|
|
1333
|
+
beforeCreate<T extends Query>(this: T, cb: BeforeHook<T>): T;
|
|
1334
|
+
_beforeCreate<T extends Query>(this: T, cb: BeforeHook<T>): T;
|
|
1335
|
+
afterCreate<T extends Query>(this: T, cb: AfterHook<T>): T;
|
|
1336
|
+
_afterCreate<T extends Query>(this: T, cb: AfterHook<T>): T;
|
|
1337
|
+
beforeUpdate<T extends Query>(this: T, cb: BeforeHook<T>): T;
|
|
1338
|
+
_beforeUpdate<T extends Query>(this: T, cb: BeforeHook<T>): T;
|
|
1339
|
+
afterUpdate<T extends Query>(this: T, cb: AfterHook<T>): T;
|
|
1340
|
+
_afterUpdate<T extends Query>(this: T, cb: AfterHook<T>): T;
|
|
1341
|
+
beforeSave<T extends Query>(this: T, cb: BeforeHook<T>): T;
|
|
1342
|
+
_beforeSave<T extends Query>(this: T, cb: BeforeHook<T>): T;
|
|
1343
|
+
afterSave<T extends Query>(this: T, cb: AfterHook<T>): T;
|
|
1344
|
+
_afterSave<T extends Query>(this: T, cb: AfterHook<T>): T;
|
|
1345
|
+
beforeDelete<T extends Query>(this: T, cb: BeforeHook<T>): T;
|
|
1346
|
+
_beforeDelete<T extends Query>(this: T, cb: BeforeHook<T>): T;
|
|
1347
|
+
afterDelete<T extends Query>(this: T, cb: AfterHook<T>): T;
|
|
1348
|
+
_afterDelete<T extends Query>(this: T, cb: AfterHook<T>): T;
|
|
1342
1349
|
}
|
|
1343
1350
|
|
|
1344
1351
|
type UpsertCreateArg<T extends Query> = CreateData<T> | (() => CreateData<T>);
|
|
@@ -1400,7 +1407,7 @@ type OrderArg<T extends Query, Key extends PropertyKey = keyof T['selectable'] |
|
|
|
1400
1407
|
}[keyof T['result']]> = Key | {
|
|
1401
1408
|
[K in Key]?: SortDir;
|
|
1402
1409
|
} | RawExpression;
|
|
1403
|
-
interface QueryMethods extends Omit<AsMethods, 'result'>, Aggregate, Select, From, Join, With, Union, Json, Create, Update, Delete, Transaction, For, ColumnInfoMethods, Omit<Where, 'result'>, Clear, Having, Window, Then, QueryLog,
|
|
1410
|
+
interface QueryMethods extends Omit<AsMethods, 'result'>, Aggregate, Select, From, Join, With, Union, Json, Create, Update, Delete, Transaction, For, ColumnInfoMethods, Omit<Where, 'result'>, Clear, Having, Window, Then, QueryLog, QueryHooks, QueryUpsertOrCreate, QueryGet, MergeQueryMethods, RawMethods, CopyMethods {
|
|
1404
1411
|
}
|
|
1405
1412
|
declare class QueryMethods {
|
|
1406
1413
|
windows: EmptyObject;
|
|
@@ -3727,4 +3734,4 @@ declare const testTransaction: {
|
|
|
3727
3734
|
close(arg: Arg): Promise<void>;
|
|
3728
3735
|
};
|
|
3729
3736
|
|
|
3730
|
-
export { Adapter, AdapterConfig, AdapterOptions, AddQuerySelect, AddQueryWith,
|
|
3737
|
+
export { Adapter, AdapterConfig, AdapterOptions, AddQuerySelect, AddQueryWith, AfterHook, AfterHookKey, Aggregate, Aggregate1ArgumentTypes, AggregateArg, AggregateItem, AggregateItemArg, AggregateItemOptions, AggregateOptions, AliasOrTable, ArrayColumn, ArrayData, ArrayOfColumnsObjects, BaseRelation, BeforeHook, BeforeHookKey, BelongsToRelation, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BooleanExpression, BoxColumn, ByteaColumn, CharColumn, CidrColumn, CircleColumn, CitextColumn, Clear, ClearStatement, ColumnData, ColumnFromDbParams, ColumnInfo, ColumnInfoMethods, ColumnInfoQueryData, ColumnNameOfTable, ColumnOperators, ColumnParser, ColumnType, ColumnTypes, ColumnsObject, ColumnsParsers, ColumnsShape, CommonQueryData, CopyOptions, CopyQueryData, Create, CreateCtx, CreateData, CreateMethodsNames, CustomTypeColumn, DateBaseColumn, DateColumn, DateTimeBaseClass, DateTimeTzBaseClass, Db, DbOptions, DbResult, DbTableOptions, DecimalBaseColumn, DecimalColumn, DefaultColumnTypes, Delete, DeleteMethodsNames, DeleteQueryData, DomainColumn, DoublePrecisionColumn, DropMode, EnumColumn, Expression, ExpressionOfType, ExpressionOutput, For, ForeignKey, ForeignKeyAction, ForeignKeyMatch, ForeignKeyOptions, ForeignKeyTable, From, FromArgs, FromResult, GetArg, HasAndBelongsToManyRelation, HasManyRelation, HasOneRelation, Having, HavingArg, HavingItem, IdentityColumn, IndexColumnOptions, IndexOptions, InetColumn, InsertQueryData, IntegerBaseColumn, IntegerColumn, IntervalColumn, IsolationLevel, JSONColumn, JSONTextColumn, JSONTypes, Join, JoinArgs, JoinCallback, JoinFirstArg, JoinItem, JoinLateralCallback, JoinLateralItem, JoinLateralResult, JoinResult, JoinedParsers, JoinedShapes, Json, JsonItem, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MergeQuery, MergeQueryMethods, MoneyColumn, MoreThanOneRowError, NoPrimaryKeyOption, NotFoundError, NumberAsStringBaseColumn, NumberBaseColumn, NumberColumn, NumberColumnData, NumberExpression, OnConflictItem, OnConflictMergeUpdate, OnConflictQueryBuilder, OnQueryBuilder, Operators, OrchidOrmError, OrchidOrmInternalError, OrderArg, OrderItem, PathColumn, PluckResultColumnType, PointColumn, PolygonColumn, Query, QueryArraysResult, QueryBase, QueryData, QueryError, QueryErrorName, QueryGet, QueryHooks, QueryLog, QueryLogObject, QueryLogOptions, QueryLogger, QueryMethods, QueryResult, QueryReturnType, QueryReturnsAll, QueryThen, QueryUpsertOrCreate, QueryWithTable, RawMethods, RealColumn, Relation, RelationQuery, RelationQueryBase, RelationQueryData, RelationsBase, Select, SelectAgg, SelectArg, SelectFunctionItem, SelectItem, SelectQueryData, Selectable, SelectableBase, SelectableFromShape, SerialColumn, SerialColumnData, SetQueryReturns, SetQueryReturnsAll, SetQueryReturnsColumnInfo, SetQueryReturnsOne, SetQueryReturnsOneOptional, SetQueryReturnsPluck, SetQueryReturnsRowCount, SetQueryReturnsRows, SetQueryReturnsValue, SetQueryReturnsValueOptional, SetQueryReturnsVoid, SetQueryTableAlias, SetQueryWith, SimpleJoinItem, SingleColumnIndexOptions, SmallIntColumn, SmallSerialColumn, SortDir, StringColumn, StringExpression, TableData, TextBaseColumn, TextColumn, TextColumnData, Then, TimeColumn, TimeInterval, TimestampColumn, TimestampTZColumn, ToSqlCtx, ToSqlOptions, Transaction, TransactionAdapter, TransactionOptions, TruncateQueryData, TsQueryColumn, TsVectorColumn, TypeParsers, UUIDColumn, UnhandledTypeError, Union, UnionArg, UnionItem, UnionKind, UnknownColumn, Update, UpdateCtx, UpdateData, UpdateQueryData, UpdateQueryDataItem, UpdateQueryDataObject, UpdatedAtDataInjector, UpsertCreateArg, UpsertData, UpsertResult, UpsertThis, VarCharColumn, VirtualColumn, Where, WhereArg, WhereInArg, WhereInColumn, WhereInItem, WhereInValues, WhereItem, WhereJsonPathEqualsItem, WhereOnItem, WhereOnJoinItem, WhereQueryBuilder, WhereResult, WindowArg, WindowArgDeclaration, WindowDeclaration, WindowFunctionOptions, WindowItem, With, WithDataBase, WithDataItem, WithItem, WithOptions, XMLColumn, addOr, addOrNot, addParserForRawExpression, addParserForSelectItem, addParserToQuery, addQueryHook, addQueryOn, addQueryOrOn, addWhere, addWhereIn, addWhereNot, aggregate1FunctionNames, anyShape, checkIfASimpleQuery, cloneQueryArrays, columnCheckToCode, columnCode, columnForeignKeysToCode, columnIndexesToCode, columnTypes, utils as columnUtils, columnsByType, columnsShapeToCode, constraintPropsToCode, constraintToCode, createDb, foreignKeyArgumentToCode, getClonedQueryData, getColumnTypes, getConstraintKind, getQueryAs, getRaw, getShapeFromSelect, getTableData, getValueKey, handleResult, identityToCode, indexToCode, instantiateColumn, isQueryReturnsAll, isRequiredRelationKey, jsonTypes, logColors, logParamToLogObject, makeRegexToFindInSql, makeSql, newTableData, parseRecord, parseResult, primaryKeyToCode, processSelectArg, pushQueryArray, pushQueryOn, pushQueryOrOn, pushQueryValue, queryMethodByReturnType, queryTypeWithLimitOne, quote, quoteString, referencesArgsToCode, relationQueryKey, resetTableData, setQueryObjectValue, simplifyColumnDefault, testTransaction, toSql, toSqlCacheKey };
|
package/dist/index.js
CHANGED
|
@@ -4445,54 +4445,76 @@ class Aggregate {
|
|
|
4445
4445
|
}
|
|
4446
4446
|
}
|
|
4447
4447
|
|
|
4448
|
-
|
|
4448
|
+
const addQueryHook = (q, ...[key, cb]) => {
|
|
4449
|
+
if (key.endsWith("Save")) {
|
|
4450
|
+
const when = key.slice(0, -4);
|
|
4451
|
+
pushQueryValue(q, when + "Update", cb);
|
|
4452
|
+
pushQueryValue(q, when + "Create", cb);
|
|
4453
|
+
} else {
|
|
4454
|
+
pushQueryValue(q, key, cb);
|
|
4455
|
+
}
|
|
4456
|
+
return q;
|
|
4457
|
+
};
|
|
4458
|
+
class QueryHooks {
|
|
4449
4459
|
beforeQuery(cb) {
|
|
4450
4460
|
return this.clone()._beforeQuery(cb);
|
|
4451
4461
|
}
|
|
4452
4462
|
_beforeQuery(cb) {
|
|
4453
|
-
return
|
|
4463
|
+
return addQueryHook(this, "beforeQuery", cb);
|
|
4454
4464
|
}
|
|
4455
4465
|
afterQuery(cb) {
|
|
4456
4466
|
return this.clone()._afterQuery(cb);
|
|
4457
4467
|
}
|
|
4458
4468
|
_afterQuery(cb) {
|
|
4459
|
-
return
|
|
4469
|
+
return addQueryHook(this, "afterQuery", cb);
|
|
4460
4470
|
}
|
|
4461
4471
|
beforeCreate(cb) {
|
|
4462
4472
|
return this.clone()._beforeCreate(cb);
|
|
4463
4473
|
}
|
|
4464
4474
|
_beforeCreate(cb) {
|
|
4465
|
-
return
|
|
4475
|
+
return addQueryHook(this, "beforeCreate", cb);
|
|
4466
4476
|
}
|
|
4467
4477
|
afterCreate(cb) {
|
|
4468
4478
|
return this.clone()._afterCreate(cb);
|
|
4469
4479
|
}
|
|
4470
4480
|
_afterCreate(cb) {
|
|
4471
|
-
return
|
|
4481
|
+
return addQueryHook(this, "afterCreate", cb);
|
|
4472
4482
|
}
|
|
4473
4483
|
beforeUpdate(cb) {
|
|
4474
4484
|
return this.clone()._beforeUpdate(cb);
|
|
4475
4485
|
}
|
|
4476
4486
|
_beforeUpdate(cb) {
|
|
4477
|
-
return
|
|
4487
|
+
return addQueryHook(this, "beforeUpdate", cb);
|
|
4478
4488
|
}
|
|
4479
4489
|
afterUpdate(cb) {
|
|
4480
4490
|
return this.clone()._afterUpdate(cb);
|
|
4481
4491
|
}
|
|
4482
4492
|
_afterUpdate(cb) {
|
|
4483
|
-
return
|
|
4493
|
+
return addQueryHook(this, "afterUpdate", cb);
|
|
4494
|
+
}
|
|
4495
|
+
beforeSave(cb) {
|
|
4496
|
+
return this.clone()._beforeSave(cb);
|
|
4497
|
+
}
|
|
4498
|
+
_beforeSave(cb) {
|
|
4499
|
+
return addQueryHook(this, "beforeSave", cb);
|
|
4500
|
+
}
|
|
4501
|
+
afterSave(cb) {
|
|
4502
|
+
return this.clone()._afterSave(cb);
|
|
4503
|
+
}
|
|
4504
|
+
_afterSave(cb) {
|
|
4505
|
+
return addQueryHook(this, "afterSave", cb);
|
|
4484
4506
|
}
|
|
4485
4507
|
beforeDelete(cb) {
|
|
4486
4508
|
return this.clone()._beforeDelete(cb);
|
|
4487
4509
|
}
|
|
4488
4510
|
_beforeDelete(cb) {
|
|
4489
|
-
return
|
|
4511
|
+
return addQueryHook(this, "beforeDelete", cb);
|
|
4490
4512
|
}
|
|
4491
4513
|
afterDelete(cb) {
|
|
4492
4514
|
return this.clone()._afterDelete(cb);
|
|
4493
4515
|
}
|
|
4494
4516
|
_afterDelete(cb) {
|
|
4495
|
-
return
|
|
4517
|
+
return addQueryHook(this, "afterDelete", cb);
|
|
4496
4518
|
}
|
|
4497
4519
|
}
|
|
4498
4520
|
|
|
@@ -4791,21 +4813,21 @@ const then = async (q, adapter, resolve, reject) => {
|
|
|
4791
4813
|
let logData;
|
|
4792
4814
|
const localError = queryError;
|
|
4793
4815
|
try {
|
|
4794
|
-
let
|
|
4795
|
-
let
|
|
4816
|
+
let beforeHooks;
|
|
4817
|
+
let afterHooks;
|
|
4796
4818
|
if (q.query.type === "insert") {
|
|
4797
|
-
|
|
4798
|
-
|
|
4819
|
+
beforeHooks = q.query.beforeCreate;
|
|
4820
|
+
afterHooks = q.query.afterCreate;
|
|
4799
4821
|
} else if (q.query.type === "update") {
|
|
4800
|
-
|
|
4801
|
-
|
|
4822
|
+
beforeHooks = q.query.beforeUpdate;
|
|
4823
|
+
afterHooks = q.query.afterUpdate;
|
|
4802
4824
|
} else if (q.query.type === "delete") {
|
|
4803
|
-
|
|
4804
|
-
|
|
4825
|
+
beforeHooks = q.query.beforeDelete;
|
|
4826
|
+
afterHooks = q.query.afterDelete;
|
|
4805
4827
|
}
|
|
4806
|
-
if (
|
|
4828
|
+
if (beforeHooks || q.query.beforeQuery) {
|
|
4807
4829
|
await Promise.all(
|
|
4808
|
-
|
|
4830
|
+
getHooks(beforeHooks, q.query.beforeQuery).map((cb) => cb(q))
|
|
4809
4831
|
);
|
|
4810
4832
|
}
|
|
4811
4833
|
sql = q.toSql();
|
|
@@ -4824,9 +4846,9 @@ const then = async (q, adapter, resolve, reject) => {
|
|
|
4824
4846
|
sql = void 0;
|
|
4825
4847
|
}
|
|
4826
4848
|
const result = q.query.handleResult(q, queryResult);
|
|
4827
|
-
if (
|
|
4849
|
+
if (afterHooks || q.query.afterQuery) {
|
|
4828
4850
|
await Promise.all(
|
|
4829
|
-
|
|
4851
|
+
getHooks(q.query.afterQuery, afterHooks).map(
|
|
4830
4852
|
(query) => query(q, result)
|
|
4831
4853
|
)
|
|
4832
4854
|
);
|
|
@@ -4959,7 +4981,7 @@ const parseValue = (value, parsers) => {
|
|
|
4959
4981
|
}
|
|
4960
4982
|
return value;
|
|
4961
4983
|
};
|
|
4962
|
-
const
|
|
4984
|
+
const getHooks = (first, second) => {
|
|
4963
4985
|
return first && second ? [...first, ...second] : first ? first : second;
|
|
4964
4986
|
};
|
|
4965
4987
|
|
|
@@ -6408,7 +6430,7 @@ orchidCore.applyMixins(QueryMethods, [
|
|
|
6408
6430
|
Window,
|
|
6409
6431
|
Then,
|
|
6410
6432
|
QueryLog,
|
|
6411
|
-
|
|
6433
|
+
QueryHooks,
|
|
6412
6434
|
QueryUpsertOrCreate,
|
|
6413
6435
|
QueryGet,
|
|
6414
6436
|
MergeQueryMethods,
|
|
@@ -6750,9 +6772,9 @@ exports.PluckResultColumnType = PluckResultColumnType;
|
|
|
6750
6772
|
exports.PointColumn = PointColumn;
|
|
6751
6773
|
exports.PolygonColumn = PolygonColumn;
|
|
6752
6774
|
exports.QueryBase = QueryBase;
|
|
6753
|
-
exports.QueryCallbacks = QueryCallbacks;
|
|
6754
6775
|
exports.QueryError = QueryError;
|
|
6755
6776
|
exports.QueryGet = QueryGet;
|
|
6777
|
+
exports.QueryHooks = QueryHooks;
|
|
6756
6778
|
exports.QueryLog = QueryLog;
|
|
6757
6779
|
exports.QueryMethods = QueryMethods;
|
|
6758
6780
|
exports.QueryUpsertOrCreate = QueryUpsertOrCreate;
|
|
@@ -6788,6 +6810,7 @@ exports.addOrNot = addOrNot;
|
|
|
6788
6810
|
exports.addParserForRawExpression = addParserForRawExpression;
|
|
6789
6811
|
exports.addParserForSelectItem = addParserForSelectItem;
|
|
6790
6812
|
exports.addParserToQuery = addParserToQuery;
|
|
6813
|
+
exports.addQueryHook = addQueryHook;
|
|
6791
6814
|
exports.addQueryOn = addQueryOn;
|
|
6792
6815
|
exports.addQueryOrOn = addQueryOrOn;
|
|
6793
6816
|
exports.addWhere = addWhere;
|