Creation

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 */
    <data>,
    /* {int|string}. -- optional. The id of the entity.
                        If not passed the Datastore will automatically 
                        generate a random one
     */
    <id>,
    /* {Array} -- optional. Ancestors of the entity. ex: ['ParentEntity', 1234 ] */
    <ancestors>,
    /* {string} -- optional. A specific namespace */
    <namespace>
)

example:
const blogPost = new BlogPost({title: 'title of the post', author: 'John Snow' });

id

(optional)

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.

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.

ancestors

(optional)

You can define the ancestors of the entity.

namespace

(optional)

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.

Last updated

Was this helpful?