Skip to content

Instantly share code, notes, and snippets.

@stepanstipl
Last active October 22, 2019 00:03
Show Gist options
  • Save stepanstipl/73675a92b38d2d761016513754543d1c to your computer and use it in GitHub Desktop.
Save stepanstipl/73675a92b38d2d761016513754543d1c to your computer and use it in GitHub Desktop.
Example on using Datastore with nested entities

Firestore in Datastore Mode

- Quickstart with nested Embedded Entities

Setup

Run

  • If using the Datastore Emulator
    • Start the emulator - gcloud beta emulators datastore start
    • Set env variable (in the same terminal where you start the app) - export DATASTORE_EMULATOR_HOST=localhost:8081
  • node test.js
node_modules/
package-lock.json
{
"flat": {
"name": "Alice",
"contexts": {
"gpii-default": {
"name": "Default preferences",
"preferences": {
"http://registry.gpii.net/applications/com.microsoft.windows.onscreenKeyboard": {}
}
}
}
}
}
// Imports the Google Cloud client library
const {Datastore} = require('@google-cloud/datastore');
const fs = require('fs');
// Creates a client
const datastore = new Datastore();
async function quickstart() {
const key1 = datastore.key(["First", "abc"]);
const key2 = datastore.key(["Second", "def"]);
const data1 = {
description: 'Buy milk',
hello: 'World!',
};
const data2 = {
description: 'Buy bread',
hello: 'World!',
};
const entities = [
{
key: key1,
data: data1,
},
{
key: key2,
data: data2,
},
];
// Saves the entities
await datastore.upsert(entities);
console.log('Saved: ' + entities.length);
// Batch query
const [records] = await datastore.get([key1, key2]);
console.log('Returned: ' + records.length);
records.forEach(
r => console.log(`- ${r[datastore.KEY].kind}: ${r[datastore.KEY].name}`)
);
}
quickstart();
// Imports the Google Cloud client library
const {Datastore} = require('@google-cloud/datastore');
const fs = require('fs');
// Creates a client
const datastore = new Datastore();
async function quickstart() {
// The kind for the new entity
const kind = 'Task';
// The name/ID for the new entity
const name = 'sampletask1';
// The Cloud Datastore key for the new entity
const taskKey = datastore.key([kind, name]);
// Data I'm trying to save to the DB
const data = {
description: 'Buy milk',
hello: 'World!',
the: {
answer: "to",
the: "universe",
is: 42,
and: [ "some", "more", "stuff" ],
},
deeper: {
nested: {
structures: {
can: {
be: {
up: {
to: {
20: {
levels: {
deep: "yes sir",
},
},
},
},
},
},
},
},
},
};
// Prepares the new entity
const task = {
key: taskKey,
data: data,
};
// Saves the entity
await datastore.save(task);
console.log(`Saved ${task.key.name}: ${task.data.description}`);
// Read back my entity
read = await datastore.get(taskKey);
console.log(read);
// Print one nested value
console.log("And the answer is: " + read[0].the.is);
// Let's try to load & save JSON
const alice = JSON.parse(fs.readFileSync('alice.json'));
const prefKey = datastore.key([kind, "pref1"]);
const pref = {
key: prefKey,
data: alice
};
// Save it
await datastore.save(pref);
console.log(`Saved ${pref.key.name}`);
// And read it back
read = await datastore.get(prefKey);
console.log(read);
}
quickstart();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment