proto.io 0.0.175 → 0.0.176

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 (42) hide show
  1. package/dist/adapters/file/database.d.ts +2 -2
  2. package/dist/adapters/file/database.js +1 -0
  3. package/dist/adapters/file/database.js.map +1 -1
  4. package/dist/adapters/file/database.mjs +1 -0
  5. package/dist/adapters/file/database.mjs.map +1 -1
  6. package/dist/adapters/file/filesystem.d.ts +2 -2
  7. package/dist/adapters/file/google-cloud-storage.d.ts +2 -2
  8. package/dist/adapters/storage/progres.d.ts +3 -2
  9. package/dist/adapters/storage/progres.js +51 -29
  10. package/dist/adapters/storage/progres.js.map +1 -1
  11. package/dist/adapters/storage/progres.mjs +52 -30
  12. package/dist/adapters/storage/progres.mjs.map +1 -1
  13. package/dist/client.d.ts +3 -3
  14. package/dist/client.js +1 -1
  15. package/dist/client.mjs +2 -2
  16. package/dist/index.d.ts +3 -3
  17. package/dist/index.js +13 -4
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs +14 -5
  20. package/dist/index.mjs.map +1 -1
  21. package/dist/internals/{index-BQqYdjaH.d.ts → index-B0DE6MVr.d.ts} +2 -2
  22. package/dist/internals/index-B0DE6MVr.d.ts.map +1 -0
  23. package/dist/internals/{index-BYZpj31o.d.ts → index-BZcW5wgM.d.ts} +16 -1
  24. package/dist/internals/index-BZcW5wgM.d.ts.map +1 -0
  25. package/dist/internals/{index-B9CWU0y7.d.ts → index-Bh60s4Tq.d.ts} +2 -2
  26. package/dist/internals/index-Bh60s4Tq.d.ts.map +1 -0
  27. package/dist/internals/{index-CvA0tbwx.js → index-Cd6nNb--.js} +15 -1
  28. package/dist/internals/index-Cd6nNb--.js.map +1 -0
  29. package/dist/internals/{index-BPbXumUi.mjs → index-tEsKpX2d.mjs} +15 -1
  30. package/dist/internals/index-tEsKpX2d.mjs.map +1 -0
  31. package/dist/internals/{random-DVOUDDGg.js → random-CvVZek_8.js} +13 -1
  32. package/dist/internals/random-CvVZek_8.js.map +1 -0
  33. package/dist/internals/{random-BMQpRlGH.mjs → random-DGiu1IIq.mjs} +14 -2
  34. package/dist/internals/random-DGiu1IIq.mjs.map +1 -0
  35. package/package.json +1 -1
  36. package/dist/internals/index-B9CWU0y7.d.ts.map +0 -1
  37. package/dist/internals/index-BPbXumUi.mjs.map +0 -1
  38. package/dist/internals/index-BQqYdjaH.d.ts.map +0 -1
  39. package/dist/internals/index-BYZpj31o.d.ts.map +0 -1
  40. package/dist/internals/index-CvA0tbwx.js.map +0 -1
  41. package/dist/internals/random-BMQpRlGH.mjs.map +0 -1
  42. package/dist/internals/random-DVOUDDGg.js.map +0 -1
@@ -1,5 +1,5 @@
1
1
  import _ from 'lodash';
2
- import { i as isPointer, a as isRelation, b as isShape, e as decodeUpdateOp, s as shapePaths, c as isPrimitive, T as TObject, f as isVector, _ as _encodeValue, g as _decodeValue, h as dimensionOf, j as _typeof } from '../../internals/index-CTsc042s.mjs';
2
+ import { a as isRelation, i as isPointer, b as isShape, e as decodeUpdateOp, s as shapePaths, c as isPrimitive, T as TObject, f as isVector, _ as _encodeValue, g as _decodeValue, h as dimensionOf, j as _typeof } from '../../internals/index-CTsc042s.mjs';
3
3
  import { Pool, types } from 'pg';
4
4
  import QueryStream from 'pg-query-stream';
5
5
  import { asyncStream } from '@o2ter/utils-js';
@@ -7,7 +7,7 @@ import Decimal from 'decimal.js';
7
7
  import { escapeLiteral, escapeIdentifier } from 'pg/lib/utils';
