Build a Datastore Key object.
Package
@google-cloud/datastoreExamples
//-
// Create an incomplete key with a kind value of `Company`.
//-
const {Datastore} = require(' @google-cloud/datastore
');
const datastore = new Datastore
();
const key = datastore. key
('Company');
//-
// Create a complete key with a kind value of `Company` and id`123`.
//-
const {Datastore} = require(' @google-cloud/datastore
');
const datastore = new Datastore
();
const key = datastore. key
(['Company', 123]);
//-
// If the ID integer is outside the bounds of a JavaScript Number
// object, create an Int.
//-
const {Datastore} = require(' @google-cloud/datastore
');
const datastore = new Datastore
();
const key = datastore. key
([
'Company',
datastore. int
('100000000000001234')
]);
const {Datastore} = require(' @google-cloud/datastore
');
const datastore = new Datastore
();
// Create a complete key with a kind value of `Company` and name `Google`.
// Note: `id` is used for numeric identifiers and `name` is used otherwise.
const key = datastore. key
(['Company', 'Google']);
//-
// Create a complete key from a provided namespace and path.
//-
const {Datastore} = require(' @google-cloud/datastore
');
const datastore = new Datastore
();
const key = datastore. key
({
namespace: 'My-NS',
path: ['Company', 123]
});
Serialize the key for later re-use.
const {Datastore} = require(' @google-cloud/datastore
');
const datastore = new Datastore
();
const key = datastore. key
({
namespace: 'My-NS',
path: ['Company', 123]
});
// Later...
const key = datastore. key
( key
. serialized
);
Constructors
(constructor)(options)
constructor
(
options
:
KeyOptions
);
Constructs a new instance of the Key
class
Parameter
Name
Description
options
KeyOptions
Properties
id
id
?:
string
;
Property Value
Type
Description
string
kind
kind
:
string
;
Property Value
Type
Description
string
name
name
?:
string
;
Property Value
Type
Description
string
namespace
namespace
?:
string
;
Property Value
Type
Description
string
parent
parent
?:
Key
;
Property Value
Type
Description
Key
path
path
:
Array<string
|
number
> ;
Property Value
Type
Description
Array
<string | number>
serialized
get
serialized
()
:
{
namespace
:
string
|
undefined
;
path
:
(
string
|
Int
)[];
};
Access the serialized
property for a library-compatible way to re-use a key.
Property Value
Type
Description
{
namespace: string | undefined;
path: (string | entity.Int
)[];
}
const key = datastore.key({
namespace: 'My-NS',
path: ['Company', 123]
});
// Later...
const key = datastore.key(key.serialized);