@nymphjs/nymph 1.0.0-beta.9 → 1.0.0-beta.91

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 (96) hide show
  1. package/CHANGELOG.md +436 -0
  2. package/README.md +10 -20
  3. package/dist/Entity.d.ts +277 -10
  4. package/dist/Entity.js +543 -148
  5. package/dist/Entity.js.map +1 -1
  6. package/dist/Entity.test.js +53 -13
  7. package/dist/Entity.test.js.map +1 -1
  8. package/dist/Entity.types.d.ts +249 -23
  9. package/dist/Entity.types.js +1 -2
  10. package/dist/Nymph.d.ts +431 -12
  11. package/dist/Nymph.js +692 -180
  12. package/dist/Nymph.js.map +1 -1
  13. package/dist/Nymph.types.d.ts +65 -7
  14. package/dist/Nymph.types.js +3 -5
  15. package/dist/Nymph.types.js.map +1 -1
  16. package/dist/conf/d.d.ts +43 -0
  17. package/dist/conf/d.js +1 -2
  18. package/dist/conf/defaults.d.ts +1 -1
  19. package/dist/conf/defaults.js +26 -3
  20. package/dist/conf/defaults.js.map +1 -1
  21. package/dist/conf/index.d.ts +2 -2
  22. package/dist/conf/index.js +2 -8
  23. package/dist/conf/index.js.map +1 -1
  24. package/dist/driver/NymphDriver.d.ts +137 -39
  25. package/dist/driver/NymphDriver.js +344 -194
  26. package/dist/driver/NymphDriver.js.map +1 -1
  27. package/dist/driver/index.d.ts +2 -2
  28. package/dist/driver/index.js +3 -23
  29. package/dist/driver/index.js.map +1 -1
  30. package/dist/driver/testArtifacts.d.ts +2 -2
  31. package/dist/driver/testArtifacts.js +557 -92
  32. package/dist/driver/testArtifacts.js.map +1 -1
  33. package/dist/errors/ClassNotAvailableError.js +1 -5
  34. package/dist/errors/ClassNotAvailableError.js.map +1 -1
  35. package/dist/errors/EntityConflictError.js +1 -5
  36. package/dist/errors/EntityConflictError.js.map +1 -1
  37. package/dist/errors/EntityInvalidDataError.d.ts +6 -0
  38. package/dist/errors/EntityInvalidDataError.js +8 -6
  39. package/dist/errors/EntityInvalidDataError.js.map +1 -1
  40. package/dist/errors/EntityIsSleepingReferenceError.js +1 -5
  41. package/dist/errors/EntityIsSleepingReferenceError.js.map +1 -1
  42. package/dist/errors/EntityUniqueConstraintError.d.ts +3 -0
  43. package/dist/errors/EntityUniqueConstraintError.js +7 -0
  44. package/dist/errors/EntityUniqueConstraintError.js.map +1 -0
  45. package/dist/errors/InvalidParametersError.js +1 -5
  46. package/dist/errors/InvalidParametersError.js.map +1 -1
  47. package/dist/errors/InvalidStateError.d.ts +3 -0
  48. package/dist/errors/InvalidStateError.js +7 -0
  49. package/dist/errors/InvalidStateError.js.map +1 -0
  50. package/dist/errors/NotConfiguredError.js +1 -5
  51. package/dist/errors/NotConfiguredError.js.map +1 -1
  52. package/dist/errors/QueryFailedError.js +2 -5
  53. package/dist/errors/QueryFailedError.js.map +1 -1
  54. package/dist/errors/UnableToConnectError.js +1 -5
  55. package/dist/errors/UnableToConnectError.js.map +1 -1
  56. package/dist/errors/index.d.ts +10 -8
  57. package/dist/errors/index.js +10 -24
  58. package/dist/errors/index.js.map +1 -1
  59. package/dist/index.d.ts +11 -11
  60. package/dist/index.js +14 -34
  61. package/dist/index.js.map +1 -1
  62. package/dist/nymphJoiProps.js +10 -15
  63. package/dist/nymphJoiProps.js.map +1 -1
  64. package/dist/testArtifacts.d.ts +18 -6
  65. package/dist/testArtifacts.js +32 -37
  66. package/dist/testArtifacts.js.map +1 -1
  67. package/dist/testMocks.d.ts +15 -12
  68. package/dist/testMocks.js +40 -29
  69. package/dist/testMocks.js.map +1 -1
  70. package/dist/utils.d.ts +4 -4
  71. package/dist/utils.js +40 -29
  72. package/dist/utils.js.map +1 -1
  73. package/jest.config.js +11 -2
  74. package/package.json +22 -19
  75. package/src/Entity.test.ts +52 -7
  76. package/src/Entity.ts +458 -147
  77. package/src/Entity.types.ts +64 -74
  78. package/src/Nymph.ts +754 -321
  79. package/src/Nymph.types.ts +131 -26
  80. package/src/conf/d.ts +25 -0
  81. package/src/conf/defaults.ts +34 -1
  82. package/src/conf/index.ts +2 -2
  83. package/src/driver/NymphDriver.ts +362 -359
  84. package/src/driver/index.ts +2 -2
  85. package/src/driver/testArtifacts.ts +609 -185
  86. package/src/errors/EntityUniqueConstraintError.ts +6 -0
  87. package/src/errors/InvalidStateError.ts +6 -0
  88. package/src/errors/index.ts +12 -8
  89. package/src/index.ts +11 -11
  90. package/src/nymphJoiProps.ts +2 -2
  91. package/src/testArtifacts.ts +19 -20
  92. package/src/testMocks.ts +60 -35
  93. package/src/type/locutus.d.ts +3 -0
  94. package/src/utils.ts +39 -18
  95. package/tsconfig.json +5 -3
  96. package/typedoc.json +4 -0
