pqb 0.4.6 → 0.4.8

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.
Files changed (53) hide show
  1. package/dist/index.d.ts +25 -11
  2. package/dist/index.esm.js +78 -40
  3. package/dist/index.esm.js.map +1 -1
  4. package/dist/index.js +79 -39
  5. package/dist/index.js.map +1 -1
  6. package/package.json +1 -1
  7. package/src/columnSchema/columnType.test.ts +220 -0
  8. package/src/columnSchema/columnType.ts +7 -0
  9. package/src/columnSchema/columnTypes.test.ts +55 -53
  10. package/src/columnSchema/dateTime.ts +11 -0
  11. package/src/columnSchema/timestamps.test.ts +3 -4
  12. package/src/columnSchema/timestamps.ts +1 -1
  13. package/src/columnsOperators.test.ts +18 -19
  14. package/src/columnsOperators.ts +1 -1
  15. package/src/common.ts +17 -30
  16. package/src/db.ts +23 -9
  17. package/src/errors.test.ts +2 -4
  18. package/src/index.ts +1 -1
  19. package/src/query.ts +7 -1
  20. package/src/queryMethods/aggregate.test.ts +9 -7
  21. package/src/queryMethods/create.test.ts +48 -5
  22. package/src/queryMethods/create.ts +28 -6
  23. package/src/queryMethods/for.test.ts +1 -2
  24. package/src/queryMethods/for.ts +1 -1
  25. package/src/queryMethods/from.test.ts +2 -3
  26. package/src/queryMethods/get.test.ts +5 -5
  27. package/src/queryMethods/having.test.ts +6 -5
  28. package/src/queryMethods/index.ts +1 -0
  29. package/src/queryMethods/json.ts +2 -2
  30. package/src/queryMethods/merge.test.ts +10 -4
  31. package/src/queryMethods/queryMethods.test.ts +10 -12
  32. package/src/queryMethods/queryMethods.ts +7 -4
  33. package/src/queryMethods/raw.test.ts +19 -0
  34. package/src/queryMethods/raw.ts +27 -0
  35. package/src/queryMethods/select.test.ts +4 -4
  36. package/src/queryMethods/then.test.ts +2 -4
  37. package/src/queryMethods/union.test.ts +11 -6
  38. package/src/queryMethods/update.test.ts +21 -3
  39. package/src/queryMethods/update.ts +11 -1
  40. package/src/queryMethods/where.test.ts +65 -56
  41. package/src/queryMethods/where.ts +1 -1
  42. package/src/queryMethods/with.test.ts +5 -6
  43. package/src/sql/common.ts +0 -1
  44. package/src/sql/fromAndAs.ts +1 -1
  45. package/src/sql/insert.ts +1 -1
  46. package/src/sql/join.ts +1 -1
  47. package/src/sql/orderBy.ts +1 -1
  48. package/src/sql/select.ts +1 -2
  49. package/src/sql/toSql.ts +1 -1
  50. package/src/sql/update.ts +1 -1
  51. package/src/sql/where.ts +1 -1
  52. package/src/sql/window.ts +3 -4
  53. package/src/sql/with.ts +1 -1
package/dist/index.js CHANGED
@@ -70,6 +70,9 @@ class ColumnType {
70
70
  this.parseItem = fn;
71
71
  return this;
72
72
  }
73
+ as(_) {
74
+ return this;
75
+ }
73
76
  toSQL() {
74
77
  return this.dataType;
75
78
  }
@@ -119,33 +122,6 @@ class ColumnType {
119
122
  }
120
123
  }
121
124
 
122
- const raw = (...args) => {
123
- if (typeof args[0] === "string") {
124
- return {
125
- __raw: args[0],
126
- __values: args.slice(1)
127
- };
128
- } else {
129
- return {
130
- __column: args[0],
131
- __raw: args[1],
132
- __values: args.slice(2)
133
- };
134
- }
135
- };
136
- const isRaw = (obj) => "__raw" in obj;
137
- const getRaw = (raw2, values) => {
138
- values.push(...raw2.__values);
139
- return raw2.__raw;
140
- };
141
- const getRawSql = (raw2) => {
142
- return raw2.__raw;
143
- };
144
- const EMPTY_OBJECT = {};
145
- const getQueryParsers = (q) => {
146
- return q.query.parsers || q.columnsParsers;
147
- };
148
-
149
125
  const singleQuoteRegex = /'/g;
150
126
  const doubleQuoteRegex = /"/g;