8
8
  import { a as QueryCoditionalSelector, b as QueryFieldSelector, c as QueryExpressionSelector, d as QueryDistanceExpression, e as QueryCoditionalExpression, f as QueryComparisonExpression, g as QueryNotExpression, h as QueryArrayExpression, i as QueryValueExpression, j as QueryKeyExpression, Q as QuerySelector, F as FieldSelectorExpression } from '../../internals/index-DHQhYGsJ.mjs';
9
9
  import '@o2ter/crypto-js';
10
- import { r as resolveColumn, a as resolveDataType, g as generateId, Q as QueryValidator } from '../../internals/random-BMQpRlGH.mjs';
10
+ import { r as resolveColumn, a as resolveDataType, g as generateId, Q as QueryValidator } from '../../internals/random-DGiu1IIq.mjs';
11
11
  import { P as PVK } from '../../internals/private-BUpLAMZi.mjs';
12
12
 
13
13
  //
@@ -233,22 +233,25 @@ class QueryCompiler {
233
233
  return this.idx++;
234
234
  }
235
235
  _makeContext(query) {
236
- const context = this._encodeIncludes(query.className, query.includes, query.matches);
236
+ const context = this._encodeIncludes(query);
237
237
  return {
238
238
  ...context,
239
239
  sorting: _encodeSorting(context.includes, context.populates, query.sort),
240
240
  };
241
241
  }
