Each entity is an instance of a Model and represents an entity stored in the Datastore of a certain Kind. To create an entity you call the Model constructor
new Model(
/* {object} -- The entity data to be saved */
/* {int|string}. -- optional. The id of the entity.
If not passed the Datastore will automatically
generate a random one
/* {Array} -- optional. Ancestors of the entity. ex: ['ParentEntity', 1234 ] */
/* {string} -- optional. A specific namespace */
const blogPost = new BlogPost({title: 'title of the post', author: 'John Snow' });


By default, if you don't pass an id when you create an instance, the entity id will be auto-generated. If you want to manually give the entity an id, pass it as a second parameter during the instantiation.
// String id
const blogPost = new BlogPost(data, 'stringId');
// Integer ir
const blogPost = new BlogPost(data, 1234);
If the ID integer is outside the bounds of a JavaScript Number object, you have to create an Int calling the int() method from @google-cloud/datastore
Reminder: gstore.ds is an alias to the google-cloud datastore instance.
// long Key ID
const blogPost = new BlogPost(data,'100000000000001234'));


You can define the ancestors of the entity.
// Auto generated id with an ancestor
const blogPost = new BlogPost(data, null, ['Parent', 'keyname']);
// Manual id on an ancestor
const blogPost = new BlogPost(data, 1234, ['Parent', 'keyname']);


By default entities keys are generated with the default namespace (defined when setting up the '@google-cloud/datastore'). You can create models instances on another namespace by passing it as a third parameter.
// Creates an entity with auto-generated id on the namespace ""
const blogPost = new BlogPost(data, null, null, '');
Last modified 3yr ago
Copy link