@onehat/data 1.7.2 → 1.7.3

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.
@@ -63,6 +63,43 @@ describe('Repository Base', function() {
63
63
  })
64
64
  expect(repository.id).to.be.not.eq('foo');
65
65
  });
66
+
67
+ it('_createMethods', async function() {
68
+
69
+ // There is no test method by default
70
+ expect(this.repository.testMethod).to.not.exist;
71
+
72
+ // Set up custom repository with testMethod
73
+ const schema1 = new Schema({
74
+ name: 'baz',
75
+ model: {
76
+ idProperty: 'key',
77
+ displayProperty: 'value',
78
+ properties: [
79
+ { name: 'key', type: 'int' },
80
+ { name: 'value' },
81
+ ],
82
+ },
83
+ repository: {
84
+ type: 'null',
85
+ methods: {
86
+ testMethod: function() {
87
+ this.bar = 'test me';
88
+ },
89
+ },
90
+ },
91
+ }),
92
+ repository = new this.Repository({
93
+ schema: schema1,
94
+ data: [],
95
+ });
96
+ await repository.initialize();
97
+ schema1.setBoundRepository(repository);
98
+
99
+ // Perform testMethod test
100
+ repository.testMethod();
101
+ expect(repository.bar).to.be.eq('test me');
102
+ });
66
103
  });
67
104
 
68
105
  describe('loading', function() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.7.2",
3
+ "version": "1.7.3",
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
  }
@@ -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
  // / / ____ ____ _____/ /