pqb 0.4.0 → 0.4.2
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 +61 -48
- package/dist/index.esm.js +136 -117
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +136 -117
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/columnSchema/columnType.test.ts +1 -1
- package/src/db.test.ts +2 -2
- package/src/errors.test.ts +4 -4
- package/src/queryMethods/aggregate.test.ts +11 -11
- package/src/queryMethods/aggregate.ts +5 -3
- package/src/queryMethods/callbacks.test.ts +6 -6
- package/src/queryMethods/callbacks.ts +8 -8
- package/src/queryMethods/columnInfo.ts +13 -19
- package/src/queryMethods/{insert.test.ts → create.test.ts} +108 -98
- package/src/queryMethods/{insert.ts → create.ts} +139 -118
- package/src/queryMethods/delete.test.ts +2 -2
- package/src/queryMethods/get.test.ts +2 -2
- package/src/queryMethods/index.ts +1 -1
- package/src/queryMethods/json.test.ts +1 -1
- package/src/queryMethods/log.test.ts +29 -8
- package/src/queryMethods/merge.test.ts +3 -3
- package/src/queryMethods/queryMethods.test.ts +4 -4
- package/src/queryMethods/queryMethods.ts +3 -3
- package/src/queryMethods/select.test.ts +7 -7
- package/src/queryMethods/then.ts +2 -2
- package/src/queryMethods/update.test.ts +8 -8
- package/src/queryMethods/update.ts +7 -7
- package/src/queryMethods/upsert.ts +3 -3
- package/src/queryMethods/window.test.ts +2 -2
- package/src/relations.ts +18 -7
- package/src/sql/insert.ts +31 -19
- package/src/sql/types.ts +3 -2
- package/src/test-utils.ts +4 -1
package/dist/index.js
CHANGED
|
@@ -1121,15 +1121,49 @@ const getFrom = (model, query, values) => {
|
|
|
1121
1121
|
return quoteSchemaAndTable(query.schema, model.table);
|
|
1122
1122
|
};
|
|
1123
1123
|
|
|
1124
|
+
const pushQueryArray = (q, key, value) => {
|
|
1125
|
+
if (!q.query[key])
|
|
1126
|
+
q.query[key] = value;
|
|
1127
|
+
else
|
|
1128
|
+
q.query[key].push(
|
|
1129
|
+
...value
|
|
1130
|
+
);
|
|
1131
|
+
return q;
|
|
1132
|
+
};
|
|
1133
|
+
const pushQueryValue = (q, key, value) => {
|
|
1134
|
+
pushOrNewArrayToObject(
|
|
1135
|
+
q.query,
|
|
1136
|
+
key,
|
|
1137
|
+
value
|
|
1138
|
+
);
|
|
1139
|
+
return q;
|
|
1140
|
+
};
|
|
1141
|
+
const setQueryObjectValue = (q, object, key, value) => {
|
|
1142
|
+
if (!q.query[object])
|
|
1143
|
+
q.query[object] = {
|
|
1144
|
+
[key]: value
|
|
1145
|
+
};
|
|
1146
|
+
else
|
|
1147
|
+
q.query[object][key] = value;
|
|
1148
|
+
return q;
|
|
1149
|
+
};
|
|
1150
|
+
|
|
1124
1151
|
const pushInsertSql = (ctx, model, query, quotedAs) => {
|
|
1125
1152
|
const quotedColumns = query.columns.map(q);
|
|
1126
|
-
ctx.sql.push(
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1153
|
+
ctx.sql.push(`INSERT INTO ${quotedAs}(${quotedColumns.join(", ")})`);
|
|
1154
|
+
if (query.fromQuery) {
|
|
1155
|
+
const q2 = query.fromQuery.clone();
|
|
1156
|
+
pushQueryValue(
|
|
1157
|
+
q2,
|
|
1158
|
+
"select",
|
|
1159
|
+
isRaw(query.values) ? query.values : raw(encodeRow(ctx, query.values[0]))
|
|
1160
|
+
);
|
|
1161
|
+
ctx.sql.push(q2.toSql({ values: ctx.values }).text);
|
|
1162
|
+
} else {
|
|
1163
|
+
ctx.sql.push(
|
|
1164
|
+
`VALUES ${isRaw(query.values) ? getRaw(query.values, ctx.values) : query.values.map((row) => `(${encodeRow(ctx, row)})`).join(", ")}`
|
|
1165
|
+
);
|
|
1166
|
+
}
|
|
1133
1167
|
if (query.onConflict) {
|
|
1134
1168
|
ctx.sql.push("ON CONFLICT");
|
|
1135
1169
|
const { expr, type } = query.onConflict;
|
|
@@ -1172,6 +1206,11 @@ const pushInsertSql = (ctx, model, query, quotedAs) => {
|
|
|
1172
1206
|
pushWhereStatementSql(ctx, model, query, quotedAs);
|
|
1173
1207
|
pushReturningSql(ctx, model, query, quotedAs);
|
|
1174
1208
|
};
|
|
1209
|
+
const encodeRow = (ctx, row) => {
|
|
1210
|
+
return row.map(
|
|
1211
|
+
(value) => value === void 0 ? "DEFAULT" : addValue(ctx.values, value)
|
|
1212
|
+
).join(", ");
|
|
1213
|
+
};
|
|
1175
1214
|
const pushReturningSql = (ctx, model, query, quotedAs) => {
|
|
1176
1215
|
if (query.select) {
|
|
1177
1216
|
ctx.sql.push(`RETURNING ${selectToSql(ctx, model, query, quotedAs)}`);
|
|
@@ -3145,33 +3184,6 @@ class TransactionAdapter {
|
|
|
3145
3184
|
}
|
|
3146
3185
|
}
|
|
3147
3186
|
|
|
3148
|
-
const pushQueryArray = (q, key, value) => {
|
|
3149
|
-
if (!q.query[key])
|
|
3150
|
-
q.query[key] = value;
|
|
3151
|
-
else
|
|
3152
|
-
q.query[key].push(
|
|
3153
|
-
...value
|
|
3154
|
-
);
|
|
3155
|
-
return q;
|
|
3156
|
-
};
|
|
3157
|
-
const pushQueryValue = (q, key, value) => {
|
|
3158
|
-
pushOrNewArrayToObject(
|
|
3159
|
-
q.query,
|
|
3160
|
-
key,
|
|
3161
|
-
value
|
|
3162
|
-
);
|
|
3163
|
-
return q;
|
|
3164
|
-
};
|
|
3165
|
-
const setQueryObjectValue = (q, object, key, value) => {
|
|
3166
|
-
if (!q.query[object])
|
|
3167
|
-
q.query[object] = {
|
|
3168
|
-
[key]: value
|
|
3169
|
-
};
|
|
3170
|
-
else
|
|
3171
|
-
q.query[object][key] = value;
|
|
3172
|
-
return q;
|
|
3173
|
-
};
|
|
3174
|
-
|
|
3175
3187
|
const getValueKey = Symbol("get");
|
|
3176
3188
|
const _get = (q, returnType, arg) => {
|
|
3177
3189
|
q.query.returnType = returnType;
|
|
@@ -3243,8 +3255,8 @@ const then = async (q, resolve, reject) => {
|
|
|
3243
3255
|
let beforeCallbacks;
|
|
3244
3256
|
let afterCallbacks;
|
|
3245
3257
|
if (q.query.type === "insert") {
|
|
3246
|
-
beforeCallbacks = q.query.
|
|
3247
|
-
afterCallbacks = q.query.
|
|
3258
|
+
beforeCallbacks = q.query.beforeCreate;
|
|
3259
|
+
afterCallbacks = q.query.afterCreate;
|
|
3248
3260
|
} else if (q.query.type === "update") {
|
|
3249
3261
|
beforeCallbacks = q.query.beforeUpdate;
|
|
3250
3262
|
afterCallbacks = q.query.afterUpdate;
|
|
@@ -3605,7 +3617,9 @@ class Aggregate {
|
|
|
3605
3617
|
return this.clone()._count(arg, options);
|
|
3606
3618
|
}
|
|
3607
3619
|
_count(arg = "*", options) {
|
|
3608
|
-
return get(
|
|
3620
|
+
return get(
|
|
3621
|
+
this._selectCount(arg, options)
|
|
3622
|
+
);
|
|
3609
3623
|
}
|
|
3610
3624
|
selectCount(arg, options) {
|
|
3611
3625
|
return this.clone()._selectCount(arg, options);
|
|
@@ -3851,17 +3865,17 @@ class QueryCallbacks {
|
|
|
3851
3865
|
_afterQuery(cb) {
|
|
3852
3866
|
return pushQueryValue(this, "afterQuery", cb);
|
|
3853
3867
|
}
|
|
3854
|
-
|
|
3855
|
-
return this.clone().
|
|
3868
|
+
beforeCreate(cb) {
|
|
3869
|
+
return this.clone()._beforeCreate(cb);
|
|
3856
3870
|
}
|
|
3857
|
-
|
|
3858
|
-
return pushQueryValue(this, "
|
|
3871
|
+
_beforeCreate(cb) {
|
|
3872
|
+
return pushQueryValue(this, "beforeCreate", cb);
|
|
3859
3873
|
}
|
|
3860
|
-
|
|
3861
|
-
return this.clone().
|
|
3874
|
+
afterCreate(cb) {
|
|
3875
|
+
return this.clone()._afterCreate(cb);
|
|
3862
3876
|
}
|
|
3863
|
-
|
|
3864
|
-
return pushQueryValue(this, "
|
|
3877
|
+
_afterCreate(cb) {
|
|
3878
|
+
return pushQueryValue(this, "afterCreate", cb);
|
|
3865
3879
|
}
|
|
3866
3880
|
beforeUpdate(cb) {
|
|
3867
3881
|
return this.clone()._beforeUpdate(cb);
|
|
@@ -3944,22 +3958,16 @@ class ColumnInfoMethods {
|
|
|
3944
3958
|
if (column) {
|
|
3945
3959
|
this.query.column = column;
|
|
3946
3960
|
}
|
|
3947
|
-
this.
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
3954
|
-
|
|
3955
|
-
|
|
3956
|
-
|
|
3957
|
-
});
|
|
3958
|
-
resolve == null ? void 0 : resolve(info);
|
|
3959
|
-
}
|
|
3960
|
-
},
|
|
3961
|
-
reject
|
|
3962
|
-
);
|
|
3961
|
+
this.query.handleResult = async (_, result) => {
|
|
3962
|
+
if (column) {
|
|
3963
|
+
return rowToColumnInfo(result.rows[0]);
|
|
3964
|
+
} else {
|
|
3965
|
+
const info = {};
|
|
3966
|
+
result.rows.forEach((row) => {
|
|
3967
|
+
info[row.column_name] = rowToColumnInfo(row);
|
|
3968
|
+
});
|
|
3969
|
+
return info;
|
|
3970
|
+
}
|
|
3963
3971
|
};
|
|
3964
3972
|
return this;
|
|
3965
3973
|
}
|
|
@@ -4157,7 +4165,17 @@ var __spreadValues$4 = (a, b) => {
|
|
|
4157
4165
|
}
|
|
4158
4166
|
return a;
|
|
4159
4167
|
};
|
|
4160
|
-
const
|
|
4168
|
+
const handleSelect = (q) => {
|
|
4169
|
+
var _a;
|
|
4170
|
+
const select = (_a = q.query.select) == null ? void 0 : _a[0];
|
|
4171
|
+
const isCount = typeof select === "object" && "function" in select && select.function === "count";
|
|
4172
|
+
if (isCount) {
|
|
4173
|
+
q.query.select = void 0;
|
|
4174
|
+
} else if (isCount || !q.query.select) {
|
|
4175
|
+
q.query.select = ["*"];
|
|
4176
|
+
}
|
|
4177
|
+
};
|
|
4178
|
+
const processCreateItem = (item, rowIndex, ctx, columns, columnsMap) => {
|
|
4161
4179
|
Object.keys(item).forEach((key) => {
|
|
4162
4180
|
if (ctx.relations[key]) {
|
|
4163
4181
|
if (ctx.relations[key].type === "belongsTo") {
|
|
@@ -4189,13 +4207,13 @@ const processInsertItem = (item, rowIndex, ctx, columns, columnsMap) => {
|
|
|
4189
4207
|
}
|
|
4190
4208
|
});
|
|
4191
4209
|
};
|
|
4192
|
-
const
|
|
4210
|
+
const createCtx = (q) => ({
|
|
4193
4211
|
prependRelations: {},
|
|
4194
4212
|
appendRelations: {},
|
|
4195
4213
|
requiredReturning: {},
|
|
4196
4214
|
relations: q.relations
|
|
4197
4215
|
});
|
|
4198
|
-
const
|
|
4216
|
+
const getSingleReturnType = (q) => {
|
|
4199
4217
|
const { select, returnType = "all" } = q.query;
|
|
4200
4218
|
if (select) {
|
|
4201
4219
|
return returnType === "all" ? "one" : returnType;
|
|
@@ -4203,7 +4221,7 @@ const getInsertSingleReturnType = (q) => {
|
|
|
4203
4221
|
return "rowCount";
|
|
4204
4222
|
}
|
|
4205
4223
|
};
|
|
4206
|
-
const
|
|
4224
|
+
const getManyReturnType = (q) => {
|
|
4207
4225
|
const { select, returnType } = q.query;
|
|
4208
4226
|
if (select) {
|
|
4209
4227
|
return returnType === "one" || returnType === "oneOrThrow" ? "all" : returnType;
|
|
@@ -4211,18 +4229,18 @@ const getInsertManyReturnType = (q) => {
|
|
|
4211
4229
|
return "rowCount";
|
|
4212
4230
|
}
|
|
4213
4231
|
};
|
|
4214
|
-
const
|
|
4232
|
+
const handleOneData = (q, data, ctx) => {
|
|
4215
4233
|
const columns = [];
|
|
4216
4234
|
const columnsMap = {};
|
|
4217
4235
|
const defaults = q.query.defaults;
|
|
4218
4236
|
if (defaults) {
|
|
4219
4237
|
data = __spreadValues$4(__spreadValues$4({}, defaults), data);
|
|
4220
4238
|
}
|
|
4221
|
-
|
|
4239
|
+
processCreateItem(data, 0, ctx, columns, columnsMap);
|
|
4222
4240
|
const values = [columns.map((key) => data[key])];
|
|
4223
4241
|
return { columns, values };
|
|
4224
4242
|
};
|
|
4225
|
-
const
|
|
4243
|
+
const handleManyData = (q, data, ctx) => {
|
|
4226
4244
|
const columns = [];
|
|
4227
4245
|
const columnsMap = {};
|
|
4228
4246
|
const defaults = q.query.defaults;
|
|
@@ -4230,7 +4248,7 @@ const handleInsertManyData = (q, data, ctx) => {
|
|
|
4230
4248
|
data = data.map((item) => __spreadValues$4(__spreadValues$4({}, defaults), item));
|
|
4231
4249
|
}
|
|
4232
4250
|
data.forEach((item, i) => {
|
|
4233
|
-
|
|
4251
|
+
processCreateItem(item, i, ctx, columns, columnsMap);
|
|
4234
4252
|
});
|
|
4235
4253
|
const values = Array(data.length);
|
|
4236
4254
|
data.forEach((item, i) => {
|
|
@@ -4324,67 +4342,68 @@ const insert = (self, {
|
|
|
4324
4342
|
q.query.returnType = appendRelationsKeys.length ? "all" : returnType;
|
|
4325
4343
|
return q;
|
|
4326
4344
|
};
|
|
4327
|
-
class
|
|
4328
|
-
|
|
4329
|
-
return this.clone().
|
|
4345
|
+
class Create {
|
|
4346
|
+
create(data) {
|
|
4347
|
+
return this.clone()._create(data);
|
|
4330
4348
|
}
|
|
4331
|
-
|
|
4332
|
-
|
|
4349
|
+
_create(data) {
|
|
4350
|
+
var _a;
|
|
4351
|
+
handleSelect(this);
|
|
4352
|
+
const ctx = createCtx(this);
|
|
4353
|
+
const obj = handleOneData(this, data, ctx);
|
|
4354
|
+
let { columns } = obj;
|
|
4355
|
+
const { fromQuery } = this.query;
|
|
4356
|
+
if (fromQuery) {
|
|
4357
|
+
if (!queryTypeWithLimitOne[fromQuery.query.returnType]) {
|
|
4358
|
+
throw new Error(
|
|
4359
|
+
"Cannot create based on a query which returns multiple records"
|
|
4360
|
+
);
|
|
4361
|
+
}
|
|
4362
|
+
const queryColumns = [];
|
|
4363
|
+
(_a = fromQuery.query.select) == null ? void 0 : _a.forEach((item) => {
|
|
4364
|
+
if (typeof item === "string") {
|
|
4365
|
+
const index = item.indexOf(".");
|
|
4366
|
+
queryColumns.push(index === -1 ? item : item.slice(index + 1));
|
|
4367
|
+
} else if ("selectAs" in item) {
|
|
4368
|
+
queryColumns.push(...Object.keys(item.selectAs));
|
|
4369
|
+
}
|
|
4370
|
+
});
|
|
4371
|
+
queryColumns.push(...columns);
|
|
4372
|
+
columns = queryColumns;
|
|
4373
|
+
}
|
|
4333
4374
|
return insert(
|
|
4334
4375
|
this,
|
|
4335
|
-
|
|
4336
|
-
|
|
4376
|
+
{ columns, values: obj.values },
|
|
4377
|
+
getSingleReturnType(this),
|
|
4337
4378
|
ctx
|
|
4338
4379
|
);
|
|
4339
4380
|
}
|
|
4340
|
-
|
|
4341
|
-
return this.clone().
|
|
4381
|
+
createMany(data) {
|
|
4382
|
+
return this.clone()._createMany(data);
|
|
4342
4383
|
}
|
|
4343
|
-
|
|
4344
|
-
|
|
4384
|
+
_createMany(data) {
|
|
4385
|
+
handleSelect(this);
|
|
4386
|
+
const ctx = createCtx(this);
|
|
4345
4387
|
return insert(
|
|
4346
4388
|
this,
|
|
4347
|
-
|
|
4348
|
-
|
|
4389
|
+
handleManyData(this, data, ctx),
|
|
4390
|
+
getManyReturnType(this),
|
|
4349
4391
|
ctx
|
|
4350
4392
|
);
|
|
4351
4393
|
}
|
|
4352
|
-
insertRaw(data) {
|
|
4353
|
-
return this.clone()._insertRaw(data);
|
|
4354
|
-
}
|
|
4355
|
-
_insertRaw(data) {
|
|
4356
|
-
return insert(
|
|
4357
|
-
this,
|
|
4358
|
-
data,
|
|
4359
|
-
getInsertManyReturnType(this)
|
|
4360
|
-
);
|
|
4361
|
-
}
|
|
4362
|
-
create(data) {
|
|
4363
|
-
return this.clone()._create(data);
|
|
4364
|
-
}
|
|
4365
|
-
_create(data) {
|
|
4366
|
-
if (!this.query.select) {
|
|
4367
|
-
this.query.select = ["*"];
|
|
4368
|
-
}
|
|
4369
|
-
return this.clone()._insert(data);
|
|
4370
|
-
}
|
|
4371
|
-
createMany(data) {
|
|
4372
|
-
return this.clone()._createMany(data);
|
|
4373
|
-
}
|
|
4374
|
-
_createMany(data) {
|
|
4375
|
-
if (!this.query.select) {
|
|
4376
|
-
this.query.select = ["*"];
|
|
4377
|
-
}
|
|
4378
|
-
return this.clone()._insertMany(data);
|
|
4379
|
-
}
|
|
4380
4394
|
createRaw(data) {
|
|
4381
4395
|
return this.clone()._createRaw(data);
|
|
4382
4396
|
}
|
|
4383
4397
|
_createRaw(data) {
|
|
4384
|
-
|
|
4385
|
-
|
|
4386
|
-
|
|
4387
|
-
|
|
4398
|
+
handleSelect(this);
|
|
4399
|
+
return insert(this, data, getManyReturnType(this));
|
|
4400
|
+
}
|
|
4401
|
+
createFrom(query, data) {
|
|
4402
|
+
return this.clone()._createFrom(query, data);
|
|
4403
|
+
}
|
|
4404
|
+
_createFrom(query, data) {
|
|
4405
|
+
this.query.fromQuery = query;
|
|
4406
|
+
return this._create(data);
|
|
4388
4407
|
}
|
|
4389
4408
|
defaults(data) {
|
|
4390
4409
|
return this.clone()._defaults(data);
|
|
@@ -5327,7 +5346,7 @@ class QueryUpsert {
|
|
|
5327
5346
|
const { handleResult } = this.query;
|
|
5328
5347
|
this.query.handleResult = async (q, queryResult) => {
|
|
5329
5348
|
if (queryResult.rowCount === 0) {
|
|
5330
|
-
return q.
|
|
5349
|
+
return q.create(data.create);
|
|
5331
5350
|
} else if (queryResult.rowCount > 1) {
|
|
5332
5351
|
throw new MoreThanOneRowError(
|
|
5333
5352
|
`Only one row was expected to find for upsert, found ${queryResult.rowCount} rows.`
|
|
@@ -5513,7 +5532,7 @@ applyMixins(QueryMethods, [
|
|
|
5513
5532
|
With,
|
|
5514
5533
|
Union,
|
|
5515
5534
|
Json,
|
|
5516
|
-
|
|
5535
|
+
Create,
|
|
5517
5536
|
Update,
|
|
5518
5537
|
Delete,
|
|
5519
5538
|
Transaction,
|
|
@@ -5682,6 +5701,7 @@ exports.Clear = Clear;
|
|
|
5682
5701
|
exports.ColumnInfoMethods = ColumnInfoMethods;
|
|
5683
5702
|
exports.ColumnType = ColumnType;
|
|
5684
5703
|
exports.ColumnsObject = ColumnsObject;
|
|
5704
|
+
exports.Create = Create;
|
|
5685
5705
|
exports.DateBaseColumn = DateBaseColumn;
|
|
5686
5706
|
exports.DateColumn = DateColumn;
|
|
5687
5707
|
exports.DateTimeBaseClass = DateTimeBaseClass;
|
|
@@ -5697,7 +5717,6 @@ exports.For = For;
|
|
|
5697
5717
|
exports.From = From;
|
|
5698
5718
|
exports.Having = Having;
|
|
5699
5719
|
exports.InetColumn = InetColumn;
|
|
5700
|
-
exports.Insert = Insert;
|
|
5701
5720
|
exports.IntegerBaseColumn = IntegerBaseColumn;
|
|
5702
5721
|
exports.IntegerColumn = IntegerColumn;
|
|
5703
5722
|
exports.IntervalColumn = IntervalColumn;
|