Datastore.prototype.save(entities: Entities,gaxOptions?: CallOptions,): Promise<SaveResponse>
Insert or update the specified object(s). If a key is incomplete, its associated object is inserted and the original Key object is updated to contain the generated ID.
This method will determine the correct Datastore method to execute
(upsert, insert, or update) by using the key(s) provided. For
example, if you provide an incomplete key (one without an ID), the request
will create a new entity and have its ID automatically assigned. If you
provide a complete key, the entity will be updated with the data specified.
By default, all properties are indexed. To prevent a property from being
included in all indexes, you must supply an excludeFromIndexes array.
To prevent large properties from being included in all indexes, you must supply
excludeLargeProperties: true.
See below for an example.
Example 1
Example 1
//- // Save a single entity. // // Notice that we are providing an incomplete key. After saving, the // original Key object used to save will be updated to contain the path // with its generated ID. //- const key = datastore.key('Company'); const entity = { key: key, data: { rating: '10' } }; datastore.save(entity, (err) => { console.log(key.path); // [ 'Company', 5669468231434240 ] console.log(key.namespace); // undefined }); //- // Save a single entity using a provided name instead of auto-generated ID. // // Here we are providing a key with name instead of an ID. After saving, // the original Key object used to save will be updated to contain the // path with the name instead of a generated ID. //- const key = datastore.key(['Company', 'donutshack']); const entity = { key: key, data: { name: 'DonutShack', rating: 8 } }; datastore.save(entity, (err) => { console.log(key.path); // ['Company', 'donutshack'] console.log(key.namespace); // undefined }); //- // Save a single entity with a provided namespace. Namespaces allow for // multitenancy. To read more about this, see // [the Datastore docs on key concepts](https://goo.gl/M1LUAu). // // Here we are providing a key with namespace. //- const key = datastore.key({ namespace: 'my-namespace', path: ['Company', 'donutshack'] }); const entity = { key: key, data: { name: 'DonutShack', rating: 8 } }; datastore.save(entity, (err) => { console.log(key.path); // ['Company', 'donutshack'] console.log(key.namespace); // 'my-namespace' }); //- // Save different types of data, including ints, doubles, dates, booleans, // blobs, and lists. // // Notice that we are providing an incomplete key. After saving, the // original Key object used to save will be updated to contain the path // with its generated ID. //- const key = datastore.key('Company'); const entity = { key: key, data: { name: 'DonutShack', rating: datastore.int(10), worth: datastore.double(123456.78), location: datastore.geoPoint({ latitude: 40.6894, longitude: -74.0447 }), numDonutsServed: 45, founded: new Date('Tue May 12 2015 15:30:00 GMT-0400 (EDT)'), isStartup: true, donutEmoji: Buffer.from('\uD83C\uDF69'), keywords: [ 'donut', 'coffee', 'yum' ] } }; datastore.save(entity, (err, apiResponse) => {}); //- // Use an array, `excludeFromIndexes`, to exclude properties from indexing. // This will allow storing string values larger than 1500 bytes. //- const entity = { key: datastore.key('Company'), excludeFromIndexes: [ 'description', 'embeddedEntity.description', 'arrayValue[]', 'arrayValue[].description' ], data: { description: 'Long string (...)', embeddedEntity: { description: 'Long string (...)' }, arrayValue: [ 'Long string (...)', { description: 'Long string (...)' } ] } }; datastore.save(entity, (err, apiResponse) => {}); //- // Use boolean `excludeLargeProperties`, to auto exclude Large properties from indexing. // This will allow storing string values larger than 1500 bytes. //- const entity = { key: datastore.key('Company'), data: { description: 'Long string (...)', embeddedEntity: { description: 'Long string (...)' }, arrayValue: [ 'Long string (...)', { description: 'Long string (...)' } ] }, excludeLargeProperties: true }; datastore.save(entity, (err, apiResponse) => {}); //- // Save multiple entities at once. //- const companyKey = datastore.key(['Company', 123]); const productKey = datastore.key(['Product', 'Computer']); const entities = [ { key: companyKey, data: { HQ: 'Dallas, TX' } }, { key: productKey, data: { vendor: 'Dell' } } ]; datastore.save(entities, (err, apiResponse) => {}); //- // Explicitly attempt to 'insert' a specific entity. //- const userKey = datastore.key(['User', 'chilts']); const entity = { key: userKey, method: 'insert', data: { fullName: 'Andrew Chilton' } }; datastore.save(entity, (err, apiResponse) => {}); //- // Returns a Promise if callback is omitted. //- datastore.save(entity).then((data) => { const apiResponse = data[0]; });
Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.