pqb 0.2.7 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +6 -6
- package/dist/index.esm.js +12 -9
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +12 -9
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/queryMethods/then.ts +2 -2
- package/src/queryMethods/update.ts +24 -20
package/package.json
CHANGED
package/src/queryMethods/then.ts
CHANGED
|
@@ -18,10 +18,10 @@ export const queryMethodByReturnType: Record<
|
|
|
18
18
|
'query' | 'arrays'
|
|
19
19
|
> = {
|
|
20
20
|
all: 'query',
|
|
21
|
-
one: 'query',
|
|
22
|
-
oneOrThrow: 'query',
|
|
23
21
|
rows: 'arrays',
|
|
24
22
|
pluck: 'arrays',
|
|
23
|
+
one: 'query',
|
|
24
|
+
oneOrThrow: 'query',
|
|
25
25
|
value: 'arrays',
|
|
26
26
|
valueOrThrow: 'arrays',
|
|
27
27
|
rowCount: 'arrays',
|
|
@@ -15,6 +15,7 @@ import { WhereArg, WhereResult } from './where';
|
|
|
15
15
|
import { EmptyObject, MaybeArray } from '../utils';
|
|
16
16
|
import { InsertData } from './insert';
|
|
17
17
|
import { parseResult, queryMethodByReturnType } from './then';
|
|
18
|
+
import { UpdateQueryData } from '../sql';
|
|
18
19
|
|
|
19
20
|
export type UpdateData<T extends Query> = {
|
|
20
21
|
[K in keyof T['type']]?: T['type'][K] | RawExpression;
|
|
@@ -40,21 +41,22 @@ type UpdateBelongsToData<T extends Query, Rel extends BelongsToRelation> =
|
|
|
40
41
|
| {
|
|
41
42
|
create: InsertData<Rel['nestedCreateQuery']>;
|
|
42
43
|
}
|
|
43
|
-
| (T['returnType'] extends '
|
|
44
|
-
?
|
|
44
|
+
| (T['returnType'] extends 'all'
|
|
45
|
+
? never
|
|
46
|
+
: {
|
|
45
47
|
upsert: {
|
|
46
48
|
update: UpdateData<Rel['model']>;
|
|
47
49
|
create: InsertData<Rel['nestedCreateQuery']>;
|
|
48
50
|
};
|
|
49
|
-
}
|
|
50
|
-
: never);
|
|
51
|
+
});
|
|
51
52
|
|
|
52
53
|
type UpdateHasOneData<T extends Query, Rel extends HasOneRelation> =
|
|
53
54
|
| { disconnect: boolean }
|
|
54
55
|
| { delete: boolean }
|
|
55
56
|
| { update: UpdateData<Rel['model']> }
|
|
56
|
-
| (T['returnType'] extends '
|
|
57
|
-
?
|
|
57
|
+
| (T['returnType'] extends 'all'
|
|
58
|
+
? never
|
|
59
|
+
:
|
|
58
60
|
| { set: WhereArg<Rel['model']> }
|
|
59
61
|
| {
|
|
60
62
|
upsert: {
|
|
@@ -64,8 +66,7 @@ type UpdateHasOneData<T extends Query, Rel extends HasOneRelation> =
|
|
|
64
66
|
}
|
|
65
67
|
| {
|
|
66
68
|
create: InsertData<Rel['nestedCreateQuery']>;
|
|
67
|
-
}
|
|
68
|
-
: never);
|
|
69
|
+
});
|
|
69
70
|
|
|
70
71
|
type UpdateHasManyData<T extends Query, Rel extends HasManyRelation> = {
|
|
71
72
|
disconnect?: MaybeArray<WhereArg<Rel['model']>>;
|
|
@@ -74,12 +75,12 @@ type UpdateHasManyData<T extends Query, Rel extends HasManyRelation> = {
|
|
|
74
75
|
where: MaybeArray<WhereArg<Rel['model']>>;
|
|
75
76
|
data: UpdateData<Rel['model']>;
|
|
76
77
|
};
|
|
77
|
-
} & (T['returnType'] extends '
|
|
78
|
-
?
|
|
78
|
+
} & (T['returnType'] extends 'all'
|
|
79
|
+
? EmptyObject
|
|
80
|
+
: {
|
|
79
81
|
set?: MaybeArray<WhereArg<Rel['model']>>;
|
|
80
82
|
create?: InsertData<Rel['nestedCreateQuery']>[];
|
|
81
|
-
}
|
|
82
|
-
: EmptyObject);
|
|
83
|
+
});
|
|
83
84
|
|
|
84
85
|
type UpdateHasAndBelongsToManyData<Rel extends HasAndBelongsToManyRelation> = {
|
|
85
86
|
disconnect?: MaybeArray<WhereArg<Rel['model']>>;
|
|
@@ -127,6 +128,10 @@ const applyCountChange = <T extends Query>(
|
|
|
127
128
|
return self as unknown as UpdateResult<T>;
|
|
128
129
|
};
|
|
129
130
|
|
|
131
|
+
const checkIfUpdateIsEmpty = (q: UpdateQueryData) => {
|
|
132
|
+
return !q.data?.some((item) => isRaw(item) || Object.keys(item).length);
|
|
133
|
+
};
|
|
134
|
+
|
|
130
135
|
export class Update {
|
|
131
136
|
update<T extends Query, ForceAll extends boolean = false>(
|
|
132
137
|
this: T,
|
|
@@ -157,6 +162,9 @@ export class Update {
|
|
|
157
162
|
if (isRaw(data)) {
|
|
158
163
|
pushQueryValue(this, 'data', data);
|
|
159
164
|
} else {
|
|
165
|
+
const update: Record<string, unknown> = { ...data };
|
|
166
|
+
pushQueryValue(this, 'data', update);
|
|
167
|
+
|
|
160
168
|
const relations = this.relations as Record<string, Relation>;
|
|
161
169
|
|
|
162
170
|
const prependRelations: Record<string, Record<string, unknown>> = {};
|
|
@@ -164,7 +172,6 @@ export class Update {
|
|
|
164
172
|
|
|
165
173
|
const originalReturnType = this.query.returnType;
|
|
166
174
|
|
|
167
|
-
const update: Record<string, unknown> = { ...data };
|
|
168
175
|
for (const key in data) {
|
|
169
176
|
if (relations[key]) {
|
|
170
177
|
delete update[key];
|
|
@@ -188,8 +195,9 @@ export class Update {
|
|
|
188
195
|
} = {};
|
|
189
196
|
|
|
190
197
|
const prependRelationKeys = Object.keys(prependRelations);
|
|
198
|
+
let willSetKeys = false;
|
|
191
199
|
if (prependRelationKeys.length) {
|
|
192
|
-
|
|
200
|
+
willSetKeys = prependRelationKeys.some((relationName) => {
|
|
193
201
|
const data = prependRelations[relationName] as NestedUpdateOneItem;
|
|
194
202
|
|
|
195
203
|
return (
|
|
@@ -198,11 +206,9 @@ export class Update {
|
|
|
198
206
|
}
|
|
199
207
|
).nestedUpdate(this, update, data, state);
|
|
200
208
|
});
|
|
209
|
+
}
|
|
201
210
|
|
|
202
|
-
|
|
203
|
-
delete this.query.type;
|
|
204
|
-
}
|
|
205
|
-
} else if (!Object.keys(update).length) {
|
|
211
|
+
if (!willSetKeys && checkIfUpdateIsEmpty(this.query as UpdateQueryData)) {
|
|
206
212
|
delete this.query.type;
|
|
207
213
|
}
|
|
208
214
|
|
|
@@ -283,8 +289,6 @@ export class Update {
|
|
|
283
289
|
if (prependRelationKeys.length || appendRelationKeys.length) {
|
|
284
290
|
query.wrapInTransaction = true;
|
|
285
291
|
}
|
|
286
|
-
|
|
287
|
-
pushQueryValue(this, 'data', update);
|
|
288
292
|
}
|
|
289
293
|
|
|
290
294
|
if (!query.select) {
|