pqb 0.2.2 → 0.2.3
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 +58 -47
- package/dist/index.esm.js +197 -150
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +197 -150
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/columnSchema/columnsSchema.ts +15 -4
- package/src/columnSchema/number.ts +3 -0
- package/src/queryMethods/aggregate.test.ts +2 -2
- package/src/queryMethods/insert.test.ts +217 -205
- package/src/queryMethods/insert.ts +354 -274
- package/src/queryMethods/update.test.ts +3 -3
- package/src/queryMethods/upsert.test.ts +1 -1
- package/src/queryMethods/window.test.ts +2 -2
- package/src/utils.test.ts +37 -0
- package/src/utils.ts +9 -0
package/dist/index.d.ts
CHANGED
|
@@ -230,59 +230,57 @@ declare class WhereQueryBuilder<Q extends QueryBase = QueryBase> extends Where i
|
|
|
230
230
|
clone<T extends this>(this: T): T;
|
|
231
231
|
}
|
|
232
232
|
|
|
233
|
-
declare type
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
[Key in keyof T['relations']]: T['relations'][Key] extends BelongsToRelation ? SetOptional<{
|
|
240
|
-
[K in T['relations'][Key]['options']['foreignKey']]: T['relations'][Key]['options']['foreignKey'] extends keyof T['inputType'] ? T['inputType'][T['relations'][Key]['options']['foreignKey']] : never;
|
|
241
|
-
}, DefaultKeys> | {
|
|
242
|
-
[K in Key]: {
|
|
243
|
-
create?: InsertData<T['relations'][Key]['nestedCreateQuery']>;
|
|
244
|
-
connect?: WhereArg<T['relations'][Key]['model']>;
|
|
245
|
-
};
|
|
246
|
-
} : T['relations'][Key] extends HasOneRelation ? 'through' extends T['relations'][Key]['options'] ? {} : {
|
|
247
|
-
[K in Key]?: {
|
|
248
|
-
create?: InsertData<T['relations'][Key]['nestedCreateQuery']>;
|
|
249
|
-
connect?: WhereArg<T['relations'][Key]['model']>;
|
|
250
|
-
};
|
|
251
|
-
} : T['relations'][Key] extends Relation ? 'through' extends T['relations'][Key]['options'] ? {} : {
|
|
252
|
-
[K in Key]?: {
|
|
253
|
-
create?: InsertData<T['relations'][Key]['nestedCreateQuery']>[];
|
|
254
|
-
connect?: WhereArg<T['relations'][Key]['model']>[];
|
|
255
|
-
connectOrCreate?: {
|
|
256
|
-
where: WhereArg<T['relations'][Key]['model']>;
|
|
257
|
-
create: InsertData<T['relations'][Key]['nestedCreateQuery']>;
|
|
258
|
-
}[];
|
|
259
|
-
};
|
|
260
|
-
} : {};
|
|
233
|
+
declare type InsertData<T extends Query, DefaultKeys extends PropertyKey = keyof T[defaultsKey], Data = SetOptional<T['inputType'], DefaultKeys>> = [keyof T['relations']] extends [never] ? Data : OmitBelongsToForeignKeys<T['relations'], Data> & InsertRelationData<T>;
|
|
234
|
+
declare type OmitBelongsToForeignKeys<R extends RelationsBase, Data> = Omit<Data, {
|
|
235
|
+
[K in keyof R]: R[K] extends BelongsToRelation ? R[K]['options']['foreignKey'] : never;
|
|
236
|
+
}[keyof R]>;
|
|
237
|
+
declare type InsertRelationData<T extends Query> = {
|
|
238
|
+
[Key in keyof T['relations']]: T['relations'][Key] extends BelongsToRelation ? InsertBelongsToData<T, Key, T['relations'][Key]> : T['relations'][Key] extends HasOneRelation ? InsertHasOneData<T, Key, T['relations'][Key]> : T['relations'][Key] extends HasManyRelation | HasAndBelongsToManyRelation ? InsertHasManyData<T, Key, T['relations'][Key]> : {};
|
|
261
239
|
}[keyof T['relations']];
|
|
240
|
+
declare type InsertBelongsToData<T extends Query, Key extends keyof T['relations'], Rel extends BelongsToRelation> = SetOptional<{
|
|
241
|
+
[K in Rel['options']['foreignKey']]: Rel['options']['foreignKey'] extends keyof T['inputType'] ? T['inputType'][Rel['options']['foreignKey']] : never;
|
|
242
|
+
}, keyof T[defaultsKey]> | {
|
|
243
|
+
[K in Key]: {
|
|
244
|
+
create?: InsertData<Rel['nestedCreateQuery']>;
|
|
245
|
+
connect?: WhereArg<Rel['model']>;
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
declare type InsertHasOneData<T extends Query, Key extends keyof T['relations'], Rel extends HasOneRelation> = 'through' extends Rel['options'] ? {} : {
|
|
249
|
+
[K in Key]?: {
|
|
250
|
+
create?: InsertData<Rel['nestedCreateQuery']>;
|
|
251
|
+
connect?: WhereArg<Rel['model']>;
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
declare type InsertHasManyData<T extends Query, Key extends keyof T['relations'], Rel extends HasManyRelation | HasAndBelongsToManyRelation> = 'through' extends Rel['options'] ? {} : {
|
|
255
|
+
[K in Key]?: {
|
|
256
|
+
create?: InsertData<Rel['nestedCreateQuery']>[];
|
|
257
|
+
connect?: WhereArg<Rel['model']>[];
|
|
258
|
+
connectOrCreate?: {
|
|
259
|
+
where: WhereArg<Rel['model']>;
|
|
260
|
+
create: InsertData<Rel['nestedCreateQuery']>;
|
|
261
|
+
}[];
|
|
262
|
+
};
|
|
263
|
+
};
|
|
264
|
+
declare type InsertRawData = {
|
|
265
|
+
columns: string[];
|
|
266
|
+
values: RawExpression;
|
|
267
|
+
};
|
|
262
268
|
declare type InsertOneResult<T extends Query> = T['hasSelect'] extends false ? SetQueryReturnsRowCount<T> : T['returnType'] extends 'all' ? SetQueryReturnsOne<T> : T['returnType'] extends 'one' ? SetQueryReturnsOne<T> : T;
|
|
263
269
|
declare type InsertManyResult<T extends Query> = T['hasSelect'] extends false ? SetQueryReturnsRowCount<T> : T['returnType'] extends 'one' | 'oneOrThrow' ? SetQueryReturnsAll<T> : T;
|
|
264
270
|
declare type OnConflictArg<T extends Query> = keyof T['shape'] | (keyof T['shape'])[] | RawExpression;
|
|
265
271
|
declare class Insert {
|
|
266
272
|
insert<T extends Query>(this: T, data: InsertData<T>): InsertOneResult<T>;
|
|
267
|
-
insert<T extends Query>(this: T, data: InsertData<T>[] | {
|
|
268
|
-
columns: string[];
|
|
269
|
-
values: RawExpression;
|
|
270
|
-
}): InsertManyResult<T>;
|
|
271
273
|
_insert<T extends Query>(this: T, data: InsertData<T>): InsertOneResult<T>;
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
274
|
+
insertMany<T extends Query>(this: T, data: InsertData<T>[]): InsertManyResult<T>;
|
|
275
|
+
_insertMany<T extends Query>(this: T, data: InsertData<T>[]): InsertManyResult<T>;
|
|
276
|
+
insertRaw<T extends Query>(this: T, data: InsertRawData): InsertManyResult<T>;
|
|
277
|
+
_insertRaw<T extends Query>(this: T, data: InsertRawData): InsertManyResult<T>;
|
|
276
278
|
create<T extends Query>(this: T, data: InsertData<T>): SetQueryReturnsOne<T>;
|
|
277
|
-
create<T extends Query>(this: T, data: InsertData<T>[] | {
|
|
278
|
-
columns: string[];
|
|
279
|
-
values: RawExpression;
|
|
280
|
-
}): SetQueryReturnsAll<T>;
|
|
281
279
|
_create<T extends Query>(this: T, data: InsertData<T>): SetQueryReturnsOne<T>;
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
280
|
+
createMany<T extends Query>(this: T, data: InsertData<T>[]): SetQueryReturnsAll<T>;
|
|
281
|
+
_createMany<T extends Query>(this: T, data: InsertData<T>[]): SetQueryReturnsAll<T>;
|
|
282
|
+
createRaw<T extends Query>(this: T, data: InsertRawData): SetQueryReturnsAll<T>;
|
|
283
|
+
_createRaw<T extends Query>(this: T, data: InsertRawData): SetQueryReturnsAll<T>;
|
|
286
284
|
defaults<T extends Query, Data extends Partial<InsertData<T>>>(this: T, data: Data): T & {
|
|
287
285
|
[defaultsKey]: Record<keyof Data, true>;
|
|
288
286
|
};
|
|
@@ -1348,6 +1346,10 @@ declare type ToSqlCtx = {
|
|
|
1348
1346
|
};
|
|
1349
1347
|
declare const toSql: (model: Query, values?: unknown[]) => Sql;
|
|
1350
1348
|
|
|
1349
|
+
declare type SomeIsTrue<T extends unknown[]> = T extends [
|
|
1350
|
+
infer Head,
|
|
1351
|
+
...infer Tail
|
|
1352
|
+
] ? Head extends true ? true : SomeIsTrue<Tail> : false;
|
|
1351
1353
|
declare type MaybeArray<T> = T | T[];
|
|
1352
1354
|
declare type SetOptional<T, K extends PropertyKey> = Omit<T, K> & {
|
|
1353
1355
|
[P in K]?: P extends keyof T ? T[P] : never;
|
|
@@ -2140,9 +2142,15 @@ declare type ColumnsShape = Record<string, ColumnType>;
|
|
|
2140
2142
|
declare type ColumnShapeOutput<Shape extends ColumnsShape> = {
|
|
2141
2143
|
[K in keyof Shape]: ColumnOutput<Shape[K]>;
|
|
2142
2144
|
};
|
|
2143
|
-
declare type
|
|
2145
|
+
declare type OptionalColumnsForInput<Shape extends ColumnsShape> = {
|
|
2146
|
+
[K in keyof Shape]: SomeIsTrue<[
|
|
2147
|
+
Shape[K]['isNullable'],
|
|
2148
|
+
Shape[K]['hasDefault']
|
|
2149
|
+
]> extends true ? K : never;
|
|
2150
|
+
}[keyof Shape];
|
|
2151
|
+
declare type ColumnShapeInput<Shape extends ColumnsShape> = SetOptional<{
|
|
2144
2152
|
[K in keyof Shape]: ColumnInput<Shape[K]>;
|
|
2145
|
-
}
|
|
2153
|
+
}, OptionalColumnsForInput<Shape>>;
|
|
2146
2154
|
declare class ColumnsObject<Shape extends ColumnsShape> extends ColumnType<{
|
|
2147
2155
|
[K in keyof Shape]: Shape[K]['type'];
|
|
2148
2156
|
}, typeof Operators.any> {
|
|
@@ -2483,13 +2491,16 @@ declare class DoublePrecisionColumn extends NumberAsStringBaseColumn {
|
|
|
2483
2491
|
declare class SmallSerialColumn extends IntegerBaseColumn {
|
|
2484
2492
|
dataType: "smallserial";
|
|
2485
2493
|
parseItem: typeof parseInt;
|
|
2494
|
+
hasDefault: true;
|
|
2486
2495
|
}
|
|
2487
2496
|
declare class SerialColumn extends IntegerBaseColumn {
|
|
2488
2497
|
dataType: "serial";
|
|
2489
2498
|
parseItem: typeof parseInt;
|
|
2499
|
+
hasDefault: true;
|
|
2490
2500
|
}
|
|
2491
2501
|
declare class BigSerialColumn extends NumberAsStringBaseColumn {
|
|
2492
2502
|
dataType: "bigserial";
|
|
2503
|
+
hasDefault: true;
|
|
2493
2504
|
}
|
|
2494
2505
|
|
|
2495
2506
|
declare type BaseStringData = ColumnData & {
|
|
@@ -4315,4 +4326,4 @@ declare class UnhandledTypeError extends PormInternalError {
|
|
|
4315
4326
|
constructor(value: never);
|
|
4316
4327
|
}
|
|
4317
4328
|
|
|
4318
|
-
export { Adapter, AdapterOptions, AddQueryJoinedTable, AddQuerySelect, AddQueryWith, AfterCallback, Aggregate, Aggregate1ArgumentTypes, AggregateArg, AggregateItem, AggregateItemArg, AggregateItemOptions, AggregateOptions, AliasOrTable, AnyColumnType, AnyColumnTypeCreator, ArrayCardinality, ArrayColumn, ArrayData, ArrayOfColumnsObjects, AssertArray, BaseNumberData, BaseRelation, BaseStringData, BeforeCallback, BelongsToNestedInsert, BelongsToNestedUpdate, BelongsToRelation, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BooleanExpression, BoxColumn, ByteaColumn, CharColumn, CidrColumn, CircleColumn, Clear, ClearStatement, CoalesceString, ColumnData, ColumnInfo, ColumnInfoMethods, ColumnInfoQueryData, ColumnInput, ColumnNameOfModel, ColumnOperators, ColumnOutput, ColumnParser, ColumnShapeInput, ColumnShapeOutput, ColumnType, ColumnTypes, ColumnTypesBase, ColumnsObject, ColumnsParsers, ColumnsShape, CommonQueryData, DateBaseColumn, DateColumn, DateColumnData, DateTimeBaseClass, DateTimeColumnData, DateTimeWithTimeZoneBaseClass, Db, DbOptions, DbTableOptions, DecimalBaseColumn, DecimalColumn, DecimalColumnData, DeepPartial, DefaultSelectColumns, Delete, DeleteQueryData, DoublePrecisionColumn, DropMode, EMPTY_OBJECT, EmptyObject, EnumColumn, EnumLike, Except, Expression, ExpressionOfType, ExpressionOutput, FilterTuple, For, ForeignKey, ForeignKeyModel, ForeignKeyModelWithColumns, ForeignKeyOptions, From, GetArg, GetTypeOrRaw, GetTypesOrRaw, HasAndBelongsToManyRelation, HasManyNestedInsert, HasManyNestedUpdate, HasManyRelation, HasOneNestedInsert, HasOneNestedUpdate, HasOneRelation, Having, HavingArg, HavingItem, IndexColumnOptions, IndexOptions, InetColumn, Insert, InsertData, InsertQueryData, IntegerBaseColumn, IntegerColumn, IntervalColumn, IsEqual, JSONAny, JSONArray, JSONBigInt, JSONBoolean, JSONColumn, JSONDate, JSONDiscriminatedObject, JSONDiscriminatedUnion, JSONEnum, JSONInstanceOf, JSONIntersection, JSONLazy, JSONLiteral, JSONMap, JSONNaN, JSONNativeEnum, JSONNever, JSONNotNullable, JSONNotNullish, JSONNull, JSONNullable, JSONNullish, JSONNumber, JSONObject, JSONObjectShape, JSONOptional, JSONRecord, JSONRecordKeyType, JSONRequired, JSONSet, JSONString, JSONTextColumn, JSONTuple, JSONTupleItems, JSONType, JSONTypeAny, JSONTypeData, JSONTypes, JSONUndefined, JSONUnion, JSONUnknown, JSONVoid, Join, JoinArgs, JoinCallback, JoinCallbackArg, JoinItem, JoinedTablesBase, Json, JsonItem, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MaybeArray, Merge, MoneyColumn, MoreThanOneRowError, NestedInsertItem, NestedInsertManyItems, NestedInsertOneItem, NestedUpdateItem, NestedUpdateManyItems, NestedUpdateOneItem, NotFoundError, NullableColumn, NumberAsStringBaseColumn, NumberBaseColumn, NumberColumn, NumberColumnData, NumberExpression, OnConflictItem, OnConflictMergeUpdate, OnConflictQueryBuilder, OnQueryBuilder, Operator, Operators,
|
|
4329
|
+
export { Adapter, AdapterOptions, AddQueryJoinedTable, AddQuerySelect, AddQueryWith, AfterCallback, Aggregate, Aggregate1ArgumentTypes, AggregateArg, AggregateItem, AggregateItemArg, AggregateItemOptions, AggregateOptions, AliasOrTable, AnyColumnType, AnyColumnTypeCreator, ArrayCardinality, ArrayColumn, ArrayData, ArrayOfColumnsObjects, AssertArray, BaseNumberData, BaseRelation, BaseStringData, BeforeCallback, BelongsToNestedInsert, BelongsToNestedUpdate, BelongsToRelation, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BooleanExpression, BoxColumn, ByteaColumn, CharColumn, CidrColumn, CircleColumn, Clear, ClearStatement, CoalesceString, ColumnData, ColumnInfo, ColumnInfoMethods, ColumnInfoQueryData, ColumnInput, ColumnNameOfModel, ColumnOperators, ColumnOutput, ColumnParser, ColumnShapeInput, ColumnShapeOutput, ColumnType, ColumnTypes, ColumnTypesBase, ColumnsObject, ColumnsParsers, ColumnsShape, CommonQueryData, DateBaseColumn, DateColumn, DateColumnData, DateTimeBaseClass, DateTimeColumnData, DateTimeWithTimeZoneBaseClass, Db, DbOptions, DbTableOptions, DecimalBaseColumn, DecimalColumn, DecimalColumnData, DeepPartial, DefaultSelectColumns, Delete, DeleteQueryData, DoublePrecisionColumn, DropMode, EMPTY_OBJECT, EmptyObject, EnumColumn, EnumLike, Except, Expression, ExpressionOfType, ExpressionOutput, FilterTuple, For, ForeignKey, ForeignKeyModel, ForeignKeyModelWithColumns, ForeignKeyOptions, From, GetArg, GetTypeOrRaw, GetTypesOrRaw, HasAndBelongsToManyRelation, HasManyNestedInsert, HasManyNestedUpdate, HasManyRelation, HasOneNestedInsert, HasOneNestedUpdate, HasOneRelation, Having, HavingArg, HavingItem, IndexColumnOptions, IndexOptions, InetColumn, Insert, InsertData, InsertQueryData, IntegerBaseColumn, IntegerColumn, IntervalColumn, IsEqual, JSONAny, JSONArray, JSONBigInt, JSONBoolean, JSONColumn, JSONDate, JSONDiscriminatedObject, JSONDiscriminatedUnion, JSONEnum, JSONInstanceOf, JSONIntersection, JSONLazy, JSONLiteral, JSONMap, JSONNaN, JSONNativeEnum, JSONNever, JSONNotNullable, JSONNotNullish, JSONNull, JSONNullable, JSONNullish, JSONNumber, JSONObject, JSONObjectShape, JSONOptional, JSONRecord, JSONRecordKeyType, JSONRequired, JSONSet, JSONString, JSONTextColumn, JSONTuple, JSONTupleItems, JSONType, JSONTypeAny, JSONTypeData, JSONTypes, JSONUndefined, JSONUnion, JSONUnknown, JSONVoid, Join, JoinArgs, JoinCallback, JoinCallbackArg, JoinItem, JoinedTablesBase, Json, JsonItem, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MaybeArray, Merge, MoneyColumn, MoreThanOneRowError, NestedInsertItem, NestedInsertManyItems, NestedInsertOneItem, NestedUpdateItem, NestedUpdateManyItems, NestedUpdateOneItem, NotFoundError, NullableColumn, NumberAsStringBaseColumn, NumberBaseColumn, NumberColumn, NumberColumnData, NumberExpression, OnConflictItem, OnConflictMergeUpdate, OnConflictQueryBuilder, OnQueryBuilder, Operator, Operators, OrderArg, OrderItem, OutputTypeOfTuple, OutputTypeOfTupleWithRest, OwnTypeProps, PathColumn, PluckResultColumnType, PointColumn, PolygonColumn, PormError, PormInternalError, Primitive, PropertyKeyUnionToArray, Query, QueryArraysResult, QueryBase, QueryCallbacks, QueryData, QueryGet, QueryInput, QueryLog, QueryLogObject, QueryLogOptions, QueryLogger, QueryMethods, QueryResult, QueryResultRow, QueryReturnType, QuerySelectAll, QueryUpsert, QueryWithTable, RawExpression, RealColumn, Relation, RelationQuery, RelationQueryBase, RelationsBase, Select, SelectAgg, SelectArg, SelectFunctionItem, SelectItem, SelectQueryData, Selectable, SelectableBase, SerialColumn, SetOptional, SetQueryJoinedTables, SetQueryReturns, SetQueryReturnsAll, SetQueryReturnsColumnInfo, SetQueryReturnsOne, SetQueryReturnsOneOptional, SetQueryReturnsPluck, SetQueryReturnsRowCount, SetQueryReturnsRows, SetQueryReturnsValue, SetQueryReturnsValueOptional, SetQueryReturnsVoid, SetQueryTableAlias, SetQueryWindows, SetQueryWith, SimpleSpread, SingleColumnIndexOptions, SmallIntColumn, SmallSerialColumn, SomeIsTrue, SortDir, Spread, Sql, StringColumn, StringExpression, StringKey, TableData, TableSchema, TextBaseColumn, TextColumn, TextColumnData, Then, ThenResult, TimeColumn, TimeInterval, TimeWithTimeZoneColumn, TimestampColumn, TimestampWithTimeZoneColumn, ToSqlCtx, Transaction, TransactionAdapter, TruncateQueryData, TsQueryColumn, TsVectorColumn, TypeParsers, UUIDColumn, UnhandledTypeError, Union, UnionArg, UnionItem, UnionToArray, UnionToIntersection, UnionToOvlds, UnknownKeysParam, Update, UpdateData, UpdateQueryData, UpsertData, UpsertResult, UpsertThis, ValidationContext, VarCharColumn, 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, addQueryOn, addQueryOrOn, addQuestionMarks, addWhere, addWhereIn, addWhereNot, aggregate1FunctionNames, applyMixins, array, arrayToEnum, baseObjectOutputType, columnTypes, utils as columnUtils, constructType, createDb, createOperator, defaultsKey, discriminatedUnion, emptyObject, enumType, flatten, getClonedQueryData, getColumnTypes, getQueryAs, getQueryParsers, getRaw, getTableData, getValidEnumValues, getValueKey, handleResult, identity, instanceOf, intersection, isRaw, isRequiredRelationKey, joinTruthy, jsonTypes, lazy, literal, logColors, logParamToLogObject, map, nativeEnum, newTableData, noop, notNullable, notNullish, nullable, nullish, object, optional, parseRecord, parseResult, processSelectArg, pushQueryArray, pushQueryOn, pushQueryOrOn, pushQueryValue, queryKeysOfNotSimpleQuery, queryMethodByReturnType, quote, raw, record, relationQueryKey, removeFromQuery, required, resetTableData, scalarTypes, set, setQueryObjectValue, toArray, toSql, tuple, union };
|
package/dist/index.esm.js
CHANGED
|
@@ -630,6 +630,7 @@ class SmallSerialColumn extends IntegerBaseColumn {
|
|
|
630
630
|
super(...arguments);
|
|
631
631
|
this.dataType = "smallserial";
|
|
632
632
|
this.parseItem = parseInt;
|
|
633
|
+
this.hasDefault = true;
|
|
633
634
|
}
|
|
634
635
|
}
|
|
635
636
|
class SerialColumn extends IntegerBaseColumn {
|
|
@@ -637,12 +638,14 @@ class SerialColumn extends IntegerBaseColumn {
|
|
|
637
638
|
super(...arguments);
|
|
638
639
|
this.dataType = "serial";
|
|
639
640
|
this.parseItem = parseInt;
|
|
641
|
+
this.hasDefault = true;
|
|
640
642
|
}
|
|
641
643
|
}
|
|
642
644
|
class BigSerialColumn extends NumberAsStringBaseColumn {
|
|
643
645
|
constructor() {
|
|
644
646
|
super(...arguments);
|
|
645
647
|
this.dataType = "bigserial";
|
|
648
|
+
this.hasDefault = true;
|
|
646
649
|
}
|
|
647
650
|
}
|
|
648
651
|
|
|
@@ -4255,28 +4258,31 @@ var __spreadValues$3 = (a, b) => {
|
|
|
4255
4258
|
}
|
|
4256
4259
|
return a;
|
|
4257
4260
|
};
|
|
4258
|
-
const processInsertItem = (item, rowIndex,
|
|
4261
|
+
const processInsertItem = (item, rowIndex, ctx, columns, columnsMap) => {
|
|
4259
4262
|
Object.keys(item).forEach((key) => {
|
|
4260
|
-
if (relations[key]) {
|
|
4261
|
-
if (relations[key].type === "belongsTo") {
|
|
4262
|
-
const foreignKey = relations[key].options.foreignKey;
|
|
4263
|
+
if (ctx.relations[key]) {
|
|
4264
|
+
if (ctx.relations[key].type === "belongsTo") {
|
|
4265
|
+
const foreignKey = ctx.relations[key].options.foreignKey;
|
|
4263
4266
|
let columnIndex = columnsMap[foreignKey];
|
|
4264
4267
|
if (columnIndex === void 0) {
|
|
4265
4268
|
columnsMap[foreignKey] = columnIndex = columns.length;
|
|
4266
4269
|
columns.push(foreignKey);
|
|
4267
4270
|
}
|
|
4268
|
-
if (!prependRelations[key])
|
|
4269
|
-
prependRelations[key] = [];
|
|
4270
|
-
prependRelations[key].push([
|
|
4271
|
+
if (!ctx.prependRelations[key])
|
|
4272
|
+
ctx.prependRelations[key] = [];
|
|
4273
|
+
ctx.prependRelations[key].push([
|
|
4271
4274
|
rowIndex,
|
|
4272
4275
|
columnIndex,
|
|
4273
4276
|
item[key]
|
|
4274
4277
|
]);
|
|
4275
4278
|
} else {
|
|
4276
|
-
requiredReturning[relations[key].primaryKey] = true;
|
|
4277
|
-
if (!appendRelations[key])
|
|
4278
|
-
appendRelations[key] = [];
|
|
4279
|
-
appendRelations[key].push([
|
|
4279
|
+
ctx.requiredReturning[ctx.relations[key].primaryKey] = true;
|
|
4280
|
+
if (!ctx.appendRelations[key])
|
|
4281
|
+
ctx.appendRelations[key] = [];
|
|
4282
|
+
ctx.appendRelations[key].push([
|
|
4283
|
+
rowIndex,
|
|
4284
|
+
item[key]
|
|
4285
|
+
]);
|
|
4280
4286
|
}
|
|
4281
4287
|
} else if (columnsMap[key] === void 0) {
|
|
4282
4288
|
columnsMap[key] = columns.length;
|
|
@@ -4284,152 +4290,175 @@ const processInsertItem = (item, rowIndex, relations, prependRelations, appendRe
|
|
|
4284
4290
|
}
|
|
4285
4291
|
});
|
|
4286
4292
|
};
|
|
4287
|
-
|
|
4288
|
-
|
|
4289
|
-
|
|
4293
|
+
const createInsertCtx = (q) => ({
|
|
4294
|
+
prependRelations: {},
|
|
4295
|
+
appendRelations: {},
|
|
4296
|
+
requiredReturning: {},
|
|
4297
|
+
relations: q.relations
|
|
4298
|
+
});
|
|
4299
|
+
const getInsertSingleReturnType = (q) => {
|
|
4300
|
+
const { select, returnType } = q.query;
|
|
4301
|
+
if (select) {
|
|
4302
|
+
return returnType === "all" ? "one" : returnType;
|
|
4303
|
+
} else {
|
|
4304
|
+
return "rowCount";
|
|
4290
4305
|
}
|
|
4291
|
-
|
|
4292
|
-
|
|
4293
|
-
|
|
4294
|
-
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
|
|
4300
|
-
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
|
|
4305
|
-
|
|
4306
|
-
|
|
4307
|
-
|
|
4306
|
+
};
|
|
4307
|
+
const getInsertManyReturnType = (q) => {
|
|
4308
|
+
const { select, returnType } = q.query;
|
|
4309
|
+
if (select) {
|
|
4310
|
+
return returnType === "one" || returnType === "oneOrThrow" ? "all" : returnType;
|
|
4311
|
+
} else {
|
|
4312
|
+
return "rowCount";
|
|
4313
|
+
}
|
|
4314
|
+
};
|
|
4315
|
+
const handleInsertOneData = (q, data, ctx) => {
|
|
4316
|
+
const columns = [];
|
|
4317
|
+
const columnsMap = {};
|
|
4318
|
+
const defaults = q.query.defaults;
|
|
4319
|
+
if (defaults) {
|
|
4320
|
+
data = __spreadValues$3(__spreadValues$3({}, defaults), data);
|
|
4321
|
+
}
|
|
4322
|
+
processInsertItem(data, 0, ctx, columns, columnsMap);
|
|
4323
|
+
const values = [columns.map((key) => data[key])];
|
|
4324
|
+
return { columns, values };
|
|
4325
|
+
};
|
|
4326
|
+
const handleInsertManyData = (q, data, ctx) => {
|
|
4327
|
+
const columns = [];
|
|
4328
|
+
const columnsMap = {};
|
|
4329
|
+
const defaults = q.query.defaults;
|
|
4330
|
+
if (defaults) {
|
|
4331
|
+
data = data.map((item) => __spreadValues$3(__spreadValues$3({}, defaults), item));
|
|
4332
|
+
}
|
|
4333
|
+
data.forEach((item, i) => {
|
|
4334
|
+
processInsertItem(item, i, ctx, columns, columnsMap);
|
|
4335
|
+
});
|
|
4336
|
+
const values = Array(data.length);
|
|
4337
|
+
data.forEach((item, i) => {
|
|
4338
|
+
values[i] = columns.map((key) => item[key]);
|
|
4339
|
+
});
|
|
4340
|
+
return { columns, values };
|
|
4341
|
+
};
|
|
4342
|
+
const insert = (self, {
|
|
4343
|
+
columns,
|
|
4344
|
+
values
|
|
4345
|
+
}, returnType, ctx) => {
|
|
4346
|
+
const q = self;
|
|
4347
|
+
const returning = q.query.select;
|
|
4348
|
+
delete q.query.and;
|
|
4349
|
+
delete q.query.or;
|
|
4350
|
+
q.query.type = "insert";
|
|
4351
|
+
q.query.columns = columns;
|
|
4352
|
+
q.query.values = values;
|
|
4353
|
+
if (!ctx) {
|
|
4354
|
+
q.query.returnType = returnType;
|
|
4355
|
+
return q;
|
|
4356
|
+
}
|
|
4357
|
+
const prependRelationsKeys = Object.keys(ctx.prependRelations);
|
|
4358
|
+
if (prependRelationsKeys.length) {
|
|
4359
|
+
pushQueryArray(
|
|
4360
|
+
q,
|
|
4361
|
+
"beforeQuery",
|
|
4362
|
+
prependRelationsKeys.map((relationName) => {
|
|
4363
|
+
return async (q2) => {
|
|
4364
|
+
const relationData = ctx.prependRelations[relationName];
|
|
4365
|
+
const relation = ctx.relations[relationName];
|
|
4366
|
+
const inserted = await relation.nestedInsert(
|
|
4367
|
+
q2,
|
|
4368
|
+
relationData.map(([, , data]) => data)
|
|
4369
|
+
);
|
|
4370
|
+
const primaryKey = relation.options.primaryKey;
|
|
4371
|
+
relationData.forEach(([rowIndex, columnIndex], index) => {
|
|
4372
|
+
values[rowIndex][columnIndex] = inserted[index][primaryKey];
|
|
4373
|
+
});
|
|
4374
|
+
};
|
|
4375
|
+
})
|
|
4376
|
+
);
|
|
4377
|
+
}
|
|
4378
|
+
const appendRelationsKeys = Object.keys(ctx.appendRelations);
|
|
4379
|
+
if (appendRelationsKeys.length) {
|
|
4380
|
+
if (!(returning == null ? void 0 : returning.includes("*"))) {
|
|
4381
|
+
const requiredColumns = Object.keys(ctx.requiredReturning);
|
|
4382
|
+
if (!returning) {
|
|
4383
|
+
q.query.select = requiredColumns;
|
|
4308
4384
|
} else {
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4385
|
+
q.query.select = [
|
|
4386
|
+
.../* @__PURE__ */ new Set([...returning, ...requiredColumns])
|
|
4387
|
+
];
|
|
4312
4388
|
}
|
|
4313
|
-
} else {
|
|
4314
|
-
returnType = "rowCount";
|
|
4315
4389
|
}
|
|
4316
|
-
|
|
4317
|
-
|
|
4318
|
-
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
if (defaults) {
|
|
4325
|
-
data = data.map((item) => __spreadValues$3(__spreadValues$3({}, defaults), item));
|
|
4326
|
-
}
|
|
4327
|
-
data.forEach((item, i) => {
|
|
4328
|
-
processInsertItem(
|
|
4329
|
-
item,
|
|
4330
|
-
i,
|
|
4331
|
-
relations,
|
|
4332
|
-
prependRelations,
|
|
4333
|
-
appendRelations,
|
|
4334
|
-
requiredReturning,
|
|
4335
|
-
columns,
|
|
4336
|
-
columnsMap
|
|
4390
|
+
let resultOfTypeAll;
|
|
4391
|
+
if (returnType !== "all") {
|
|
4392
|
+
const { handleResult } = q.query;
|
|
4393
|
+
q.query.handleResult = async (q2, queryResult) => {
|
|
4394
|
+
resultOfTypeAll = await handleResult(q2, queryResult);
|
|
4395
|
+
if (queryMethodByReturnType[returnType] === "arrays") {
|
|
4396
|
+
queryResult.rows.forEach(
|
|
4397
|
+
(row, i) => queryResult.rows[i] = Object.values(row)
|
|
4337
4398
|
);
|
|
4338
|
-
});
|
|
4339
|
-
values = Array(data.length);
|
|
4340
|
-
data.forEach((item, i) => {
|
|
4341
|
-
values[i] = columns.map((key) => item[key]);
|
|
4342
|
-
});
|
|
4343
|
-
} else {
|
|
4344
|
-
if (defaults) {
|
|
4345
|
-
data = __spreadValues$3(__spreadValues$3({}, defaults), data);
|
|
4346
4399
|
}
|
|
4347
|
-
|
|
4348
|
-
|
|
4349
|
-
0,
|
|
4350
|
-
relations,
|
|
4351
|
-
prependRelations,
|
|
4352
|
-
appendRelations,
|
|
4353
|
-
requiredReturning,
|
|
4354
|
-
columns,
|
|
4355
|
-
columnsMap
|
|
4356
|
-
);
|
|
4357
|
-
values = [columns.map((key) => data[key])];
|
|
4358
|
-
}
|
|
4359
|
-
}
|
|
4360
|
-
const prependRelationsKeys = Object.keys(prependRelations);
|
|
4361
|
-
if (prependRelationsKeys.length) {
|
|
4362
|
-
pushQueryArray(
|
|
4363
|
-
q,
|
|
4364
|
-
"beforeQuery",
|
|
4365
|
-
prependRelationsKeys.map((relationName) => {
|
|
4366
|
-
return async (q2) => {
|
|
4367
|
-
const relationData = prependRelations[relationName];
|
|
4368
|
-
const relation = relations[relationName];
|
|
4369
|
-
const inserted = await relation.nestedInsert(
|
|
4370
|
-
q2,
|
|
4371
|
-
relationData.map(([, , data2]) => data2)
|
|
4372
|
-
);
|
|
4373
|
-
const primaryKey = relation.options.primaryKey;
|
|
4374
|
-
relationData.forEach(([rowIndex, columnIndex], index) => {
|
|
4375
|
-
values[rowIndex][columnIndex] = inserted[index][primaryKey];
|
|
4376
|
-
});
|
|
4377
|
-
};
|
|
4378
|
-
})
|
|
4379
|
-
);
|
|
4400
|
+
return parseResult(q2, returnType, queryResult);
|
|
4401
|
+
};
|
|
4380
4402
|
}
|
|
4381
|
-
|
|
4382
|
-
|
|
4383
|
-
|
|
4384
|
-
|
|
4385
|
-
|
|
4386
|
-
|
|
4387
|
-
|
|
4388
|
-
|
|
4389
|
-
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
|
|
4396
|
-
q.query.handleResult = async (q2, queryResult) => {
|
|
4397
|
-
resultOfTypeAll = await handleResult(q2, queryResult);
|
|
4398
|
-
if (queryMethodByReturnType[returnType] === "arrays") {
|
|
4399
|
-
queryResult.rows.forEach(
|
|
4400
|
-
(row, i) => queryResult.rows[i] = Object.values(row)
|
|
4401
|
-
);
|
|
4402
|
-
}
|
|
4403
|
-
return parseResult(q2, returnType, queryResult);
|
|
4403
|
+
pushQueryArray(
|
|
4404
|
+
q,
|
|
4405
|
+
"afterQuery",
|
|
4406
|
+
appendRelationsKeys.map((relationName) => {
|
|
4407
|
+
return (q2, result) => {
|
|
4408
|
+
var _a, _b;
|
|
4409
|
+
const all = resultOfTypeAll || result;
|
|
4410
|
+
return (_b = (_a = ctx.relations[relationName]).nestedInsert) == null ? void 0 : _b.call(
|
|
4411
|
+
_a,
|
|
4412
|
+
q2,
|
|
4413
|
+
ctx.appendRelations[relationName].map(([rowIndex, data]) => [
|
|
4414
|
+
all[rowIndex],
|
|
4415
|
+
data
|
|
4416
|
+
])
|
|
4417
|
+
);
|
|
4404
4418
|
};
|
|
4405
|
-
}
|
|
4406
|
-
|
|
4407
|
-
|
|
4408
|
-
|
|
4409
|
-
|
|
4410
|
-
|
|
4411
|
-
|
|
4412
|
-
|
|
4413
|
-
|
|
4414
|
-
|
|
4415
|
-
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4420
|
-
|
|
4421
|
-
|
|
4422
|
-
|
|
4423
|
-
)
|
|
4424
|
-
|
|
4425
|
-
|
|
4426
|
-
|
|
4427
|
-
|
|
4428
|
-
|
|
4429
|
-
|
|
4430
|
-
|
|
4431
|
-
|
|
4432
|
-
return
|
|
4419
|
+
})
|
|
4420
|
+
);
|
|
4421
|
+
}
|
|
4422
|
+
if (prependRelationsKeys.length || appendRelationsKeys.length) {
|
|
4423
|
+
q.query.wrapInTransaction = true;
|
|
4424
|
+
}
|
|
4425
|
+
q.query.returnType = appendRelationsKeys.length ? "all" : returnType;
|
|
4426
|
+
return q;
|
|
4427
|
+
};
|
|
4428
|
+
class Insert {
|
|
4429
|
+
insert(data) {
|
|
4430
|
+
return this.clone()._insert(data);
|
|
4431
|
+
}
|
|
4432
|
+
_insert(data) {
|
|
4433
|
+
const ctx = createInsertCtx(this);
|
|
4434
|
+
return insert(
|
|
4435
|
+
this,
|
|
4436
|
+
handleInsertOneData(this, data, ctx),
|
|
4437
|
+
getInsertSingleReturnType(this),
|
|
4438
|
+
ctx
|
|
4439
|
+
);
|
|
4440
|
+
}
|
|
4441
|
+
insertMany(data) {
|
|
4442
|
+
return this.clone()._insertMany(data);
|
|
4443
|
+
}
|
|
4444
|
+
_insertMany(data) {
|
|
4445
|
+
const ctx = createInsertCtx(this);
|
|
4446
|
+
return insert(
|
|
4447
|
+
this,
|
|
4448
|
+
handleInsertManyData(this, data, ctx),
|
|
4449
|
+
getInsertManyReturnType(this),
|
|
4450
|
+
ctx
|
|
4451
|
+
);
|
|
4452
|
+
}
|
|
4453
|
+
insertRaw(data) {
|
|
4454
|
+
return this.clone()._insertRaw(data);
|
|
4455
|
+
}
|
|
4456
|
+
_insertRaw(data) {
|
|
4457
|
+
return insert(
|
|
4458
|
+
this,
|
|
4459
|
+
data,
|
|
4460
|
+
getInsertManyReturnType(this)
|
|
4461
|
+
);
|
|
4433
4462
|
}
|
|
4434
4463
|
create(data) {
|
|
4435
4464
|
return this.clone()._create(data);
|
|
@@ -4438,7 +4467,25 @@ class Insert {
|
|
|
4438
4467
|
if (!this.query.select) {
|
|
4439
4468
|
this.query.select = ["*"];
|
|
4440
4469
|
}
|
|
4441
|
-
return this.
|
|
4470
|
+
return this.clone()._insert(data);
|
|
4471
|
+
}
|
|
4472
|
+
createMany(data) {
|
|
4473
|
+
return this.clone()._createMany(data);
|
|
4474
|
+
}
|
|
4475
|
+
_createMany(data) {
|
|
4476
|
+
if (!this.query.select) {
|
|
4477
|
+
this.query.select = ["*"];
|
|
4478
|
+
}
|
|
4479
|
+
return this.clone()._insertMany(data);
|
|
4480
|
+
}
|
|
4481
|
+
createRaw(data) {
|
|
4482
|
+
return this.clone()._createRaw(data);
|
|
4483
|
+
}
|
|
4484
|
+
_createRaw(data) {
|
|
4485
|
+
if (!this.query.select) {
|
|
4486
|
+
this.query.select = ["*"];
|
|
4487
|
+
}
|
|
4488
|
+
return this.clone()._insertRaw(data);
|
|
4442
4489
|
}
|
|
4443
4490
|
defaults(data) {
|
|
4444
4491
|
return this.clone()._defaults(data);
|