@onehat/data 1.7.1 → 1.7.5

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.
@@ -77,6 +77,9 @@ describe('Entity', function() {
77
77
 
78
78
  entity.markSaved();
79
79
  expect(entity.isTempId).to.be.false;
80
+
81
+ entity.isTempId = true;
82
+ expect(entity.isTempId).to.be.true;
80
83
  });
81
84
 
82
85
  it('clone', function() {
@@ -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.1",
3
+ "version": "1.7.5",
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
  /**
@@ -774,6 +773,14 @@ class Entity extends EventEmitter {
774
773
  return this.getIdProperty().isTempId;
775
774
  }
776
775
 
776
+ /**
777
+ * Marks this Entity's idProperty's isTempId field
778
+ * @return {boolean} isTempId
779
+ */
780
+ set isTempId(bool) {
781
+ this.getIdProperty().isTempId = bool;
782
+ }
783
+
777
784
  /**
778
785
  * Gets the "Display" Property object for this Entity.
779
786
  * This is the Property whose value can easily identify the whole Entity itself.
@@ -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.