Skip to content

Commit 15a713a

Browse files
authored
fix(entity): add "id" property to entity after it has been saved (#180)
fix #172
1 parent 75dc869 commit 15a713a

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

lib/entity.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,23 @@ class Entity {
3434
this.entityKey = createKey(this, id, ancestors, namespace);
3535
}
3636

37+
this.setId();
38+
3739
// create entityData from data passed
3840
this.entityData = buildEntityData(this, data || {});
3941

42+
/**
43+
* Create virtual properties (getters and setters for entityData object)
44+
*/
45+
Object.keys(this.schema.paths)
46+
.filter(pathKey => ({}.hasOwnProperty.call(this.schema.paths, pathKey)))
47+
.forEach(pathKey => Object.defineProperty(this, pathKey, {
48+
get: function getProp() { return this.entityData[pathKey]; },
49+
set: function setProp(newValue) {
50+
this.entityData[pathKey] = newValue;
51+
},
52+
}));
53+
4054
// wrap entity with hook methods
4155
hooks.wrap(this);
4256

@@ -104,6 +118,8 @@ class Entity {
104118
});
105119
}
106120

121+
_this.setId();
122+
107123
return _this;
108124
}
109125

@@ -245,6 +261,10 @@ class Entity {
245261
return this;
246262
}
247263

264+
setId() {
265+
this.id = this.entityKey.id || this.entityKey.name;
266+
}
267+
248268
/**
249269
* Return a Model from Gstore
250270
* @param name : model name

lib/model.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ class Model extends Entity {
4242
ModelInstance.prototype.gstore = gstore;
4343
ModelInstance.gstore = gstore;
4444

45-
/**
46-
* Create virtual properties (getters and setters for entityData object)
47-
*/
48-
Object.keys(schema.paths)
49-
.filter(key => ({}.hasOwnProperty.call(schema.paths, key)))
50-
.forEach(key => Object.defineProperty(ModelInstance.prototype, key, {
51-
get: function getProp() { return this.entityData[key]; },
52-
set: function setProp(newValue) {
53-
this.entityData[key] = newValue;
54-
},
55-
}));
56-
5745
/**
5846
* Create virtual properties (getters and setters for "virtuals" defined on the Schema)
5947
*/

test/integration/entity.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ const getAddress = (addressBookEntity = null) => {
5353
return address;
5454
};
5555

56-
const getUser = addressEntity => {
57-
const key = UserModel.key(getId());
56+
const getUser = (addressEntity, id = getId()) => {
57+
const key = UserModel.key(id);
5858
allKeys.push(key);
5959
const data = { address: addressEntity.entityKey };
6060
const user = new UserModel(data, null, null, null, key);
@@ -81,7 +81,6 @@ describe('Entity (Integration Tests)', () => {
8181
this.skip();
8282
}
8383
generatedIds = [];
84-
// return gstore.save([...users, ...addresses]);
8584
return gstore.save([addressBook, address]);
8685
});
8786

@@ -109,6 +108,16 @@ describe('Entity (Integration Tests)', () => {
109108
expect(entityFetched.entityData.address).deep.equal(address.entityKey);
110109
})
111110
));
111+
112+
it('should add the id or name to the entity', async () => {
113+
const entity1 = await user.save();
114+
expect(entity1.id).equal(entity1.entityKey.name);
115+
116+
const user2 = getUser(address, 1234);
117+
const entity2 = await user2.save();
118+
119+
expect(entity2.id).equal(entity2.entityKey.id);
120+
});
112121
});
113122

114123
describe('populate()', () => {

0 commit comments

Comments
 (0)