pqb 0.6.1 → 0.7.1
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/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/dist/index.d.ts +47 -11
- package/dist/index.esm.js +72 -5
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +72 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/adapter.test.ts +1 -1
- package/src/columnSchema/columnType.test.ts +2 -1
- package/src/columnSchema/columnTypes.test.ts +1 -1
- package/src/columnSchema/timestamps.test.ts +2 -2
- package/src/columnsOperators.test.ts +1 -1
- package/src/db.test.ts +83 -1
- package/src/db.ts +27 -10
- package/src/errors.test.ts +6 -1
- package/src/queryMethods/aggregate.test.ts +1 -1
- package/src/queryMethods/callbacks.test.ts +1 -1
- package/src/queryMethods/clear.test.ts +1 -1
- package/src/queryMethods/columnInfo.test.ts +5 -1
- package/src/queryMethods/copy.test.ts +63 -0
- package/src/queryMethods/copy.ts +17 -0
- package/src/queryMethods/create.test.ts +1 -1
- package/src/queryMethods/delete.test.ts +1 -1
- package/src/queryMethods/for.test.ts +5 -1
- package/src/queryMethods/from.test.ts +6 -1
- package/src/queryMethods/get.test.ts +7 -1
- package/src/queryMethods/having.test.ts +6 -1
- package/src/queryMethods/join.test.ts +6 -1
- package/src/queryMethods/json.test.ts +1 -1
- package/src/queryMethods/log.test.ts +6 -1
- package/src/queryMethods/log.ts +1 -0
- package/src/queryMethods/merge.test.ts +1 -1
- package/src/queryMethods/queryMethods.test.ts +1 -1
- package/src/queryMethods/queryMethods.ts +4 -1
- package/src/queryMethods/raw.test.ts +1 -1
- package/src/queryMethods/select.test.ts +1 -1
- package/src/queryMethods/then.test.ts +6 -1
- package/src/queryMethods/transaction.test.ts +1 -1
- package/src/queryMethods/union.test.ts +1 -1
- package/src/queryMethods/update.test.ts +1 -1
- package/src/queryMethods/upsert.test.ts +6 -1
- package/src/queryMethods/where.test.ts +1 -1
- package/src/queryMethods/window.test.ts +1 -1
- package/src/queryMethods/with.test.ts +6 -1
- package/src/sql/copy.ts +59 -0
- package/src/sql/data.ts +29 -1
- package/src/sql/toSql.ts +6 -0
- package/src/{test-utils.ts → test-utils/test-utils.ts} +7 -7
- package/src/test-utils/user.csv +1 -0
- package/src/utils.test.ts +1 -1
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Roman Kushyn
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
CHANGED
|
@@ -301,7 +301,33 @@ declare type ColumnInfoQueryData = CommonQueryData & {
|
|
|
301
301
|
type: 'columnInfo';
|
|
302
302
|
column?: string;
|
|
303
303
|
};
|
|
304
|
-
declare type
|
|
304
|
+
declare type CopyQueryData = CommonQueryData & {
|
|
305
|
+
type: 'copy';
|
|
306
|
+
copy: CopyOptions;
|
|
307
|
+
};
|
|
308
|
+
declare type CopyOptions<Column = string> = {
|
|
309
|
+
columns?: Column[];
|
|
310
|
+
format?: 'text' | 'csv' | 'binary';
|
|
311
|
+
freeze?: boolean;
|
|
312
|
+
delimiter?: string;
|
|
313
|
+
null?: string;
|
|
314
|
+
header?: boolean | 'match';
|
|
315
|
+
quote?: string;
|
|
316
|
+
escape?: string;
|
|
317
|
+
forceQuote?: Column[] | '*';
|
|
318
|
+
forceNotNull?: Column[];
|
|
319
|
+
forceNull?: Column[];
|
|
320
|
+
encoding?: string;
|
|
321
|
+
} & ({
|
|
322
|
+
from: string | {
|
|
323
|
+
program: string;
|
|
324
|
+
};
|
|
325
|
+
} | {
|
|
326
|
+
to: string | {
|
|
327
|
+
program: string;
|
|
328
|
+
};
|
|
329
|
+
});
|
|
330
|
+
declare type QueryData = SelectQueryData | InsertQueryData | UpdateQueryData | DeleteQueryData | TruncateQueryData | ColumnInfoQueryData | CopyQueryData;
|
|
305
331
|
declare const cloneQueryArrays: (q: QueryData) => void;
|
|
306
332
|
|
|
307
333
|
declare type Sql = {
|
|
@@ -830,9 +856,18 @@ declare class UnhandledTypeError extends PormInternalError {
|
|
|
830
856
|
constructor(value: never);
|
|
831
857
|
}
|
|
832
858
|
|
|
859
|
+
declare type NoPrimaryKeyOption = 'error' | 'warning' | 'ignore';
|
|
860
|
+
declare type DbOptions<CT extends ColumnTypesBase> = ({
|
|
861
|
+
adapter: Adapter;
|
|
862
|
+
} | Omit<AdapterOptions, 'log'>) & QueryLogOptions & {
|
|
863
|
+
columnTypes: CT;
|
|
864
|
+
autoPreparedStatements?: boolean;
|
|
865
|
+
noPrimaryKey?: NoPrimaryKeyOption;
|
|
866
|
+
};
|
|
833
867
|
declare type DbTableOptions = {
|
|
834
868
|
schema?: string;
|
|
835
869
|
autoPreparedStatements?: boolean;
|
|
870
|
+
noPrimaryKey?: NoPrimaryKeyOption;
|
|
836
871
|
} & QueryLogOptions;
|
|
837
872
|
declare const anyShape: Record<string, ColumnType<unknown, Operators, unknown>>;
|
|
838
873
|
interface Db<Table extends string | undefined = undefined, Shape extends ColumnsShape = Record<string, never>, Relations extends Query['relations'] = Query['relations'], CT extends ColumnTypesBase = ColumnTypesBase> extends QueryMethods {
|
|
@@ -894,12 +929,6 @@ declare type DbResult<CT extends ColumnTypesBase> = Db<string, Record<string, ne
|
|
|
894
929
|
adapter: Adapter;
|
|
895
930
|
close: Adapter['close'];
|
|
896
931
|
};
|
|
897
|
-
declare type DbOptions<CT extends ColumnTypesBase> = ({
|
|
898
|
-
adapter: Adapter;
|
|
899
|
-
} | Omit<AdapterOptions, 'log'>) & QueryLogOptions & {
|
|
900
|
-
columnTypes: CT;
|
|
901
|
-
autoPreparedStatements?: boolean;
|
|
902
|
-
};
|
|
903
932
|
declare const createDb: <CT extends ColumnTypesBase>({ log, logger, columnTypes: ct, ...options }: DbOptions<CT>) => DbResult<CT>;
|
|
904
933
|
|
|
905
934
|
declare type WithArgsOptions = Omit<WithOptions, 'columns'> & {
|
|
@@ -1253,6 +1282,7 @@ declare type QueryLogObject = {
|
|
|
1253
1282
|
};
|
|
1254
1283
|
declare type QueryLogger = {
|
|
1255
1284
|
log(message: string): void;
|
|
1285
|
+
warn(message: string): void;
|
|
1256
1286
|
error(message: string): void;
|
|
1257
1287
|
};
|
|
1258
1288
|
declare type QueryLogOptions = {
|
|
@@ -1327,6 +1357,12 @@ declare class RawMethods {
|
|
|
1327
1357
|
raw<T extends Query, C extends ColumnType>(this: T, ...args: RawArgs<T['columnTypes'], C>): RawExpression<C>;
|
|
1328
1358
|
}
|
|
1329
1359
|
|
|
1360
|
+
declare type CopyArg<T extends Query> = CopyOptions<keyof T['shape']>;
|
|
1361
|
+
declare class CopyMethods {
|
|
1362
|
+
copy<T extends Query>(this: T, arg: CopyArg<T>): T;
|
|
1363
|
+
_copy<T extends Query>(this: T, arg: CopyArg<T>): T;
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1330
1366
|
declare type WindowArg<T extends Query> = Record<string, WindowArgDeclaration<T> | RawExpression>;
|
|
1331
1367
|
declare type WindowArgDeclaration<T extends Query = Query> = {
|
|
1332
1368
|
partitionBy?: Expression<T> | Expression<T>[];
|
|
@@ -1339,7 +1375,7 @@ declare type OrderArg<T extends Query> = keyof T['selectable'] | {
|
|
|
1339
1375
|
nulls: 'FIRST' | 'LAST';
|
|
1340
1376
|
};
|
|
1341
1377
|
} | RawExpression;
|
|
1342
|
-
interface QueryMethods extends Aggregate, Select, From, Join, With, Union, Json, Create, Update, Delete, Transaction, For, ColumnInfoMethods, Where, Clear, Having, Window, Then, QueryLog, QueryCallbacks, QueryUpsert, QueryGet, MergeQueryMethods, RawMethods {
|
|
1378
|
+
interface QueryMethods extends Aggregate, Select, From, Join, With, Union, Json, Create, Update, Delete, Transaction, For, ColumnInfoMethods, Where, Clear, Having, Window, Then, QueryLog, QueryCallbacks, QueryUpsert, QueryGet, MergeQueryMethods, RawMethods, CopyMethods {
|
|
1343
1379
|
}
|
|
1344
1380
|
declare class QueryMethods {
|
|
1345
1381
|
windows: EmptyObject;
|
|
@@ -4129,9 +4165,9 @@ declare const jsonTypes: {
|
|
|
4129
4165
|
nativeEnum: <T_6 extends EnumLike>(givenEnum: T_6) => JSONNativeEnum<T_6>;
|
|
4130
4166
|
nullable: <T_7 extends JSONTypeAny>(type: T_7) => JSONNullable<T_7>;
|
|
4131
4167
|
nullish: <T_8 extends JSONTypeAny>(type: T_8) => JSONNullish<T_8>;
|
|
4132
|
-
object: <T_9 extends JSONObjectShape, UnknownKeys extends UnknownKeysParam = "strip", Catchall extends JSONTypeAny = JSONTypeAny>(shape: T_9) => JSONObject<T_9, UnknownKeys, Catchall, JSONTypeAny extends Catchall ? addQuestionMarks<{ [k_1 in keyof T_9]: T_9[k_1]["type"]; }> extends infer T_10 ? { [k in keyof T_10]: addQuestionMarks<{ [k_1 in keyof T_9]: T_9[k_1]["type"]; }>[k]; } : never : (addQuestionMarks<{ [k_1 in keyof T_9]: T_9[k_1]["type"]; }> extends infer T_10 ? { [k in keyof T_10]: addQuestionMarks<{ [k_1 in keyof T_9]: T_9[k_1]["type"]; }>[k]; } : never) & {
|
|
4168
|
+
object: <T_9 extends JSONObjectShape, UnknownKeys extends UnknownKeysParam = "strip", Catchall extends JSONTypeAny = JSONTypeAny>(shape: T_9) => JSONObject<T_9, UnknownKeys, Catchall, JSONTypeAny extends Catchall ? addQuestionMarks<{ [k_1 in keyof T_9]: T_9[k_1]["type"]; }> extends infer T_10 extends object ? { [k in keyof T_10]: addQuestionMarks<{ [k_1 in keyof T_9]: T_9[k_1]["type"]; }>[k]; } : never : (addQuestionMarks<{ [k_1 in keyof T_9]: T_9[k_1]["type"]; }> extends infer T_10 extends object ? { [k in keyof T_10]: addQuestionMarks<{ [k_1 in keyof T_9]: T_9[k_1]["type"]; }>[k]; } : never) & {
|
|
4133
4169
|
[k: string]: Catchall["type"];
|
|
4134
|
-
} extends infer T_11 ? { [k_2 in keyof T_11]: ((addQuestionMarks<{ [k_1 in keyof T_9]: T_9[k_1]["type"]; }> extends infer T_10 ? { [k in keyof T_10]: addQuestionMarks<{ [k_1 in keyof T_9]: T_9[k_1]["type"]; }>[k]; } : never) & {
|
|
4170
|
+
} extends infer T_11 extends object ? { [k_2 in keyof T_11]: ((addQuestionMarks<{ [k_1 in keyof T_9]: T_9[k_1]["type"]; }> extends infer T_10 extends object ? { [k in keyof T_10]: addQuestionMarks<{ [k_1 in keyof T_9]: T_9[k_1]["type"]; }>[k]; } : never) & {
|
|
4135
4171
|
[k: string]: Catchall["type"];
|
|
4136
4172
|
})[k_2]; } : never>;
|
|
4137
4173
|
optional: <T_12 extends JSONTypeAny>(type: T_12) => JSONOptional<T_12>;
|
|
@@ -4351,4 +4387,4 @@ declare const setQueryObjectValue: <T extends {
|
|
|
4351
4387
|
query: QueryData;
|
|
4352
4388
|
}>(q: T, object: string, key: string, value: unknown) => T;
|
|
4353
4389
|
|
|
4354
|
-
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, Create, CreateData, CreateMethodsNames, DateBaseColumn, DateColumn, DateColumnData, DateTimeBaseClass, DateTimeColumnData, DateTimeWithTimeZoneBaseClass, Db, DbOptions, DbTableOptions, DecimalBaseColumn, DecimalColumn, DecimalColumnData, DeepPartial, DefaultSelectColumns, Delete, DeleteMethodsNames, DeleteQueryData, DoublePrecisionColumn, DropMode, EMPTY_OBJECT, EmptyObject, EnumColumn, EnumLike, Except, Expression, ExpressionOfType, ExpressionOutput, FilterTuple, For, ForeignKey, ForeignKeyModel, ForeignKeyModelWithColumns, ForeignKeyOptions, From, GetArg, HasAndBelongsToManyRelation, HasManyNestedInsert, HasManyNestedUpdate, HasManyRelation, HasOneNestedInsert, HasOneNestedUpdate, HasOneRelation, Having, HavingArg, HavingItem, IndexColumnOptions, IndexOptions, InetColumn, 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, MergeQuery, MergeQueryMethods, 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, Query, QueryArraysResult, QueryBase, QueryCallbacks, QueryData, QueryError, QueryErrorName, QueryGet, QueryInput, QueryLog, QueryLogObject, QueryLogOptions, QueryLogger, QueryMethods, QueryResult, QueryResultRow, QueryReturnType, QueryReturnsAll, QuerySelectAll, QueryThen, QueryUpsert, QueryWithTable, RawExpression, RawMethods, RealColumn, Relation, RelationQuery, RelationQueryBase, RelationQueryData, 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, SinglePrimaryKey, SmallIntColumn, SmallSerialColumn, SomeIsTrue, SortDir, Spread, Sql, StringColumn, StringExpression, StringKey, TableData, TextBaseColumn, TextColumn, TextColumnData, Then, ThenResult, TimeColumn, TimeInterval, TimeWithTimeZoneColumn, TimestampColumn, TimestampWithTimeZoneColumn, ToSqlCtx, ToSqlOptions, Transaction, TransactionAdapter, TruncateQueryData, TsQueryColumn, TsVectorColumn, TypeParsers, UUIDColumn, UnhandledTypeError, Union, UnionArg, UnionItem, UnionKind, UnknownKeysParam, Update, UpdateData, UpdateQueryData, UpdateQueryDataItem, UpdateQueryDataObject, UpdatedAtDataInjector, 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, anyShape, applyMixins, array, arrayToEnum, baseObjectOutputType, checkIfASimpleQuery, cloneQueryArrays, columnTypes, utils as columnUtils, constructType, createDb, createOperator, defaultsKey, discriminatedUnion, emptyObject, enumType, flatten, getClonedQueryData, getColumnTypes, getQueryAs, getQueryParsers, getRaw, getRawSql, getTableData, getValidEnumValues, getValueKey, handleResult, identity, instanceOf, intersection, isQueryReturnsAll, isRaw, isRequiredRelationKey, joinTruthy, jsonTypes, lazy, literal, logColors, logParamToLogObject, makeRegexToFindInSql, makeSql, map, nativeEnum, newTableData, noop, notNullable, notNullish, nullable, nullish, object, optional, parseRecord, parseResult, processSelectArg, pushOrNewArray, pushOrNewArrayToObject, pushQueryArray, pushQueryOn, pushQueryOrOn, pushQueryValue, queryMethodByReturnType, queryTypeWithLimitOne, quote, raw, record, relationQueryKey, required, resetTableData, scalarTypes, set, setQueryObjectValue, toArray, toSql, toSqlCacheKey, tuple, union };
|
|
4390
|
+
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, CopyOptions, CopyQueryData, Create, CreateData, CreateMethodsNames, DateBaseColumn, DateColumn, DateColumnData, DateTimeBaseClass, DateTimeColumnData, DateTimeWithTimeZoneBaseClass, Db, DbOptions, DbTableOptions, DecimalBaseColumn, DecimalColumn, DecimalColumnData, DeepPartial, DefaultSelectColumns, Delete, DeleteMethodsNames, DeleteQueryData, DoublePrecisionColumn, DropMode, EMPTY_OBJECT, EmptyObject, EnumColumn, EnumLike, Except, Expression, ExpressionOfType, ExpressionOutput, FilterTuple, For, ForeignKey, ForeignKeyModel, ForeignKeyModelWithColumns, ForeignKeyOptions, From, GetArg, HasAndBelongsToManyRelation, HasManyNestedInsert, HasManyNestedUpdate, HasManyRelation, HasOneNestedInsert, HasOneNestedUpdate, HasOneRelation, Having, HavingArg, HavingItem, IndexColumnOptions, IndexOptions, InetColumn, 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, MergeQuery, MergeQueryMethods, MoneyColumn, MoreThanOneRowError, NestedInsertItem, NestedInsertManyItems, NestedInsertOneItem, NestedUpdateItem, NestedUpdateManyItems, NestedUpdateOneItem, NoPrimaryKeyOption, 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, Query, QueryArraysResult, QueryBase, QueryCallbacks, QueryData, QueryError, QueryErrorName, QueryGet, QueryInput, QueryLog, QueryLogObject, QueryLogOptions, QueryLogger, QueryMethods, QueryResult, QueryResultRow, QueryReturnType, QueryReturnsAll, QuerySelectAll, QueryThen, QueryUpsert, QueryWithTable, RawExpression, RawMethods, RealColumn, Relation, RelationQuery, RelationQueryBase, RelationQueryData, 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, SinglePrimaryKey, SmallIntColumn, SmallSerialColumn, SomeIsTrue, SortDir, Spread, Sql, StringColumn, StringExpression, StringKey, TableData, TextBaseColumn, TextColumn, TextColumnData, Then, ThenResult, TimeColumn, TimeInterval, TimeWithTimeZoneColumn, TimestampColumn, TimestampWithTimeZoneColumn, ToSqlCtx, ToSqlOptions, Transaction, TransactionAdapter, TruncateQueryData, TsQueryColumn, TsVectorColumn, TypeParsers, UUIDColumn, UnhandledTypeError, Union, UnionArg, UnionItem, UnionKind, UnknownKeysParam, Update, UpdateData, UpdateQueryData, UpdateQueryDataItem, UpdateQueryDataObject, UpdatedAtDataInjector, 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, anyShape, applyMixins, array, arrayToEnum, baseObjectOutputType, checkIfASimpleQuery, cloneQueryArrays, columnTypes, utils as columnUtils, constructType, createDb, createOperator, defaultsKey, discriminatedUnion, emptyObject, enumType, flatten, getClonedQueryData, getColumnTypes, getQueryAs, getQueryParsers, getRaw, getRawSql, getTableData, getValidEnumValues, getValueKey, handleResult, identity, instanceOf, intersection, isQueryReturnsAll, isRaw, isRequiredRelationKey, joinTruthy, jsonTypes, lazy, literal, logColors, logParamToLogObject, makeRegexToFindInSql, makeSql, map, nativeEnum, newTableData, noop, notNullable, notNullish, nullable, nullish, object, optional, parseRecord, parseResult, processSelectArg, pushOrNewArray, pushOrNewArrayToObject, pushQueryArray, pushQueryOn, pushQueryOrOn, pushQueryValue, queryMethodByReturnType, queryTypeWithLimitOne, quote, raw, record, relationQueryKey, required, resetTableData, scalarTypes, set, setQueryObjectValue, toArray, toSql, toSqlCacheKey, tuple, union };
|
package/dist/index.esm.js
CHANGED
|
@@ -1308,6 +1308,45 @@ const pushColumnInfoSql = (ctx, table, query) => {
|
|
|
1308
1308
|
}
|
|
1309
1309
|
};
|
|
1310
1310
|
|
|
1311
|
+
const pushCopySql = (ctx, model, query, quotedAs) => {
|
|
1312
|
+
const { sql } = ctx;
|
|
1313
|
+
const { copy } = query;
|
|
1314
|
+
const columns = copy.columns ? `(${copy.columns.map(q).join(", ")})` : "";
|
|
1315
|
+
const target = "from" in copy ? copy.from : copy.to;
|
|
1316
|
+
sql.push(
|
|
1317
|
+
`COPY ${q(model.table)}${columns} ${"from" in copy ? "FROM" : "TO"} ${typeof target === "string" ? quote(target) : `PROGRAM ${quote(target.program)}`}`
|
|
1318
|
+
);
|
|
1319
|
+
if (Object.keys(copy).length > (copy.columns ? 2 : 1)) {
|
|
1320
|
+
const options = [];
|
|
1321
|
+
if (copy.format)
|
|
1322
|
+
options.push(`FORMAT ${copy.format}`);
|
|
1323
|
+
if (copy.freeze)
|
|
1324
|
+
options.push(`FREEZE ${copy.freeze}`);
|
|
1325
|
+
if (copy.delimiter)
|
|
1326
|
+
options.push(`DELIMITER ${quote(copy.delimiter)}`);
|
|
1327
|
+
if (copy.null)
|
|
1328
|
+
options.push(`NULL ${quote(copy.null)}`);
|
|
1329
|
+
if (copy.header)
|
|
1330
|
+
options.push(`HEADER ${copy.header}`);
|
|
1331
|
+
if (copy.quote)
|
|
1332
|
+
options.push(`QUOTE ${quote(copy.quote)}`);
|
|
1333
|
+
if (copy.escape)
|
|
1334
|
+
options.push(`ESCAPE ${quote(copy.escape)}`);
|
|
1335
|
+
if (copy.forceQuote)
|
|
1336
|
+
options.push(
|
|
1337
|
+
`FORCE_QUOTE ${copy.forceQuote === "*" ? "*" : `(${copy.forceQuote.map(q).join(", ")})`}`
|
|
1338
|
+
);
|
|
1339
|
+
if (copy.forceNotNull)
|
|
1340
|
+
options.push(`FORCE_NOT_NULL (${copy.forceNotNull.map(q).join(", ")})`);
|
|
1341
|
+
if (copy.forceNull)
|
|
1342
|
+
options.push(`FORCE_NULL (${copy.forceNull.map(q).join(", ")})`);
|
|
1343
|
+
if (copy.encoding)
|
|
1344
|
+
options.push(`ENCODING ${quote(copy.encoding)}`);
|
|
1345
|
+
sql.push(`WITH (${options.join(", ")})`);
|
|
1346
|
+
}
|
|
1347
|
+
pushWhereStatementSql(ctx, model, query, quotedAs);
|
|
1348
|
+
};
|
|
1349
|
+
|
|
1311
1350
|
const toSqlCacheKey = Symbol("toSqlCache");
|
|
1312
1351
|
const toSql = (model, options) => {
|
|
1313
1352
|
return !(options == null ? void 0 : options.clearCache) && model.query[toSqlCacheKey] || (model.query[toSqlCacheKey] = makeSql(model, options));
|
|
@@ -1352,6 +1391,10 @@ const makeSql = (model, { values = [] } = {}) => {
|
|
|
1352
1391
|
pushDeleteSql(ctx, model, query, quotedAs2);
|
|
1353
1392
|
return { text: sql.join(" "), values };
|
|
1354
1393
|
}
|
|
1394
|
+
if (query.type === "copy") {
|
|
1395
|
+
pushCopySql(ctx, model, query, quotedAs2);
|
|
1396
|
+
return { text: sql.join(" "), values };
|
|
1397
|
+
}
|
|
1355
1398
|
}
|
|
1356
1399
|
const quotedAs = model.table && q(query.as || model.table);
|
|
1357
1400
|
sql.push("SELECT");
|
|
@@ -5462,6 +5505,19 @@ class RawMethods {
|
|
|
5462
5505
|
}
|
|
5463
5506
|
}
|
|
5464
5507
|
|
|
5508
|
+
class CopyMethods {
|
|
5509
|
+
copy(arg) {
|
|
5510
|
+
return this.clone()._copy(arg);
|
|
5511
|
+
}
|
|
5512
|
+
_copy(arg) {
|
|
5513
|
+
Object.assign(this.query, {
|
|
5514
|
+
type: "copy",
|
|
5515
|
+
copy: arg
|
|
5516
|
+
});
|
|
5517
|
+
return this;
|
|
5518
|
+
}
|
|
5519
|
+
}
|
|
5520
|
+
|
|
5465
5521
|
class QueryMethods {
|
|
5466
5522
|
all() {
|
|
5467
5523
|
return this.clone()._all();
|
|
@@ -5649,7 +5705,8 @@ applyMixins(QueryMethods, [
|
|
|
5649
5705
|
QueryUpsert,
|
|
5650
5706
|
QueryGet,
|
|
5651
5707
|
MergeQueryMethods,
|
|
5652
|
-
RawMethods
|
|
5708
|
+
RawMethods,
|
|
5709
|
+
CopyMethods
|
|
5653
5710
|
]);
|
|
5654
5711
|
|
|
5655
5712
|
var __defProp = Object.defineProperty;
|
|
@@ -5690,7 +5747,7 @@ class Db {
|
|
|
5690
5747
|
this.columnTypes = columnTypes;
|
|
5691
5748
|
this.whereQueryBuilder = WhereQueryBuilder;
|
|
5692
5749
|
this.onQueryBuilder = OnQueryBuilder;
|
|
5693
|
-
var _a;
|
|
5750
|
+
var _a, _b;
|
|
5694
5751
|
this.__model = this;
|
|
5695
5752
|
const logger = options.logger || console;
|
|
5696
5753
|
this.query = {
|
|
@@ -5706,8 +5763,17 @@ class Db {
|
|
|
5706
5763
|
this.primaryKeys = Object.keys(shape).filter(
|
|
5707
5764
|
(key) => shape[key].isPrimaryKey
|
|
5708
5765
|
);
|
|
5766
|
+
const primaryKeysFromData = (_b = getTableData().primaryKey) == null ? void 0 : _b.columns;
|
|
5767
|
+
if (primaryKeysFromData)
|
|
5768
|
+
this.primaryKeys.push(...primaryKeysFromData);
|
|
5709
5769
|
if (this.primaryKeys.length === 1) {
|
|
5710
5770
|
this.singlePrimaryKey = this.primaryKeys[0];
|
|
5771
|
+
} else if (this.primaryKeys.length === 0 && shape !== anyShape && options.noPrimaryKey !== "ignore") {
|
|
5772
|
+
const message = `Table ${table} has no primary key`;
|
|
5773
|
+
if (options.noPrimaryKey === "error")
|
|
5774
|
+
throw new Error(message);
|
|
5775
|
+
else
|
|
5776
|
+
logger.warn(message);
|
|
5711
5777
|
}
|
|
5712
5778
|
const columns = Object.keys(
|
|
5713
5779
|
shape
|
|
@@ -5757,18 +5823,19 @@ const createDb = (_a) => {
|
|
|
5757
5823
|
"logger",
|
|
5758
5824
|
"columnTypes"
|
|
5759
5825
|
]);
|
|
5760
|
-
var _a2;
|
|
5826
|
+
var _a2, _b2;
|
|
5761
5827
|
const adapter = "adapter" in options ? options.adapter : new Adapter(options);
|
|
5762
5828
|
const commonOptions = {
|
|
5763
5829
|
log,
|
|
5764
5830
|
logger,
|
|
5765
|
-
autoPreparedStatements: (_a2 = options.autoPreparedStatements) != null ? _a2 : false
|
|
5831
|
+
autoPreparedStatements: (_a2 = options.autoPreparedStatements) != null ? _a2 : false,
|
|
5832
|
+
noPrimaryKey: (_b2 = options.noPrimaryKey) != null ? _b2 : "error"
|
|
5766
5833
|
};
|
|
5767
5834
|
const qb = new Db(
|
|
5768
5835
|
adapter,
|
|
5769
5836
|
void 0,
|
|
5770
5837
|
void 0,
|
|
5771
|
-
|
|
5838
|
+
anyShape,
|
|
5772
5839
|
ct,
|
|
5773
5840
|
commonOptions
|
|
5774
5841
|
);
|