pqb 0.7.13 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/index.d.ts +53 -48
- package/dist/index.esm.js +103 -103
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +103 -103
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/columnSchema/columnType.test.ts +2 -2
- package/src/columnSchema/columnType.ts +9 -9
- package/src/columnSchema/columnTypes.ts +27 -13
- package/src/columnSchema/timestamps.test.ts +6 -6
- package/src/db.ts +1 -1
- package/src/query.ts +1 -1
- package/src/queryMethods/create.ts +6 -6
- package/src/queryMethods/for.ts +3 -3
- package/src/queryMethods/having.ts +1 -1
- package/src/queryMethods/join.ts +4 -4
- package/src/queryMethods/json.ts +1 -1
- package/src/queryMethods/queryMethods.ts +2 -2
- package/src/queryMethods/select.ts +3 -3
- package/src/queryMethods/update.ts +17 -17
- package/src/queryMethods/where.test.ts +1 -1
- package/src/queryMethods/where.ts +4 -4
- package/src/relations.ts +1 -1
- package/src/sql/aggregate.ts +2 -2
- package/src/sql/copy.ts +3 -3
- package/src/sql/delete.ts +5 -5
- package/src/sql/fromAndAs.ts +4 -4
- package/src/sql/having.ts +7 -7
- package/src/sql/insert.ts +5 -5
- package/src/sql/join.ts +16 -16
- package/src/sql/select.ts +6 -6
- package/src/sql/toSql.ts +24 -24
- package/src/sql/update.ts +4 -4
- package/src/sql/where.ts +18 -18
package/package.json
CHANGED
|
@@ -34,7 +34,7 @@ describe('column base', () => {
|
|
|
34
34
|
expect(column.hidden().isHidden).toBe(true);
|
|
35
35
|
});
|
|
36
36
|
|
|
37
|
-
test('
|
|
37
|
+
test('table with hidden column should omit from select it by default', () => {
|
|
38
38
|
const User = db('user', (t) => ({
|
|
39
39
|
id: t.serial().primaryKey(),
|
|
40
40
|
name: t.text(),
|
|
@@ -53,7 +53,7 @@ describe('column base', () => {
|
|
|
53
53
|
);
|
|
54
54
|
});
|
|
55
55
|
|
|
56
|
-
test('
|
|
56
|
+
test('table with hidden column still allows to select it', () => {
|
|
57
57
|
const User = db('user', (t) => ({
|
|
58
58
|
id: t.serial().primaryKey(),
|
|
59
59
|
name: t.text(),
|
|
@@ -100,17 +100,17 @@ export type IndexOptions = {
|
|
|
100
100
|
|
|
101
101
|
export type SingleColumnIndexOptions = IndexColumnOptions & IndexOptions;
|
|
102
102
|
|
|
103
|
-
export type
|
|
103
|
+
export type ForeignKeyTable = new () => {
|
|
104
104
|
table: string;
|
|
105
105
|
};
|
|
106
106
|
|
|
107
|
-
export type
|
|
107
|
+
export type ForeignKeyTableWithColumns = new () => {
|
|
108
108
|
table: string;
|
|
109
109
|
columns: { shape: ColumnsShape };
|
|
110
110
|
};
|
|
111
111
|
|
|
112
|
-
export type
|
|
113
|
-
StringKey<keyof InstanceType<
|
|
112
|
+
export type ColumnNameOfTable<Table extends ForeignKeyTableWithColumns> =
|
|
113
|
+
StringKey<keyof InstanceType<Table>['columns']['shape']>;
|
|
114
114
|
|
|
115
115
|
const addColumnData = <T extends ColumnType, K extends keyof ColumnData>(
|
|
116
116
|
q: T,
|
|
@@ -157,15 +157,15 @@ export abstract class ColumnType<
|
|
|
157
157
|
|
|
158
158
|
foreignKey<
|
|
159
159
|
T extends ColumnType,
|
|
160
|
-
|
|
161
|
-
Column extends
|
|
160
|
+
Table extends ForeignKeyTableWithColumns,
|
|
161
|
+
Column extends ColumnNameOfTable<Table>,
|
|
162
162
|
>(
|
|
163
163
|
this: T,
|
|
164
|
-
fn: () =>
|
|
164
|
+
fn: () => Table,
|
|
165
165
|
column: Column,
|
|
166
166
|
options?: ForeignKeyOptions,
|
|
167
167
|
): Omit<T, 'foreignKeyData'> & {
|
|
168
|
-
foreignKeyData: ForeignKey<InstanceType<
|
|
168
|
+
foreignKeyData: ForeignKey<InstanceType<Table>['table'], [Column]>;
|
|
169
169
|
};
|
|
170
170
|
foreignKey<T extends ColumnType, Table extends string, Column extends string>(
|
|
171
171
|
this: T,
|
|
@@ -176,7 +176,7 @@ export abstract class ColumnType<
|
|
|
176
176
|
foreignKeyData: ForeignKey<Table, [Column]>;
|
|
177
177
|
};
|
|
178
178
|
foreignKey(
|
|
179
|
-
fnOrTable: (() =>
|
|
179
|
+
fnOrTable: (() => ForeignKeyTable) | string,
|
|
180
180
|
column: string,
|
|
181
181
|
options: ForeignKeyOptions = {},
|
|
182
182
|
) {
|
|
@@ -47,14 +47,14 @@ import { JSONColumn, JSONTextColumn, JSONTypes } from './json';
|
|
|
47
47
|
import { JSONTypeAny } from './json/typeBase';
|
|
48
48
|
import { ArrayColumn } from './array';
|
|
49
49
|
import {
|
|
50
|
-
|
|
50
|
+
ColumnNameOfTable,
|
|
51
51
|
ColumnType,
|
|
52
52
|
ColumnTypesBase,
|
|
53
|
-
|
|
53
|
+
ForeignKeyTable,
|
|
54
54
|
IndexColumnOptions,
|
|
55
55
|
IndexOptions,
|
|
56
56
|
ForeignKeyOptions,
|
|
57
|
-
|
|
57
|
+
ForeignKeyTableWithColumns,
|
|
58
58
|
} from './columnType';
|
|
59
59
|
import { emptyObject, EmptyObject, MaybeArray, toArray } from '../utils';
|
|
60
60
|
import { ColumnsShape } from './columnsSchema';
|
|
@@ -63,15 +63,29 @@ import { timestamps } from './timestamps';
|
|
|
63
63
|
export type ColumnTypes = typeof columnTypes;
|
|
64
64
|
|
|
65
65
|
export type TableData = {
|
|
66
|
-
primaryKey?:
|
|
67
|
-
indexes:
|
|
68
|
-
foreignKeys:
|
|
66
|
+
primaryKey?: TableData.PrimaryKey;
|
|
67
|
+
indexes: TableData.Index[];
|
|
68
|
+
foreignKeys: TableData.ForeignKey[];
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export namespace TableData {
|
|
72
|
+
export type PrimaryKey = {
|
|
73
|
+
columns: string[];
|
|
74
|
+
options?: { name?: string };
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type Index = {
|
|
78
|
+
columns: IndexColumnOptions[];
|
|
79
|
+
options: IndexOptions;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export type ForeignKey = {
|
|
69
83
|
columns: string[];
|
|
70
|
-
fnOrTable: (() =>
|
|
84
|
+
fnOrTable: (() => ForeignKeyTable) | string;
|
|
71
85
|
foreignColumns: string[];
|
|
72
86
|
options: ForeignKeyOptions;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
75
89
|
|
|
76
90
|
export const newTableData = (): TableData => ({
|
|
77
91
|
indexes: [],
|
|
@@ -221,11 +235,11 @@ export const columnTypes = {
|
|
|
221
235
|
};
|
|
222
236
|
|
|
223
237
|
function foreignKey<
|
|
224
|
-
|
|
225
|
-
Columns extends [
|
|
238
|
+
Table extends ForeignKeyTableWithColumns,
|
|
239
|
+
Columns extends [ColumnNameOfTable<Table>, ...ColumnNameOfTable<Table>[]],
|
|
226
240
|
>(
|
|
227
241
|
columns: string[],
|
|
228
|
-
fn: () =>
|
|
242
|
+
fn: () => Table,
|
|
229
243
|
foreignColumns: Columns,
|
|
230
244
|
options?: ForeignKeyOptions,
|
|
231
245
|
): EmptyObject;
|
|
@@ -240,7 +254,7 @@ function foreignKey<
|
|
|
240
254
|
): EmptyObject;
|
|
241
255
|
function foreignKey(
|
|
242
256
|
columns: string[],
|
|
243
|
-
fnOrTable: (() =>
|
|
257
|
+
fnOrTable: (() => ForeignKeyTable) | string,
|
|
244
258
|
foreignColumns: string[],
|
|
245
259
|
options: ForeignKeyOptions = {},
|
|
246
260
|
) {
|
|
@@ -3,13 +3,13 @@ import { db, expectSql, now, useTestDatabase } from '../test-utils/test-utils';
|
|
|
3
3
|
describe('timestamps', () => {
|
|
4
4
|
useTestDatabase();
|
|
5
5
|
|
|
6
|
-
const
|
|
6
|
+
const table = db('user', (t) => ({
|
|
7
7
|
name: t.text().primaryKey(),
|
|
8
8
|
...t.timestamps(),
|
|
9
9
|
}));
|
|
10
10
|
|
|
11
11
|
it('should update updatedAt column when updating', async () => {
|
|
12
|
-
const query =
|
|
12
|
+
const query = table.where().update({});
|
|
13
13
|
await query;
|
|
14
14
|
|
|
15
15
|
expectSql(
|
|
@@ -22,7 +22,7 @@ describe('timestamps', () => {
|
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
it('should not update updatedAt column when updating it via object', async () => {
|
|
25
|
-
const query =
|
|
25
|
+
const query = table.where().update({ updatedAt: now });
|
|
26
26
|
await query;
|
|
27
27
|
|
|
28
28
|
expectSql(
|
|
@@ -36,7 +36,7 @@ describe('timestamps', () => {
|
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
it('should update updatedAt when updating with raw sql', async () => {
|
|
39
|
-
const query =
|
|
39
|
+
const query = table
|
|
40
40
|
.where()
|
|
41
41
|
.updateRaw(db.raw('name = $name', { name: 'name' }));
|
|
42
42
|
|
|
@@ -53,7 +53,7 @@ describe('timestamps', () => {
|
|
|
53
53
|
});
|
|
54
54
|
|
|
55
55
|
it('should update updatedAt when updating with raw sql which has updatedAt somewhere but not in set', async () => {
|
|
56
|
-
const query =
|
|
56
|
+
const query = table.where().updateRaw(db.raw('"createdAt" = "updatedAt"'));
|
|
57
57
|
await query;
|
|
58
58
|
|
|
59
59
|
expectSql(
|
|
@@ -66,7 +66,7 @@ describe('timestamps', () => {
|
|
|
66
66
|
});
|
|
67
67
|
|
|
68
68
|
it('should not update updatedAt column when updating with raw sql which contains `updatedAt = `', async () => {
|
|
69
|
-
const query =
|
|
69
|
+
const query = table
|
|
70
70
|
.where()
|
|
71
71
|
.updateRaw(db.raw('"updatedAt" = $time', { time: now }));
|
|
72
72
|
|
package/src/db.ts
CHANGED
package/src/query.ts
CHANGED
|
@@ -72,14 +72,14 @@ type CreateBelongsToData<
|
|
|
72
72
|
}
|
|
73
73
|
| {
|
|
74
74
|
create?: never;
|
|
75
|
-
connect: WhereArg<Rel['
|
|
75
|
+
connect: WhereArg<Rel['table']>;
|
|
76
76
|
connectOrCreate?: never;
|
|
77
77
|
}
|
|
78
78
|
| {
|
|
79
79
|
create?: never;
|
|
80
80
|
connect?: never;
|
|
81
81
|
connectOrCreate: {
|
|
82
|
-
where: WhereArg<Rel['
|
|
82
|
+
where: WhereArg<Rel['table']>;
|
|
83
83
|
create: CreateData<Rel['nestedCreateQuery']>;
|
|
84
84
|
};
|
|
85
85
|
};
|
|
@@ -101,14 +101,14 @@ type CreateHasOneData<
|
|
|
101
101
|
}
|
|
102
102
|
| {
|
|
103
103
|
create?: never;
|
|
104
|
-
connect: WhereArg<Rel['
|
|
104
|
+
connect: WhereArg<Rel['table']>;
|
|
105
105
|
connectOrCreate?: never;
|
|
106
106
|
}
|
|
107
107
|
| {
|
|
108
108
|
create?: never;
|
|
109
109
|
connect?: never;
|
|
110
110
|
connectOrCreate: {
|
|
111
|
-
where?: WhereArg<Rel['
|
|
111
|
+
where?: WhereArg<Rel['table']>;
|
|
112
112
|
create?: CreateData<Rel['nestedCreateQuery']>;
|
|
113
113
|
};
|
|
114
114
|
};
|
|
@@ -124,9 +124,9 @@ type CreateHasManyData<
|
|
|
124
124
|
: {
|
|
125
125
|
[K in Key]?: {
|
|
126
126
|
create?: CreateData<Rel['nestedCreateQuery']>[];
|
|
127
|
-
connect?: WhereArg<Rel['
|
|
127
|
+
connect?: WhereArg<Rel['table']>[];
|
|
128
128
|
connectOrCreate?: {
|
|
129
|
-
where: WhereArg<Rel['
|
|
129
|
+
where: WhereArg<Rel['table']>;
|
|
130
130
|
create: CreateData<Rel['nestedCreateQuery']>;
|
|
131
131
|
}[];
|
|
132
132
|
};
|
package/src/queryMethods/for.ts
CHANGED
|
@@ -15,10 +15,10 @@ const forQueryBuilder = <T extends Query>(
|
|
|
15
15
|
tableNames?: string[] | RawExpression,
|
|
16
16
|
) => {
|
|
17
17
|
(q.query as SelectQueryData).for = { type, tableNames };
|
|
18
|
-
q.
|
|
19
|
-
q.
|
|
18
|
+
q.__table = Object.create(q.__table);
|
|
19
|
+
q.__table.__table = q.__table;
|
|
20
20
|
|
|
21
|
-
Object.assign(q.
|
|
21
|
+
Object.assign(q.__table, {
|
|
22
22
|
noWait<T extends ForQueryBuilder<Query>>(this: T): T {
|
|
23
23
|
return this.clone()._noWait();
|
|
24
24
|
},
|
|
@@ -29,7 +29,7 @@ export type HavingArg<T extends Query = Query> =
|
|
|
29
29
|
| RawExpression;
|
|
30
30
|
|
|
31
31
|
const processHavingArg = <T extends Query>(arg: HavingArg<T>): HavingItem => {
|
|
32
|
-
if ('
|
|
32
|
+
if ('__table' in arg || isRaw(arg)) {
|
|
33
33
|
return arg;
|
|
34
34
|
} else {
|
|
35
35
|
const processed = { ...arg } as Record<
|
package/src/queryMethods/join.ts
CHANGED
|
@@ -78,7 +78,7 @@ type JoinResult<
|
|
|
78
78
|
? A
|
|
79
79
|
: T['relations'] extends Record<string, Relation>
|
|
80
80
|
? A extends keyof T['relations']
|
|
81
|
-
? T['relations'][A]['
|
|
81
|
+
? T['relations'][A]['table']
|
|
82
82
|
: A extends keyof T['withData']
|
|
83
83
|
? T['withData'][A] extends WithDataItem
|
|
84
84
|
? {
|
|
@@ -116,7 +116,7 @@ export type JoinCallback<
|
|
|
116
116
|
};
|
|
117
117
|
};
|
|
118
118
|
shape: T['withData'][Arg]['shape'];
|
|
119
|
-
|
|
119
|
+
__table: Query;
|
|
120
120
|
relations: RelationsBase;
|
|
121
121
|
withData: WithDataBase;
|
|
122
122
|
}
|
|
@@ -125,7 +125,7 @@ export type JoinCallback<
|
|
|
125
125
|
? Arg
|
|
126
126
|
: T['relations'] extends Record<string, Relation>
|
|
127
127
|
? Arg extends keyof T['relations']
|
|
128
|
-
? T['relations'][Arg]['
|
|
128
|
+
? T['relations'][Arg]['table']
|
|
129
129
|
: never
|
|
130
130
|
: never
|
|
131
131
|
>,
|
|
@@ -140,7 +140,7 @@ type JoinCallbackResult<
|
|
|
140
140
|
? Arg
|
|
141
141
|
: T['relations'] extends Record<string, Relation>
|
|
142
142
|
? Arg extends keyof T['relations']
|
|
143
|
-
? T['relations'][Arg]['
|
|
143
|
+
? T['relations'][Arg]['table']
|
|
144
144
|
: Arg extends keyof T['withData']
|
|
145
145
|
? T['withData'][Arg] extends WithDataItem
|
|
146
146
|
? {
|
package/src/queryMethods/json.ts
CHANGED
|
@@ -53,7 +53,7 @@ export class Json {
|
|
|
53
53
|
_json<T extends Query>(
|
|
54
54
|
this: T,
|
|
55
55
|
): SetQueryReturnsValueOptional<T, StringColumn> {
|
|
56
|
-
const q = this._wrap(this.
|
|
56
|
+
const q = this._wrap(this.__table.clone()) as T;
|
|
57
57
|
q._getOptional(
|
|
58
58
|
raw(
|
|
59
59
|
queryTypeWithLimitOne[this.query.returnType]
|
|
@@ -104,7 +104,7 @@ export interface QueryMethods
|
|
|
104
104
|
|
|
105
105
|
export class QueryMethods {
|
|
106
106
|
windows!: EmptyObject;
|
|
107
|
-
|
|
107
|
+
__table!: Query;
|
|
108
108
|
|
|
109
109
|
all<T extends Query>(this: T): SetQueryReturnsAll<T> {
|
|
110
110
|
return this.clone()._all();
|
|
@@ -169,7 +169,7 @@ export class QueryMethods {
|
|
|
169
169
|
}
|
|
170
170
|
|
|
171
171
|
clone<T extends QueryBase>(this: T): T {
|
|
172
|
-
const cloned = Object.create(this.
|
|
172
|
+
const cloned = Object.create(this.__table);
|
|
173
173
|
cloned.query = getClonedQueryData(this.query);
|
|
174
174
|
return cloned;
|
|
175
175
|
}
|
|
@@ -50,11 +50,11 @@ type SelectResult<
|
|
|
50
50
|
: T['relations'] extends Record<string, Relation>
|
|
51
51
|
? Arg extends keyof T['relations']
|
|
52
52
|
? T['relations'][Arg]['returns'] extends 'many'
|
|
53
|
-
? ArrayOfColumnsObjects<T['relations'][Arg]['
|
|
53
|
+
? ArrayOfColumnsObjects<T['relations'][Arg]['table']['result']>
|
|
54
54
|
: T['relations'][Arg]['options']['required'] extends true
|
|
55
|
-
? ColumnsObject<T['relations'][Arg]['
|
|
55
|
+
? ColumnsObject<T['relations'][Arg]['table']['result']>
|
|
56
56
|
: NullableColumn<
|
|
57
|
-
ColumnsObject<T['relations'][Arg]['
|
|
57
|
+
ColumnsObject<T['relations'][Arg]['table']['result']>
|
|
58
58
|
>
|
|
59
59
|
: never
|
|
60
60
|
: never;
|
|
@@ -36,9 +36,9 @@ export type UpdateData<T extends Query> = {
|
|
|
36
36
|
|
|
37
37
|
type UpdateBelongsToData<T extends Query, Rel extends BelongsToRelation> =
|
|
38
38
|
| { disconnect: boolean }
|
|
39
|
-
| { set: WhereArg<Rel['
|
|
39
|
+
| { set: WhereArg<Rel['table']> }
|
|
40
40
|
| { delete: boolean }
|
|
41
|
-
| { update: UpdateData<Rel['
|
|
41
|
+
| { update: UpdateData<Rel['table']> }
|
|
42
42
|
| {
|
|
43
43
|
create: CreateData<Rel['nestedCreateQuery']>;
|
|
44
44
|
}
|
|
@@ -46,7 +46,7 @@ type UpdateBelongsToData<T extends Query, Rel extends BelongsToRelation> =
|
|
|
46
46
|
? never
|
|
47
47
|
: {
|
|
48
48
|
upsert: {
|
|
49
|
-
update: UpdateData<Rel['
|
|
49
|
+
update: UpdateData<Rel['table']>;
|
|
50
50
|
create: CreateData<Rel['nestedCreateQuery']>;
|
|
51
51
|
};
|
|
52
52
|
});
|
|
@@ -54,14 +54,14 @@ type UpdateBelongsToData<T extends Query, Rel extends BelongsToRelation> =
|
|
|
54
54
|
type UpdateHasOneData<T extends Query, Rel extends HasOneRelation> =
|
|
55
55
|
| { disconnect: boolean }
|
|
56
56
|
| { delete: boolean }
|
|
57
|
-
| { update: UpdateData<Rel['
|
|
57
|
+
| { update: UpdateData<Rel['table']> }
|
|
58
58
|
| (QueryReturnsAll<T['returnType']> extends true
|
|
59
59
|
? never
|
|
60
60
|
:
|
|
61
|
-
| { set: WhereArg<Rel['
|
|
61
|
+
| { set: WhereArg<Rel['table']> }
|
|
62
62
|
| {
|
|
63
63
|
upsert: {
|
|
64
|
-
update: UpdateData<Rel['
|
|
64
|
+
update: UpdateData<Rel['table']>;
|
|
65
65
|
create: CreateData<Rel['nestedCreateQuery']>;
|
|
66
66
|
};
|
|
67
67
|
}
|
|
@@ -70,26 +70,26 @@ type UpdateHasOneData<T extends Query, Rel extends HasOneRelation> =
|
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
type UpdateHasManyData<T extends Query, Rel extends HasManyRelation> = {
|
|
73
|
-
disconnect?: MaybeArray<WhereArg<Rel['
|
|
74
|
-
delete?: MaybeArray<WhereArg<Rel['
|
|
73
|
+
disconnect?: MaybeArray<WhereArg<Rel['table']>>;
|
|
74
|
+
delete?: MaybeArray<WhereArg<Rel['table']>>;
|
|
75
75
|
update?: {
|
|
76
|
-
where: MaybeArray<WhereArg<Rel['
|
|
77
|
-
data: UpdateData<Rel['
|
|
76
|
+
where: MaybeArray<WhereArg<Rel['table']>>;
|
|
77
|
+
data: UpdateData<Rel['table']>;
|
|
78
78
|
};
|
|
79
79
|
} & (QueryReturnsAll<T['returnType']> extends true
|
|
80
80
|
? EmptyObject
|
|
81
81
|
: {
|
|
82
|
-
set?: MaybeArray<WhereArg<Rel['
|
|
82
|
+
set?: MaybeArray<WhereArg<Rel['table']>>;
|
|
83
83
|
create?: CreateData<Rel['nestedCreateQuery']>[];
|
|
84
84
|
});
|
|
85
85
|
|
|
86
86
|
type UpdateHasAndBelongsToManyData<Rel extends HasAndBelongsToManyRelation> = {
|
|
87
|
-
disconnect?: MaybeArray<WhereArg<Rel['
|
|
88
|
-
set?: MaybeArray<WhereArg<Rel['
|
|
89
|
-
delete?: MaybeArray<WhereArg<Rel['
|
|
87
|
+
disconnect?: MaybeArray<WhereArg<Rel['table']>>;
|
|
88
|
+
set?: MaybeArray<WhereArg<Rel['table']>>;
|
|
89
|
+
delete?: MaybeArray<WhereArg<Rel['table']>>;
|
|
90
90
|
update?: {
|
|
91
|
-
where: MaybeArray<WhereArg<Rel['
|
|
92
|
-
data: UpdateData<Rel['
|
|
91
|
+
where: MaybeArray<WhereArg<Rel['table']>>;
|
|
92
|
+
data: UpdateData<Rel['table']>;
|
|
93
93
|
};
|
|
94
94
|
create?: CreateData<Rel['nestedCreateQuery']>[];
|
|
95
95
|
};
|
|
@@ -213,7 +213,7 @@ export class Update {
|
|
|
213
213
|
if (ctx.updateLater) {
|
|
214
214
|
await Promise.all(ctx.updateLaterPromises as Promise<void>[]);
|
|
215
215
|
|
|
216
|
-
const t = this.
|
|
216
|
+
const t = this.__table.clone().transacting(q);
|
|
217
217
|
const keys = this.primaryKeys;
|
|
218
218
|
(
|
|
219
219
|
t._whereIn as unknown as (
|
|
@@ -143,7 +143,7 @@ export abstract class Where implements QueryBase {
|
|
|
143
143
|
abstract shape: ColumnsShape;
|
|
144
144
|
abstract relations: RelationsBase;
|
|
145
145
|
abstract withData: WithDataBase;
|
|
146
|
-
abstract
|
|
146
|
+
abstract __table: Query;
|
|
147
147
|
|
|
148
148
|
query = {} as QueryData;
|
|
149
149
|
table?: string;
|
|
@@ -437,21 +437,21 @@ export class WhereQueryBuilder<Q extends QueryBase = QueryBase>
|
|
|
437
437
|
selectable!: Q['selectable'];
|
|
438
438
|
shape: Q['shape'];
|
|
439
439
|
relations!: Q['relations'];
|
|
440
|
-
|
|
440
|
+
__table: Query;
|
|
441
441
|
withData = {};
|
|
442
442
|
|
|
443
443
|
constructor(q: QueryBase | string, shape: ColumnsShape) {
|
|
444
444
|
super();
|
|
445
445
|
this.table = typeof q === 'object' ? q.table : q;
|
|
446
446
|
this.shape = shape;
|
|
447
|
-
this.
|
|
447
|
+
this.__table = this as unknown as Query;
|
|
448
448
|
if (typeof q === 'object' && q.query.as) {
|
|
449
449
|
this.query.as = q.query.as;
|
|
450
450
|
}
|
|
451
451
|
}
|
|
452
452
|
|
|
453
453
|
clone<T extends this>(this: T): T {
|
|
454
|
-
const cloned = Object.create(this.
|
|
454
|
+
const cloned = Object.create(this.__table);
|
|
455
455
|
cloned.query = getClonedQueryData(this.query);
|
|
456
456
|
return cloned as unknown as T;
|
|
457
457
|
}
|
package/src/relations.ts
CHANGED
package/src/sql/aggregate.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { ToSqlCtx } from './toSql';
|
|
|
9
9
|
|
|
10
10
|
export const aggregateToSql = (
|
|
11
11
|
ctx: ToSqlCtx,
|
|
12
|
-
|
|
12
|
+
table: QueryBase,
|
|
13
13
|
item: AggregateItem,
|
|
14
14
|
quotedAs?: string,
|
|
15
15
|
) => {
|
|
@@ -58,7 +58,7 @@ export const aggregateToSql = (
|
|
|
58
58
|
if (options.filter || options.filterOr) {
|
|
59
59
|
const whereSql = whereToSql(
|
|
60
60
|
ctx,
|
|
61
|
-
|
|
61
|
+
table,
|
|
62
62
|
{
|
|
63
63
|
and: options.filter ? [options.filter] : undefined,
|
|
64
64
|
or: options.filterOr?.map((item) => [item]),
|
package/src/sql/copy.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { pushWhereStatementSql } from './where';
|
|
|
7
7
|
|
|
8
8
|
export const pushCopySql = (
|
|
9
9
|
ctx: ToSqlCtx,
|
|
10
|
-
|
|
10
|
+
table: Query,
|
|
11
11
|
query: CopyQueryData,
|
|
12
12
|
quotedAs?: string,
|
|
13
13
|
) => {
|
|
@@ -19,7 +19,7 @@ export const pushCopySql = (
|
|
|
19
19
|
const target = 'from' in copy ? copy.from : copy.to;
|
|
20
20
|
|
|
21
21
|
sql.push(
|
|
22
|
-
`COPY ${q(
|
|
22
|
+
`COPY ${q(table.table as string)}${columns} ${
|
|
23
23
|
'from' in copy ? 'FROM' : 'TO'
|
|
24
24
|
} ${
|
|
25
25
|
typeof target === 'string'
|
|
@@ -55,5 +55,5 @@ export const pushCopySql = (
|
|
|
55
55
|
sql.push(`WITH (${options.join(', ')})`);
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
pushWhereStatementSql(ctx,
|
|
58
|
+
pushWhereStatementSql(ctx, table, query, quotedAs);
|
|
59
59
|
};
|
package/src/sql/delete.ts
CHANGED
|
@@ -8,11 +8,11 @@ import { DeleteQueryData } from './data';
|
|
|
8
8
|
|
|
9
9
|
export const pushDeleteSql = (
|
|
10
10
|
ctx: ToSqlCtx,
|
|
11
|
-
|
|
11
|
+
table: QueryBase,
|
|
12
12
|
query: DeleteQueryData,
|
|
13
13
|
quotedAs: string,
|
|
14
14
|
) => {
|
|
15
|
-
const from = q(
|
|
15
|
+
const from = q(table.table as string);
|
|
16
16
|
ctx.sql.push(`DELETE FROM ${from}`);
|
|
17
17
|
|
|
18
18
|
if (from !== quotedAs) {
|
|
@@ -22,7 +22,7 @@ export const pushDeleteSql = (
|
|
|
22
22
|
let conditions: string | undefined;
|
|
23
23
|
if (query.join?.length) {
|
|
24
24
|
const items = query.join.map((item) =>
|
|
25
|
-
processJoinItem(ctx,
|
|
25
|
+
processJoinItem(ctx, table, item.args, quotedAs),
|
|
26
26
|
);
|
|
27
27
|
|
|
28
28
|
ctx.sql.push(`USING ${items.map((item) => item.target).join(', ')}`);
|
|
@@ -33,7 +33,7 @@ export const pushDeleteSql = (
|
|
|
33
33
|
.join(' AND ');
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
pushWhereStatementSql(ctx,
|
|
36
|
+
pushWhereStatementSql(ctx, table, query, quotedAs);
|
|
37
37
|
|
|
38
38
|
if (conditions?.length) {
|
|
39
39
|
if (query.and?.length || query.or?.length) {
|
|
@@ -43,5 +43,5 @@ export const pushDeleteSql = (
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
pushReturningSql(ctx,
|
|
46
|
+
pushReturningSql(ctx, table, query, quotedAs);
|
|
47
47
|
};
|
package/src/sql/fromAndAs.ts
CHANGED
|
@@ -7,14 +7,14 @@ import { SelectQueryData } from './data';
|
|
|
7
7
|
|
|
8
8
|
export const pushFromAndAs = (
|
|
9
9
|
ctx: ToSqlCtx,
|
|
10
|
-
|
|
10
|
+
table: QueryBase,
|
|
11
11
|
query: SelectQueryData,
|
|
12
12
|
quotedAs?: string,
|
|
13
13
|
) => {
|
|
14
14
|
ctx.sql.push('FROM');
|
|
15
15
|
if (query.fromOnly) ctx.sql.push('ONLY');
|
|
16
16
|
|
|
17
|
-
const from = getFrom(
|
|
17
|
+
const from = getFrom(table, query, ctx.values);
|
|
18
18
|
ctx.sql.push(from);
|
|
19
19
|
|
|
20
20
|
if (query.as && quotedAs && quotedAs !== from) {
|
|
@@ -23,7 +23,7 @@ export const pushFromAndAs = (
|
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
const getFrom = (
|
|
26
|
-
|
|
26
|
+
table: QueryBase,
|
|
27
27
|
query: SelectQueryData,
|
|
28
28
|
values: unknown[],
|
|
29
29
|
) => {
|
|
@@ -51,5 +51,5 @@ const getFrom = (
|
|
|
51
51
|
return quoteSchemaAndTable(query.schema, query.from);
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
return quoteSchemaAndTable(query.schema,
|
|
54
|
+
return quoteSchemaAndTable(query.schema, table.table as string);
|
|
55
55
|
};
|