pqb 0.4.7 → 0.4.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.js CHANGED
@@ -70,6 +70,9 @@ class ColumnType {
70
70
  this.parseItem = fn;
71
71
  return this;
72
72
  }
73
+ as(column) {
74
+ return addColumnData(this, "as", column);
75
+ }
73
76
  toSQL() {
74
77
  return this.dataType;
75
78
  }
@@ -1953,6 +1956,14 @@ class DateBaseColumn extends ColumnType {
1953
1956
  this.data = {};
1954
1957
  this.operators = Operators.date;
1955
1958
  }
1959
+ asNumber() {
1960
+ return this.encode((input) => new Date(input)).parse(
1961
+ Date.parse
1962
+ );
1963
+ }
1964
+ asDate() {
1965
+ return this.parse((input) => new Date(input));
1966
+ }
1956
1967
  }
1957
1968
  assignMethodsToClass(DateBaseColumn, dateTypeMethods);
1958
1969
  class DateColumn extends DateBaseColumn {
@@ -4178,8 +4189,9 @@ const handleSelect = (q) => {
4178
4189
  q.query.select = ["*"];
4179
4190
  }
4180
4191
  };
4181
- const processCreateItem = (item, rowIndex, ctx, columns, columnsMap) => {
4192
+ const processCreateItem = (item, rowIndex, ctx, columns, encoders, columnsMap, shape) => {
4182
4193
  Object.keys(item).forEach((key) => {
4194
+ var _a;
4183
4195
  if (ctx.relations[key]) {
4184
4196
  if (ctx.relations[key].type === "belongsTo") {
4185
4197
  const foreignKey = ctx.relations[key].options.foreignKey;
@@ -4204,8 +4216,9 @@ const processCreateItem = (item, rowIndex, ctx, columns, columnsMap) => {
4204
4216
  item[key]
4205
4217
  ]);
4206
4218
  }
4207
- } else if (columnsMap[key] === void 0) {
4219
+ } else if (columnsMap[key] === void 0 && (shape[key] || shape === anyShape)) {
4208
4220
  columnsMap[key] = columns.length;
4221
+ encoders[key] = (_a = shape[key]) == null ? void 0 : _a.encodeFn;
4209
4222
  columns.push(key);
4210
4223
  }
4211
4224
  });
@@ -4232,30 +4245,37 @@ const getManyReturnType = (q) => {
4232
4245
  return "rowCount";
4233
4246
  }
4234
4247
  };
4248
+ const mapColumnValues = (columns, encoders, data) => {
4249
+ return columns.map(
4250
+ (key) => encoders[key] ? encoders[key](data[key]) : data[key]
4251
+ );
4252
+ };
4235
4253
  const handleOneData = (q, data, ctx) => {
4236
4254
  const columns = [];
4255
+ const encoders = {};
4237
4256
  const columnsMap = {};
4238
4257
  const defaults = q.query.defaults;
4239
4258
  if (defaults) {
4240
4259
  data = __spreadValues$4(__spreadValues$4({}, defaults), data);
4241
4260
  }
4242
- processCreateItem(data, 0, ctx, columns, columnsMap);
4243
- const values = [columns.map((key) => data[key])];
4261
+ processCreateItem(data, 0, ctx, columns, encoders, columnsMap, q.shape);
4262
+ const values = [mapColumnValues(columns, encoders, data)];
4244
4263
  return { columns, values };
4245
4264
  };
4246
4265
  const handleManyData = (q, data, ctx) => {
4247
4266
  const columns = [];
4267
+ const encoders = {};
4248
4268
  const columnsMap = {};
4249
4269
  const defaults = q.query.defaults;
4250
4270
  if (defaults) {
4251
4271
  data = data.map((item) => __spreadValues$4(__spreadValues$4({}, defaults), item));
4252
4272
  }
4253
4273
  data.forEach((item, i) => {
4254
- processCreateItem(item, i, ctx, columns, columnsMap);
4274
+ processCreateItem(item, i, ctx, columns, encoders, columnsMap, q.shape);
4255
4275
  });
4256
4276
  const values = Array(data.length);
4257
4277
  data.forEach((item, i) => {
4258
- values[i] = columns.map((key) => item[key]);
4278
+ values[i] = mapColumnValues(columns, encoders, item);
4259
4279
  });
4260
4280
  return { columns, values };
4261
4281
  };
@@ -5127,7 +5147,7 @@ class Update {
5127
5147
  const data = args[0];
5128
5148
  const set = __spreadValues$1({}, data);
5129
5149
  pushQueryValue(this, "updateData", set);
5130
- const relations = this.relations;
5150
+ const { relations, shape } = this;
5131
5151
  const prependRelations = {};
5132
5152
  const appendRelations = {};
5133
5153
  const originalReturnType = query.returnType || "all";
@@ -5145,6 +5165,12 @@ class Update {
5145
5165
  }
5146
5166
  appendRelations[key] = data[key];
5147
5167
  }
5168
+ } else if (!shape[key] && shape !== anyShape) {
5169
+ delete set[key];
5170
+ } else {
5171
+ const encode = shape[key].encodeFn;
5172
+ if (encode)
5173
+ set[key] = encode(set[key]);
5148
5174
  }
5149
5175
  }
5150
5176
  const state = {};
@@ -5609,8 +5635,9 @@ var __objRest = (source, exclude) => {
5609
5635
  }
5610
5636
  return target;
5611
5637
  };
5638
+ const anyShape = {};
5612
5639
  class Db {
5613
- constructor(adapter, queryBuilder, table = void 0, shape = {}, columnTypes, options) {
5640
+ constructor(adapter, queryBuilder, table = void 0, shape = anyShape, columnTypes, options) {
5614
5641
  this.adapter = adapter;
5615
5642
  this.queryBuilder = queryBuilder;
5616
5643
  this.table = table;
@@ -5818,6 +5845,7 @@ exports.addWhere = addWhere;
5818
5845
  exports.addWhereIn = addWhereIn;
5819
5846
  exports.addWhereNot = addWhereNot;
5820
5847
  exports.aggregate1FunctionNames = aggregate1FunctionNames;
5848
+ exports.anyShape = anyShape;
5821
5849
  exports.applyMixins = applyMixins;
5822
5850
  exports.array = array;
5823
5851
  exports.arrayToEnum = arrayToEnum;