@nymphjs/nymph 1.0.0-beta.6 → 1.0.0-beta.61
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 +277 -0
- package/README.md +13 -21
- package/dist/Entity.d.ts +269 -7
- package/dist/Entity.js +431 -89
- package/dist/Entity.js.map +1 -1
- package/dist/Entity.test.js +38 -16
- package/dist/Entity.test.js.map +1 -1
- package/dist/Entity.types.d.ts +225 -19
- package/dist/Nymph.d.ts +423 -11
- package/dist/Nymph.js +649 -156
- package/dist/Nymph.js.map +1 -1
- package/dist/Nymph.types.d.ts +31 -7
- package/dist/Nymph.types.js +2 -1
- package/dist/Nymph.types.js.map +1 -1
- package/dist/conf/d.d.ts +26 -0
- package/dist/conf/defaults.js +22 -0
- package/dist/conf/defaults.js.map +1 -1
- package/dist/driver/NymphDriver.d.ts +111 -36
- package/dist/driver/NymphDriver.js +169 -68
- package/dist/driver/NymphDriver.js.map +1 -1
- package/dist/driver/testArtifacts.d.ts +1 -1
- package/dist/driver/testArtifacts.js +285 -18
- package/dist/driver/testArtifacts.js.map +1 -1
- package/dist/errors/EntityInvalidDataError.d.ts +6 -0
- package/dist/errors/EntityInvalidDataError.js +6 -0
- package/dist/errors/EntityInvalidDataError.js.map +1 -1
- package/dist/errors/EntityUniqueConstraintError.d.ts +3 -0
- package/dist/errors/EntityUniqueConstraintError.js +11 -0
- package/dist/errors/EntityUniqueConstraintError.js.map +1 -0
- package/dist/errors/InvalidStateError.d.ts +3 -0
- package/dist/errors/InvalidStateError.js +11 -0
- package/dist/errors/InvalidStateError.js.map +1 -0
- package/dist/errors/QueryFailedError.js.map +1 -1
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.js +2 -0
- package/dist/errors/index.js.map +1 -1
- package/dist/testArtifacts.d.ts +17 -5
- package/dist/testArtifacts.js +23 -19
- package/dist/testArtifacts.js.map +1 -1
- package/dist/testMocks.d.ts +15 -10
- package/dist/testMocks.js +34 -11
- package/dist/testMocks.js.map +1 -1
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +21 -5
- package/dist/utils.js.map +1 -1
- package/package.json +16 -14
- package/src/Entity.test.ts +30 -10
- package/src/Entity.ts +406 -135
- package/src/Entity.types.ts +32 -66
- package/src/Nymph.ts +626 -196
- package/src/Nymph.types.ts +97 -26
- package/src/conf/d.ts +8 -0
- package/src/conf/defaults.ts +24 -0
- package/src/driver/NymphDriver.ts +187 -243
- package/src/driver/testArtifacts.ts +327 -146
- package/src/errors/EntityUniqueConstraintError.ts +6 -0
- package/src/errors/InvalidStateError.ts +6 -0
- package/src/errors/index.ts +4 -0
- package/src/nymphJoiProps.ts +2 -2
- package/src/testArtifacts.ts +18 -19
- package/src/testMocks.ts +64 -32
- package/src/utils.ts +22 -8
- package/tsconfig.json +1 -1
- package/typedoc.json +4 -0
package/src/errors/index.ts
CHANGED
|
@@ -6,8 +6,12 @@ export * from './EntityInvalidDataError';
|
|
|
6
6
|
|
|
7
7
|
export * from './EntityIsSleepingReferenceError';
|
|
8
8
|
|
|
9
|
+
export * from './EntityUniqueConstraintError';
|
|
10
|
+
|
|
9
11
|
export * from './InvalidParametersError';
|
|
10
12
|
|
|
13
|
+
export * from './InvalidStateError';
|
|
14
|
+
|
|
11
15
|
export * from './NotConfiguredError';
|
|
12
16
|
|
|
13
17
|
export * from './QueryFailedError';
|
package/src/nymphJoiProps.ts
CHANGED
|
@@ -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
|
};
|
package/src/testArtifacts.ts
CHANGED
|
@@ -17,8 +17,10 @@ export type TestModelData = {
|
|
|
17
17
|
[k: string]: TestModel & TestModelData;
|
|
18
18
|
};
|
|
19
19
|
parent?: TestModel & TestModelData;
|
|
20
|
-
// For
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
39
|
+
constructor() {
|
|
40
|
+
super();
|
|
40
41
|
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
this.$addTag('test');
|
|
43
|
+
this.$data.boolean = true;
|
|
44
|
+
this.$data.uniques = [];
|
|
43
45
|
}
|
|
44
46
|
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
82
|
-
return (await super.factory(guid)) as TestBModel & TestModelData;
|
|
83
|
-
}
|
|
79
|
+
export type TestEmptyModelData = {};
|
|
84
80
|
|
|
85
|
-
|
|
86
|
-
|
|
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,7 +1,12 @@
|
|
|
1
1
|
import { guid } from '@nymphjs/guid';
|
|
2
2
|
|
|
3
3
|
import type Nymph from './Nymph';
|
|
4
|
-
import {
|
|
4
|
+
import type { EntityInstanceType } from './Entity';
|
|
5
|
+
import type {
|
|
6
|
+
EntityConstructor,
|
|
7
|
+
EntityInterface,
|
|
8
|
+
EntityJson,
|
|
9
|
+
} from './Entity.types';
|
|
5
10
|
import { Options, Selector } from './Nymph.types';
|
|
6
11
|
import { ClassNotAvailableError } from './errors';
|
|
7
12
|
import Entity from './Entity';
|
|
@@ -15,34 +20,34 @@ export class MockNymphDriver {
|
|
|
15
20
|
this.nymph = nymph;
|
|
16
21
|
}
|
|
17
22
|
|
|
18
|
-
public
|
|
23
|
+
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
19
24
|
options: Options<T> & { return: 'guid' },
|
|
20
25
|
...selectors: Selector[]
|
|
21
|
-
): string | null
|
|
22
|
-
public
|
|
26
|
+
): Promise<string | null>;
|
|
27
|
+
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
23
28
|
options: Options<T>,
|
|
24
29
|
...selectors: Selector[]
|
|
25
|
-
):
|
|
26
|
-
public
|
|
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
|
|
33
|
+
guid: string,
|
|
34
|
+
): Promise<string | null>;
|
|
35
|
+
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
31
36
|
options: Options<T>,
|
|
32
|
-
guid: string
|
|
33
|
-
):
|
|
34
|
-
public
|
|
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
|
-
):
|
|
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
|
|
45
|
-
(options.class
|
|
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;
|
|
@@ -51,7 +56,7 @@ export class MockNymphDriver {
|
|
|
51
56
|
entity.tags = entityJson.tags;
|
|
52
57
|
entity.$putData(entityJson.data, {});
|
|
53
58
|
|
|
54
|
-
return entity as
|
|
59
|
+
return entity as EntityInstanceType<T>;
|
|
55
60
|
}
|
|
56
61
|
}
|
|
57
62
|
|
|
@@ -59,26 +64,53 @@ export class MockNymph {
|
|
|
59
64
|
private entityClasses: { [k: string]: EntityConstructor } = {};
|
|
60
65
|
public driver: MockNymphDriver;
|
|
61
66
|
public Tilmeld: any = undefined;
|
|
67
|
+
public Entity: typeof Entity;
|
|
62
68
|
|
|
63
69
|
public constructor() {
|
|
64
70
|
this.driver = new MockNymphDriver(this);
|
|
65
71
|
|
|
66
72
|
// class NymphEntity extends Entity {}
|
|
67
|
-
this.addEntityClass(Entity);
|
|
73
|
+
this.Entity = this.addEntityClass(Entity);
|
|
68
74
|
}
|
|
69
75
|
|
|
70
|
-
public addEntityClass(
|
|
71
|
-
|
|
72
|
-
|
|
76
|
+
public addEntityClass<T extends EntityConstructor>(EntityClass: T): T {
|
|
77
|
+
const nymph = this;
|
|
78
|
+
this.entityClasses[EntityClass.class] = class extends EntityClass {
|
|
79
|
+
static nymph: Nymph = nymph as unknown as Nymph;
|
|
80
|
+
|
|
81
|
+
constructor(...args: any[]) {
|
|
82
|
+
super(...args);
|
|
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];
|
|
101
|
+
}
|
|
102
|
+
throw new ClassNotAvailableError(`Tried to use missing class "${key}".`);
|
|
73
103
|
}
|
|
74
104
|
|
|
75
|
-
public
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
105
|
+
public getEntityClassByEtype(etype: string): EntityConstructor {
|
|
106
|
+
for (const EntityClass of Object.values(this.entityClasses)) {
|
|
107
|
+
if (EntityClass.ETYPE === etype) {
|
|
108
|
+
return EntityClass;
|
|
109
|
+
}
|
|
80
110
|
}
|
|
81
|
-
throw new ClassNotAvailableError(
|
|
111
|
+
throw new ClassNotAvailableError(
|
|
112
|
+
`Tried to use missing class by etype "${etype}".`,
|
|
113
|
+
);
|
|
82
114
|
}
|
|
83
115
|
|
|
84
116
|
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
@@ -88,21 +120,21 @@ export class MockNymph {
|
|
|
88
120
|
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
89
121
|
options: Options<T>,
|
|
90
122
|
...selectors: Selector[]
|
|
91
|
-
): Promise<
|
|
123
|
+
): Promise<EntityInstanceType<T> | null>;
|
|
92
124
|
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
93
125
|
options: Options<T> & { return: 'guid' },
|
|
94
|
-
guid: string
|
|
126
|
+
guid: string,
|
|
95
127
|
): Promise<string | null>;
|
|
96
128
|
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
97
129
|
options: Options<T>,
|
|
98
|
-
guid: string
|
|
99
|
-
): Promise<
|
|
130
|
+
guid: string,
|
|
131
|
+
): Promise<EntityInstanceType<T> | null>;
|
|
100
132
|
public async getEntity<T extends EntityConstructor = EntityConstructor>(
|
|
101
133
|
options: Options<T> = {},
|
|
102
134
|
...selectors: Selector[] | string[]
|
|
103
|
-
): Promise<
|
|
135
|
+
): Promise<EntityInstanceType<T> | string | null> {
|
|
104
136
|
// @ts-ignore: The selector type is correct here.
|
|
105
|
-
return this.driver.
|
|
137
|
+
return await this.driver.getEntity(options, ...selectors);
|
|
106
138
|
}
|
|
107
139
|
|
|
108
140
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
[
|
|
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(
|
|
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/tsconfig.json
CHANGED
package/typedoc.json
ADDED