@@ -0,0 +1,6 @@
1
+ export class EntityUniqueConstraintError extends Error {
2
+ constructor(message: string) {
3
+ super(message);
4
+ this.name = 'EntityUniqueConstraintError';
5
+ }
6
+ }
@@ -0,0 +1,6 @@
1
+ export class InvalidStateError extends Error {
2
+ constructor(message: string) {
3
+ super(message);
4
+ this.name = 'InvalidStateError';
5
+ }
6
+ }
@@ -1,15 +1,19 @@
1
- export * from './ClassNotAvailableError';
1
+ export * from './ClassNotAvailableError.js';
2
2
 
3
- export * from './EntityConflictError';
3
+ export * from './EntityConflictError.js';
4
4
 
5
- export * from './EntityInvalidDataError';
5
+ export * from './EntityInvalidDataError.js';
6
6
 
7
- export * from './EntityIsSleepingReferenceError';
7
+ export * from './EntityIsSleepingReferenceError.js';
8
8
 
9
- export * from './InvalidParametersError';
9
+ export * from './EntityUniqueConstraintError.js';
10
10
 
11
- export * from './NotConfiguredError';
11
+ export * from './InvalidParametersError.js';
12
12
 
13
- export * from './QueryFailedError';
13
+ export * from './InvalidStateError.js';
14
14
 
15
- export * from './UnableToConnectError';
15
+ export * from './NotConfiguredError.js';
16
+
17
+ export * from './QueryFailedError.js';
18
+
19
+ export * from './UnableToConnectError.js';
package/src/index.ts CHANGED
@@ -1,20 +1,20 @@
1
- export * from './conf';
2
- export * from './driver';
3
- export * from './errors';
1
+ export * from './conf/index.js';
2
+ export * from './driver/index.js';
3
+ export * from './errors/index.js';
4
4
 
5
- export * from './Entity.types';
5
+ export * from './Entity.types.js';
6
6
 
7
- export * from './Entity';
8
- import Entity from './Entity';
7
+ export * from './Entity.js';
8
+ import Entity from './Entity.js';
9
9
  export { Entity };
10
10
 
11
- export * from './Nymph.types';
11
+ export * from './Nymph.types.js';
12
12
 
13
- export * from './Nymph';
14
- import Nymph from './Nymph';
13
+ export * from './Nymph.js';
14
+ import Nymph from './Nymph.js';
15
15
  export { Nymph };
16
16
 
17
- import nymphJoiProps from './nymphJoiProps';
17
+ import nymphJoiProps from './nymphJoiProps.js';
18
18
  export { nymphJoiProps };
19
19
 
