@afora/core 0.0.5 → 0.0.6

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 (33) hide show
  1. package/README.md +2 -0
  2. package/dist/core/application/types/application.interface.d.ts +45 -11
  3. package/dist/core/blueprint/types/slice.interface.d.ts +5 -2
  4. package/dist/core/entity/abstract-entity.d.ts +1 -1
  5. package/dist/core/entity/types/entity.interface.d.ts +13 -7
  6. package/dist/core/exception/exception.d.ts +10 -1
  7. package/dist/core/output/status.d.ts +2 -2
  8. package/dist/core/property/property.d.ts +4 -4
  9. package/dist/core/property/types/optional-property.type.d.ts +2 -2
  10. package/dist/core/property/types/property-type.interface.d.ts +17 -0
  11. package/dist/core/record/abstract-record.d.ts +4 -4
  12. package/dist/core/record/exceptions.d.ts +2 -0
  13. package/dist/core/record/name.d.ts +16 -0
  14. package/dist/core/record/record.d.ts +4 -3
  15. package/dist/core/record/types/plugin.interface.d.ts +11 -0
  16. package/dist/core/record/types/record.interface.d.ts +71 -10
  17. package/dist/core/repository/cursor.d.ts +13 -0
  18. package/dist/core/repository/exceptions.d.ts +10 -0
  19. package/dist/core/repository/query.d.ts +4 -4
  20. package/dist/core/repository/repository.d.ts +1 -1
  21. package/dist/core/repository/types/query.interface.d.ts +37 -0
  22. package/dist/core/repository/types/repository.interface.d.ts +10 -6
  23. package/dist/core/use-case/steps/types/steps.interface.d.ts +1 -1
  24. package/dist/experimental.js +3 -2
  25. package/dist/{index-hn82hm31.js → index-3nd9bjnc.js} +69 -10
  26. package/dist/{index-8m80wjzf.js → index-gbgc40xe.js} +188 -39
  27. package/dist/index.d.ts +7 -6
  28. package/dist/index.js +8 -2
  29. package/dist/std/properties/password.d.ts +5 -1
  30. package/dist/std/record-plugins.d.ts +5 -5
  31. package/dist/testing/index.js +91 -2
  32. package/dist/testing/provider-contract.d.ts +5 -0
  33. package/package.json +1 -1
@@ -52,7 +52,7 @@ export declare abstract class AbstractRepository<Entity extends AnyRepositoryEnt
52
52
  * unique key (`id`, `options.unique`, a `unique` property), giving the entity
53
53
  * or `undefined`; `findManyBy<Key>` for any other key, giving a list;
54
54
  * `create`, `update`, `delete`, `count`, and the queries `findMany`,
55
- * `exists` and `findPage`. Every method is async and reads and writes through `provider`
55
+ * `exists`, `findPage` and `findCursor`. Every method is async and reads and writes through `provider`
56
56
  * (a new `inMemoryProvider()` by default), which gets the variables of
57
57
  * `environmentDependencies` as `context.env`. The repository can be a
58
58
  * dependency of a use case, and `RepositoryOf` extends it with a class.
@@ -30,6 +30,41 @@ export interface RepositoryPage<Entity> {
30
30
  readonly limit: number;
31
31
  readonly offset: number;
32
32
  }