242
- _encodeIncludes(className, includes, matches) {
242
+ _encodeIncludes(query) {
243
243
  const names = {};
244
244
  const populates = {};
245
- for (const include of includes) {
246
- const { paths: [colname, ...subpath], dataType } = resolveColumn(this.schema, className, include);
245
+ const countMatches = [];
246
+ for (const include of query.includes) {
247
+ const { paths: [colname, ...subpath], dataType } = resolveColumn(this.schema, query.className, include);
247
248
  names[colname] = dataType;
249
+ if (isRelation(dataType) && _.includes(query.countMatches, colname))
250
+ countMatches.push(colname);
248
251
  if (isPointer(dataType) || isRelation(dataType)) {
249
252
  if (_.isEmpty(subpath))
250
253
  throw Error(`Invalid path: ${include}`);
251
- const _matches = matches[colname];
254
+ const _matches = query.matches[colname];
252
255
  populates[colname] = populates[colname] ?? {
253
256
  name: `t${this.nextIdx()}`,
254
257
  className: dataType.target,
@@ -274,13 +277,22 @@ class QueryCompiler {
274
277
  }
275
278
  }
276
279
  for (const [colname, populate] of _.toPairs(populates)) {
277
- const _matches = matches[colname];
278
- const { includes, populates } = this._encodeIncludes(populate.className, populate.subpaths, _matches.matches);
280
+ const _matches = query.matches[colname];
281
+ const { includes, populates, countMatches } = this._encodeIncludes({
282
+ className: populate.className,
283
+ includes: populate.subpaths,
284
+ matches: _matches.matches,
285
+ countMatches: [
286
+ ..._.filter(query.countMatches, x => _.startsWith(x, `${colname}.`)).map(x => x.slice(colname.length + 1)),
287
+ ..._matches.countMatches ?? [],
288
+ ],
289
+ });
279
290
  populate.sort = _encodeSorting(includes, populates, _matches.sort);
280
291
  populate.includes = includes;
281
292
  populate.populates = populates;
293
+ populate.countMatches = countMatches;
282
294
  }
283
- return { includes: names, populates };
295
+ return { includes: names, populates, countMatches };
284
296
  }
285
297
  _baseSelectQuery(query, options) {
286
298
  const context = this._makeContext(query);
@@ -324,7 +336,7 @@ class QueryCompiler {
324
336
  };
325
337
  }
326
338
  _refetch(name, query) {
327
- const _context = this._encodeIncludes(query.className, query.includes, query.matches);
339
+ const _context = this._encodeIncludes(query);
328
340
  const populates = _.mapValues(_context.populates, (populate) => this.dialect.encodePopulate(this, populate, { className: query.className, name }));
329
341
  const stages = _.fromPairs(_.flatMap(_.values(populates), (p) => _.toPairs(p)));
330
342
  const _populates = this._selectPopulateMap(_context, query.className, name);
@@ -358,7 +370,10 @@ class QueryCompiler {
358
370
  `;
359
371
  }
360
372
  _encodeUpdateAttrs(className, attrs) {
361
- const updates = [];
373
+ const updates = [
374
+ sql `__v = __v + 1`,
375
+ sql `_updated_at = NOW()`,
376
+ ];
362
377
  for (const [path, op] of _.toPairs(attrs)) {
363
378
  const { paths: [column, ...subpath], dataType } = resolveColumn(this.schema, className, path);
364
379
  if (isShape(dataType)) {
@@ -443,7 +458,7 @@ class QueryCompiler {
443
458
  return _.map(context.populates, (populate, field) => this.dialect.selectPopulate(this, {
444
459
  className,
445
460
  name,
446
- }, populate, field));
461
+ }, populate, field, _.includes(context.countMatches, field)));
447
462
  }
448
463
  insert(options, attrs) {
449
464
  const _attrs = _.toPairs({
@@ -492,8 +507,7 @@ class QueryCompiler {
492
507
  return sql `
493
508
  , ${{ identifier: name }} AS (
494
509
  UPDATE ${{ identifier: query.className }}
495
- SET __v = __v + 1, _updated_at = NOW()
496
- ${!_.isEmpty(update) ? sql `, ${this._encodeUpdateAttrs(query.className, update)}` : sql ``}
510
+ SET ${this._encodeUpdateAttrs(query.className, update)}
497
511
  WHERE ${{ identifier: query.className }}._id IN (SELECT ${{ identifier: fetchName }}._id FROM ${{ identifier: fetchName }})
498
512
  RETURNING *
499
513
  )
@@ -505,8 +519,7 @@ class QueryCompiler {
505
519
  return this._modifyQuery(query, (fetchName) => {
506
520
  return sql `
507
521
  UPDATE ${{ identifier: query.className }}
508
- SET __v = __v + 1, _updated_at = NOW()
509
- ${!_.isEmpty(update) ? sql `, ${this._encodeUpdateAttrs(query.className, update)}` : sql ``}
522
+ SET ${this._encodeUpdateAttrs(query.className, update)}
510
523
  WHERE ${{ identifier: query.className }}._id IN (SELECT ${{ identifier: fetchName }}._id FROM ${{ identifier: fetchName }})
511
524
  RETURNING _id
512
525
  `;
@@ -524,8 +537,7 @@ class QueryCompiler {
524
537
  return sql `
525
538
  , ${{ identifier: updateName }} AS (
526
539
  UPDATE ${{ identifier: query.className }}
527
- SET __v = __v + 1, _updated_at = NOW()
528
- ${!_.isEmpty(update) ? sql `, ${this._encodeUpdateAttrs(query.className, update)}` : sql ``}
540
+ SET ${this._encodeUpdateAttrs(query.className, update)}
529
541
  WHERE ${{ identifier: query.className }}._id IN (SELECT ${{ identifier: fetchName }}._id FROM ${{ identifier: fetchName }})
530
542
  RETURNING *
531
543
  )
@@ -556,17 +568,16 @@ class QueryCompiler {
556
568
  return sql `
557
569
  , ${{ identifier: updateName }} AS (
558
570
  UPDATE ${{ identifier: query.className }}
559
- SET __v = __v + 1, _updated_at = NOW()
560
- ${!_.isEmpty(update) ? sql `, ${this._encodeUpdateAttrs(query.className, update)}` : sql ``}
571
+ SET ${this._encodeUpdateAttrs(query.className, update)}
561
572
  WHERE ${{ identifier: query.className }}._id IN (SELECT ${{ identifier: fetchName }}._id FROM ${{ identifier: fetchName }})
562
- RETURNING _id, 0 AS ${{ identifier: 'result' }}
573
+ RETURNING _id, __v
563
574
  )
564
575
  , ${{ identifier: insertName }} AS (
565
576
  INSERT INTO ${{ identifier: query.className }}
566
577
  (${_.map(_insert, x => sql `${{ identifier: x[0] }}`)})
567
578
  SELECT ${_.map(_insert, x => sql `${x[1]} AS ${{ identifier: x[0] }}`)}
568
579
  WHERE NOT EXISTS(SELECT * FROM ${{ identifier: updateName }})
569
- RETURNING _id, 1 AS ${{ identifier: 'result' }}
580
+ RETURNING _id, __v
570
581
  )
571
582
  SELECT * FROM ${{ identifier: updateName }}
572
583
  UNION
@@ -660,7 +671,9 @@ class SqlStorage {
660
671
  }
661
672
  else if (isRelation(type)) {
662
673
  const _value = _.get(value, path);
663
- if (_.isArray(_value))
674
+ if (_.isString(_value) && _value.match(/^\d+$/g))
675
+ _.set(result, path, parseInt(_value));
676
+ else if (_.isArray(_value))
664
677
  _.set(result, path, _value.map(x => this._decodeObject(type.target, x)));
665
678
  }
666
679
  else {
@@ -696,7 +709,9 @@ class SqlStorage {
696
709
  }
697
710
  }
698
711
  else if (isRelation(dataType)) {
699
- if (_.isArray(value))
712
+ if (_.isString(value) && value.match(/^\d+$/g))
713
+ obj[PVK].attributes[key] = parseInt(value);
714
+ else if (_.isArray(value))
700
715
  obj[PVK].attributes[key] = value.map(x => this._decodeObject(dataType.target, x));
701
716
  }
702
717
  else {
@@ -807,9 +822,10 @@ class SqlStorage {
807
822
  async upsertMany(query, update, setOnInsert) {
808
823
  const compiler = this._makeCompiler(true, query.extraFilter);
809
824
  const upserted = await this.query(compiler.upsertMany(query, update, setOnInsert));
825
+ const inserted = _.filter(upserted, x => x.__v === 0).length;
810
826
  return {
811
- updated: _.filter(upserted, x => x.result === 0).length,
812
- inserted: _.filter(upserted, x => x.result === 1).length,
827
+ updated: upserted.length - inserted,
828
+ inserted: inserted,
813
829
  };
814
830
  }
815
831
  async deleteOne(query) {
@@ -1988,11 +2004,17 @@ const _selectRelationPopulate = (compiler, parent, populate, field, encode) => {
1988
2004
  ${compiler.selectLock ? compiler.isUpdate ? sql `FOR UPDATE NOWAIT` : sql `FOR SHARE NOWAIT` : sql ``}
1989
2005
  `;
1990
2006
  };
1991
- const selectPopulate = (compiler, parent, populate, field) => {
2007
+ const selectPopulate = (compiler, parent, populate, field, countMatches) => {
1992
2008
  if (populate.type === 'relation') {
1993
2009
  return {
1994
2010
  columns: [
1995
- sql `
2011
+ countMatches ? sql `
2012
+ (
2013
+ SELECT COUNT(*) FROM (
2014
+ ${_selectRelationPopulate(compiler, parent, populate, field, false)}
2015
+ ) ${{ identifier: populate.name }}
2016
+ ) AS ${{ identifier: field }}
2017
+ ` : sql `
1996
2018
  ARRAY(
1997
2019
  SELECT to_jsonb(${{ identifier: populate.name }}) FROM (
1998
2020
  ${_selectRelationPopulate(compiler, parent, populate, field, true)}
@@ -2093,7 +2115,7 @@ const encodePopulate = (compiler, parent, remix) => {
2093
2115
  parent.filter && compiler._encodeFilter(parent, parent.filter),
2094
2116
  compiler.extraFilter && compiler._encodeFilter(parent, compiler.extraFilter(parent.className)),
2095
2117
  ]);
2096
- const _populates = _.map(parent.populates, (populate, field) => selectPopulate(compiler, parent, populate, field));
2118
+ const _populates = _.map(parent.populates, (populate, field) => selectPopulate(compiler, parent, populate, field, _.includes(parent.countMatches, field)));
2097
2119
  const _joins = _.compact(_.map(_populates, ({ join }) => join));
2098
2120
  const _includes = _.pickBy(parent.includes, v => isPrimitive(v));
2099
2121
  const { joins: _joins2 = [], field: _foreignField = undefined, rows = false, } = parent.foreignField ? encodeForeignField(compiler, {