@nymphjs/nymph 1.0.0-beta.5 → 1.0.0-beta.50

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 (50) hide show
  1. package/CHANGELOG.md +231 -0
  2. package/README.md +15 -15
  3. package/dist/Entity.d.ts +24 -3
  4. package/dist/Entity.js +194 -86
  5. package/dist/Entity.js.map +1 -1
  6. package/dist/Entity.test.js +34 -17
  7. package/dist/Entity.test.js.map +1 -1
  8. package/dist/Entity.types.d.ts +3 -1
  9. package/dist/Nymph.d.ts +22 -7
  10. package/dist/Nymph.js +214 -68
  11. package/dist/Nymph.js.map +1 -1
  12. package/dist/Nymph.types.d.ts +18 -5
  13. package/dist/Nymph.types.js +1 -1
  14. package/dist/Nymph.types.js.map +1 -1
  15. package/dist/driver/NymphDriver.d.ts +1 -22
  16. package/dist/driver/NymphDriver.js +85 -105
  17. package/dist/driver/NymphDriver.js.map +1 -1
  18. package/dist/driver/testArtifacts.js +208 -159
  19. package/dist/driver/testArtifacts.js.map +1 -1
  20. package/dist/errors/InvalidStateError.d.ts +3 -0
  21. package/dist/errors/InvalidStateError.js +11 -0
  22. package/dist/errors/InvalidStateError.js.map +1 -0
  23. package/dist/errors/QueryFailedError.js.map +1 -1
  24. package/dist/errors/index.d.ts +1 -0
  25. package/dist/errors/index.js +1 -0
  26. package/dist/errors/index.js.map +1 -1
  27. package/dist/testArtifacts.d.ts +10 -3
  28. package/dist/testArtifacts.js +20 -11
  29. package/dist/testArtifacts.js.map +1 -1
  30. package/dist/testMocks.d.ts +10 -7
  31. package/dist/testMocks.js +22 -10
  32. package/dist/testMocks.js.map +1 -1
  33. package/dist/utils.d.ts +1 -1
  34. package/dist/utils.js +11 -5
  35. package/dist/utils.js.map +1 -1
  36. package/package.json +15 -15
  37. package/src/Entity.test.ts +33 -11
  38. package/src/Entity.ts +303 -127
  39. package/src/Entity.types.ts +13 -1
  40. package/src/Nymph.ts +392 -88
  41. package/src/Nymph.types.ts +95 -24
  42. package/src/driver/NymphDriver.ts +203 -282
  43. package/src/driver/testArtifacts.ts +238 -157
  44. package/src/errors/InvalidStateError.ts +6 -0
  45. package/src/errors/index.ts +2 -0
  46. package/src/nymphJoiProps.ts +2 -2
  47. package/src/testArtifacts.ts +28 -10
  48. package/src/testMocks.ts +42 -25
  49. package/src/utils.ts +22 -8
  50. package/typedoc.json +4 -0
@@ -0,0 +1,6 @@
1
+ export class InvalidStateError extends Error {
2
+ constructor(message: string) {
3
+ super(message);
4
+ this.name = 'InvalidStateError';
5
+ }
6
+ }
@@ -8,6 +8,8 @@ export * from './EntityIsSleepingReferenceError';
8
8
 
9
9
  export * from './InvalidParametersError';
10
10
 
11
+ export * from './InvalidStateError';
12
+
11
13
  export * from './NotConfiguredError';
12
14
 
13
15
  export * from './QueryFailedError';
@@ -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
  };
@@ -38,17 +38,15 @@ export class TestModel extends Entity<TestModelData> {
38
38
  return (await super.factory(guid)) as TestModel & TestModelData;
39
39
  }
40
40
 