33
+ /**
34
+ * The query of `findCursor`: as `RepositoryQuery`, with the `next` of the
35
+ * previous page as `after` in place of `offset`. `where`, `orderBy` and
36
+ * `withDeleted` must stay the same from one page to the next.
37
+ */
38
+ export interface RepositoryCursorQuery<Entity = unknown> extends Omit<RepositoryQuery<Entity>, "offset" | "limit"> {
39
+ /** Positive integer; 20 when left out. */
40
+ readonly limit?: number;
41
+ /** The `next` of the page before; the first page when left out. */
42
+ readonly after?: string;
43
+ }
44
+ /** A page read by position: its items and, when there are more, the cursor of the next. */
45
+ export interface RepositoryCursorPage<Entity> {
46
+ readonly items: readonly EntityInstanceOf<Entity>[];
47
+ readonly limit: number;
48
+ /** What to give as `after` for the next page; `undefined` on the last one. */
49
+ readonly next: string | undefined;
50
+ }
51
+ /**
52
+ * What a provider gets from `findCursor` on `findMany`: `after` holds one
53
+ * value for each order of `orderBy`, those of the last row already read, and
54
+ * the provider gives the rows strictly after it in that order. Only a
55
+ * provider with `capabilities.keyset` is given it.
56
+ */
57
+ export interface ProviderQuery extends RepositoryQuery {
58
+ readonly after?: readonly unknown[];
59
+ }
60
+ export interface RepositoryFindCursor<Entity> {
61
+ /**
62
+ * A page of `limit` items (20 by default) after the cursor `after`. Unlike
63
+ * `findPage` it reads one page with one query, does not count, and stays
64
+ * correct when rows are added or removed between pages.
65
+ */
66
+ findCursor(query?: RepositoryCursorQuery<Entity>): Promise<RepositoryCursorPage<Entity>>;
67
+ }
33
68
  export interface RepositoryFindMany<Entity> {
34
69
  findMany(query?: RepositoryQuery<Entity>): Promise<EntityInstanceOf<Entity>[]>;
35
70
  }
