@onehat/data 1.7.3 → 1.7.4

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.
@@ -12,6 +12,11 @@ describe('Repository Base', function() {
12
12
  { name: 'key', type: 'int' },
13
13
  { name: 'value' },
14
14
  ],
15
+ associations: {
16
+ hasMany: [
17
+ 'bar'
18
+ ],
19
+ },
15
20
  },
16
21
  repository: 'null',
17
22
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.7.3",
3
+ "version": "1.7.4",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -1183,6 +1183,49 @@ export default class Repository extends EventEmitter {
1183
1183
  });
1184
1184
  }
1185
1185
 
1186
+ /**
1187
+ * Gets the Schema object
1188
+ * @return {Schema} schema
1189
+ */
1190
+ getSchema = () => {
1191
+ if (this.isDestroyed) {
1192
+ throw Error('this.getSchema is no longer valid. Entity has been destroyed.');
1193
+ }
1194
+ return this.schema;
1195
+ }
1196
+
1197
+ /**
1198
+ * Gets the associated Repository
1199
+ * @param {string} repositoryName - Name of the Repository to retrieve
1200
+ * @return {boolean} hasProperty
1201
+ */
1202
+ getAssociatedRepository = (repositoryName) => {
1203
+ if (this.isDestroyed) {
1204
+ throw Error('this.getAssociatedRepository is no longer valid. Entity has been destroyed.');
1205
+ }
1206
+
1207
+ const schema = this.getSchema();
1208
+ if (!schema.model.associations.hasOne.includes(repositoryName) &&
1209
+ !schema.model.associations.hasMany.includes(repositoryName) &&
1210
+ !schema.model.associations.belongsTo.includes(repositoryName) &&
1211
+ !schema.model.associations.belongsToMany.includes(repositoryName)
1212
+ ) {
1213
+ throw Error(repositoryName + ' is not associated with this schema');
1214
+ }
1215
+
1216
+ const oneHatData = this.oneHatData;
1217
+ if (!oneHatData) {
1218
+ throw Error('No global oneHatData object');
1219
+ }
1220
+
1221
+ const associatedRepository = oneHatData.getRepository(repositoryName);
1222
+ if (!associatedRepository) {
1223
+ throw Error('Repository ' + repositoryName + ' cannot be found');
1224
+ }
1225
+
1226
+ return associatedRepository;
1227
+ }
1228
+
1186
1229
  /**
1187
1230
  * Utility function.
1188
1231
  * Detects if entity is in the current page of the storage medium.