151
127
  const quoteValue$1 = (value) => {
@@ -184,6 +160,23 @@ const quote = (value) => {
184
160
  return `'${JSON.stringify(value).replace(singleQuoteRegex, "''")}'`;
185
161
  };
186
162
 
163
+ const raw = (sql, ...values) => ({
164
+ __raw: sql,
165
+ __values: values
166
+ });
167
+ const isRaw = (obj) => "__raw" in obj;
168
+ const getRaw = (raw2, values) => {
169
+ values.push(...raw2.__values);
170
+ return raw2.__raw;
171
+ };
172
+ const getRawSql = (raw2) => {
173
+ return raw2.__raw;
174
+ };
175
+ const EMPTY_OBJECT = {};
176
+ const getQueryParsers = (q) => {
177
+ return q.query.parsers || q.columnsParsers;
178
+ };
179
+
187
180
  const q = (sql) => `"${sql}"`;
188
181
  const qc = (column, quotedAs) => quotedAs ? `${quotedAs}.${q(column)}` : column;
189
182
  const quoteFullColumn = (fullColumn, quotedAs) => {
@@ -1963,6 +1956,14 @@ class DateBaseColumn extends ColumnType {
1963
1956
  this.data = {};
1964
1957
  this.operators = Operators.date;
1965
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
+ }
1966
1967
  }
1967
1968
  assignMethodsToClass(DateBaseColumn, dateTypeMethods);
1968
1969
  class DateColumn extends DateBaseColumn {
@@ -4188,8 +4189,9 @@ const handleSelect = (q) => {
4188
4189
  q.query.select = ["*"];
4189
4190
  }
4190
4191
  };
4191
- const processCreateItem = (item, rowIndex, ctx, columns, columnsMap) => {
4192
+ const processCreateItem = (item, rowIndex, ctx, columns, encoders, columnsMap, shape) => {
4192
4193
  Object.keys(item).forEach((key) => {
4194
+ var _a;
4193
4195
  if (ctx.relations[key]) {
4194
4196
  if (ctx.relations[key].type === "belongsTo") {
4195
4197
  const foreignKey = ctx.relations[key].options.foreignKey;
@@ -4214,8 +4216,9 @@ const processCreateItem = (item, rowIndex, ctx, columns, columnsMap) => {
4214
4216
  item[key]
4215
4217
  ]);
4216
4218
  }
4217
- } else if (columnsMap[key] === void 0) {
4219
+ } else if (columnsMap[key] === void 0 && (shape[key] || shape === anyShape)) {
4218
4220
  columnsMap[key] = columns.length;
4221
+ encoders[key] = (_a = shape[key]) == null ? void 0 : _a.encodeFn;
4219
4222
  columns.push(key);
4220
4223
  }
4221
4224
  });
@@ -4242,30 +4245,37 @@ const getManyReturnType = (q) => {
4242
4245
  return "rowCount";
4243
4246
  }
4244
4247
  };
4248
+ const mapColumnValues = (columns, encoders, data) => {
4249
+ return columns.map(
4250
+ (key) => encoders[key] ? encoders[key](data[key]) : data[key]
4251
+ );
4252
+ };
4245
4253
  const handleOneData = (q, data, ctx) => {
4246
4254
  const columns = [];
4255
+ const encoders = {};
4247
4256
  const columnsMap = {};
4248
4257
  const defaults = q.query.defaults;
4249
4258
  if (defaults) {
4250
4259
  data = __spreadValues$4(__spreadValues$4({}, defaults), data);
4251
4260
  }
4252
- processCreateItem(data, 0, ctx, columns, columnsMap);
4253
- const values = [columns.map((key) => data[key])];
4261
+ processCreateItem(data, 0, ctx, columns, encoders, columnsMap, q.shape);
4262
+ const values = [mapColumnValues(columns, encoders, data)];
4254
4263
  return { columns, values };
4255
4264
  };
4256
4265
  const handleManyData = (q, data, ctx) => {
4257
4266
  const columns = [];
4267
+ const encoders = {};
4258
4268
  const columnsMap = {};
4259
4269
  const defaults = q.query.defaults;
4260
4270
  if (defaults) {
4261
4271
  data = data.map((item) => __spreadValues$4(__spreadValues$4({}, defaults), item));
4262
4272
  }
4263
4273
  data.forEach((item, i) => {
4264
- processCreateItem(item, i, ctx, columns, columnsMap);
4274
+ processCreateItem(item, i, ctx, columns, encoders, columnsMap, q.shape);
4265
4275
  });
4266
4276
  const values = Array(data.length);
4267
4277
  data.forEach((item, i) => {
4268
- values[i] = columns.map((key) => item[key]);
4278
+ values[i] = mapColumnValues(columns, encoders, item);
4269
4279
  });
4270
4280
  return { columns, values };
4271
4281
  };
@@ -4766,7 +4776,7 @@ class Json {
4766
4776
  _json() {
4767
4777
  const q = this._wrap(this.__model.clone());
4768
4778
  q._getOptional(
4769
- raw(
4779
+ this.raw(
4770
4780
  queryTypeWithLimitOne[this.query.returnType] ? `row_to_json("t".*)` : `COALESCE(json_agg(row_to_json("t".*)), '[]')`
4771
4781
  )
4772
4782
  );
@@ -5137,7 +5147,7 @@ class Update {
5137
5147
  const data = args[0];
5138
5148
  const set = __spreadValues$1({}, data);
5139
5149
  pushQueryValue(this, "updateData", set);
5140
- const relations = this.relations;
5150
+ const { relations, shape } = this;
5141
5151
  const prependRelations = {};
5142
5152
  const appendRelations = {};
5143
5153
  const originalReturnType = query.returnType || "all";
@@ -5155,6 +5165,12 @@ class Update {
5155
5165
  }
5156
5166
  appendRelations[key] = data[key];
5157
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]);
5158
5174
  }
5159
5175
  }
5160
5176
  const state = {};
@@ -5381,6 +5397,23 @@ class QueryUpsert {
5381
5397
  }
5382
5398
  }
