Repository navigation
Closed
Labels
api: datastoreIssues related to the Datastore API.Issues related to the Datastore API.type: questionRequest for information or clarification. Not an issue.Request for information or clarification. Not an issue.
Milestone
Description
Comments appreciated.
If it doesn't we might want to consider updating documentation to avoid whatever problems it might have.
/cc @stephenplusplus @ryanseys
var express = require('express');
var bodyParser = require('body-parser');
var gcloud = require('gcloud');
var util = require('util');
var app = express();
// Book data
var books = [
{ name: "War and Peace", author: "Leo Tolstoy" },
{ name: "The Count of Monte Cristo", author: "Alexandre Dumas" }
];
if (!process.env.PROJECT_ID) {
console.log("The environment variable PROJECT_ID must be set to the project's id");
process.exit(1);
}
// Initialize the store
var dataset = gcloud.datastore.dataset({
projectId: process.env.PROJECT_ID
});
// Add a book
function addBook(transaction, book) {
transaction.save({ key: dataset.key('Book'), data: book });
}
// Find a book
function findBooks(transaction, book, cb) {
var query = dataset.createQuery(['Book']);
book = book || {};
if (book.author)
query = query.filter('author = ', book.author);
if (book.name)
query = query.filter('name = ', book.name);
transaction.runQuery(query, function (err, entities, endCursor) {
if (err) return cb(err);
cb(null, entities);
});
}
// Ensure initial books are at least there.
function ensureExists(book, cb) {
dataset.runInTransaction(function (transaction, done) {
findBooks(transaction, book, function (err, entities) {
if (!err && entities.length == 0) {
addBook(transaction, book);
}
done();
});
}, cb);
}
books.forEach(function (book) {
ensureExists(book, function (err) {
console.log('Initial book error', err);
});
});
// Add middle-ware
app.use(bodyParser.json());
// Add routes
app.get("/", function(req, res) {
res.send('Hello World!');
});
function booksQuery(req, res, author) {
dataset.runInTransaction(function (transaction, done) {
findBooks(transaction, { author: author }, function (err, books) {
if (err) return res.sendStatus(500);
res.json(books.map(function (entity) {
return entity.data;
}));
done();
});
}, function (err) {
if (err) return res.sendStatus(500);
});
}
app.get("/books", function (req, res) {
booksQuery(req, res);
});
app.get("/books/:author", function (req, res) {
booksQuery(req, res, req.param.author);
});
app.put("/books", function (req, res) {
if (!req.body || !req.body.name || !req.body.author) return res.sendStatus(400);
ensureExists({ author : req.body.author, name: req.body.name }, function (err) {
if (err) return res.sendStatus(500);
res.sendStatus(200);
});
});
// Start server
var server = app.listen(8080, function () {
console.log('Server running at http://localhost:%s', server.address().port);
});Metadata
Metadata
Assignees
Labels
api: datastoreIssues related to the Datastore API.Issues related to the Datastore API.type: questionRequest for information or clarification. Not an issue.Request for information or clarification. Not an issue.
Activity
ryanseys commentedon Jan 31, 2015
I found the following hard issues:
req.param.authorshould bereq.params.author.console.log('Initial book error', err);should beif(err) console.log('Initial book error', err);credentialsto datastore initialization.Here's an updated version that fixes the issues above and features a bit more documentation. In addition to this, there are certain issues such as lack of braces on if statements, lack of new lines in if statements which aren't killers here and allow the application to still be functional, but differ from the more strict style guides we have adopted in the gcloud-node library. I haven't fixed those in this code and tried to assume the style that was already given to me.
If you would like me to update the following snippet with those style guidelines, please let me know.
chuckjaz commentedon Feb 2, 2015
This example originally came from me.
I made some significant modifications to this file:
The file is now in a repository here, repeated here:
I have significantly simplified the example, removing the code to pre-initialize the datastore as it unnecessarily complicated the example. The population of the data will be handled elsewhere.
I didn't include the credentials because this assumes you will be authenticated via the metadata store (running in GCE or Managed VMs).
ryanseys commentedon Feb 2, 2015
Looks awesome now! 👍
chuckjaz commentedon Feb 2, 2015
Thanks!
One issue I brought up with JJ privately but wanted to bring up publicly is the projectId. The initialization of the datastore looks like:
This seems unnecessary to me as, if you are in GCE or Managed VMs, the project id can be retrieved from the metadata store [http://metadata.google.internal/computeMetadata/v1/project/project-id] as in,
Could we make the default project id the value of requesting the project-id from the metadata server?
Chuck.
ryanseys commentedon Feb 2, 2015
I might be mistaken but in Google App Engine and/or Managed VMs you can use the
process.env.GAE_LONG_APP_IDenvironment variable to get theprojectId.stephenplusplus commentedon Feb 2, 2015
Good idea to automatically use the environment's project ID. I believe we did that way back, then I forgot to re-implement. Left a to do on the PR that is tidying up the GAE story: #328
chuckjaz commentedon Feb 2, 2015
Thanks. I will close this issue as the review is done.
chore: add dev dependencies for cloud-rad ref docs (#365)
chore: add dev dependencies for cloud-rad ref docs (#365)
chore: add dev dependencies for cloud-rad ref docs (#365)
39 remaining items
build: ignore proto files in test coverage (#365)
fix: improve TypeScript types (#365)
chore(main): release 4.2.0 (#365)
build: ignore proto files in test coverage (#365)
fix(deps): update dependency google-auth-library to v3 (#365)
chore: migrate code from googleapis/gcp-metadata (#53)
fix(build): switch primary branch to main (#365)
fix: query should be on the list of extra args (#365)
fix: query should be on the list of extra args (#365)
refactor(ts): added ts style fix for test/histogram.ts (#365)
fix: remove timeout rule from streaming uploads (#365)
chore: update eslintignore config (#365)
chore: update eslintignore config (#365)
chore: update eslintignore config (#365)
chore: update eslintignore config (googleapis#365)