pqb 0.2.1 → 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 +59 -48
- 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/columnType.ts +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.js
CHANGED
|
@@ -634,6 +634,7 @@ class SmallSerialColumn extends IntegerBaseColumn {
|
|
|
634
634
|
super(...arguments);
|
|
635
635
|
this.dataType = "smallserial";
|
|
636
636
|
this.parseItem = parseInt;
|
|
637
|
+
this.hasDefault = true;
|
|
637
638
|
}
|
|
638
639
|
}
|
|
639
640
|
class SerialColumn extends IntegerBaseColumn {
|
|
@@ -641,12 +642,14 @@ class SerialColumn extends IntegerBaseColumn {
|
|
|
641
642
|
super(...arguments);
|
|
642
643
|
this.dataType = "serial";
|
|
643
644
|
this.parseItem = parseInt;
|
|
645
|
+
this.hasDefault = true;
|
|
644
646
|
}
|
|
645
647
|
}
|
|
646
648
|
class BigSerialColumn extends NumberAsStringBaseColumn {
|
|
647
649
|
constructor() {
|
|
648
650
|
super(...arguments);
|
|
649
651
|
this.dataType = "bigserial";
|
|
652
|
+
this.hasDefault = true;
|
|
650
653
|
}
|
|
651
654
|
}
|
|
652
655
|
|
|
@@ -4259,28 +4262,31 @@ var __spreadValues$3 = (a, b) => {
|
|
|
4259
4262
|
}
|
|
4260
4263
|
return a;
|
|
4261
4264
|
};
|
|
4262
|
-
const processInsertItem = (item, rowIndex,
|
|
4265
|
+
const processInsertItem = (item, rowIndex, ctx, columns, columnsMap) => {
|
|
4263
4266
|
Object.keys(item).forEach((key) => {
|
|
4264
|
-
if (relations[key]) {
|
|
4265
|
-
if (relations[key].type === "belongsTo") {
|
|
4266
|
-
const foreignKey = relations[key].options.foreignKey;
|
|
4267
|
+
if (ctx.relations[key]) {
|
|
4268
|
+
if (ctx.relations[key].type === "belongsTo") {
|
|
4269
|
+
const foreignKey = ctx.relations[key].options.foreignKey;
|
|
4267
4270
|
let columnIndex = columnsMap[foreignKey];
|
|
4268
4271
|
if (columnIndex === void 0) {
|
|
4269
4272
|
columnsMap[foreignKey] = columnIndex = columns.length;
|
|
4270
4273
|
columns.push(foreignKey);
|
|
4271
4274
|
}
|
|
4272
|
-
if (!prependRelations[key])
|
|
4273
|
-
prependRelations[key] = [];
|
|
4274
|
-
prependRelations[key].push([
|
|
4275
|
+
if (!ctx.prependRelations[key])
|
|
4276
|
+
ctx.prependRelations[key] = [];
|
|
4277
|
+
ctx.prependRelations[key].push([
|
|
4275
4278
|
rowIndex,
|
|
4276
4279
|
columnIndex,
|
|
4277
4280
|
item[key]
|
|
4278
4281
|
]);
|
|
4279
4282
|
} else {
|
|
4280
|
-
requiredReturning[relations[key].primaryKey] = true;
|
|
4281
|
-
if (!appendRelations[key])
|
|
4282
|
-
appendRelations[key] = [];
|
|
4283
|
-
appendRelations[key].push([
|
|
4283
|
+
ctx.requiredReturning[ctx.relations[key].primaryKey] = true;
|
|
4284
|
+
if (!ctx.appendRelations[key])
|
|
4285
|
+
ctx.appendRelations[key] = [];
|
|
4286
|
+
ctx.appendRelations[key].push([
|
|
4287
|
+
rowIndex,
|
|
4288
|
+
item[key]
|
|
4289
|
+
]);
|
|
4284
4290
|
}
|
|
4285
4291
|
} else if (columnsMap[key] === void 0) {
|
|
4286
4292
|
columnsMap[key] = columns.length;
|
|
@@ -4288,152 +4294,175 @@ const processInsertItem = (item, rowIndex, relations, prependRelations, appendRe
|
|
|
4288
4294
|
}
|
|
4289
4295
|
});
|
|
4290
4296
|
};
|
|
4291
|
-
|
|
4292
|
-
|
|
4293
|
-
|
|
4297
|
+
const createInsertCtx = (q) => ({
|
|
4298
|
+
prependRelations: {},
|
|
4299
|
+
appendRelations: {},
|
|
4300
|
+
requiredReturning: {},
|
|
4301
|
+
relations: q.relations
|
|
4302
|
+
});
|
|
4303
|
+
const getInsertSingleReturnType = (q) => {
|
|
4304
|
+
const { select, returnType } = q.query;
|
|
4305
|
+
if (select) {
|
|
4306
|
+
return returnType === "all" ? "one" : returnType;
|
|
4307
|
+
} else {
|
|
4308
|
+
return "rowCount";
|
|
4294
4309
|
}
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
|
|
4300
|
-
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
|
|
4305
|
-
|
|
4306
|
-
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4310
|
+
};
|
|
4311
|
+
const getInsertManyReturnType = (q) => {
|
|
4312
|
+
const { select, returnType } = q.query;
|
|
4313
|
+
if (select) {
|
|
4314
|
+
return returnType === "one" || returnType === "oneOrThrow" ? "all" : returnType;
|
|
4315
|
+
} else {
|
|
4316
|
+
return "rowCount";
|
|
4317
|
+
}
|
|
4318
|
+
};
|
|
4319
|
+
const handleInsertOneData = (q, data, ctx) => {
|
|
4320
|
+
const columns = [];
|
|
4321
|
+
const columnsMap = {};
|
|
4322
|
+
const defaults = q.query.defaults;
|
|
4323
|
+
if (defaults) {
|
|
4324
|
+
data = __spreadValues$3(__spreadValues$3({}, defaults), data);
|
|
4325
|
+
}
|
|
4326
|
+
processInsertItem(data, 0, ctx, columns, columnsMap);
|
|
4327
|
+
const values = [columns.map((key) => data[key])];
|
|
4328
|
+
return { columns, values };
|
|
4329
|
+
};
|
|
4330
|
+
const handleInsertManyData = (q, data, ctx) => {
|
|
4331
|
+
const columns = [];
|
|
4332
|
+
const columnsMap = {};
|
|
4333
|
+
const defaults = q.query.defaults;
|
|
4334
|
+
if (defaults) {
|
|
4335
|
+
data = data.map((item) => __spreadValues$3(__spreadValues$3({}, defaults), item));
|
|
4336
|
+
}
|
|
4337
|
+
data.forEach((item, i) => {
|
|
4338
|
+
processInsertItem(item, i, ctx, columns, columnsMap);
|
|
4339
|
+
});
|
|
4340
|
+
const values = Array(data.length);
|
|
4341
|
+
data.forEach((item, i) => {
|
|
4342
|
+
values[i] = columns.map((key) => item[key]);
|
|
4343
|
+
});
|
|
4344
|
+
return { columns, values };
|
|
4345
|
+
};
|
|
4346
|
+
const insert = (self, {
|
|
4347
|
+
columns,
|
|
4348
|
+
values
|
|
4349
|
+
}, returnType, ctx) => {
|
|
4350
|
+
const q = self;
|
|
4351
|
+
const returning = q.query.select;
|
|
4352
|
+
delete q.query.and;
|
|
4353
|
+
delete q.query.or;
|
|
4354
|
+
q.query.type = "insert";
|
|
4355
|
+
q.query.columns = columns;
|
|
4356
|
+
q.query.values = values;
|
|
4357
|
+
if (!ctx) {
|
|
4358
|
+
q.query.returnType = returnType;
|
|
4359
|
+
return q;
|
|
4360
|
+
}
|
|
4361
|
+
const prependRelationsKeys = Object.keys(ctx.prependRelations);
|
|
4362
|
+
if (prependRelationsKeys.length) {
|
|
4363
|
+
pushQueryArray(
|
|
4364
|
+
q,
|
|
4365
|
+
"beforeQuery",
|
|
4366
|
+
prependRelationsKeys.map((relationName) => {
|
|
4367
|
+
return async (q2) => {
|
|
4368
|
+
const relationData = ctx.prependRelations[relationName];
|
|
4369
|
+
const relation = ctx.relations[relationName];
|
|
4370
|
+
const inserted = await relation.nestedInsert(
|
|
4371
|
+
q2,
|
|
4372
|
+
relationData.map(([, , data]) => data)
|
|
4373
|
+
);
|
|
4374
|
+
const primaryKey = relation.options.primaryKey;
|
|
4375
|
+
relationData.forEach(([rowIndex, columnIndex], index) => {
|
|
4376
|
+
values[rowIndex][columnIndex] = inserted[index][primaryKey];
|
|
4377
|
+
});
|
|
4378
|
+
};
|
|
4379
|
+
})
|
|
4380
|
+
);
|
|
4381
|
+
}
|
|
4382
|
+
const appendRelationsKeys = Object.keys(ctx.appendRelations);
|
|
4383
|
+
if (appendRelationsKeys.length) {
|
|
4384
|
+
if (!(returning == null ? void 0 : returning.includes("*"))) {
|
|
4385
|
+
const requiredColumns = Object.keys(ctx.requiredReturning);
|
|
4386
|
+
if (!returning) {
|
|
4387
|
+
q.query.select = requiredColumns;
|
|
4312
4388
|
} else {
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4389
|
+
q.query.select = [
|
|
4390
|
+
.../* @__PURE__ */ new Set([...returning, ...requiredColumns])
|
|
4391
|
+
];
|
|
4316
4392
|
}
|
|
4317
|
-
} else {
|
|
4318
|
-
returnType = "rowCount";
|
|
4319
4393
|
}
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
|
|
4325
|
-
|
|
4326
|
-
|
|
4327
|
-
|
|
4328
|
-
if (defaults) {
|
|
4329
|
-
data = data.map((item) => __spreadValues$3(__spreadValues$3({}, defaults), item));
|
|
4330
|
-
}
|
|
4331
|
-
data.forEach((item, i) => {
|
|
4332
|
-
processInsertItem(
|
|
4333
|
-
item,
|
|
4334
|
-
i,
|
|
4335
|
-
relations,
|
|
4336
|
-
prependRelations,
|
|
4337
|
-
appendRelations,
|
|
4338
|
-
requiredReturning,
|
|
4339
|
-
columns,
|
|
4340
|
-
columnsMap
|
|
4394
|
+
let resultOfTypeAll;
|
|
4395
|
+
if (returnType !== "all") {
|
|
4396
|
+
const { handleResult } = q.query;
|
|
4397
|
+
q.query.handleResult = async (q2, queryResult) => {
|
|
4398
|
+
resultOfTypeAll = await handleResult(q2, queryResult);
|
|
4399
|
+
if (queryMethodByReturnType[returnType] === "arrays") {
|
|
4400
|
+
queryResult.rows.forEach(
|
|
4401
|
+
(row, i) => queryResult.rows[i] = Object.values(row)
|
|
4341
4402
|
);
|
|
4342
|
-
});
|
|
4343
|
-
values = Array(data.length);
|
|
4344
|
-
data.forEach((item, i) => {
|
|
4345
|
-
values[i] = columns.map((key) => item[key]);
|
|
4346
|
-
});
|
|
4347
|
-
} else {
|
|
4348
|
-
if (defaults) {
|
|
4349
|
-
data = __spreadValues$3(__spreadValues$3({}, defaults), data);
|
|
4350
4403
|
}
|
|
4351
|
-
|
|
4352
|
-
|
|
4353
|
-
0,
|
|
4354
|
-
relations,
|
|
4355
|
-
prependRelations,
|
|
4356
|
-
appendRelations,
|
|
4357
|
-
requiredReturning,
|
|
4358
|
-
columns,
|
|
4359
|
-
columnsMap
|
|
4360
|
-
);
|
|
4361
|
-
values = [columns.map((key) => data[key])];
|
|
4362
|
-
}
|
|
4363
|
-
}
|
|
4364
|
-
const prependRelationsKeys = Object.keys(prependRelations);
|
|
4365
|
-
if (prependRelationsKeys.length) {
|
|
4366
|
-
pushQueryArray(
|
|
4367
|
-
q,
|
|
4368
|
-
"beforeQuery",
|
|
4369
|
-
prependRelationsKeys.map((relationName) => {
|
|
4370
|
-
return async (q2) => {
|
|
4371
|
-
const relationData = prependRelations[relationName];
|
|
4372
|
-
const relation = relations[relationName];
|
|
4373
|
-
const inserted = await relation.nestedInsert(
|
|
4374
|
-
q2,
|
|
4375
|
-
relationData.map(([, , data2]) => data2)
|
|
4376
|
-
);
|
|
4377
|
-
const primaryKey = relation.options.primaryKey;
|
|
4378
|
-
relationData.forEach(([rowIndex, columnIndex], index) => {
|
|
4379
|
-
values[rowIndex][columnIndex] = inserted[index][primaryKey];
|
|
4380
|
-
});
|
|
4381
|
-
};
|
|
4382
|
-
})
|
|
4383
|
-
);
|
|
4404
|
+
return parseResult(q2, returnType, queryResult);
|
|
4405
|
+
};
|
|
4384
4406
|
}
|
|
4385
|
-
|
|
4386
|
-
|
|
4387
|
-
|
|
4388
|
-
|
|
4389
|
-
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
|
|
4396
|
-
|
|
4397
|
-
|
|
4398
|
-
|
|
4399
|
-
|
|
4400
|
-
q.query.handleResult = async (q2, queryResult) => {
|
|
4401
|
-
resultOfTypeAll = await handleResult(q2, queryResult);
|
|
4402
|
-
if (queryMethodByReturnType[returnType] === "arrays") {
|
|
4403
|
-
queryResult.rows.forEach(
|
|
4404
|
-
(row, i) => queryResult.rows[i] = Object.values(row)
|
|
4405
|
-
);
|
|
4406
|
-
}
|
|
4407
|
-
return parseResult(q2, returnType, queryResult);
|
|
4407
|
+
pushQueryArray(
|
|
4408
|
+
q,
|
|
4409
|
+
"afterQuery",
|
|
4410
|
+
appendRelationsKeys.map((relationName) => {
|
|
4411
|
+
return (q2, result) => {
|
|
4412
|
+
var _a, _b;
|
|
4413
|
+
const all = resultOfTypeAll || result;
|
|
4414
|
+
return (_b = (_a = ctx.relations[relationName]).nestedInsert) == null ? void 0 : _b.call(
|
|
4415
|
+
_a,
|
|
4416
|
+
q2,
|
|
4417
|
+
ctx.appendRelations[relationName].map(([rowIndex, data]) => [
|
|
4418
|
+
all[rowIndex],
|
|
4419
|
+
data
|
|
4420
|
+
])
|
|
4421
|
+
);
|
|
4408
4422
|
};
|
|
4409
|
-
}
|
|
4410
|
-
|
|
4411
|
-
|
|
4412
|
-
|
|
4413
|
-
|
|
4414
|
-
|
|
4415
|
-
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4420
|
-
|
|
4421
|
-
|
|
4422
|
-
|
|
4423
|
-
|
|
4424
|
-
|
|
4425
|
-
|
|
4426
|
-
|
|
4427
|
-
)
|
|
4428
|
-
|
|
4429
|
-
|
|
4430
|
-
|
|
4431
|
-
|
|
4432
|
-
|
|
4433
|
-
|
|
4434
|
-
|
|
4435
|
-
|
|
4436
|
-
return
|
|
4423
|
+
})
|
|
4424
|
+
);
|
|
4425
|
+
}
|
|
4426
|
+
if (prependRelationsKeys.length || appendRelationsKeys.length) {
|
|
4427
|
+
q.query.wrapInTransaction = true;
|
|
4428
|
+
}
|
|
4429
|
+
q.query.returnType = appendRelationsKeys.length ? "all" : returnType;
|
|
4430
|
+
return q;
|
|
4431
|
+
};
|
|
4432
|
+
class Insert {
|
|
4433
|
+
insert(data) {
|
|
4434
|
+
return this.clone()._insert(data);
|
|
4435
|
+
}
|
|
4436
|
+
_insert(data) {
|
|
4437
|
+
const ctx = createInsertCtx(this);
|
|
4438
|
+
return insert(
|
|
4439
|
+
this,
|
|
4440
|
+
handleInsertOneData(this, data, ctx),
|
|
4441
|
+
getInsertSingleReturnType(this),
|
|
4442
|
+
ctx
|
|
4443
|
+
);
|
|
4444
|
+
}
|
|
4445
|
+
insertMany(data) {
|
|
4446
|
+
return this.clone()._insertMany(data);
|
|
4447
|
+
}
|
|
4448
|
+
_insertMany(data) {
|
|
4449
|
+
const ctx = createInsertCtx(this);
|
|
4450
|
+
return insert(
|
|
4451
|
+
this,
|
|
4452
|
+
handleInsertManyData(this, data, ctx),
|
|
4453
|
+
getInsertManyReturnType(this),
|
|
4454
|
+
ctx
|
|
4455
|
+
);
|
|
4456
|
+
}
|
|
4457
|
+
insertRaw(data) {
|
|
4458
|
+
return this.clone()._insertRaw(data);
|
|
4459
|
+
}
|
|
4460
|
+
_insertRaw(data) {
|
|
4461
|
+
return insert(
|
|
4462
|
+
this,
|
|
4463
|
+
data,
|
|
4464
|
+
getInsertManyReturnType(this)
|
|
4465
|
+
);
|
|
4437
4466
|
}
|
|
4438
4467
|
create(data) {
|
|
4439
4468
|
return this.clone()._create(data);
|
|
@@ -4442,7 +4471,25 @@ class Insert {
|
|
|
4442
4471
|
if (!this.query.select) {
|
|
4443
4472
|
this.query.select = ["*"];
|
|
4444
4473
|
}
|
|
4445
|
-
return this.
|
|
4474
|
+
return this.clone()._insert(data);
|
|
4475
|
+
}
|
|
4476
|
+
createMany(data) {
|
|
4477
|
+
return this.clone()._createMany(data);
|
|
4478
|
+
}
|
|
4479
|
+
_createMany(data) {
|
|
4480
|
+
if (!this.query.select) {
|
|
4481
|
+
this.query.select = ["*"];
|
|
4482
|
+
}
|
|
4483
|
+
return this.clone()._insertMany(data);
|
|
4484
|
+
}
|
|
4485
|
+
createRaw(data) {
|
|
4486
|
+
return this.clone()._createRaw(data);
|
|
4487
|
+
}
|
|
4488
|
+
_createRaw(data) {
|
|
4489
|
+
if (!this.query.select) {
|
|
4490
|
+
this.query.select = ["*"];
|
|
4491
|
+
}
|
|
4492
|
+
return this.clone()._insertRaw(data);
|
|
4446
4493
|
}
|
|
4447
4494
|
defaults(data) {
|
|
4448
4495
|
return this.clone()._defaults(data);
|