20
- export * from './utils';
20
+ export * from './utils.js';
@@ -4,7 +4,7 @@ export default {
4
4
  guid: Joi.alternatives()
5
5
  .try(
6
6
  Joi.any().only().allow(null),
7
- Joi.string().trim(false).length(24).hex()
7
+ Joi.string().trim(false).length(24).hex(),
8
8
  )
9
9
  .required(),
10
10
  cdate: Joi.alternatives()
@@ -18,7 +18,7 @@ export default {
18
18
  Joi.string().pattern(/[[\x01-\x1F\x7F]]/, {
19
19
  name: 'control characters',
20
20
  invert: true,
21
- })
21
+ }),
22
22
  )
23
23
  .required(),
24
24
  };
@@ -1,4 +1,4 @@
1
- import Entity from './Entity';
1
+ import Entity from './Entity.js';
2
2
 
3
3
  export type TestModelData = {
4
4
  name?: string;
@@ -17,8 +17,10 @@ export type TestModelData = {
17
17
  [k: string]: TestModel & TestModelData;
18
18
  };
19
19
  parent?: TestModel & TestModelData;
20
- // For Import/Export Tests.
20
+ // For import/export tests.
21
21
  index?: string;
22
+ // For uniqueness tests.
23
+ uniques?: string[];
22
24
  };
23
25
 
24
26
  /**
@@ -34,21 +36,16 @@ export class TestModel extends Entity<TestModelData> {
34
36
  protected $protectedTags = ['test', 'notag'];
35
37
  protected $allowlistTags? = ['newtag'];
36
38
 
37
- static async factory(guid?: string): Promise<TestModel & TestModelData> {
38
- return (await super.factory(guid)) as TestModel & TestModelData;
39
- }
39
+ constructor() {
40
+ super();
40
41
 
41
- static factorySync(guid?: string): TestModel & TestModelData {
42
- return super.factorySync(guid) as TestModel & TestModelData;
42
+ this.$addTag('test');
43
+ this.$data.boolean = true;
44
+ this.$data.uniques = [];
43
45
  }
44
46
 
45
- constructor(guid?: string) {
46
- super(guid);
47
-
48
- if (this.guid == null) {
49
- this.$addTag('test');
50
- this.$data.boolean = true;
51
- }
47
+ public async $getUniques() {
48
+ return this.$data.uniques ?? [];
52
49
  }
53
50
 
54
51
  public $useProtectedData() {
@@ -77,12 +74,14 @@ export class TestModel extends Entity<TestModelData> {
77
74
  export class TestBModel extends TestModel {
78
75
  static ETYPE = 'test_b_model';
79
76
  static class = 'TestBModel';
77
+ }
80
78
 
81
- static async factory(guid?: string): Promise<TestBModel & TestModelData> {
82
- return (await super.factory(guid)) as TestBModel & TestModelData;
83
- }
79
+ export type TestEmptyModelData = {};
84
80
 
85
- static factorySync(guid?: string): TestBModel & TestModelData {
86
- return super.factorySync(guid) as TestBModel & TestModelData;
87
- }
81
+ /**
82
+ * This class is a test class that extends the Entity class.
83
+ */
84
+ export class TestEmptyModel extends Entity<TestEmptyModelData> {
85
+ static ETYPE = 'test_empty_model';
86
+ static class = 'TestEmptyModel';
88
87
  }
package/src/testMocks.ts CHANGED
@@ -1,10 +1,15 @@
1
1
  import { guid } from '@nymphjs/guid';
2
2
 
3
- import type Nymph from './Nymph';
4
- import { EntityConstructor, EntityInterface, EntityJson } from './Entity.types';
5
- import { Options, Selector } from './Nymph.types';
6
- import { ClassNotAvailableError } from './errors';
7
- import Entity from './Entity';
3
+ import type Nymph from './Nymph.js';
4
+ import type { EntityInstanceType } from './Entity.js';
5
+ import type {
6
+ EntityConstructor,
7
+ EntityInterface,
8
+ EntityJson,
9
+ } from './Entity.types.js';
10
+ import { Options, Selector } from './Nymph.types.js';
11
+ import { ClassNotAvailableError } from './errors/index.js';
12
+ import Entity from './Entity.js';
8
13
 
9
14
  const entities: { [k: string]: EntityJson } = {};
