@atscript/utils-db 0.1.28 → 0.1.29
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 +31 -15
- package/dist/index.d.ts +25 -78
- package/dist/index.mjs +19 -15
- package/package.json +4 -3
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
|
|
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(
|
|
196
|
-
if (!
|
|
197
|
-
if (Array.isArray(
|
|
198
|
-
const entries = Object.entries(
|
|
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,22 @@ var AtscriptDbTable = class {
|
|
|
290
291
|
return this.adapter.deleteOne(filter);
|
|
291
292
|
}
|
|
292
293
|
/**
|
|
293
|
-
* Finds a single record matching the
|
|
294
|
-
*/ async findOne(
|
|
295
|
-
return this.adapter.findOne(
|
|
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
|
|
299
|
-
*/ async findMany(
|
|
300
|
-
return this.adapter.findMany(
|
|
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
|
|
304
|
-
*/ async count(
|
|
305
|
-
|
|
304
|
+
* Counts records matching the query.
|
|
305
|
+
*/ async count(query = {
|
|
306
|
+
filter: {},
|
|
307
|
+
controls: {}
|
|
308
|
+
}) {
|
|
309
|
+
return this.adapter.count(query);
|
|
306
310
|
}
|
|
307
311
|
async updateMany(filter, data) {
|
|
308
312
|
return this.adapter.updateMany(filter, data);
|
|
@@ -592,4 +596,16 @@ exports.BaseDbAdapter = BaseDbAdapter
|
|
|
592
596
|
exports.NoopLogger = NoopLogger
|
|
593
597
|
exports.decomposePatch = decomposePatch
|
|
594
598
|
exports.getKeyProps = getKeyProps
|
|
595
|
-
exports
|
|
599
|
+
Object.defineProperty(exports, 'isPrimitive', {
|
|
600
|
+
enumerable: true,
|
|
601
|
+
get: function () {
|
|
602
|
+
return __uniqu_core.isPrimitive;
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
exports.resolveDesignType = resolveDesignType
|
|
606
|
+
Object.defineProperty(exports, 'walkFilter', {
|
|
607
|
+
enumerable: true,
|
|
608
|
+
get: function () {
|
|
609
|
+
return __uniqu_core.walkFilter;
|
|
610
|
+
}
|
|
611
|
+
});
|
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:
|
|
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:
|
|
248
|
-
abstract updateOne(filter:
|
|
249
|
-
abstract deleteOne(filter:
|
|
250
|
-
abstract findOne(
|
|
251
|
-
abstract findMany(
|
|
252
|
-
abstract count(
|
|
253
|
-
abstract updateMany(filter:
|
|
254
|
-
abstract replaceMany(filter:
|
|
255
|
-
abstract deleteMany(filter:
|
|
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
|
|
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(
|
|
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
|
|
339
|
+
* Finds a single record matching the query.
|
|
393
340
|
*/
|
|
394
|
-
findOne(
|
|
341
|
+
findOne(query: Uniquery<DataType>): Promise<DataType | null>;
|
|
395
342
|
/**
|
|
396
|
-
* Finds all records matching the
|
|
343
|
+
* Finds all records matching the query.
|
|
397
344
|
*/
|
|
398
|
-
findMany(
|
|
345
|
+
findMany(query: Uniquery<DataType>): Promise<DataType[]>;
|
|
399
346
|
/**
|
|
400
|
-
* Counts records matching the
|
|
347
|
+
* Counts records matching the query.
|
|
401
348
|
*/
|
|
402
|
-
count(
|
|
403
|
-
updateMany(filter:
|
|
404
|
-
replaceMany(filter:
|
|
405
|
-
deleteMany(filter:
|
|
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>):
|
|
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,
|
|
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
|
|
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(
|
|
172
|
-
if (!
|
|
173
|
-
if (Array.isArray(
|
|
174
|
-
const entries = Object.entries(
|
|
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,22 @@ var AtscriptDbTable = class {
|
|
|
266
267
|
return this.adapter.deleteOne(filter);
|
|
267
268
|
}
|
|
268
269
|
/**
|
|
269
|
-
* Finds a single record matching the
|
|
270
|
-
*/ async findOne(
|
|
271
|
-
return this.adapter.findOne(
|
|
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
|
|
275
|
-
*/ async findMany(
|
|
276
|
-
return this.adapter.findMany(
|
|
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
|
|
280
|
-
*/ async count(
|
|
281
|
-
|
|
280
|
+
* Counts records matching the query.
|
|
281
|
+
*/ async count(query = {
|
|
282
|
+
filter: {},
|
|
283
|
+
controls: {}
|
|
284
|
+
}) {
|
|
285
|
+
return this.adapter.count(query);
|
|
282
286
|
}
|
|
283
287
|
async updateMany(filter, data) {
|
|
284
288
|
return this.adapter.updateMany(filter, data);
|
|
@@ -563,4 +567,4 @@ var BaseDbAdapter = class {
|
|
|
563
567
|
};
|
|
564
568
|
|
|
565
569
|
//#endregion
|
|
566
|
-
export { AtscriptDbTable, BaseDbAdapter, NoopLogger, decomposePatch, getKeyProps, resolveDesignType };
|
|
570
|
+
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.
|
|
3
|
+
"version": "0.1.29",
|
|
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
|
-
"@
|
|
40
|
-
"@atscript/
|
|
39
|
+
"@uniqu/core": "^0.0.1",
|
|
40
|
+
"@atscript/core": "^0.1.29",
|
|
41
|
+
"@atscript/typescript": "^0.1.29"
|
|
41
42
|
},
|
|
42
43
|
"scripts": {
|
|
43
44
|
"pub": "pnpm publish --access public",
|