@onehat/data 1.6.3 → 1.6.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.
@@ -27,6 +27,14 @@ const GroupsUsers = {
27
27
 
28
28
  },
29
29
 
30
+ entity: {
31
+ methods: {
32
+ testMethod: function() {
33
+ this.users__login_count = this.users__login_count +1;
34
+ },
35
+ },
36
+ },
37
+
30
38
  repository: 'onebuild',
31
39
 
32
40
  };
@@ -17,6 +17,13 @@ describe('Entity', function() {
17
17
  { name: 'baz', mapping: 'baz.test.val', type: 'bool', defaultValue: null, },
18
18
  ],
19
19
  },
20
+ entity: {
21
+ methods: {
22
+ testMethod: function() {
23
+ this.bar = 'test me';
24
+ },
25
+ },
26
+ },
20
27
  });
21
28
  this.data = {
22
29
  foo: 1,
@@ -71,7 +78,6 @@ describe('Entity', function() {
71
78
  entity.markSaved();
72
79
  expect(entity.isTempId).to.be.false;
73
80
  });
74
-
75
81
 
76
82
  it('clone', function() {
77
83
  const entity = this.entity;
@@ -100,6 +106,11 @@ describe('Entity', function() {
100
106
  expect(entity.properties.bar.name).to.be.eq('bar');
101
107
  expect(entity.properties.baz.name).to.be.eq('baz');
102
108
  });
109
+
110
+ it('_createMethods', function() {
111
+ this.entity.testMethod();
112
+ expect(this.entity.bar).to.be.eq('test me');
113
+ });
103
114
 
104
115
  it('loadOriginalData', function() {
105
116
  const data = {
@@ -11,6 +11,7 @@ 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.repository.type).to.be.eq('onebuild');
14
+ expect(this.schema.entity.methods.testMethod).to.be.a('function');
14
15
  });
15
16
 
16
17
  it('clone', function() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.6.3",
3
+ "version": "1.6.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
@@ -150,10 +150,28 @@ class Entity extends EventEmitter {
150
150
 
151
151
  initialize = () => {
152
152
  this.properties = this._createProperties();
153
+ this._createMethods();
153
154
  this.reset();
154
155
  this.isInitialized = true;
155
156
  }
156
157
 
158
+ /**
159
+ * Creates the methods for this Entity, based on Schema.
160
+ * @private
161
+ */
162
+ _createMethods = () => {
163
+ if (this.isDestroyed) {
164
+ throw Error('this._createMethods is no longer valid. Entity has been destroyed.');
165
+ }
166
+ const methodDefinitions = this.schema.entity.methods;
167
+ if (_.isEmpty(methodDefinitions)) {
168
+ return;
169
+ }
170
+ _.each(methodDefinitions, (method, name) => {
171
+ this[name] = method; // NOTE: Methods must be defined in schema as "function() {}", not as "() => {}" so "this" will be assigned correctly
172
+ });
173
+ }
174
+
157
175
  /**
158
176
  * Generates a new unique id and assigns it to this entity.
159
177
  */
@@ -1411,8 +1411,8 @@ export default class Repository extends EventEmitter {
1411
1411
  * Helper for delete()
1412
1412
  */
1413
1413
  removeEntity = async (entity) => {
1414
- this.entities = _.filter(this.entities, e => e !== entity);
1415
- entity.destroy();
1414
+ this.entities = _.filter(this.entities, e => e !== entity);
1415
+ entity.destroy();
1416
1416
  }
1417
1417
 
1418
1418
  /**
@@ -5,11 +5,12 @@ import _ from 'lodash';
5
5
 
6
6
  /**
7
7
  * Class represents the Schema definition for Model and Source
8
- * This is basically just a big config object, used to instantiate a Model and Source.
8
+ * This is basically just a big config object, used to instantiate an Entity, and Repository.
9
9
  * Usage:
10
10
  * - const schema = new Schema({
11
11
  * name: 'Users',
12
12
  * model: {},
13
+ * entity: {},
13
14
  * repository: {},
14
15
  * });
15
16
  *
@@ -96,6 +97,10 @@ export default class Schema extends EventEmitter {
96
97
  belongsToMany: [],
97
98
  },
98
99
  },
100
+
101
+ entity: {
102
+ methods: {}, // NOTE: Methods must be defined as "function() {}", not as "() => {}" so "this" will be assigned correctly
103
+ },
99
104
 
100
105
  /**
101
106
  * @member {object|string} repository - Config for Repository