proto.io 0.0.180 → 0.0.182

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 (32) hide show
  1. package/dist/adapters/file/database.d.ts +2 -2
  2. package/dist/adapters/file/filesystem.d.ts +2 -2
  3. package/dist/adapters/file/google-cloud-storage.d.ts +2 -2
  4. package/dist/adapters/storage/progres.d.ts +16 -35
  5. package/dist/adapters/storage/progres.js +31 -26
  6. package/dist/adapters/storage/progres.js.map +1 -1
  7. package/dist/adapters/storage/progres.mjs +31 -26
  8. package/dist/adapters/storage/progres.mjs.map +1 -1
  9. package/dist/client.d.ts +3 -3
  10. package/dist/client.js +1 -1
  11. package/dist/client.mjs +2 -2
  12. package/dist/index.d.ts +3 -3
  13. package/dist/index.js +244 -3
  14. package/dist/index.js.map +1 -1
  15. package/dist/index.mjs +245 -4
  16. package/dist/index.mjs.map +1 -1
  17. package/dist/internals/{index-B7qz_VeO.d.ts → index-BZupzq1Y.d.ts} +2 -2
  18. package/dist/internals/index-BZupzq1Y.d.ts.map +1 -0
  19. package/dist/internals/{index-DwCNTJb5.js → index-BnDahEvz.js} +85 -1
  20. package/dist/internals/index-BnDahEvz.js.map +1 -0
  21. package/dist/internals/{index-CPodB1P0.mjs → index-CmJh3t_k.mjs} +85 -1
  22. package/dist/internals/index-CmJh3t_k.mjs.map +1 -0
  23. package/dist/internals/{index-BgZqiNxF.d.ts → index-YpB-hXxf.d.ts} +84 -1
  24. package/dist/internals/index-YpB-hXxf.d.ts.map +1 -0
  25. package/dist/internals/{index-CyXddWjz.d.ts → index-cKx59cIc.d.ts} +3 -2
  26. package/dist/internals/index-cKx59cIc.d.ts.map +1 -0
  27. package/package.json +1 -1
  28. package/dist/internals/index-B7qz_VeO.d.ts.map +0 -1
  29. package/dist/internals/index-BgZqiNxF.d.ts.map +0 -1
  30. package/dist/internals/index-CPodB1P0.mjs.map +0 -1
  31. package/dist/internals/index-CyXddWjz.d.ts.map +0 -1
  32. package/dist/internals/index-DwCNTJb5.js.map +0 -1
@@ -1,5 +1,5 @@
1
- import { T as TSchema, P as ProtoService } from '../../internals/index-BgZqiNxF.js';
2
- import { F as FileStorageBase, a as FileStorageOptions } from '../../internals/index-B7qz_VeO.js';
1
+ import { T as TSchema, P as ProtoService } from '../../internals/index-YpB-hXxf.js';
2
+ import { F as FileStorageBase, a as FileStorageOptions } from '../../internals/index-BZupzq1Y.js';
3
3
  import '@o2ter/utils-js';
4
4
  import 'jsonwebtoken';
5
5
  import '@o2ter/server-js';
@@ -1,5 +1,5 @@
1
- import { P as ProtoService } from '../../internals/index-BgZqiNxF.js';
2
- import { F as FileStorageBase, a as FileStorageOptions } from '../../internals/index-B7qz_VeO.js';
1
+ import { P as ProtoService } from '../../internals/index-YpB-hXxf.js';
2
+ import { F as FileStorageBase, a as FileStorageOptions } from '../../internals/index-BZupzq1Y.js';
3
3
  import '@o2ter/utils-js';
4
4
  import 'jsonwebtoken';
5
5
  import '@o2ter/server-js';
@@ -1,7 +1,7 @@
1
1
  import * as _google_cloud_storage from '@google-cloud/storage';
2
2
  import { Storage } from '@google-cloud/storage';