10
15
 
@@ -15,43 +20,43 @@ export class MockNymphDriver {
15
20
  this.nymph = nymph;
16
21
  }
17
22
 
18
- public getEntitySync<T extends EntityConstructor = EntityConstructor>(
23
+ public async getEntity<T extends EntityConstructor = EntityConstructor>(
19
24
  options: Options<T> & { return: 'guid' },
20
25
  ...selectors: Selector[]
21
- ): string | null;
22
- public getEntitySync<T extends EntityConstructor = EntityConstructor>(
26
+ ): Promise<string | null>;
27
+ public async getEntity<T extends EntityConstructor = EntityConstructor>(
23
28
  options: Options<T>,
24
29
  ...selectors: Selector[]
25
- ): ReturnType<T['factorySync']> | null;
26
- public getEntitySync<T extends EntityConstructor = EntityConstructor>(
30
+ ): Promise<EntityInstanceType<T> | null>;
31
+ public async getEntity<T extends EntityConstructor = EntityConstructor>(
27
32
  options: Options<T> & { return: 'guid' },
28
- guid: string
29
- ): string | null;
30
- public getEntitySync<T extends EntityConstructor = EntityConstructor>(
33
+ guid: string,
34
+ ): Promise<string | null>;
35
+ public async getEntity<T extends EntityConstructor = EntityConstructor>(
31
36
  options: Options<T>,
32
- guid: string
33
- ): ReturnType<T['factorySync']> | null;
34
- public getEntitySync<T extends EntityConstructor = EntityConstructor>(
37
+ guid: string,
38
+ ): Promise<EntityInstanceType<T> | null>;
39
+ public async getEntity<T extends EntityConstructor = EntityConstructor>(
35
40
  options: Options<T> = {},
36
41
  ...selectors: Selector[] | string[]
37
- ): ReturnType<T['factorySync']> | string | null {
42
+ ): Promise<EntityInstanceType<T> | string | null> {
38
43
  const guid =
39
44
  typeof selectors[0] === 'string' ? selectors[0] : selectors[0].guid;
40
45
  if (!options || !guid || typeof guid !== 'string' || !(guid in entities)) {
41
46
  return null;
42
47
  }
43
48
 
44
- const entity: EntityInterface = (
45
- (options.class as T) ?? this.nymph.getEntityClass('Entity')
49
+ const entity = (
50
+ (options.class ?? this.nymph.getEntityClass('Entity')) as T
46
51
  ).factorySync();
47
52
  const entityJson = entities[guid];
48
53
  entity.guid = guid;
49
54
  entity.cdate = entityJson.cdate;
50
55
  entity.mdate = entityJson.mdate;
51
56
  entity.tags = entityJson.tags;
52
- entity.$putData(entityJson.data, {});
57
+ entity.$putData(entityJson.data, {}, 'server');
53
58
 
54
- return entity as ReturnType<T['factorySync']>;
59
+ return entity as EntityInstanceType<T>;
55
60
  }
56
61
  }
57
62
 
@@ -68,24 +73,44 @@ export class MockNymph {
68
73
  this.Entity = this.addEntityClass(Entity);
69
74
  }
70
75
 
71
- public addEntityClass<T extends EntityConstructor>(entityClass: T): T {
76
+ public addEntityClass<T extends EntityConstructor>(EntityClass: T): T {
72
77
  const nymph = this;
73
- class NymphEntity extends entityClass {
78
+ this.entityClasses[EntityClass.class] = class extends EntityClass {
74
79
  static nymph: Nymph = nymph as unknown as Nymph;
75
80
 
76
81
  constructor(...args: any[]) {
77
82
  super(...args);
78
83
  }
84
+ };
85
+ return this.entityClasses[EntityClass.class] as T;
86
+ }
87
+
88
+ public getEntityClass(className: string): EntityConstructor;
89
+ public getEntityClass<T extends EntityConstructor>(className: T): T;
90
+ public getEntityClass<T extends EntityConstructor = EntityConstructor>(
91
+ className: T | string,
92
+ ): T | EntityConstructor {
93
+ let key: string | null = null;
94
+ if (typeof className === 'string') {
95
+ key = className;
96
+ } else {
97
+ key = className.class;
98
+ }
99
+ if (key in this.entityClasses) {
100
+ return this.entityClasses[key];
79
101
  }
80
- this.entityClasses[entityClass.class] = NymphEntity;
81
- return NymphEntity;
102
+ throw new ClassNotAvailableError(`Tried to use missing class "${key}".`);
82
103
  }
83
104
 
84
- public getEntityClass(className: string): EntityConstructor {
85
- if (className in this.entityClasses) {
86
- return this.entityClasses[className];
105
+ public getEntityClassByEtype(etype: string): EntityConstructor {
106
+ for (const EntityClass of Object.values(this.entityClasses)) {
107
+ if (EntityClass.ETYPE === etype) {
108
+ return EntityClass;
109
+ }
87
110
  }
88
- throw new ClassNotAvailableError('Tried to use class: ' + className);
111
+ throw new ClassNotAvailableError(
112
+ `Tried to use missing class by etype "${etype}".`,
113
+ );
89
114
  }
90
115
 
91
116
  public async getEntity<T extends EntityConstructor = EntityConstructor>(
@@ -95,21 +120,21 @@ export class MockNymph {
95
120
  public async getEntity<T extends EntityConstructor = EntityConstructor>(
96
121
  options: Options<T>,
97
122
  ...selectors: Selector[]
98
- ): Promise<ReturnType<T['factorySync']> | null>;
123
+ ): Promise<EntityInstanceType<T> | null>;
99
124
  public async getEntity<T extends EntityConstructor = EntityConstructor>(
100
125
  options: Options<T> & { return: 'guid' },
101
- guid: string
126
+ guid: string,
102
127
  ): Promise<string | null>;
103
128
  public async getEntity<T extends EntityConstructor = EntityConstructor>(
104
129
  options: Options<T>,
105
- guid: string
106
- ): Promise<ReturnType<T['factorySync']> | null>;
130
+ guid: string,
131
+ ): Promise<EntityInstanceType<T> | null>;
107
132
  public async getEntity<T extends EntityConstructor = EntityConstructor>(
108
133
  options: Options<T> = {},
109
134
  ...selectors: Selector[] | string[]
110
- ): Promise<ReturnType<T['factorySync']> | string | null> {
135
+ ): Promise<EntityInstanceType<T> | string | null> {
111
136
  // @ts-ignore: The selector type is correct here.
112
- return this.driver.getEntitySync(options, ...selectors);
137
+ return await this.driver.getEntity(options, ...selectors);
113
138
  }
114
139
 
115
140
  public async saveEntity(entity: EntityInterface): Promise<boolean> {
@@ -0,0 +1,3 @@
1
+ declare module 'locutus/php/datetime/strtotime.js' {
2
+ export default function (str: string, now?: number): number;
3
+ }
package/src/utils.ts CHANGED
@@ -1,7 +1,7 @@
1
- import Entity from './Entity';
2
- import { EntityReference } from './Entity.types';
3
- import Nymph from './Nymph';
4
- import { Options, Selector } from './Nymph.types';
1
+ import Entity from './Entity.js';
2
+ import { EntityReference } from './Entity.types.js';
3
+ import Nymph from './Nymph.js';
4
+ import { Options, Selector } from './Nymph.types.js';
5
5
 
6
6
  export function xor(a: any, b: any): boolean {
7
7
  return !!(a && !b) || (!a && b);
@@ -17,7 +17,8 @@ export function uniqueStrings(array: string[]) {
17
17
 
18
18
  export function classNamesToEntityConstructors(
19
19
  nymph: Nymph,
20
- selectors: Selector[]
20
+ selectors: Selector[],
21
+ enforceRestEnabledFlag = false,
21
22
  ): Selector[] {
22
23
  const newSelectors: Selector[] = [];
23
24
 
@@ -45,22 +46,35 @@ export function classNamesToEntityConstructors(
45
46
  for (let i = 0; i < tmpArr.length; i++) {
46
47
  const name = tmpArr[i][0];
47
48
  const [qrefOptions, ...selectors] = tmpArr[i][1];
48
- const QrefEntityClass =
49
- typeof qrefOptions.class === 'string'
50
- ? nymph.getEntityClass(qrefOptions.class)
51
- : qrefOptions.class ?? nymph.getEntityClass('Entity');
49
+ const QrefEntityClass = qrefOptions.class
50
+ ? nymph.getEntityClass(qrefOptions.class)
51
+ : nymph.getEntityClass('Entity');
52
+ if (enforceRestEnabledFlag && !QrefEntityClass.restEnabled) {
53
+ throw new Error('Not accessible.');
54
+ }
52
55
  const options = { ...qrefOptions, class: QrefEntityClass };
53
56
  if (!newSelector[key]) {
54
57
  newSelector[key] = [];
55
58
  }
56
59
  (newSelector[key] as [string, [Options, ...Selector[]]][]).push([
57
60
  name,
58
- [options, ...classNamesToEntityConstructors(nymph, selectors)],
61
+ [
62
+ options,
63
+ ...classNamesToEntityConstructors(
64
+ nymph,
65
+ selectors,
66
+ enforceRestEnabledFlag,
67
+ ),
68
+ ],
59
69
  ]);
60
70
  }
61
71
  } else if (key === 'selector' || key === '!selector') {
62
72
  const tmpArr = (Array.isArray(value) ? value : [value]) as Selector[];
63
- newSelector[key] = classNamesToEntityConstructors(nymph, tmpArr);
73
+ newSelector[key] = classNamesToEntityConstructors(
74
+ nymph,
75
+ tmpArr,
76
+ enforceRestEnabledFlag,
77
+ );
64
78
  } else {
65
79
  // @ts-ignore: ts doesn't know what value is here.
66
80
  newSelector[key] = value;
@@ -73,17 +87,22 @@ export function classNamesToEntityConstructors(
73
87
  return newSelectors;
74
88
  }
75
89
 
76
- export function entitiesToReferences(item: any): any {
77
- if (item instanceof Entity && typeof item.$toReference === 'function') {
90
+ export function entitiesToReferences(item: any, existingOnly?: boolean): any {
91
+ if (item == null || Buffer.isBuffer(item) || ArrayBuffer.isView(item)) {
92
+ return item;
93
+ } else if (
94
+ item instanceof Entity &&
95
+ typeof item.$toReference === 'function'
96
+ ) {
78
97
  // Convert entities to references.
79
- return item.$toReference();
98
+ return item.$toReference(existingOnly);
80
99
  } else if (Array.isArray(item)) {
81
100
  // Recurse into lower arrays.
82
- return item.map((entry) => entitiesToReferences(entry));
101
+ return item.map((entry) => entitiesToReferences(entry, existingOnly));
83
102
  } else if (item instanceof Object) {
84
103
  let newObj = Object.create(item);
85
104
  for (let [key, value] of Object.entries(item)) {
86
- newObj[key] = entitiesToReferences(value);
105
+ newObj[key] = entitiesToReferences(value, existingOnly);
87
106
  }
88
107
  return newObj;
89
108
  }
@@ -94,9 +113,11 @@ export function entitiesToReferences(item: any): any {
94
113
  export function referencesToEntities(
95
114
  item: any,
96
115
  nymph: Nymph,
97
- useSkipAc = false
116
+ useSkipAc = false,
98
117
  ): any {
99
- if (Array.isArray(item)) {
118
+ if (item == null || Buffer.isBuffer(item) || ArrayBuffer.isView(item)) {
119
+ return item;
120
+ } else if (Array.isArray(item)) {
100
121
  // Check if it's a reference.
101
122
  if (item[0] === 'nymph_entity_reference') {
102
123
  try {
package/tsconfig.json CHANGED
@@ -2,10 +2,12 @@
2
2
  "extends": "@tsconfig/recommended/tsconfig.json",
3
3
 
4
4
  "compilerOptions": {
5
- "lib": ["ES2021"],
6
- "target": "ES2021",
5
+ "module": "ES2022",
6
+ "moduleResolution": "node",
7
+ "lib": ["ES2022"],
8
+ "target": "ES2022",
7
9
  "noImplicitAny": true,
8
- "removeComments": true,
10
+ "removeComments": false,
9
11
  "sourceMap": true,
10
12
  "outDir": "dist",
11
13
  "resolveJsonModule": true,
package/typedoc.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": ["../../typedoc.base.json"],
3
+ "entryPoints": ["src/index.ts"]
4
+ }