@nymphjs/nymph 1.0.0-beta.1 → 1.0.0-beta.11

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.
@@ -3,16 +3,20 @@ import strtotime from 'locutus/php/datetime/strtotime';
3
3
  import { guid } from '@nymphjs/guid';
4
4
 
5
5
  import type Nymph from '../Nymph';
6
- import { TestBModel, TestModel, TestModelData } from '../testArtifacts';
6
+ import {
7
+ TestBModel as TestBModelClass,
8
+ TestModel as TestModelClass,
9
+ TestModelData,
10
+ } from '../testArtifacts';
7
11
 
8
12
  export function QueriesTest(
9
13
  nymph: Nymph,
10
14
  it: (name: string, fn: () => void) => void
11
15
  ) {
12
- nymph.addEntityClass(TestModel);
13
- nymph.addEntityClass(TestBModel);
16
+ const TestModel = nymph.addEntityClass(TestModelClass);
17
+ const TestBModel = nymph.addEntityClass(TestBModelClass);
14
18
 
15
- let testEntity: TestModel & TestModelData;
19
+ let testEntity: TestModelClass & TestModelData;
16
20
  let testGuid: string;
17
21
  let refGuid: string;
18
22
  let createdMultiple = false;
@@ -43,7 +47,7 @@ This one's zip code is 92064.`;
43
47
  expect(testEntity.guid).not.toBeNull();
44
48
  testGuid = testEntity.guid as string;
45
49
 
46
- const entityReferenceTest: TestModel & TestModelData = new TestModel();
50
+ const entityReferenceTest: TestModelClass & TestModelData = new TestModel();
47
51
  entityReferenceTest.string = 'wrong';
48
52
  entityReferenceTest.timestamp = strtotime('-2 days') * 1000;
49
53
  expect(await entityReferenceTest.$save()).toEqual(true);
@@ -59,7 +63,7 @@ This one's zip code is 92064.`;
59
63
  testEntity = nymph.driver.getEntitySync(
60
64
  { class: TestModel },
61
65
  testGuid
62
- ) as TestModel & TestModelData;
66
+ ) as TestModelClass & TestModelData;
63
67
  expect(testEntity).toBeInstanceOf(TestModel);
64
68
  expect(testEntity.guid).toEqual(testGuid);
65
69
 
@@ -67,7 +71,7 @@ This one's zip code is 92064.`;
67
71
  testEntity = (await nymph.getEntity(
68
72
  { class: TestModel },
69
73
  testGuid
70
- )) as TestModel & TestModelData;
74
+ )) as TestModelClass & TestModelData;
71
75
  expect(testEntity).toBeInstanceOf(TestModel);
72
76
  expect(testEntity.guid).toEqual(testGuid);
73
77
  }
@@ -191,7 +195,7 @@ This one's zip code is 92064.`;
191
195
  testEntity = (await transaction.getEntity(
192
196
  { class: TestModel },
193
197
  testGuid
194
- )) as TestModel & TestModelData;
198
+ )) as TestModelClass & TestModelData;
195
199
 
196
200
  expect(testEntity.guid).toEqual(testGuid);
197
201
 
@@ -1728,9 +1732,6 @@ export function UIDTest(
1728
1732
  nymph: Nymph,
1729
1733
  it: (name: string, fn: () => void) => void
1730
1734
  ) {
1731
- nymph.addEntityClass(TestModel);
1732
- nymph.addEntityClass(TestBModel);
1733
-
1734
1735
  it('delete old test data', async () => {
1735
1736
  expect(await nymph.deleteUID('TestUID')).toEqual(true);
1736
1737
  expect(await nymph.deleteUID('NewUID')).toEqual(true);
@@ -1772,8 +1773,8 @@ export function ExportImportTest(
1772
1773
  nymph: Nymph,
1773
1774
  it: (name: string, fn: () => void) => void
1774
1775
  ) {
1775
- nymph.addEntityClass(TestModel);
1776
- nymph.addEntityClass(TestBModel);
1776
+ const TestModel = nymph.addEntityClass(TestModelClass);
1777
+ const TestBModel = nymph.addEntityClass(TestBModelClass);
1777
1778
 
1778
1779
  async function deleteTestData() {
1779
1780
  let all = await nymph.getEntities({ class: TestModel });
package/src/testMocks.ts CHANGED
@@ -59,24 +59,31 @@ 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
84
  public getEntityClass(className: string): EntityConstructor {
76
85
  if (className in this.entityClasses) {
77
- const EntityClass = this.entityClasses[className];
78
- EntityClass.nymph = this as unknown as Nymph;
79
- return EntityClass;
86
+ return this.entityClasses[className];
80
87
  }
81
88
  throw new ClassNotAvailableError('Tried to use class: ' + className);
82
89
  }