Skip to content

Commit cdc9873

Browse files
committed
Merge branch 'release/v2.1.0'
2 parents 98f859a + c37b523 commit cdc9873

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

lib/entity.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,7 @@ function createKey(self, id, ancestors, namespace) {
140140

141141
let path;
142142
if (id) {
143-
if (is.string(id)) {
144-
id = isFinite(id) ? parseInt(id, 10) : id;
145-
} else if (!is.number(id)) {
146-
throw new Error('id must be a string or a number');
147-
}
143+
id = parseId(self, id);
148144
path = hasAncestors ? ancestors.concat([self.entityKind, id]) : [self.entityKind, id];
149145
} else {
150146
if (hasAncestors) {
@@ -159,6 +155,30 @@ function createKey(self, id, ancestors, namespace) {
159155
return namespace ? self.gstore.ds.key({ namespace, path }) : self.gstore.ds.key(path);
160156
}
161157

158+
/**
159+
* Parse the id and according to the keyType config in the Schema ("name"|"id"|<undefined>)
160+
* it will convert an '123'(string) id to 123 (int).
161+
* @param {*} self -- the entity instance
162+
* @param {*} id -- id passed in constructor
163+
*/
164+
function parseId(self, id) {
165+
const { options } = self.schema;
166+
167+
if (is.string(id)) {
168+
if (options && options.keyType === 'name') {
169+
return id;
170+
} else if (options.keyType === 'id') {
171+
return self.gstore.ds.int(id);
172+
}
173+
// auto convert string number to number
174+
return isFinite(id) ? self.gstore.ds.int(id) : id;
175+
} else if (!is.number(id)) {
176+
throw new Error('id must be a string or a number');
177+
}
178+
179+
return id;
180+
}
181+
162182
function buildEntityData(self, data) {
163183
const { schema } = self;
164184
const isJoiSchema = !is.undef(schema._joi);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gstore-node",
3-
"version": "2.0.2",
3+
"version": "2.1.0",
44
"description": "gstore-node is a Google Datastore Entity Models tools",
55
"main": "index.js",
66
"scripts": {

test/entity-test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,18 @@ describe('Entity', () => {
216216
it('---> with a full Key ("string" Integer keyname passed)', () => {
217217
const entity = new Model({}, '123');
218218

219-
expect(entity.entityKey.id).equal(123);
219+
expect(entity.entityKey.id).equal('123');
220+
});
221+
222+
it('---> with a full Key ("string" Integer **not** converted)', () => {
223+
schema = new Schema({
224+
name: { type: 'string' },
225+
}, { keyType: 'name' });
226+
Model = gstore.model('EntityKind', schema);
227+
228+
const entity = new Model({}, '123');
229+
230+
expect(entity.entityKey.name).equal('123');
220231
});
221232

222233
it('---> throw error is id passed is not string or number', () => {

0 commit comments

Comments
 (0)