@atscript/utils-db 0.1.28 → 0.1.30

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.cjs CHANGED
@@ -23,6 +23,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
23
 
24
24
  //#endregion
25
25
  const __atscript_typescript_utils = __toESM(require("@atscript/typescript/utils"));
26
+ const __uniqu_core = __toESM(require("@uniqu/core"));
26
27
 
27
28
  //#region packages/utils-db/src/logger.ts
28
29
  const NoopLogger = {
@@ -187,15 +188,15 @@ var AtscriptDbTable = class {
187
188
  return this._fieldDescriptors;
188
189
  }
189
190
  /**
190
- * Resolves a projection to a list of field names to include.
191
+ * Resolves `$select` from {@link UniqueryControls} to a list of field names.
191
192
  * - `undefined` → `undefined` (all fields)
192
193
  * - `string[]` → pass through
193
194
  * - `Record<K, 1>` → extract included keys
194
195
  * - `Record<K, 0>` → invert using known field names
195
- */ resolveProjection(projection) {
196
- if (!projection) return undefined;
197
- if (Array.isArray(projection)) return projection.length > 0 ? projection : undefined;
198
- const entries = Object.entries(projection);
196
+ */ resolveProjection(select) {
197
+ if (!select) return undefined;
198
+ if (Array.isArray(select)) return select.length > 0 ? select : undefined;
199
+ const entries = Object.entries(select);
199
200
  if (entries.length === 0) return undefined;
200
201
  const firstVal = entries[0][1];
201
202
  if (firstVal === 1) return entries.filter(([, v]) => v === 1).map(([k]) => k);
@@ -290,19 +291,23 @@ var AtscriptDbTable = class {
290
291
  return this.adapter.deleteOne(filter);
291
292
  }
292
293
  /**
293
- * Finds a single record matching the filter.
294
- */ async findOne(filter, options) {
295
- return this.adapter.findOne(filter, options);
294
+ * Finds a single record matching the query.
295
+ */ async findOne(query) {
296
+ return this.adapter.findOne(query);
296
297
  }
297
298
  /**
298
- * Finds all records matching the filter.
299
- */ async findMany(filter, options) {
300
- return this.adapter.findMany(filter, options);
299
+ * Finds all records matching the query.
300
+ */ async findMany(query) {
301
+ return this.adapter.findMany(query);
301
302
  }
302
303
  /**
303
- * Counts records matching the filter.
304
- */ async count(filter = {}) {
305
- return this.adapter.count(filter);
304
+ * Counts records matching the query.
305
+ */ async count(query) {
306
+ query ?? (query = {
307
+ filter: {},
308
+ controls: {}
309
+ });
310
+ return this.adapter.count(query);
306
311
  }
307
312
  async updateMany(filter, data) {
308
313
  return this.adapter.updateMany(filter, data);
@@ -592,4 +597,16 @@ exports.BaseDbAdapter = BaseDbAdapter
592
597
  exports.NoopLogger = NoopLogger
593
598
  exports.decomposePatch = decomposePatch
594
599
  exports.getKeyProps = getKeyProps
595
- exports.resolveDesignType = resolveDesignType
600
+ Object.defineProperty(exports, 'isPrimitive', {
601
+ enumerable: true,
602
+ get: function () {
603
+ return __uniqu_core.isPrimitive;
604
+ }
605
+ });
606
+ exports.resolveDesignType = resolveDesignType
607
+ Object.defineProperty(exports, 'walkFilter', {
608
+ enumerable: true,
609
+ get: function () {
610
+ return __uniqu_core.walkFilter;
611
+ }
612
+ });
package/dist/index.d.ts CHANGED
@@ -1,60 +1,7 @@
1
1
  import { TAtscriptAnnotatedType, TValidatorPlugin, TMetadataMap, TAtscriptDataType, Validator, TAtscriptTypeObject, TValidatorOptions, TAtscriptTypeArray } from '@atscript/typescript/utils';
2
+ import { FilterExpr, Uniquery, UniqueryControls } from '@uniqu/core';
3
+ export { FieldOpsFor, FilterExpr, FilterVisitor, Uniquery, UniqueryControls, isPrimitive, walkFilter } from '@uniqu/core';
2
4
 
3
- /**
4
- * Comparison and set operators for a single field value.
5
- * Used inside filter objects: `{ age: { $gt: 18, $lt: 65 } }`.
6
- */
7
- type TFilterOperators<V = unknown> = {
8
- $gt?: V;
9
- $gte?: V;
10
- $lt?: V;
11
- $lte?: V;
12
- $ne?: V | null;
13
- $in?: V[];
14
- $nin?: V[];
15
- $exists?: boolean;
16
- $regex?: string;
17
- };
18
- /**
19
- * Typed filter for database queries. Root-level keys from `T` get
20
- * autocompletion and value type checking. Arbitrary string keys
21
- * (e.g. dot-notation paths like `"address.city"`) are allowed via
22
- * index signature fallback.
23
- *
24
- * When used without a type parameter, behaves as `Record<string, unknown>`.
25
- *
26
- * @example
27
- * ```typescript
28
- * // Typed — autocompletion + value checking
29
- * const filter: TDbFilter<User> = { name: 'John', age: { $gt: 18 } }
30
- *
31
- * // Dot-notation — accepted via string fallback
32
- * const filter2: TDbFilter<User> = { 'address.city': 'NYC' }
33
- * ```
34
- */
35
- type TDbFilter<T = Record<string, unknown>> = {
36
- [K in keyof T & string]?: T[K] | TFilterOperators<T[K]> | null;
37
- } & Record<string, unknown> & {
38
- $and?: Array<TDbFilter<T>>;
39
- $or?: Array<TDbFilter<T>>;
40
- $not?: TDbFilter<T>;
41
- };
42
- /**
43
- * Projection for selecting/excluding fields.
44
- * Accepts either an object `{ name: 1, email: 1 }` or an array `['name', 'email']`.
45
- * Root-level keys get autocompletion, arbitrary strings allowed for dot-notation.
46
- */
47
- type TDbProjection<T = Record<string, unknown>> = (Partial<Record<keyof T & string, 0 | 1>> & Record<string, 0 | 1>) | (keyof T & string | string)[];
48
- /**
49
- * Options for find operations: sort, pagination, projection.
50
- * Root-level keys get autocompletion, arbitrary strings allowed for dot-notation.
51
- */
52
- interface TDbFindOptions<T = Record<string, unknown>> {
53
- sort?: Partial<Record<keyof T & string, 1 | -1>> & Record<string, 1 | -1>;
54
- skip?: number;
55
- limit?: number;
56
- projection?: TDbProjection<T>;
57
- }
58
5
  interface TDbInsertResult {
59
6
  insertedId: unknown;
60
7
  }
@@ -175,7 +122,7 @@ declare abstract class BaseDbAdapter {
175
122
  * @param patch - The patch payload with array operations.
176
123
  * @returns Update result.
177
124
  */
178
- nativePatch(filter: TDbFilter, patch: unknown): Promise<TDbUpdateResult>;
125
+ nativePatch(filter: FilterExpr, patch: unknown): Promise<TDbUpdateResult>;
179
126
  /**
180
127
  * Called before field flattening begins.
181
128
  * Use to extract table-level adapter-specific annotations.
@@ -244,15 +191,15 @@ declare abstract class BaseDbAdapter {
244
191
  }): Promise<void>;
245
192
  abstract insertOne(data: Record<string, unknown>): Promise<TDbInsertResult>;
246
193
  abstract insertMany(data: Array<Record<string, unknown>>): Promise<TDbInsertManyResult>;
247
- abstract replaceOne(filter: TDbFilter, data: Record<string, unknown>): Promise<TDbUpdateResult>;
248
- abstract updateOne(filter: TDbFilter, data: Record<string, unknown>): Promise<TDbUpdateResult>;
249
- abstract deleteOne(filter: TDbFilter): Promise<TDbDeleteResult>;
250
- abstract findOne(filter: TDbFilter, options?: TDbFindOptions): Promise<Record<string, unknown> | null>;
251
- abstract findMany(filter: TDbFilter, options?: TDbFindOptions): Promise<Array<Record<string, unknown>>>;
252
- abstract count(filter: TDbFilter): Promise<number>;
253
- abstract updateMany(filter: TDbFilter, data: Record<string, unknown>): Promise<TDbUpdateResult>;
254
- abstract replaceMany(filter: TDbFilter, data: Record<string, unknown>): Promise<TDbUpdateResult>;
255
- abstract deleteMany(filter: TDbFilter): Promise<TDbDeleteResult>;
194
+ abstract replaceOne(filter: FilterExpr, data: Record<string, unknown>): Promise<TDbUpdateResult>;
195
+ abstract updateOne(filter: FilterExpr, data: Record<string, unknown>): Promise<TDbUpdateResult>;
196
+ abstract deleteOne(filter: FilterExpr): Promise<TDbDeleteResult>;
197
+ abstract findOne(query: Uniquery): Promise<Record<string, unknown> | null>;
198
+ abstract findMany(query: Uniquery): Promise<Array<Record<string, unknown>>>;
199
+ abstract count(query: Uniquery): Promise<number>;
200
+ abstract updateMany(filter: FilterExpr, data: Record<string, unknown>): Promise<TDbUpdateResult>;
201
+ abstract replaceMany(filter: FilterExpr, data: Record<string, unknown>): Promise<TDbUpdateResult>;
202
+ abstract deleteMany(filter: FilterExpr): Promise<TDbDeleteResult>;
256
203
  /**
257
204
  * Synchronizes indexes between the Atscript definitions and the database.
258
205
  * Uses `this._table.indexes` for the full index definitions.
@@ -343,13 +290,13 @@ declare class AtscriptDbTable<T extends TAtscriptAnnotatedType = TAtscriptAnnota
343
290
  */
344
291
  get fieldDescriptors(): readonly TDbFieldMeta[];
345
292
  /**
346
- * Resolves a projection to a list of field names to include.
293
+ * Resolves `$select` from {@link UniqueryControls} to a list of field names.
347
294
  * - `undefined` → `undefined` (all fields)
348
295
  * - `string[]` → pass through
349
296
  * - `Record<K, 1>` → extract included keys
350
297
  * - `Record<K, 0>` → invert using known field names
351
298
  */
352
- resolveProjection(projection?: TDbProjection<DataType>): string[] | undefined;
299
+ resolveProjection(select?: UniqueryControls['$select']): string[] | undefined;
353
300
  /**
354
301
  * Creates a new validator with custom options.
355
302
  * Adapter plugins are NOT automatically included — use {@link getValidator}
@@ -389,20 +336,20 @@ declare class AtscriptDbTable<T extends TAtscriptAnnotatedType = TAtscriptAnnota
389
336
  */
390
337
  deleteOne(id: unknown): Promise<TDbDeleteResult>;
391
338
  /**
392
- * Finds a single record matching the filter.
339
+ * Finds a single record matching the query.
393
340
  */
394
- findOne(filter: TDbFilter<DataType>, options?: TDbFindOptions<DataType>): Promise<DataType | null>;
341
+ findOne(query: Uniquery<DataType>): Promise<DataType | null>;
395
342
  /**
396
- * Finds all records matching the filter.
343
+ * Finds all records matching the query.
397
344
  */
398
- findMany(filter: TDbFilter<DataType>, options?: TDbFindOptions<DataType>): Promise<DataType[]>;
345
+ findMany(query: Uniquery<DataType>): Promise<DataType[]>;
399
346
  /**
400
- * Counts records matching the filter.
347
+ * Counts records matching the query.
401
348
  */
402
- count(filter?: TDbFilter<DataType>): Promise<number>;
403
- updateMany(filter: TDbFilter<DataType>, data: Partial<DataType> & Record<string, unknown>): Promise<TDbUpdateResult>;
404
- replaceMany(filter: TDbFilter<DataType>, data: Record<string, unknown>): Promise<TDbUpdateResult>;
405
- deleteMany(filter: TDbFilter<DataType>): Promise<TDbDeleteResult>;
349
+ count(query?: Uniquery<DataType>): Promise<number>;
350
+ updateMany(filter: FilterExpr<DataType>, data: Partial<DataType> & Record<string, unknown>): Promise<TDbUpdateResult>;
351
+ replaceMany(filter: FilterExpr<DataType>, data: Record<string, unknown>): Promise<TDbUpdateResult>;
352
+ deleteMany(filter: FilterExpr<DataType>): Promise<TDbDeleteResult>;
406
353
  /**
407
354
  * Synchronizes indexes between Atscript definitions and the database.
408
355
  * Delegates to the adapter, which uses `this._table.indexes`.
@@ -433,7 +380,7 @@ declare class AtscriptDbTable<T extends TAtscriptAnnotatedType = TAtscriptAnnota
433
380
  /**
434
381
  * Extracts primary key field(s) from a payload to build a filter.
435
382
  */
436
- protected _extractPrimaryKeyFilter(payload: Record<string, unknown>): TDbFilter;
383
+ protected _extractPrimaryKeyFilter(payload: Record<string, unknown>): FilterExpr;
437
384
  /**
438
385
  * Builds a validator for a given purpose with adapter plugins.
439
386
  */
@@ -485,4 +432,4 @@ type TDbPatch<T> = {
485
432
  declare function getKeyProps(def: TAtscriptAnnotatedType<TAtscriptTypeArray>): Set<string>;
486
433
 
487
434
  export { AtscriptDbTable, BaseDbAdapter, NoopLogger, decomposePatch, getKeyProps, resolveDesignType };
488
- export type { TArrayPatch, TDbDefaultValue, TDbDeleteResult, TDbFieldMeta, TDbFilter, TDbFindOptions, TDbIndex, TDbIndexField, TDbInsertManyResult, TDbInsertResult, TDbPatch, TDbProjection, TDbUpdateResult, TFilterOperators, TGenericLogger, TIdDescriptor };
435
+ export type { TArrayPatch, TDbDefaultValue, TDbDeleteResult, TDbFieldMeta, TDbIndex, TDbIndexField, TDbInsertManyResult, TDbInsertResult, TDbPatch, TDbUpdateResult, TGenericLogger, TIdDescriptor };
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import { flattenAnnotatedType, isAnnotatedType } from "@atscript/typescript/utils";
2
+ import { isPrimitive, walkFilter } from "@uniqu/core";
2
3
 
3
4
  //#region packages/utils-db/src/logger.ts
4
5
  const NoopLogger = {
@@ -163,15 +164,15 @@ var AtscriptDbTable = class {
163
164
  return this._fieldDescriptors;
164
165
  }
165
166
  /**
166
- * Resolves a projection to a list of field names to include.
167
+ * Resolves `$select` from {@link UniqueryControls} to a list of field names.
167
168
  * - `undefined` → `undefined` (all fields)
168
169
  * - `string[]` → pass through
169
170
  * - `Record<K, 1>` → extract included keys
170
171
  * - `Record<K, 0>` → invert using known field names
171
- */ resolveProjection(projection) {
172
- if (!projection) return undefined;
173
- if (Array.isArray(projection)) return projection.length > 0 ? projection : undefined;
174
- const entries = Object.entries(projection);
172
+ */ resolveProjection(select) {
173
+ if (!select) return undefined;
174
+ if (Array.isArray(select)) return select.length > 0 ? select : undefined;
175
+ const entries = Object.entries(select);
175
176
  if (entries.length === 0) return undefined;
176
177
  const firstVal = entries[0][1];
177
178
  if (firstVal === 1) return entries.filter(([, v]) => v === 1).map(([k]) => k);
@@ -266,19 +267,23 @@ var AtscriptDbTable = class {
266
267
  return this.adapter.deleteOne(filter);
267
268
  }
268
269
  /**
269
- * Finds a single record matching the filter.
270
- */ async findOne(filter, options) {
271
- return this.adapter.findOne(filter, options);
270
+ * Finds a single record matching the query.
271
+ */ async findOne(query) {
272
+ return this.adapter.findOne(query);
272
273
  }
273
274
  /**
274
- * Finds all records matching the filter.
275
- */ async findMany(filter, options) {
276
- return this.adapter.findMany(filter, options);
275
+ * Finds all records matching the query.
276
+ */ async findMany(query) {
277
+ return this.adapter.findMany(query);
277
278
  }
278
279
  /**
279
- * Counts records matching the filter.
280
- */ async count(filter = {}) {
281
- return this.adapter.count(filter);
280
+ * Counts records matching the query.
281
+ */ async count(query) {
282
+ query ?? (query = {
283
+ filter: {},
284
+ controls: {}
285
+ });
286
+ return this.adapter.count(query);
282
287
  }
283
288
  async updateMany(filter, data) {
284
289
  return this.adapter.updateMany(filter, data);
@@ -563,4 +568,4 @@ var BaseDbAdapter = class {
563
568
  };
564
569
 
565
570
  //#endregion
566
- export { AtscriptDbTable, BaseDbAdapter, NoopLogger, decomposePatch, getKeyProps, resolveDesignType };
571
+ export { AtscriptDbTable, BaseDbAdapter, NoopLogger, decomposePatch, getKeyProps, isPrimitive, resolveDesignType, walkFilter };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/utils-db",
3
- "version": "0.1.28",
3
+ "version": "0.1.30",
4
4
  "description": "Database adapter utilities for atscript.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -36,8 +36,9 @@
36
36
  "vitest": "3.2.4"
37
37
  },
38
38
  "peerDependencies": {
39
- "@atscript/core": "^0.1.28",
40
- "@atscript/typescript": "^0.1.28"
39
+ "@uniqu/core": "^0.0.1",
40
+ "@atscript/core": "^0.1.30",
41
+ "@atscript/typescript": "^0.1.30"
41
42
  },
42
43
  "scripts": {
43
44
  "pub": "pnpm publish --access public",