
Security News
NVD Concedes Inability to Keep Pace with Surging CVE Disclosures in 2025
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
@google-cloud/datastore
Advanced tools
@google-cloud/datastore is a Node.js client library for Google Cloud Datastore, a NoSQL document database built for automatic scaling, high performance, and ease of application development. It allows you to interact with Google Cloud Datastore to perform operations such as creating, reading, updating, and deleting entities.
Create an Entity
This code sample demonstrates how to create and save a new entity in Google Cloud Datastore. It creates a 'Task' entity with a description and saves it to the datastore.
const { Datastore } = require('@google-cloud/datastore');
const datastore = new Datastore();
const taskKey = datastore.key('Task');
const task = {
key: taskKey,
data: {
description: 'Buy milk'
}
};
datastore.save(task)
.then(() => {
console.log(`Task ${taskKey.id} created successfully.`);
})
.catch(err => {
console.error('ERROR:', err);
});
Retrieve an Entity
This code sample demonstrates how to retrieve an entity from Google Cloud Datastore using its key. It fetches a 'Task' entity by its ID and logs the result.
const { Datastore } = require('@google-cloud/datastore');
const datastore = new Datastore();
const taskKey = datastore.key(['Task', 'taskId']);
datastore.get(taskKey)
.then(([task]) => {
if (!task) {
console.log('No task found.');
return;
}
console.log('Task:', task);
})
.catch(err => {
console.error('ERROR:', err);
});
Update an Entity
This code sample demonstrates how to update an existing entity in Google Cloud Datastore. It updates the description of a 'Task' entity.
const { Datastore } = require('@google-cloud/datastore');
const datastore = new Datastore();
const taskKey = datastore.key(['Task', 'taskId']);
const task = {
key: taskKey,
data: {
description: 'Buy milk and bread'
}
};
datastore.update(task)
.then(() => {
console.log(`Task ${taskKey.id} updated successfully.`);
})
.catch(err => {
console.error('ERROR:', err);
});
Delete an Entity
This code sample demonstrates how to delete an entity from Google Cloud Datastore. It deletes a 'Task' entity by its key.
const { Datastore } = require('@google-cloud/datastore');
const datastore = new Datastore();
const taskKey = datastore.key(['Task', 'taskId']);
datastore.delete(taskKey)
.then(() => {
console.log(`Task ${taskKey.id} deleted successfully.`);
})
.catch(err => {
console.error('ERROR:', err);
});
Mongoose is an ODM (Object Data Modeling) library for MongoDB and Node.js. It provides a schema-based solution to model your application data. While @google-cloud/datastore is used for Google Cloud Datastore, Mongoose is specifically designed for MongoDB, offering a rich set of features for data modeling and validation.
Firebase Admin SDK allows server-side access to Firebase services, including Firestore, which is a NoSQL document database similar to Google Cloud Datastore. While @google-cloud/datastore is specific to Google Cloud's Datastore, Firebase Admin SDK provides access to Firestore, which is part of the Firebase platform, offering real-time data synchronization and offline capabilities.
AWS SDK for JavaScript provides access to Amazon DynamoDB, a fully managed NoSQL database service. Similar to Google Cloud Datastore, DynamoDB is designed for high availability and scalability. The main difference is the cloud provider, with DynamoDB being part of AWS and @google-cloud/datastore being part of Google Cloud.
Google Cloud Datastore Client Library for Node.js
Looking for more Google APIs than just Datastore? You might want to check out google-cloud
.
Follow the activation instructions to use the Google Cloud Datastore API with your project.
$ npm install --save @google-cloud/datastore
var datastore = require('@google-cloud/datastore')({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
var key = datastore.key(['Product', 'Computer']);
datastore.get(key, function(err, entity) {
console.log(err || entity);
});
// Save data to Datastore.
var blogPostData = {
title: 'How to make the perfect homemade pasta',
author: 'Andrew Chilton',
isDraft: true
};
var blogPostKey = datastore.key('BlogPost');
datastore.save({
key: blogPostKey,
data: blogPostData
}, function(err) {
// `blogPostKey` has been updated with an ID so you can do more operations
// with it, such as an update.
blogPostData.isDraft = false;
datastore.save({
key: blogPostKey,
data: blogPostData
}, function(err) {
if (!err) {
// The blog post is now published!
}
});
});
It's incredibly easy to get authenticated and start using Google's APIs. You can set your credentials on a global basis as well as on a per-API basis. See each individual API section below to see how you can auth on a per-API-basis. This is useful if you want to use different accounts for different Google Cloud services.
If you are running this client on Google Compute Engine, we handle authentication for you with no configuration. You just need to make sure that when you set up the GCE instance, you add the correct scopes for the APIs you want to access.
// Authenticating on a global basis.
var projectId = process.env.GCLOUD_PROJECT; // E.g. 'grape-spaceship-123'
var datastore = require('@google-cloud/datastore')({
projectId: projectId
});
// ...you're good to go!
If you are not running this client on Google Compute Engine, you need a Google Developers service account. To create a service account:
var projectId = process.env.GCLOUD_PROJECT; // E.g. 'grape-spaceship-123'
var datastore = require('@google-cloud/datastore')({
projectId: projectId,
// The path to your key file:
keyFilename: '/path/to/keyfile.json'
// Or the contents of the key file:
credentials: require('./path/to/keyfile.json')
});
// ...you're good to go!
FAQs
Cloud Datastore Client Library for Node.js
The npm package @google-cloud/datastore receives a total of 165,137 weekly downloads. As such, @google-cloud/datastore popularity was classified as popular.
We found that @google-cloud/datastore demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
Security Fundamentals
Attackers use obfuscation to hide malware in open source packages. Learn how to spot these techniques across npm, PyPI, Maven, and more.
Security News
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.