@@ -57,6 +92,8 @@ export type ProviderDescription = {
57
92
  export interface ProviderCapabilities {
58
93
  readonly operators?: readonly string[];
59
94
  readonly transactions?: boolean;
95
+ /** Whether `findMany` honors `after` (`ProviderQuery`), which `findCursor` needs. */
96
+ readonly keyset?: boolean;
60
97
  }
61
98
  /** Declaration input for a named provider, implemented by providerOf in T10. */
62
99
  export interface ProviderSettings<Options extends ProviderOptions = ProviderOptions, Provider extends IRepositoryProvider = IRepositoryProvider> {
@@ -3,7 +3,7 @@ import type { AbstractEntity, EntityUniqueKeys } from "../../entity/abstract-ent
3
3
  import type { AnyEntityClass } from "../../entity/event.js";
4
4
  import type { RecordSettings } from "../../record/types/record.interface.js";
5
5
  import type { AbstractRepository } from "../repository.js";
6
- import type { ProviderCapabilities, RepositoryExists, RepositoryFindMany, RepositoryFindPage, RepositoryQuery } from "./query.interface.js";
6
+ import type { ProviderCapabilities, ProviderQuery, RepositoryExists, RepositoryFindCursor, RepositoryFindMany, RepositoryFindPage, RepositoryQuery } from "./query.interface.js";
7
7
  /** Any class made by `entityOf`, or extending one, that a repository holds. */
8
8
  export type AnyRepositoryEntity = AnyRecordClass & AnyEntityClass & {
9
9
  readonly [EntityUniqueKeys]: readonly string[];
@@ -67,9 +67,9 @@ export type RepositoryFindManyBy<Entity, Actions extends string = string> = {
67
67
  * so that a class extending a repository can override them.
68
68
  * @internal
69
69
  */
70
- export type RepositoryMethods<Entity, Actions extends string = string> = RepositoryFindBy<Entity, Actions> & RepositoryFindManyBy<Entity, Actions> & ("create" extends Actions ? RepositoryCreate<Entity> : unknown) & ("update" extends Actions ? RepositoryUpdate<Entity> : unknown) & ("delete" extends Actions ? RepositoryDelete<Entity> : unknown) & ("count" extends Actions ? RepositoryCount<Entity> : unknown) & ("findMany" extends Actions ? RepositoryFindMany<Entity> : unknown) & ("exists" extends Actions ? RepositoryExists<Entity> : unknown) & ("findPage" extends Actions ? RepositoryFindPage<Entity> : unknown);
70
+ export type RepositoryMethods<Entity, Actions extends string = string> = RepositoryFindBy<Entity, Actions> & RepositoryFindManyBy<Entity, Actions> & ("create" extends Actions ? RepositoryCreate<Entity> : unknown) & ("update" extends Actions ? RepositoryUpdate<Entity> : unknown) & ("delete" extends Actions ? RepositoryDelete<Entity> : unknown) & ("count" extends Actions ? RepositoryCount<Entity> : unknown) & ("findMany" extends Actions ? RepositoryFindMany<Entity> : unknown) & ("exists" extends Actions ? RepositoryExists<Entity> : unknown) & ("findPage" extends Actions ? RepositoryFindPage<Entity> : unknown) & ("findCursor" extends Actions ? RepositoryFindCursor<Entity> : unknown);
71
71
  /** A method a repository of `Entity` can have, e.g. `"findByEmail"`. */
72
- export type RepositoryAction<Entity> = keyof RepositoryFindBy<Entity> | keyof RepositoryFindManyBy<Entity> | "create" | "update" | "delete" | "count" | "findMany" | "exists" | "findPage";
72
+ export type RepositoryAction<Entity> = keyof RepositoryFindBy<Entity> | keyof RepositoryFindManyBy<Entity> | "create" | "update" | "delete" | "count" | "findMany" | "exists" | "findPage" | "findCursor";
73
73
  /**
74
74
  * A repository of `Entity` with the methods of `Actions`, whose provider gets
75
75
  * `Environment` as `context.env`, e.g.
@@ -82,9 +82,12 @@ export type AnyRepository = AbstractRepository<any, any, any>;
82
82
  export type RepositoryData = Readonly<Record<string, unknown>>;
83
83
  /** What a provider gets with every call: whose entities, and the environment. */
84
84
  export interface RepositoryContext<Environment = unknown> {
85
- /** The entity class, e.g. to name a table after it. */
85
+ /** The entity class; name a table after `recordNameOf(entity)`, not `entity.name`. */
86
86
  readonly entity: AnyRepositoryEntity;
87
- /** The name of the repository, for the source of its exceptions. */
87
+ /**
88
+ * The name of the repository, `<entity name>Repository`, for the source of
89
+ * its exceptions; stable under minification when the entity declares a `name`.
90
+ */
88
91
  readonly name: string;
89
92
  /** The keys no two entities may share, `id` first. */
90
93
  readonly unique: readonly string[];
@@ -99,7 +102,8 @@ export interface RepositoryContext<Environment = unknown> {
99
102
  */
100
103
  export interface IRepositoryProvider<Environment = unknown> {
101
104
  findOne(context: RepositoryContext<Environment>, query?: RepositoryQuery): Promise<RepositoryData | undefined>;
102
- findMany(context: RepositoryContext<Environment>, query?: RepositoryQuery): Promise<readonly RepositoryData[]>;
105
+ /** `query.after` is only given to a provider with `capabilities.keyset`. */
106
+ findMany(context: RepositoryContext<Environment>, query?: ProviderQuery): Promise<readonly RepositoryData[]>;
103
107
  insert(context: RepositoryContext<Environment>, data: RepositoryData): Promise<void>;
104
108
  update(context: RepositoryContext<Environment>, data: RepositoryData): Promise<void>;
105
109
  remove(context: RepositoryContext<Environment>, id: string): Promise<void>;
@@ -11,7 +11,7 @@ export type StepValue = StepReference | string | number | boolean | null | reado
11
11
  readonly [key: string]: StepValue;
12
12
  };
13
13
  /** The actions of a repository that only read. */
14
- export type StepReadAction = `findBy${string}` | `findManyBy${string}` | "findMany" | "exists" | "findPage" | "count";
14
+ export type StepReadAction = `findBy${string}` | `findManyBy${string}` | "findMany" | "exists" | "findPage" | "findCursor" | "count";
15
15
  type RepositoryKeys<Dependencies> = {
16
16
  [Key in keyof Dependencies & string]: Dependencies[Key] extends AnyRepository ? Key : never;
17
17
  }[keyof Dependencies & string];
@@ -2,6 +2,7 @@ import {
2
2
  isBlueprint2,
3
3
  isRecordClass2,
4
4
  recordFieldOf2,
5
+ recordNameOf2,
5
6
  listOf2,
6
7
  isBlueprintList2,
7
8
  AbstractRecord2,
@@ -9,7 +10,7 @@ import {
9
10
  UndeclaredBlueprintProperty2,
10
11
  optionalOf2,
11
12
  blueprintFromProperties
12
- } from "./index-hn82hm31.js";
13
+ } from "./index-3nd9bjnc.js";
13
14
 
14
15
  // src/core/blueprint/slice.ts
15
16
  function propertiesOfField(field) {
@@ -69,7 +70,7 @@ function slicedRecordOf(Original, mask, path) {
69
70
  sensitive: kept(sensitive),
70
71
  unique: kept(unique),
71
72
  change: kept(change)
72
- }, Original.name);
73
+ }, recordNameOf2(Original));
73
74
  Object.defineProperty(Sliced, Symbol.hasInstance, {
74
75
  value(value) {
75
76
  return Function.prototype[Symbol.hasInstance].call(this, value) || value instanceof Original;
@@ -17,6 +17,7 @@ var __export = (target, all) => {
17
17
  var exports_exceptions = {};
18
18
  __export(exports_exceptions, {
19
19
  IncompatibleRecordPlugin: () => IncompatibleRecordPlugin2,
20
+ InvalidRecordDeclaration: () => InvalidRecordDeclaration2,
20
21
  InvalidRecordPlugin: () => InvalidRecordPlugin2,
21
22
  InvalidRecordValue: () => InvalidRecordValue2,
22
23
  RecordPluginConflict: () => RecordPluginConflict2,
@@ -307,6 +308,31 @@ function recordFieldOf2(field) {
307
308
  return;
308
309
  }
309
310
 
311
+ // src/core/record/name.ts
312
+ var RECORD_NAME = /^[A-Za-z_$][\w$]*$/;
313
+ function isRecordName(name) {
314
+ return typeof name === "string" && RECORD_NAME.test(name);
315
+ }
316
+ function recordNameOf2(Class) {
317
+ const { recordName, name } = Class;
318
+ return typeof recordName === "string" ? recordName : String(name);
319
+ }
320
+ var declaredNames = new WeakMap;
321
+ function declareRecordName(Class, name) {
322
+ declaredNames.set(Class, name);
323
+ }
324
+ function declaredRecordNameOf(Class) {
325
+ for (let current = Class;typeof current === "function"; current = Object.getPrototypeOf(current)) {
326
+ const own = Object.getOwnPropertyDescriptor(current, "recordName")?.value;
327
+ if (typeof own === "string")
328
+ return own;
329
+ const declared = declaredNames.get(current);
330
+ if (declared !== undefined)
331
+ return declared;
332
+ }
333
+ return;
334
+ }
335
+
310
336
  // src/core/parse/issue.ts
311
337
  function withLazyCause(issue, build) {
312
338
  let cause;
@@ -454,7 +480,7 @@ function fieldAt(field, value, parent, key, source) {
454
480
  if (value instanceof record.record)
455
481
  return { value };
456
482
  if (!isPlainObject(value)) {
457
- return rejection("not_record", `must be an instance of record "${record.record.name}"`, pathAt(parent, key), source);
483
+ return rejection("not_record", `must be an instance of record "${recordNameOf2(record.record)}"`, pathAt(parent, key), source);
458
484
  }
459
485
  const result = record.record.safeParse(value);
460
486
  return result.issues ? underPath(result, pathAt(parent, key)) : result;
@@ -587,14 +613,16 @@ class Exception2 extends Error {
587
613
  message = this.message,
588
614
  details
589
615
  }) {
590
- return new Exception2({
616
+ const keepsClass = name === this.name && details === undefined && isDeclaredInstance(this);
617
+ const init = {
591
618
  layer,
592
619
  source,
593
620
  name,
594
621
  message,
595
- details,
622
+ details: keepsClass ? this.details : details,
596
623
  cause: this
597
- });
624
+ };
625
+ return keepsClass ? Reflect.construct(Exception2, [init], this.constructor) : new Exception2(init);
598
626
  }
599
627
  toJSON() {
600
628
  return { ...this.frame, trace: this.trace };
@@ -612,6 +640,13 @@ class Exception2 extends Error {
612
640
  }
613
641
  }
614
642
  var declarations = new WeakMap;
643
+ function isDeclaredInstance(exception) {
644
+ for (let current = exception.constructor;typeof current === "function"; current = Object.getPrototypeOf(current)) {
645
+ if (declarations.has(current))
646
+ return true;
647
+ }
648
+ return false;
649
+ }
615
650
  function isDeclaredException(value) {
616
651
  return typeof value === "function" && declarations.has(value);
617
652
  }
@@ -660,6 +695,11 @@ var InvalidRecordValue2 = exceptionOf2({
660
695
  errors
661
696
  }, source) => `Invalid value for ${key ? `"${key}" of ` : ""}record "${source}": ${errors.join("; ")}`
662
697
  });
698
+ var InvalidRecordDeclaration2 = exceptionOf2({
699
+ layer: "record" /* Record */,
700
+ name: "InvalidRecordDeclaration",
701
+ message: (reason, source) => `Invalid declaration of record "${source}": ${reason}`
702
+ });
663
703
  var UndeclaredRecordProperty2 = exceptionOf2({
664
704
  layer: "record" /* Record */,
665
705
  name: "UndeclaredRecordProperty",
@@ -781,7 +821,10 @@ function checkMembers(record, keys) {
781
821
  for (let prototype = Object.getPrototypeOf(record);prototype !== null && !Object.hasOwn(prototype.constructor, "blueprint"); prototype = Object.getPrototypeOf(prototype)) {
782
822
  const key = Object.getOwnPropertyNames(prototype).find((name) => keys.includes(name));
783
823
  if (key !== undefined) {
784
- throw new ReservedRecordKey2({ source: Class.name, details: { key } });
824
+ throw new ReservedRecordKey2({
825
+ source: recordNameOf2(Class),
826
+ details: { key }
827
+ });
785
828
  }
786
829
  }
787
830
  checked.add(Class);
@@ -837,7 +880,7 @@ function parseRecord(Class, value) {
837
880
  try {
838
881
  return { value: new Class(value) };
839
882
  } catch (error) {
840
- return { issues: issuesOfError(error, Class.name) };
883
+ return { issues: issuesOfError(error, recordNameOf2(Class)) };
841
884
  }
842
885
  }
843
886
  function recordValue(record) {
@@ -868,7 +911,7 @@ function recordFieldDataOf(field, value, options) {
868
911
  return value;
869
912
  }
870
913
  function storeRecordValue(record, key, value) {
871
- const source = record.constructor.name;
914
+ const source = recordNameOf2(record.constructor);
872
915
  const { properties } = record[Meta2].blueprint;
873
916
  if (!Object.hasOwn(properties, key)) {
874
917
  throw exceptionOfIssues([
@@ -921,7 +964,7 @@ class AbstractRecord2 {
921
964
  }
922
965
  constructor(value) {
923
966
  checkMembers(this, Object.keys(this[Meta2].blueprint.properties));
924
- const source = this.constructor.name;
967
+ const source = recordNameOf2(this.constructor);
925
968
  const result = parseCollection(this[Meta2].blueprint.properties, value, [], source);
926
969
  if (result.issues)
927
970
  throw exceptionOfIssues(result.issues, source);
@@ -1140,7 +1183,14 @@ function pluginContextOf(catalogs) {
1140
1183
  return Object.freeze(Object.assign({}, ...catalogs.map((catalog) => catalog.plugins)));
1141
1184
  }
1142
1185
  var recordOf3 = recordBuilderOf2();
1143
- function declareRecord(Base, declared, { plugins = [], ...declaredOptions }, name, reserved = [], context = {}) {
1186
+ function declareRecord(Base, declared, { plugins = [], name: declaredName, ...declaredOptions }, fallback, reserved = [], context = {}) {
1187
+ if (declaredName !== undefined && !isRecordName(declaredName)) {
1188
+ throw new InvalidRecordDeclaration2({
1189
+ source: fallback,
1190
+ details: `"name" must be an identifier, got ${JSON.stringify(declaredName)}.`
1191
+ });
1192
+ }
1193
+ const name = declaredName ?? fallback;
1144
1194
  const applied = applyPlugins(declared, typeof plugins === "function" ? plugins(context) : plugins, name, reserved);
1145
1195
  const blueprint = applied.blueprint;
1146
1196
  const sensitive = [
@@ -1192,6 +1242,15 @@ function declareRecord(Base, declared, { plugins = [], ...declaredOptions }, nam
1192
1242
  static [RecordPlugins] = applied.plugins;
1193
1243
  static [RecordPluginHooks] = applied.hooks;
1194
1244
  }
1245
+ if (declaredName !== undefined) {
1246
+ declareRecordName(DeclaredRecord, declaredName);
1247
+ }
1248
+ Object.defineProperty(DeclaredRecord, "recordName", {
1249
+ get() {
1250
+ return declaredName ?? this.name;
1251
+ },
1252
+ configurable: true
1253
+ });
1195
1254
  exposeRecordKeys(DeclaredRecord.prototype, keys);
1196
1255
  for (const [member, descriptor] of Object.entries(applied.descriptors)) {
1197
1256
  Object.defineProperty(DeclaredRecord.prototype, member, descriptor);
@@ -1648,4 +1707,4 @@ function schemaOfField(key, field) {
1648
1707
  });
1649
1708
  }
1650
1709
 
1651
- export { __export, isProperty2, schemaOf2, Demo2, recordPropertyLineage, propertyLineageOf2, fieldOf, Kind2, isBlueprint2, isRecordClass2, isOptionalRecord2, recordFieldOf2, listOf2, isBlueprintList2, ExceptionLayers2, UNEXPECTED_ERROR2, Exception2, isDeclaredException, declaredMessageOf, exceptionOf2, Meta2, InvalidRecordValue2, UndeclaredRecordProperty2, ReservedRecordKey2, InvalidRecordPlugin2, RecordPluginConflict2, UnknownRecordPlugin2, IncompatibleRecordPlugin2, exports_exceptions, MASK2, RESERVED_RECORD_KEYS2, recordDataOf, recordFieldDataOf, storeRecordValue, runRecordEffects, AbstractRecord2, IdentifiedRecord, isVersion, versionParts, pluginsOf2, isRecordPlugin2, hasPlugin2, recordPluginOf2, recordBuilderOf2, pluginContextOf, recordOf3, declareRecord, ReservedBlueprintKey2, UndeclaredBlueprintProperty2, exports_exceptions2, PropertyRequired2, InvalidPropertyValue2, exports_exceptions3, State2, AbstractProperty2, PropertyStates2, AbstractDefineProperty2, propertyOf2, optionalOf2, blueprintBuilderOf2, blueprintFromProperties };
1710
+ export { __export, isProperty2, schemaOf2, Demo2, recordPropertyLineage, propertyLineageOf2, fieldOf, Kind2, isBlueprint2, isRecordClass2, isOptionalRecord2, recordFieldOf2, recordNameOf2, declaredRecordNameOf, listOf2, isBlueprintList2, ExceptionLayers2, UNEXPECTED_ERROR2, Exception2, isDeclaredException, declaredMessageOf, exceptionOf2, Meta2, InvalidRecordValue2, InvalidRecordDeclaration2, UndeclaredRecordProperty2, ReservedRecordKey2, InvalidRecordPlugin2, RecordPluginConflict2, UnknownRecordPlugin2, IncompatibleRecordPlugin2, exports_exceptions, MASK2, RESERVED_RECORD_KEYS2, recordDataOf, recordFieldDataOf, storeRecordValue, runRecordEffects, AbstractRecord2, IdentifiedRecord, isVersion, versionParts, pluginsOf2, isRecordPlugin2, hasPlugin2, recordPluginOf2, recordBuilderOf2, pluginContextOf, recordOf3, declareRecord, ReservedBlueprintKey2, UndeclaredBlueprintProperty2, exports_exceptions2, PropertyRequired2, InvalidPropertyValue2, exports_exceptions3, State2, AbstractProperty2, PropertyStates2, AbstractDefineProperty2, propertyOf2, optionalOf2, blueprintBuilderOf2, blueprintFromProperties };