What is @google-cloud/datastore?
@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.
What are @google-cloud/datastore's main functionalities?
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);
});
Other packages similar to @google-cloud/datastore
mongoose
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
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.
dynamodb
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 Platform Google Cloud Platform logo](https://avatars2.githubusercontent.com/u/2810941?v=3&s=96)
![codecov](https://img.shields.io/codecov/c/github/googleapis/nodejs-datastore/master.svg?style=flat)
Cloud Datastore Client Library for Node.js
Read more about the client libraries for Cloud APIs, including the older
Google APIs Client Libraries, in Client Libraries Explained.
Table of contents:
Quickstart
Before you begin
- Select or create a Cloud Platform project.
- Enable the Google Cloud Datastore API.
- Set up authentication with a service account so you can access the
API from your local workstation.
Installing the client library
npm install @google-cloud/datastore
Using the client library
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
async function quickstart() {
const kind = 'Task';
const name = 'sampletask1';
const taskKey = datastore.key([kind, name]);
const task = {
key: taskKey,
data: {
description: 'Buy milk',
},
};
await datastore.save(task);
console.log(`Saved ${task.key.name}: ${task.data.description}`);
}
quickstart();
Samples
Samples are in the samples/
directory. The samples' README.md
has instructions for running the samples.
The Google Cloud Datastore Node.js Client API Reference documentation
also contains samples.
Versioning
This library follows Semantic Versioning.
This library is considered to be General Availability (GA). This means it
is stable; the code surface will not change in backwards-incompatible ways
unless absolutely necessary (e.g. because of critical security issues) or with
an extensive deprecation period. Issues and requests against GA libraries
are addressed with the highest priority.
More Information: Google Cloud Platform Launch Stages
Contributing
Contributions welcome! See the Contributing Guide.
License
Apache Version 2.0
See LICENSE