5383
5399
 
5400
+ class RawMethods {
5401
+ raw(...args) {
5402
+ if (typeof args[0] === "string") {
5403
+ return {
5404
+ __raw: args[0],
5405
+ __values: args.slice(1)
5406
+ };
5407
+ } else {
5408
+ return {
5409
+ __column: args[0](this.columnTypes),
5410
+ __raw: args[1],
5411
+ __values: args.slice(2)
5412
+ };
5413
+ }
5414
+ }
5415
+ }
5416
+
5384
5417
  class QueryMethods {
5385
5418
  all() {
5386
5419
  return this.clone()._all();
@@ -5500,7 +5533,7 @@ class QueryMethods {
5500
5533
  _wrap(query, as) {
5501
5534
  const sql = this.toSql();
5502
5535
  return query.as(as != null ? as : "t")._from(
5503
- raw(`(${sql.text})`, ...sql.values)
5536
+ this.raw(`(${sql.text})`, ...sql.values)
5504
5537
  );
5505
5538
  }
5506
5539
  order(...args) {
@@ -5527,7 +5560,7 @@ class QueryMethods {
5527
5560
  return this.clone()._exists();
5528
5561
  }
5529
5562
  _exists() {
5530
- const q = this._getOptional(raw("true"));
5563
+ const q = this._getOptional(this.raw("true"));
5531
5564
  q.query.notFoundDefault = false;
5532
5565
  q.query.coalesceValue = false;
5533
5566
  return q;
@@ -5570,7 +5603,8 @@ applyMixins(QueryMethods, [
5570
5603
  QueryCallbacks,
5571
5604
  QueryUpsert,
5572
5605
  QueryGet,
5573
- MergeQueryMethods
5606
+ MergeQueryMethods,
5607
+ RawMethods
5574
5608
  ]);
5575
5609
 
5576
5610
  var __defProp = Object.defineProperty;
@@ -5601,12 +5635,14 @@ var __objRest = (source, exclude) => {
5601
5635
  }
5602
5636
  return target;
5603
5637
  };
5638
+ const anyShape = {};
5604
5639
  class Db {
5605
- constructor(adapter, queryBuilder, table = void 0, shape = {}, options) {
5640
+ constructor(adapter, queryBuilder, table = void 0, shape = anyShape, columnTypes, options) {
5606
5641
  this.adapter = adapter;
5607
5642
  this.queryBuilder = queryBuilder;
5608
5643
  this.table = table;
5609
5644
  this.shape = shape;
5645
+ this.columnTypes = columnTypes;
5610
5646
  this.whereQueryBuilder = WhereQueryBuilder;
5611
5647
  this.onQueryBuilder = OnQueryBuilder;
5612
5648
  this.__model = this;
@@ -5681,6 +5717,7 @@ const createDb = (_a) => {
5681
5717
  void 0,
5682
5718
  void 0,
5683
5719
  {},
5720
+ ct,
5684
5721
  commonOptions
5685
5722
  );
5686
5723
  qb.queryBuilder = qb;
@@ -5691,6 +5728,7 @@ const createDb = (_a) => {
5691
5728
  qb,
5692
5729
  table,
5693
5730
  typeof shape === "function" ? getColumnTypes(ct, shape) : shape,
5731
+ ct,
5694
5732
  __spreadValues(__spreadValues({}, commonOptions), options2)
5695
5733
  );
5696
5734
  },
@@ -5770,6 +5808,7 @@ exports.QueryGet = QueryGet;
5770
5808
  exports.QueryLog = QueryLog;
5771
5809
  exports.QueryMethods = QueryMethods;
5772
5810
  exports.QueryUpsert = QueryUpsert;
5811
+ exports.RawMethods = RawMethods;
5773
5812
  exports.RealColumn = RealColumn;
5774
5813
  exports.Select = Select;
5775
5814
  exports.SerialColumn = SerialColumn;
@@ -5806,6 +5845,7 @@ exports.addWhere = addWhere;
5806
5845
  exports.addWhereIn = addWhereIn;
5807
5846
  exports.addWhereNot = addWhereNot;
5808
5847
  exports.aggregate1FunctionNames = aggregate1FunctionNames;
5848
+ exports.anyShape = anyShape;
5809
5849
  exports.applyMixins = applyMixins;
5810
5850
  exports.array = array;
5811
5851
  exports.arrayToEnum = arrayToEnum;