3
- import { P as ProtoService } from '../../internals/index-BgZqiNxF.js';
4
- import { F as FileStorageBase, a as FileStorageOptions } from '../../internals/index-B7qz_VeO.js';
3
+ import { P as ProtoService } from '../../internals/index-YpB-hXxf.js';
4
+ import { F as FileStorageBase, a as FileStorageOptions } from '../../internals/index-BZupzq1Y.js';
5
5
  import '@o2ter/utils-js';
6
6
  import 'jsonwebtoken';
7
7
  import '@o2ter/server-js';
@@ -1,5 +1,5 @@
1
1
  import { Pool, PoolClient, PoolConfig } from 'pg';
2
- import { _ as _TValue, T as TSchema, Q as QuerySelector, p as DecodedQuery, F as FindOptions, R as RelationOptions, q as DecodedSortOption, I as InsertOptions, r as TValue, t as FindOneOptions, u as TUpdateOp, v as FieldSelectorExpression, w as QueryExpression, x as TStorage, y as TransactionOptions, h as TObject, z as TQueryRandomOptions, A as TPubSub } from '../../internals/index-BgZqiNxF.js';
2
+ import { _ as _TValue, T as TSchema, Q as QuerySelector, p as DecodedQuery, F as FindOptions, R as RelationOptions, q as DecodedSortOption, I as InsertOptions, r as TValue, t as FindOneOptions, u as TUpdateOp, v as FieldSelectorExpression, w as QueryExpression, x as TStorage, y as TransactionOptions, h as TObject, z as TQueryRandomOptions, A as TPubSub } from '../../internals/index-YpB-hXxf.js';
3
3
  import * as _o2ter_utils_js from '@o2ter/utils-js';
4
4
  import { asyncStream } from '@o2ter/utils-js';
5
5
  import 'jsonwebtoken';
@@ -46,17 +46,19 @@ declare class PostgresDriver extends PostgresClientDriver {
46
46
  subscribe(channel: string, callback: (payload: _TValue) => void): () => void;
47
47
  }
48
48
 
