@onehat/data 1.6.5 → 1.6.6

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.
@@ -25,6 +25,13 @@ const GroupsUsers = {
25
25
  { name: 'users__last_login', mapping: 'user.last_login', type: 'datetime' },
26
26
  ],
27
27
 
28
+ associations: {
29
+ hasOne: [
30
+ 'Groups',
31
+ 'Users',
32
+ ],
33
+ },
34
+
28
35
  },
29
36
 
30
37
  entity: {
@@ -175,6 +175,21 @@ describe('OneHatData', function() {
175
175
  })();
176
176
  });
177
177
 
178
+ it('hasRepository', function() {
179
+ (async function() {
180
+ await beforeEach();
181
+
182
+ const oneHatData = new OneHatData();
183
+ oneHatData.createSchemas([
184
+ { name: 'foo', },
185
+ ]);
186
+ oneHatData.createBoundRepositories();
187
+ expect(oneHatData.hasRepository('foo')).to.be.true;
188
+
189
+ afterEach();
190
+ })();
191
+ });
192
+
178
193
  it('hasRepositoryWithId', function() {
179
194
  (async function() {
180
195
  await beforeEach();
@@ -11,6 +11,9 @@ describe('Schema', function() {
11
11
  expect(this.schema instanceof Schema).to.be.true;
12
12
  expect(this.schema.name).to.be.eq('GroupsUsers');
13
13
  expect(this.schema.model.associations.hasOne).to.be.an('array');
14
+ expect(this.schema.model.associations.hasMany).to.be.an('array');
15
+ expect(this.schema.model.associations.belongsTo).to.be.an('array');
16
+ expect(this.schema.model.associations.belongsToMany).to.be.an('array');
14
17
  expect(this.schema.entity.methods.testMethod).to.be.a('function');
15
18
  expect(this.schema.repository.type).to.be.eq('onebuild');
16
19
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.6.5",
3
+ "version": "1.6.6",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/Entity.js CHANGED
@@ -398,6 +398,17 @@ class Entity extends EventEmitter {
398
398
  return this.schema;
399
399
  }
400
400
 
401
+ /**
402
+ * Gets the Repository object
403
+ * @return {Repository} repository
404
+ */
405
+ getRepository = () => {
406
+ if (this.isDestroyed) {
407
+ throw Error('this.getRepository is no longer valid. Entity has been destroyed.');
408
+ }
409
+ return this.repository;
410
+ }
411
+
401
412
  /**
402
413
  * Alias for this.properties
403
414
  */
@@ -862,18 +873,30 @@ class Entity extends EventEmitter {
862
873
  }
863
874
 
864
875
  const schema = this.getSchema();
865
- if (!schema.associations.hasOne.includes(repositoryName) &&
866
- !schema.associations.hasMany.includes(repositoryName) &&
867
- !schema.associations.belongsTo.includes(repositoryName) &&
868
- !schema.associations.belongsToMany.includes(repositoryName)
876
+ if (!schema.model.associations.hasOne.includes(repositoryName) &&
877
+ !schema.model.associations.hasMany.includes(repositoryName) &&
878
+ !schema.model.associations.belongsTo.includes(repositoryName) &&
879
+ !schema.model.associations.belongsToMany.includes(repositoryName)
869
880
  ) {
870
- throw Error(repositoryName + ' is not associated with ' + this.getRepository().name);
881
+ throw Error(repositoryName + ' is not associated with this schema');
871
882
  }
883
+
872
884
  const repository = this.getRepository();
873
- if (!repository.oneHatData) {
885
+ if (!repository) {
886
+ throw Error('No repository on this entity');
887
+ }
888
+
889
+ const oneHatData = repository.oneHatData;
890
+ if (!oneHatData) {
874
891
  throw Error('No global oneHatData object');
875
892
  }
876
- return repository.oneHatData.getRepository(repositoryName);
893
+
894
+ const associatedRepository = oneHatData.getRepository(repositoryName);
895
+ if (!associatedRepository) {
896
+ throw Error('Repository ' + repositoryName + ' cannot be found');
897
+ }
898
+
899
+ return associatedRepository;
877
900
  }
878
901
 
879
902
 
package/src/OneHatData.js CHANGED
@@ -442,6 +442,19 @@ export class OneHatData extends EventEmitter {
442
442
  return schema.getBoundRepository();
443
443
  }
444
444
 
445
+ /**
446
+ * Checks whether the requested bound Repository exists.
447
+ * @param {string} name - Name of Schema
448
+ * @return {boolean} hasRepository
449
+ */
450
+ hasRepository = (name) => {
451
+ if (this.isDestroyed) {
452
+ throw new Error('this.getRepository is no longer valid. OneHatData has been destroyed.');
453
+ }
454
+ const repository = this.getRepository(name);
455
+ return !!repository;
456
+ }
457
+
445
458
  /**
446
459
  * Get Repositories by a filter function
447
460
  * @param {function} filter - Filter function