@onehat/data 1.7.0 → 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
  });
@@ -63,6 +68,43 @@ describe('Repository Base', function() {
63
68
  })
64
69
  expect(repository.id).to.be.not.eq('foo');
65
70
  });
71
+
72
+ it('_createMethods', async function() {
73
+
74
+ // There is no test method by default
75
+ expect(this.repository.testMethod).to.not.exist;
76
+
77
+ // Set up custom repository with testMethod
78
+ const schema1 = new Schema({
79
+ name: 'baz',
80
+ model: {
81
+ idProperty: 'key',
82
+ displayProperty: 'value',
83
+ properties: [
84
+ { name: 'key', type: 'int' },
85
+ { name: 'value' },
86
+ ],
87
+ },
88
+ repository: {
89
+ type: 'null',
90
+ methods: {
91
+ testMethod: function() {
92
+ this.bar = 'test me';
93
+ },
94
+ },
95
+ },
96
+ }),
97
+ repository = new this.Repository({
98
+ schema: schema1,
99
+ data: [],
100
+ });
101
+ await repository.initialize();
102
+ schema1.setBoundRepository(repository);
103
+
104
+ // Perform testMethod test
105
+ repository.testMethod();
106
+ expect(repository.bar).to.be.eq('test me');
107
+ });
66
108
  });
67
109
 
68
110
  describe('loading', function() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.7.0",
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",
package/src/Entity.js CHANGED
@@ -165,12 +165,11 @@ class Entity extends EventEmitter {
165
165
  throw Error('this._createMethods is no longer valid. Entity has been destroyed.');
166
166
  }
167
167
  const methodDefinitions = this.schema.entity.methods;
168
- if (_.isEmpty(methodDefinitions)) {
169
- return;
168
+ if (!_.isEmpty(methodDefinitions)) {
169
+ _.each(methodDefinitions, (method, name) => {
170
+ this[name] = method; // NOTE: Methods must be defined in schema as "function() {}", not as "() => {}" so "this" will be assigned correctly
171
+ });
170
172
  }
171
- _.each(methodDefinitions, (method, name) => {
172
- this[name] = method; // NOTE: Methods must be defined in schema as "function() {}", not as "() => {}" so "this" will be assigned correctly
173
- });
174
173
  }
175
174
 
176
175
  /**
@@ -238,9 +238,10 @@ class AjaxRepository extends Repository {
238
238
  matches = name.match(re),
239
239
  paramsToChange = isBaseParam ? this._baseParams : this._params;
240
240
 
241
+ let first, second;
241
242
  if (matches) { // name has array notation like 'conditions[username]'
242
- const first = matches[1],
243
- second = matches[2];
243
+ first = matches[1],
244
+ second = matches[2];
244
245
  if (paramsToChange && !paramsToChange.hasOwnProperty(first)) {
245
246
  paramsToChange[first] = [];
246
247
  }
@@ -548,7 +549,9 @@ class AjaxRepository extends Repository {
548
549
 
549
550
  const method = this.methods.add,
550
551
  url = this.api.batchAdd,
551
- data = _.map(entities, entity => entity.submitValues);
552
+ data = {
553
+ entities: _.map(entities, entity => entity.submitValues),
554
+ };
552
555
 
553
556
  return this._send(method, url, data)
554
557
  .then(result => {
@@ -626,7 +629,9 @@ class AjaxRepository extends Repository {
626
629
 
627
630
  const method = this.methods.edit,
628
631
  url = this.api.batchEdit,
629
- data = _.map(entities, entity => entity.submitValues);
632
+ data = {
633
+ entities: _.map(entities, entity => entity.submitValues),
634
+ };
630
635
 
631
636
  return this._send(method, url, data)
632
637
  .then(result => {
@@ -265,10 +265,28 @@ export default class Repository extends EventEmitter {
265
265
  await this.sort();
266
266
  }
267
267
 
268
+ this._createMethods();
269
+
268
270
  this.isInitialized = true;
269
271
  this.emit('initialize');
270
272
  }
271
273
 
274
+ /**
275
+ * Creates the methods for this Repository, based on Schema.
276
+ * @private
277
+ */
278
+ _createMethods = () => {
279
+ if (this.isDestroyed) {
280
+ throw Error('this._createMethods is no longer valid. Repository has been destroyed.');
281
+ }
282
+ const methodDefinitions = this.schema.repository.methods;
283
+ if (!_.isEmpty(methodDefinitions)) {
284
+ _.each(methodDefinitions, (method, name) => {
285
+ this[name] = method; // NOTE: Methods must be defined in schema as "function() {}", not as "() => {}" so "this" will be assigned correctly
286
+ });
287
+ }
288
+ }
289
+
272
290
 
273
291
  // __ __
274
292
  // / / ____ ____ _____/ /
@@ -1165,6 +1183,49 @@ export default class Repository extends EventEmitter {
1165
1183
  });
1166
1184
  }
1167
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
+
1168
1229
  /**
1169
1230
  * Utility function.
1170
1231
  * Detects if entity is in the current page of the storage medium.
@@ -1254,13 +1315,13 @@ export default class Repository extends EventEmitter {
1254
1315
  switch(operation) {
1255
1316
  case 'add':
1256
1317
  entities = this.getNonPersisted();
1257
- if (this.combineBatch) {
1318
+ if (_.size(entities) > 0) {
1319
+ if (this.combineBatch) {
1258
1320
 
1259
1321
  result = this.batchAsSynchronous ? await this._doBatchAdd(entities) : this._doBatchAdd(entities);
1260
1322
  results.push(result);
1261
1323
 
1262
- } else {
1263
- if (_.size(entities) > 0) {
1324
+ } else {
1264
1325
  for (i = 0; i < entities.length; i++) {
1265
1326
  entity = entities[i];
1266
1327
 
@@ -1275,18 +1336,16 @@ export default class Repository extends EventEmitter {
1275
1336
  }
1276
1337
  }
1277
1338
  }
1278
-
1279
-
1280
1339
  break;
1281
1340
  case 'edit':
1282
1341
  entities = this.getDirty();
1283
- if (this.combineBatch) {
1342
+ if (_.size(entities) > 0) {
1343
+ if (this.combineBatch) {
1284
1344
 
1285
- result = this.batchAsSynchronous ? await this._doBatchEdit(entities) : this._doBatchEdit(entities);
1286
- results.push(result);
1345
+ result = this.batchAsSynchronous ? await this._doBatchEdit(entities) : this._doBatchEdit(entities);
1346
+ results.push(result);
1287
1347
 
1288
- } else {
1289
- if (_.size(entities) > 0) {
1348
+ } else {
1290
1349
  for (i = 0; i < entities.length; i++) {
1291
1350
  entity = entities[i];
1292
1351
 
@@ -1304,13 +1363,13 @@ export default class Repository extends EventEmitter {
1304
1363
  break;
1305
1364
  case 'delete':
1306
1365
  entities = this.getDeleted();
1307
- if (this.combineBatch) {
1366
+ if (_.size(entities) > 0) {
1367
+ if (this.combineBatch) {
1308
1368
 
1309
1369
  result = this.batchAsSynchronous ? await this._doBatchDelete(entities) : this._doBatchDelete(entities);
1310
1370
  results.push(result);
1311
1371
 
1312
- } else {
1313
- if (_.size(entities) > 0) {
1372
+ } else {
1314
1373
  for (i = 0; i < entities.length; i++) {
1315
1374
  entity = entities[i];
1316
1375