Datastore.prototype.key(options: entity.KeyOptions): entity.Key
Helper to create a Key object, scoped to the instance's namespace by default.
You may also specify a configuration object to define a namespace and path.
Example 1
Example 1
<caption>Create an incomplete key with a kind value of `Company`. Since no Id is supplied, Datastore will generate one on save.</caption> const {Datastore} = require('@google-cloud/datastore'); const datastore = new Datastore(); const key = datastore.key('Company');
Example 2
Example 2
<caption>Create a complete key with a kind value of `Company` and Id `123`.</caption> const {Datastore} = require('@google-cloud/datastore'); const datastore = new Datastore(); const key = datastore.key(['Company', 123]);
Example 3
Example 3
<caption>If the ID integer is outside the bounds of a JavaScript Number object, create an Int.</caption> const {Datastore} = require('@google-cloud/datastore'); const datastore = new Datastore(); const key = datastore.key([ 'Company', datastore.int('100000000000001234') ]);
Example 4
Example 4
<caption>Create a complete key with a kind value of `Company` and name `Google`. Because the supplied Id is a string, Datastore will prefix it with "name=". Had the supplied Id been numeric, Datastore would prefix it with the standard, "id=".</caption> const {Datastore} = require('@google-cloud/datastore'); const datastore = new Datastore(); const key = datastore.key(['Company', 'Google']);
Example 5
Example 5
<caption>Create a complete key from a provided namespace and path.</caption> const {Datastore} = require('@google-cloud/datastore'); const datastore = new Datastore(); const key = datastore.key({ namespace: 'My-NS', path: ['Company', 123] });
Example 6
Example 6
<caption>Create a complete key that specifies an ancestor. This will create a Team entity with a name of "Datastore", which belongs to the Company with the "name=Google" key.</caption> const {Datastore} = require('@google-cloud/datastore'); const datastore = new Datastore(); const key = datastore.key(['Company', 'Google', 'Team', 'Datastore']);
Example 7
Example 7
<caption>Create a incomplete key that specifies an ancestor. This will create an Employee entity with an auto-generated Id, which belongs to the Company with the "name=Google" key.</caption> const {Datastore} = require('@google-cloud/datastore'); const datastore = new Datastore(); const key = datastore.key(['Company', 'Google', 'Employee']);