41
- static factorySync(guid?: string): TestModel & TestModelData {
42
- return super.factorySync(guid) as TestModel & TestModelData;
41
+ static factorySync(): TestModel & TestModelData {
42
+ return super.factorySync() as TestModel & TestModelData;
43
43
  }
44
44
 
45
- constructor(guid?: string) {
46
- super(guid);
45
+ constructor() {
46
+ super();
47
47
 
48
- if (this.guid == null) {
49
- this.$addTag('test');
50
- this.$data.boolean = true;
51
- }
48
+ this.$addTag('test');
49
+ this.$data.boolean = true;
52
50
  }
53
51
 
54
52
  public $useProtectedData() {
@@ -82,7 +80,27 @@ export class TestBModel extends TestModel {
82
80
  return (await super.factory(guid)) as TestBModel & TestModelData;
83
81
  }
84
82
 
85
- static factorySync(guid?: string): TestBModel & TestModelData {
86
- return super.factorySync(guid) as TestBModel & TestModelData;
83
+ static factorySync(): TestBModel & TestModelData {
84
+ return super.factorySync() as TestBModel & TestModelData;
85
+ }
86
+ }
87
+
88
+ export type TestEmptyModelData = {};
89
+
90
+ /**
91
+ * This class is a test class that extends the Entity class.
92
+ */
93
+ export class TestEmptyModel extends Entity<TestEmptyModelData> {
94
+ static ETYPE = 'test_empty_model';
95
+ static class = 'TestEmptyModel';
96
+
97
+ static async factory(
98
+ guid?: string,
99
+ ): Promise<TestEmptyModel & TestEmptyModelData> {
100
+ return (await super.factory(guid)) as TestEmptyModel & TestEmptyModelData;
101
+ }
102
+
103
+ static factorySync(): TestEmptyModel & TestEmptyModelData {
104
+ return super.factorySync() as TestEmptyModel & TestEmptyModelData;
87
105
  }
88
106
  }
package/src/testMocks.ts CHANGED
@@ -15,26 +15,26 @@ export class MockNymphDriver {
15
15
  this.nymph = nymph;
16
16
  }
17
17
 
18
- public getEntitySync<T extends EntityConstructor = EntityConstructor>(
18
+ public async getEntity<T extends EntityConstructor = EntityConstructor>(
19
19
  options: Options<T> & { return: 'guid' },
20
20
  ...selectors: Selector[]
21
- ): string | null;
22
- public getEntitySync<T extends EntityConstructor = EntityConstructor>(
21
+ ): Promise<string | null>;
22
+ public async getEntity<T extends EntityConstructor = EntityConstructor>(
23
23
  options: Options<T>,
24
24
  ...selectors: Selector[]
25
- ): ReturnType<T['factorySync']> | null;
26
- public getEntitySync<T extends EntityConstructor = EntityConstructor>(
25
+ ): Promise<ReturnType<T['factorySync']> | null>;
26
+ public async getEntity<T extends EntityConstructor = EntityConstructor>(
27
27
  options: Options<T> & { return: 'guid' },
28
- guid: string
29
- ): string | null;
30
- public getEntitySync<T extends EntityConstructor = EntityConstructor>(
28
+ guid: string,
29
+ ): Promise<string | null>;
30
+ public async getEntity<T extends EntityConstructor = EntityConstructor>(
31
31
  options: Options<T>,
32
- guid: string
33
- ): ReturnType<T['factorySync']> | null;
34
- public getEntitySync<T extends EntityConstructor = EntityConstructor>(
32
+ guid: string,
33
+ ): Promise<ReturnType<T['factorySync']> | null>;
34
+ public async getEntity<T extends EntityConstructor = EntityConstructor>(
35
35
  options: Options<T> = {},
36
36
  ...selectors: Selector[] | string[]
37
- ): ReturnType<T['factorySync']> | string | null {
37
+ ): Promise<ReturnType<T['factorySync']> | string | null> {
38
38
  const guid =
39
39
  typeof selectors[0] === 'string' ? selectors[0] : selectors[0].guid;
40
40
  if (!options || !guid || typeof guid !== 'string' || !(guid in entities)) {
@@ -59,26 +59,43 @@ export class MockNymph {
59
59
  private entityClasses: { [k: string]: EntityConstructor } = {};
60
60
  public driver: MockNymphDriver;
61
61
  public Tilmeld: any = undefined;
62
+ public Entity: typeof Entity;
62
63
 
63
64
  public constructor() {
64
65
  this.driver = new MockNymphDriver(this);
65
66
 
66
67
  // class NymphEntity extends Entity {}
67
- this.addEntityClass(Entity);
68
+ this.Entity = this.addEntityClass(Entity);
68
69
  }
69
70
 
70
- public addEntityClass(entityClass: EntityConstructor) {
71
- this.entityClasses[entityClass.class] = entityClass;
72
- entityClass.nymph = this as unknown as Nymph;
71
+ public addEntityClass<T extends EntityConstructor>(entityClass: T): T {
72
+ const nymph = this;
73
+ class NymphEntity extends entityClass {
74
+ static nymph: Nymph = nymph as unknown as Nymph;
75
+
76
+ constructor(...args: any[]) {
77
+ super(...args);
78
+ }
79
+ }
80
+ this.entityClasses[entityClass.class] = NymphEntity;
81
+ return NymphEntity;
73
82
  }
74
83
 
75
- public getEntityClass(className: string): EntityConstructor {
76
- if (className in this.entityClasses) {
77
- const EntityClass = this.entityClasses[className];
78
- EntityClass.nymph = this as unknown as Nymph;
79
- return EntityClass;
84
+ public getEntityClass<T extends EntityConstructor>(className: T): T;
85
+ public getEntityClass(className: string): EntityConstructor;
86
+ public getEntityClass<T extends EntityConstructor = EntityConstructor>(
87
+ className: T | string,
88
+ ): T | EntityConstructor {
89
+ let key: string | null = null;
90
+ if (typeof className === 'string') {
91
+ key = className;
92
+ } else {
93
+ key = className.class;
94
+ }
95
+ if (key in this.entityClasses) {
96
+ return this.entityClasses[key];
80
97
  }
81
- throw new ClassNotAvailableError('Tried to use class: ' + className);
98
+ throw new ClassNotAvailableError('Tried to use class: ' + key);
82
99
  }
83
100
 
84
101
  public async getEntity<T extends EntityConstructor = EntityConstructor>(
@@ -91,18 +108,18 @@ export class MockNymph {
91
108
  ): Promise<ReturnType<T['factorySync']> | null>;
92
109
  public async getEntity<T extends EntityConstructor = EntityConstructor>(
93
110
  options: Options<T> & { return: 'guid' },
94
- guid: string
111
+ guid: string,
95
112
  ): Promise<string | null>;
96
113
  public async getEntity<T extends EntityConstructor = EntityConstructor>(
97
114
  options: Options<T>,
98
- guid: string
115
+ guid: string,
99
116
  ): Promise<ReturnType<T['factorySync']> | null>;
100
117
  public async getEntity<T extends EntityConstructor = EntityConstructor>(
101
118
  options: Options<T> = {},
102
119
  ...selectors: Selector[] | string[]
103
120
  ): Promise<ReturnType<T['factorySync']> | string | null> {
104
121
  // @ts-ignore: The selector type is correct here.
105
- return this.driver.getEntitySync(options, ...selectors);
122
+ return await this.driver.getEntity(options, ...selectors);
106
123
  }
107
124
 
108
125
  public async saveEntity(entity: EntityInterface): Promise<boolean> {
package/src/utils.ts CHANGED
@@ -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;
@@ -94,7 +108,7 @@ export function entitiesToReferences(item: any): any {
94
108
  export function referencesToEntities(
95
109
  item: any,
96
110
  nymph: Nymph,
97
- useSkipAc = false
111
+ useSkipAc = false,
98
112
  ): any {
99
113
  if (Array.isArray(item)) {
100
114
  // Check if it's a reference.
package/typedoc.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": ["../../typedoc.base.json"],
3
+ "entryPoints": ["src/index.ts"]
4
+ }