pqb 0.4.0 → 0.4.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/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
- `INSERT INTO ${quotedAs}(${quotedColumns.join(", ")}) VALUES ${isRaw(query.values) ? getRaw(query.values, ctx.values) : query.values.map(
1128
- (row) => `(${row.map(
1129
- (value) => value === void 0 ? "DEFAULT" : addValue(ctx.values, value)
1130
- ).join(", ")})`
1131
- ).join(", ")}`
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.beforeInsert;
3247
- afterCallbacks = q.query.afterInsert;
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(this._selectCount(arg, options));
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
- beforeInsert(cb) {
3855
- return this.clone()._beforeInsert(cb);
3868
+ beforeCreate(cb) {
3869
+ return this.clone()._beforeCreate(cb);
3856
3870
  }
3857
- _beforeInsert(cb) {
3858
- return pushQueryValue(this, "beforeInsert", cb);
3871
+ _beforeCreate(cb) {
3872
+ return pushQueryValue(this, "beforeCreate", cb);
3859
3873
  }
3860
- afterInsert(cb) {
3861
- return this.clone()._afterInsert(cb);
3874
+ afterCreate(cb) {
3875
+ return this.clone()._afterCreate(cb);
3862
3876
  }
3863
- _afterInsert(cb) {
3864
- return pushQueryValue(this, "afterInsert", cb);
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.then = function(resolve, reject) {
3948
- new Then().then.call(
3949
- this,
3950
- (rows) => {
3951
- if (column) {
3952
- resolve == null ? void 0 : resolve(rowToColumnInfo(rows[0]));
3953
- } else {
3954
- const info = {};
3955
- rows.forEach((row) => {
3956
- info[row.column_name] = rowToColumnInfo(row);
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 processInsertItem = (item, rowIndex, ctx, columns, columnsMap) => {
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 createInsertCtx = (q) => ({
4210
+ const createCtx = (q) => ({
4193
4211
  prependRelations: {},
4194
4212
  appendRelations: {},
4195
4213
  requiredReturning: {},
4196
4214
  relations: q.relations
4197
4215
  });
4198
- const getInsertSingleReturnType = (q) => {
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 getInsertManyReturnType = (q) => {
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 handleInsertOneData = (q, data, ctx) => {
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
- processInsertItem(data, 0, ctx, columns, columnsMap);
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 handleInsertManyData = (q, data, ctx) => {
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
- processInsertItem(item, i, ctx, columns, columnsMap);
4251
+ processCreateItem(item, i, ctx, columns, columnsMap);
4234
4252
  });
4235
4253
  const values = Array(data.length);
4236
4254
  data.forEach((item, i) => {
@@ -4241,7 +4259,7 @@ const handleInsertManyData = (q, data, ctx) => {
4241
4259
  const insert = (self, {
4242
4260
  columns,
4243
4261
  values
4244
- }, returnType, ctx) => {
4262
+ }, returnType, ctx, fromQuery) => {
4245
4263
  const q = self;
4246
4264
  const returning = q.query.select;
4247
4265
  delete q.query.and;
@@ -4249,6 +4267,7 @@ const insert = (self, {
4249
4267
  q.query.type = "insert";
4250
4268
  q.query.columns = columns;
4251
4269
  q.query.values = values;
4270
+ q.query.fromQuery = fromQuery;
4252
4271
  if (!ctx) {
4253
4272
  q.query.returnType = returnType;
4254
4273
  return q;
@@ -4324,67 +4343,72 @@ const insert = (self, {
4324
4343
  q.query.returnType = appendRelationsKeys.length ? "all" : returnType;
4325
4344
  return q;
4326
4345
  };
4327
- class Insert {
4328
- insert(data) {
4329
- return this.clone()._insert(data);
4346
+ class Create {
4347
+ create(data) {
4348
+ return this.clone()._create(data);
4330
4349
  }
4331
- _insert(data) {
4332
- const ctx = createInsertCtx(this);
4350
+ _create(data) {
4351
+ handleSelect(this);
4352
+ const ctx = createCtx(this);
4333
4353
  return insert(
4334
4354
  this,
4335
- handleInsertOneData(this, data, ctx),
4336
- getInsertSingleReturnType(this),
4355
+ handleOneData(this, data, ctx),
4356
+ getSingleReturnType(this),
4337
4357
  ctx
4338
4358
  );
4339
4359
  }
4340
- insertMany(data) {
4341
- return this.clone()._insertMany(data);
4360
+ createMany(data) {
4361
+ return this.clone()._createMany(data);
4342
4362
  }
4343
- _insertMany(data) {
4344
- const ctx = createInsertCtx(this);
4363
+ _createMany(data) {
4364
+ handleSelect(this);
4365
+ const ctx = createCtx(this);
4345
4366
  return insert(
4346
4367
  this,
4347
- handleInsertManyData(this, data, ctx),
4348
- getInsertManyReturnType(this),
4368
+ handleManyData(this, data, ctx),
4369
+ getManyReturnType(this),
4349
4370
  ctx
4350
4371
  );
4351
4372
  }
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
4373
  createRaw(data) {
4381
4374
  return this.clone()._createRaw(data);
4382
4375
  }
4383
4376
  _createRaw(data) {
4377
+ handleSelect(this);
4378
+ return insert(this, data, getManyReturnType(this));
4379
+ }
4380
+ createFrom(query, data) {
4381
+ return this.clone()._createFrom(query, data);
4382
+ }
4383
+ _createFrom(query, data) {
4384
+ var _a;
4385
+ if (!queryTypeWithLimitOne[query.query.returnType]) {
4386
+ throw new Error(
4387
+ "createFrom accepts only a query which returns one record"
4388
+ );
4389
+ }
4384
4390
  if (!this.query.select) {
4385
4391
  this.query.select = ["*"];
4386
4392
  }
4387
- return this.clone()._insertRaw(data);
4393
+ const ctx = createCtx(this);
4394
+ const queryColumns = [];
4395
+ (_a = query.query.select) == null ? void 0 : _a.forEach((item) => {
4396
+ if (typeof item === "string") {
4397
+ const index = item.indexOf(".");
4398
+ queryColumns.push(index === -1 ? item : item.slice(index + 1));
4399
+ } else if ("selectAs" in item) {
4400
+ queryColumns.push(...Object.keys(item.selectAs));
4401
+ }
4402
+ });
4403
+ const { columns, values } = handleOneData(this, data, ctx);
4404
+ queryColumns.push(...columns);
4405
+ return insert(
4406
+ this,
4407
+ { columns: queryColumns, values },
4408
+ "one",
4409
+ ctx,
4410
+ query
4411
+ );
4388
4412
  }
4389
4413
  defaults(data) {
4390
4414
  return this.clone()._defaults(data);
@@ -5327,7 +5351,7 @@ class QueryUpsert {
5327
5351
  const { handleResult } = this.query;
5328
5352
  this.query.handleResult = async (q, queryResult) => {
5329
5353
  if (queryResult.rowCount === 0) {
5330
- return q.insert(data.create);
5354
+ return q.create(data.create);
5331
5355
  } else if (queryResult.rowCount > 1) {
5332
5356
  throw new MoreThanOneRowError(
5333
5357
  `Only one row was expected to find for upsert, found ${queryResult.rowCount} rows.`
@@ -5513,7 +5537,7 @@ applyMixins(QueryMethods, [
5513
5537
  With,
5514
5538
  Union,
5515
5539
  Json,
5516
- Insert,
5540
+ Create,
5517
5541
  Update,
5518
5542
  Delete,
5519
5543
  Transaction,
@@ -5682,6 +5706,7 @@ exports.Clear = Clear;
5682
5706
  exports.ColumnInfoMethods = ColumnInfoMethods;
5683
5707
  exports.ColumnType = ColumnType;
5684
5708
  exports.ColumnsObject = ColumnsObject;
5709
+ exports.Create = Create;
5685
5710
  exports.DateBaseColumn = DateBaseColumn;
5686
5711
  exports.DateColumn = DateColumn;
5687
5712
  exports.DateTimeBaseClass = DateTimeBaseClass;
@@ -5697,7 +5722,6 @@ exports.For = For;
5697
5722
  exports.From = From;
5698
5723
  exports.Having = Having;
5699
5724
  exports.InetColumn = InetColumn;
5700
- exports.Insert = Insert;
5701
5725
  exports.IntegerBaseColumn = IntegerBaseColumn;
5702
5726
  exports.IntegerColumn = IntegerColumn;
5703
5727
  exports.IntervalColumn = IntervalColumn;