49
- type Populate = {
49
+ type QueryContext = {
50
50
  name: string;
51
- className: string;
51
+ className?: string;
52
+ includes?: Record<string, TSchema.DataType>;
53
+ populates?: Record<string, Populate>;
54
+ countMatches?: string[];
55
+ };
56
+ type Populate = Required<QueryContext> & {
52
57
  colname: string;
53
58
  type: 'pointer' | 'relation';
54
59
  foreignField?: string;
55
60
  subpaths: string[];
56
61
  filter?: QuerySelector;
57
- includes: Record<string, TSchema.DataType>;
58
- populates: Record<string, Populate>;
59
- countMatches: string[];
60
62
  sort?: Record<string, 1 | -1> | DecodedSortOption[];
61
63
  skip?: number;
62
64
  limit?: number;
@@ -81,7 +83,6 @@ declare class QueryCompiler {
81
83
  extraFilter?: (className: string) => QuerySelector;
82
84
  });
83
85
  nextIdx(): number;
84
- private _makeContext;
85
86
  private _encodeIncludes;
86
87
  private _baseSelectQuery;
87
88
  private _refetch;
@@ -92,16 +93,9 @@ declare class QueryCompiler {
92
93
  private _encodeUpdateAttrs;
93
94
  private _encodeObjectAttrs;
94
95
  private _encodeCoditionalSelector;
95
- _encodeFilter(parent: {
96
- className?: string;
97
- name: string;
98
- populates?: Record<string, Populate>;
99
- }, filter: QuerySelector): SQL | undefined;
96
+ _encodeFilter(parent: QueryContext, filter: QuerySelector): SQL | undefined;
100
97
  private _selectIncludes;
101
- _encodeSort(sort: Record<string, 1 | -1> | DecodedSortOption[], parent: {
102
- className?: string;
103
- name: string;
104
- }): SQL;
98
+ _encodeSort(sort: Record<string, 1 | -1> | DecodedSortOption[], parent: QueryContext): SQL;
105
99
  private _selectPopulateMap;
106
100
  insert(options: InsertOptions, attrs: Record<string, TValue>): SQL;
107
101
  insertMany(options: InsertOptions, values: Record<string, TValue>[]): SQL;
@@ -121,34 +115,21 @@ interface SqlDialect {
121
115
  encodeType(colname: string, type: TSchema.DataType, value: TValue): SQL;
122
116
  decodeType(type: TSchema.Primitive | 'vector', value: any): TValue;
123
117
  updateOperation(paths: string[], dataType: TSchema.DataType, operation: TUpdateOp): SQL;
124
- selectPopulate(compiler: QueryCompiler, parent: {
118
+ selectPopulate(compiler: QueryCompiler, parent: QueryContext & {
125
119
  className: string;
126
- name: string;
127
120
  }, populate: Populate, field: string, countMatches: boolean): {
128
121
  columns: SQL[];
129
122
  join?: SQL;
130
123
  };
131
- encodeFieldExpression(compiler: QueryCompiler, parent: {
132
- className?: string;
133
- name: string;
134
- populates?: Record<string, Populate>;
135
- }, field: string, expr: FieldSelectorExpression): SQL;
136
- encodeQueryExpression(compiler: QueryCompiler, parent: {
137
- className?: string;
138
- name: string;
139
- }, expr: QueryExpression): SQL | undefined;
140
- encodePopulate(compiler: QueryCompiler, parent: Populate, remix?: {
124
+ encodeFieldExpression(compiler: QueryCompiler, parent: QueryContext, field: string, expr: FieldSelectorExpression): SQL;
125
+ encodeQueryExpression(compiler: QueryCompiler, parent: QueryContext, expr: QueryExpression): SQL | undefined;
126
+ encodePopulate(compiler: QueryCompiler, parent: Populate, remix?: QueryContext & {
141
127
  className: string;
142
- name: string;
143
128
  }): Record<string, SQL>;
144
- encodeRelation(compiler: QueryCompiler, parent: {
129
+ encodeRelation(compiler: QueryCompiler, parent: QueryContext & {
145
130
  className: string;
146
- name: string;
147
131
  }, relatedBy: NonNullable<RelationOptions['relatedBy']>): SQL;
148
- encodeSortKey(compiler: QueryCompiler, parent: {
149
- className?: string;
150
- name: string;
151
- }, key: string): SQL;
132
+ encodeSortKey(compiler: QueryCompiler, parent: QueryContext, key: string): SQL;
152
133
  random(opts: {
153
134
  weight?: string;
154
135
  }): SQL;
@@ -210,13 +210,6 @@ class QueryCompiler {
210
210
  nextIdx() {
211
211
  return this.idx++;
212
212
  }
213
- _makeContext(query) {
214
- const context = this._encodeIncludes(query);
215
- return {
216
- ...context,
217
- sorting: _encodeSorting(context.includes, context.populates, query.sort),
218
- };
219
- }
220
213
  _encodeIncludes(query) {
221
214
  const names = {};
222
215
  const populates = {};
@@ -270,16 +263,15 @@ class QueryCompiler {
270
263
  populate.populates = populates;
271
264
  populate.countMatches = countMatches;
272
265
  }
273
- return { includes: names, populates, countMatches };
266
+ return { className: query.className, includes: names, populates, countMatches: _.uniq(countMatches) };
274
267
  }
275
268
  _baseSelectQuery(query, options) {
276
- const context = this._makeContext(query);
269
+ const fetchName = `_fetch_$${query.className.toLowerCase()}`;
270
+ const context = { ...this._encodeIncludes(query), name: fetchName };
277
271
  const _stages = _.mapValues(context.populates, (populate) => this.dialect.encodePopulate(this, populate));
278
272
  const stages = _.fromPairs(_.flatMap(_.values(_stages), (p) => _.toPairs(p)));
279
- const fetchName = `_fetch_$${query.className.toLowerCase()}`;
280
- const parent = { className: query.className, name: fetchName, populates: context.populates };
281
- const baseFilter = this._encodeFilter(parent, query.filter);
282
- const populates = this._selectPopulateMap(context, query.className, fetchName);
273
+ const baseFilter = this._encodeFilter(context, query.filter);
274
+ const populates = this._selectPopulateMap(context);
283
275
  const joins = _.compact(_.map(populates, ({ join }) => join));
284
276
  const includes = {
285
277
  literal: [
@@ -292,12 +284,12 @@ class QueryCompiler {
292
284
  const filter = _.compact([
293
285
  baseFilter,
294
286
  _options?.extraFilter,
295
- query.relatedBy && this.dialect.encodeRelation(this, parent, query.relatedBy),
287
+ query.relatedBy && this.dialect.encodeRelation(this, context, query.relatedBy),
296
288
  ]);
297
289
  return {
298
290
  stages,
299
291
  fetchName,
300
- context: context,
292
+ context,
301
293
  query: sql `
302
294
  SELECT ${_options?.select ? _options?.select : sql `*`} FROM (
303
295
  SELECT ${includes}
@@ -314,10 +306,10 @@ class QueryCompiler {
314
306
  };
315
307
  }
316
308
  _refetch(name, query) {
317
- const _context = this._encodeIncludes(query);
309
+ const _context = { ...this._encodeIncludes(query), name };
318
310
  const populates = _.mapValues(_context.populates, (populate) => this.dialect.encodePopulate(this, populate, { className: query.className, name }));
319
311
  const stages = _.fromPairs(_.flatMap(_.values(populates), (p) => _.toPairs(p)));
320
- const _populates = this._selectPopulateMap(_context, query.className, name);
312
+ const _populates = this._selectPopulateMap(_context);
321
313
  const _joins = _.compact(_.map(_populates, ({ join }) => join));
322
314
  const _includes = {
323
315
  literal: [
@@ -432,11 +424,8 @@ class QueryCompiler {
432
424
  ${this.dialect.encodeSortKey(this, parent, key)} ${{ literal: order === 1 ? 'ASC' : 'DESC' }}
433
425
  `)}`;
434
426
  }
435
- _selectPopulateMap(context, className, name) {
436
- return _.map(context.populates, (populate, field) => this.dialect.selectPopulate(this, {
437
- className,
438
- name,
439
- }, populate, field, _.includes(context.countMatches, field)));
427
+ _selectPopulateMap(context) {
428
+ return _.map(context.populates, (populate, field) => this.dialect.selectPopulate(this, context, populate, field, _.includes(context.countMatches, field)));
440
429
  }
441
430
  insert(options, attrs) {
442
431
  const _attrs = _.toPairs({
@@ -444,10 +433,10 @@ class QueryCompiler {
444
433
  ...this._encodeObjectAttrs(options.className, attrs),
445
434
  });
446
435
  const name = `_insert_$${options.className.toLowerCase()}`;
447
- const context = this._makeContext(options);
436
+ const context = { ...this._encodeIncludes(options), name };
448
437
  const populates = _.mapValues(context.populates, (populate) => this.dialect.encodePopulate(this, populate));
449
438
  const stages = _.fromPairs(_.flatMap(_.values(populates), (p) => _.toPairs(p)));
450
- const _populates = this._selectPopulateMap(context, options.className, name);
439
+ const _populates = this._selectPopulateMap(context);
451
440
  const joins = _.compact(_.map(_populates, ({ join }) => join));
452
441
  return sql `
453
442
  WITH ${{ identifier: name }} AS (
@@ -566,7 +555,7 @@ class QueryCompiler {
566
555
  deleteOne(query) {
567
556
  return this._modifyQuery({ ...query, limit: 1 }, (fetchName, context) => {
568
557
  const name = `_delete_$${query.className.toLowerCase()}`;
569
- const populates = this._selectPopulateMap(context, query.className, name);
558
+ const populates = this._selectPopulateMap({ ...context, name });
570
559
  const joins = _.compact(_.map(populates, ({ join }) => join));
571
560
  return sql `
572
561
  , ${{ identifier: name }} AS (
@@ -576,7 +565,7 @@ class QueryCompiler {
576
565
  )
577
566
  SELECT ${{
578
567
  literal: [
579
- ...this._selectIncludes(name, context.includes),
568
+ ...this._selectIncludes(name, context.includes ?? {}),
580
569
  ..._.flatMap(populates, ({ columns }) => columns),
581
570
  ], separator: ',\n'
582
571
  }}
@@ -930,6 +919,7 @@ const fetchElement = (compiler, parent, field) => {
930
919
  element,
931
920
  dataType: json ? null : dataType,
932
921
  relation: {
922
+ colname,
933
923
  target: dataType.target,
934
924
  populate,
935
925
  },
@@ -2179,6 +2169,9 @@ const encodeFieldExpression = (compiler, parent, field, expr) => {
2179
2169
  break;
2180
2170
  return sql `${element} ${nullSafeEqual()} ${{ value: expr.value.objectId }}`;
2181
2171
  }
2172
+ if (relation && _.includes(parent.countMatches, relation.colname)) {
2173
+ return sql `${element} ${nullSafeEqual()} ${encodeType(colname, 'number', expr.value)}`;
2174
+ }
2182
2175
  return sql `${element} ${nullSafeEqual()} ${encodeValue(expr.value)}`;
2183
2176
  }
2184
2177
  case '$ne':
@@ -2192,6 +2185,9 @@ const encodeFieldExpression = (compiler, parent, field, expr) => {
2192
2185
  break;
2193
2186
  return sql `${element} ${nullSafeNotEqual()} ${{ value: expr.value.objectId }}`;
2194
2187
  }
2188
+ if (relation && _.includes(parent.countMatches, relation.colname)) {
2189
+ return sql `${element} ${nullSafeNotEqual()} ${encodeType(colname, 'number', expr.value)}`;
2190
+ }
2195
2191
  return sql `${element} ${nullSafeNotEqual()} ${encodeValue(expr.value)}`;
2196
2192
  }
2197
2193
  case '$gt':
@@ -2235,6 +2231,9 @@ const encodeFieldExpression = (compiler, parent, field, expr) => {
2235
2231
  else if (!_.isString(dataType) && dataType?.type === 'pointer' && expr.value instanceof index.TObject && expr.value.objectId) {
2236
2232
  return sql `${element} ${{ literal: op }} ${{ value: expr.value.objectId }}`;
2237
2233
  }
2234
+ else if (relation && _.includes(parent.countMatches, relation.colname)) {
2235
+ return sql `${element} ${{ literal: op }} ${encodeType(colname, 'number', expr.value)}`;
2236
+ }
2238
2237
  else if (!dataType) {
2239
2238
  if (expr.value instanceof Decimal || _.isNumber(expr.value)) {
2240
2239
  return sql `(
@@ -2425,6 +2424,9 @@ const encodeFieldExpression = (compiler, parent, field, expr) => {
2425
2424
  if (dataType && index._isTypeof(dataType, 'string')) {
2426
2425
  return sql `COALESCE(length(${element}), 0) = ${{ value: expr.value }}`;
2427
2426
  }
2427
+ if (relation && _.includes(parent.countMatches, relation.colname)) {
2428
+ return sql `${element} = ${{ value: expr.value }}`;
2429
+ }
2428
2430
  if (dataType && index._isTypeof(dataType, ['array', 'string[]', 'vector', 'relation'])) {
2429
2431
  return sql `COALESCE(array_length(${element}, 1), 0) = ${{ value: expr.value }}`;
2430
2432
  }
@@ -2446,6 +2448,9 @@ const encodeFieldExpression = (compiler, parent, field, expr) => {
2446
2448
  if (dataType && index._isTypeof(dataType, 'string')) {
2447
2449
  return sql `COALESCE(length(${element}), 0) ${{ literal: expr.value ? '=' : '<>' }} 0`;
2448
2450
  }
2451
+ if (relation && _.includes(parent.countMatches, relation.colname)) {
2452
+ return sql `${element} ${{ literal: expr.value ? '=' : '<>' }} 0`;
2453
+ }
2449
2454
  if (dataType && index._isTypeof(dataType, ['array', 'string[]', 'vector', 'relation'])) {
2450
2455
  return sql `COALESCE(array_length(${element}, 1), 0) ${{ literal: expr.value ? '=' : '<>' }} 0`;
2451
2456
  }