@nymphjs/nymph 1.0.0-alpha.1 → 1.0.0-alpha.13
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/CHANGELOG.md +66 -0
- package/README.md +17 -7
- package/dist/Entity.d.ts +3 -0
- package/dist/Entity.js +15 -17
- package/dist/Entity.js.map +1 -1
- package/dist/Entity.test.js +6 -3
- package/dist/Entity.test.js.map +1 -1
- package/dist/Entity.types.d.ts +4 -1
- package/dist/Nymph.d.ts +56 -53
- package/dist/Nymph.js +138 -67
- package/dist/Nymph.js.map +1 -1
- package/dist/Nymph.types.d.ts +49 -28
- package/dist/Nymph.types.js +8 -0
- package/dist/Nymph.types.js.map +1 -1
- package/dist/driver/NymphDriver.d.ts +5 -2
- package/dist/driver/NymphDriver.js +68 -39
- package/dist/driver/NymphDriver.js.map +1 -1
- package/dist/driver/testArtifacts.d.ts +4 -3
- package/dist/driver/testArtifacts.js +265 -167
- package/dist/driver/testArtifacts.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/nymphJoiProps.d.ts +8 -0
- package/dist/nymphJoiProps.js +24 -0
- package/dist/nymphJoiProps.js.map +1 -0
- package/dist/testArtifacts.js +0 -3
- package/dist/testArtifacts.js.map +1 -1
- package/dist/testMocks.d.ts +14 -13
- package/dist/testMocks.js +21 -11
- package/dist/testMocks.js.map +1 -1
- package/dist/utils.d.ts +2 -1
- package/dist/utils.js +9 -10
- package/dist/utils.js.map +1 -1
- package/package.json +3 -2
- package/src/Entity.test.ts +7 -4
- package/src/Entity.ts +22 -12
- package/src/Entity.types.ts +10 -1
- package/src/Nymph.ts +203 -119
- package/src/Nymph.types.ts +91 -22
- package/src/driver/NymphDriver.ts +93 -46
- package/src/driver/testArtifacts.ts +307 -161
- package/src/index.ts +3 -1
- package/src/nymphJoiProps.ts +24 -0
- package/src/testArtifacts.ts +0 -5
- package/src/testMocks.ts +34 -29
- package/src/utils.ts +12 -10
package/src/index.ts
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import Joi from 'joi';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
guid: Joi.alternatives()
|
|
5
|
+
.try(
|
|
6
|
+
Joi.any().only().allow(null),
|
|
7
|
+
Joi.string().trim(false).length(24).hex()
|
|
8
|
+
)
|
|
9
|
+
.required(),
|
|
10
|
+
cdate: Joi.alternatives()
|
|
11
|
+
.try(Joi.any().only().allow(null), Joi.number())
|
|
12
|
+
.required(),
|
|
13
|
+
mdate: Joi.alternatives()
|
|
14
|
+
.try(Joi.any().only().allow(null), Joi.number())
|
|
15
|
+
.required(),
|
|
16
|
+
tags: Joi.array()
|
|
17
|
+
.items(
|
|
18
|
+
Joi.string().pattern(/[[\x01-\x1F\x7F]]/, {
|
|
19
|
+
name: 'control characters',
|
|
20
|
+
invert: true,
|
|
21
|
+
})
|
|
22
|
+
)
|
|
23
|
+
.required(),
|
|
24
|
+
};
|
package/src/testArtifacts.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import Entity from './Entity';
|
|
2
|
-
import Nymph from './Nymph';
|
|
3
2
|
|
|
4
3
|
export type TestModelData = {
|
|
5
4
|
name?: string;
|
|
@@ -72,8 +71,6 @@ export class TestModel extends Entity<TestModelData> {
|
|
|
72
71
|
}
|
|
73
72
|
}
|
|
74
73
|
|
|
75
|
-
Nymph.setEntityClass(TestModel.class, TestModel);
|
|
76
|
-
|
|
77
74
|
/**
|
|
78
75
|
* This class is a test class that extends the Entity class.
|
|
79
76
|
*/
|
|
@@ -89,5 +86,3 @@ export class TestBModel extends TestModel {
|
|
|
89
86
|
return super.factorySync(guid) as TestBModel & TestModelData;
|
|
90
87
|
}
|
|
91
88
|
}
|
|
92
|
-
|
|
93
|
-
Nymph.setEntityClass(TestBModel.class, TestBModel);
|
package/src/testMocks.ts
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
|
+
import type Nymph from './Nymph';
|
|
1
2
|
import { EntityConstructor, EntityInterface, EntityJson } from './Entity.types';
|
|
2
3
|
import { Options, Selector } from './Nymph.types';
|
|
3
4
|
import newGUID from './newGUID';
|
|
4
5
|
import { ClassNotAvailableError } from './errors';
|
|
6
|
+
import Entity from './Entity';
|
|
5
7
|
|
|
6
8
|
const entities: { [k: string]: EntityJson } = {};
|
|
7
9
|
|
|
8
10
|
export class MockNymphDriver {
|
|
11
|
+
private nymph: MockNymph;
|
|
12
|
+
|
|
13
|
+
public constructor(nymph: MockNymph) {
|
|
14
|
+
this.nymph = nymph;
|
|
15
|
+
}
|
|
16
|
+
|
|
9
17
|
public getEntitySync<T extends EntityConstructor = EntityConstructor>(
|
|
10
18
|
options: Options<T> & { return: 'guid' },
|
|
11
19
|
...selectors: Selector[]
|
|
@@ -33,7 +41,7 @@ export class MockNymphDriver {
|
|
|
33
41
|
}
|
|
34
42
|
|
|
35
43
|
const entity: EntityInterface = (
|
|
36
|
-
(options.class as T) ??
|
|
44
|
+
(options.class as T) ?? this.nymph.getEntityClass('Entity')
|
|
37
45
|
).factorySync();
|
|
38
46
|
const entityJson = entities[guid];
|
|
39
47
|
entity.guid = guid;
|
|
@@ -47,51 +55,48 @@ export class MockNymphDriver {
|
|
|
47
55
|
}
|
|
48
56
|
|
|
49
57
|
export class MockNymph {
|
|
50
|
-
|
|
51
|
-
public
|
|
52
|
-
public
|
|
53
|
-
|
|
54
|
-
public
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.
|
|
58
|
+
private entityClasses: { [k: string]: EntityConstructor } = {};
|
|
59
|
+
public driver: MockNymphDriver;
|
|
60
|
+
public Tilmeld: any = undefined;
|
|
61
|
+
|
|
62
|
+
public constructor() {
|
|
63
|
+
this.driver = new MockNymphDriver(this);
|
|
64
|
+
|
|
65
|
+
// class NymphEntity extends Entity {}
|
|
66
|
+
this.addEntityClass(Entity);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public addEntityClass(entityClass: EntityConstructor) {
|
|
70
|
+
this.entityClasses[entityClass.class] = entityClass;
|
|
71
|
+
entityClass.nymph = this as unknown as Nymph;
|
|
59
72
|
}
|
|
60
73
|
|
|
61
|
-
public
|
|
74
|
+
public getEntityClass(className: string): EntityConstructor {
|
|
62
75
|
if (className in this.entityClasses) {
|
|
63
|
-
|
|
76
|
+
const EntityClass = this.entityClasses[className];
|
|
77
|
+
EntityClass.nymph = this as unknown as Nymph;
|
|
78
|
+
return EntityClass;
|
|
64
79
|
}
|
|
65
80
|
throw new ClassNotAvailableError('Tried to use class: ' + className);
|
|
66
81
|
}
|
|
67
82
|
|
|
68
|
-
public
|
|
69
|
-
T extends EntityConstructor = EntityConstructor
|
|
70
|
-
>(
|
|
83
|
+
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
71
84
|
options: Options<T> & { return: 'guid' },
|
|
72
85
|
...selectors: Selector[]
|
|
73
86
|
): Promise<string | null>;
|
|
74
|
-
public
|
|
75
|
-
T extends EntityConstructor = EntityConstructor
|
|
76
|
-
>(
|
|
87
|
+
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
77
88
|
options: Options<T>,
|
|
78
89
|
...selectors: Selector[]
|
|
79
90
|
): Promise<ReturnType<T['factorySync']> | null>;
|
|
80
|
-
public
|
|
81
|
-
T extends EntityConstructor = EntityConstructor
|
|
82
|
-
>(
|
|
91
|
+
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
83
92
|
options: Options<T> & { return: 'guid' },
|
|
84
93
|
guid: string
|
|
85
94
|
): Promise<string | null>;
|
|
86
|
-
public
|
|
87
|
-
T extends EntityConstructor = EntityConstructor
|
|
88
|
-
>(
|
|
95
|
+
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
89
96
|
options: Options<T>,
|
|
90
97
|
guid: string
|
|
91
98
|
): Promise<ReturnType<T['factorySync']> | null>;
|
|
92
|
-
public
|
|
93
|
-
T extends EntityConstructor = EntityConstructor
|
|
94
|
-
>(
|
|
99
|
+
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
95
100
|
options: Options<T> = {},
|
|
96
101
|
...selectors: Selector[] | string[]
|
|
97
102
|
): Promise<ReturnType<T['factorySync']> | string | null> {
|
|
@@ -99,7 +104,7 @@ export class MockNymph {
|
|
|
99
104
|
return this.driver.getEntitySync(options, ...selectors);
|
|
100
105
|
}
|
|
101
106
|
|
|
102
|
-
public
|
|
107
|
+
public async saveEntity(entity: EntityInterface): Promise<boolean> {
|
|
103
108
|
const className = (entity.constructor as any).class as string;
|
|
104
109
|
|
|
105
110
|
if (entity.guid == null) {
|
|
@@ -127,7 +132,7 @@ export class MockNymph {
|
|
|
127
132
|
return true;
|
|
128
133
|
}
|
|
129
134
|
|
|
130
|
-
public
|
|
135
|
+
public async deleteEntity(entity: EntityInterface): Promise<boolean> {
|
|
131
136
|
if (entity.guid != null && entity.guid in entities) {
|
|
132
137
|
delete entities[entity.guid];
|
|
133
138
|
return true;
|
package/src/utils.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import Entity from './Entity';
|
|
1
2
|
import { EntityReference } from './Entity.types';
|
|
2
3
|
import Nymph from './Nymph';
|
|
3
4
|
|
|
@@ -14,14 +15,12 @@ export function uniqueStrings(array: string[]) {
|
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
export function entitiesToReferences(item: any): any {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (item instanceof EntityClass && typeof item.$toReference === 'function') {
|
|
18
|
+
if (item instanceof Entity && typeof item.$toReference === 'function') {
|
|
20
19
|
// Convert entities to references.
|
|
21
20
|
return item.$toReference();
|
|
22
21
|
} else if (Array.isArray(item)) {
|
|
23
22
|
// Recurse into lower arrays.
|
|
24
|
-
return item.map(entitiesToReferences);
|
|
23
|
+
return item.map((entry) => entitiesToReferences(entry));
|
|
25
24
|
} else if (item instanceof Object) {
|
|
26
25
|
let newObj = Object.create(item);
|
|
27
26
|
for (let [key, value] of Object.entries(item)) {
|
|
@@ -33,27 +32,30 @@ export function entitiesToReferences(item: any): any {
|
|
|
33
32
|
return item;
|
|
34
33
|
}
|
|
35
34
|
|
|
36
|
-
export function referencesToEntities(
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
export function referencesToEntities(
|
|
36
|
+
item: any,
|
|
37
|
+
nymph: Nymph,
|
|
38
|
+
useSkipAc = false
|
|
39
|
+
): any {
|
|
39
40
|
if (Array.isArray(item)) {
|
|
40
41
|
// Check if it's a reference.
|
|
41
42
|
if (item[0] === 'nymph_entity_reference') {
|
|
42
43
|
try {
|
|
43
|
-
const EntityClass =
|
|
44
|
+
const EntityClass = nymph.getEntityClass(item[2]);
|
|
44
45
|
const entity = EntityClass.factoryReference(item as EntityReference);
|
|
45
46
|
entity.$useSkipAc(useSkipAc);
|
|
47
|
+
entity.$nymph = nymph;
|
|
46
48
|
return entity;
|
|
47
49
|
} catch (e: any) {
|
|
48
50
|
return item;
|
|
49
51
|
}
|
|
50
52
|
} else {
|
|
51
53
|
// Recurse into lower arrays.
|
|
52
|
-
return item.map((
|
|
54
|
+
return item.map((entry) => referencesToEntities(entry, nymph, useSkipAc));
|
|
53
55
|
}
|
|
54
56
|
} else if (Entity && item instanceof Object && !(item instanceof Entity)) {
|
|
55
57
|
for (let [key, value] of Object.entries(item)) {
|
|
56
|
-
item[key] = referencesToEntities(value);
|
|
58
|
+
item[key] = referencesToEntities(value, nymph);
|
|
57
59
|
}
|
|
58
60
|
}
|
|
59
61
|
// Not an array, just return it.
|