method Datastore.prototype.key
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.

Examples

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

<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

<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

<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

<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

<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

<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']);

Parameters

optional
options: entity.KeyOptions

Key path. To specify or override a namespace, you must use an object here to explicitly state it.

Return Type

entity.Key

A newly created Key from the options given.

Datastore.prototype.key(path: PathType[]): entity.Key

Parameters

path: PathType[]

Return Type

entity.Key
Datastore.prototype.key(path: string): entity.Key

Parameters

path: string

Return Type

entity.Key