firestore_update_rest.js
#! /usr/bin/node
// ---------------------------------------------------------------
// firestore_update_rest.js
//
// Feb/01/2020
//
// ---------------------------------------------------------------
var fs = require("fs")
var get_token = require('./get_token.js')
// ---------------------------------------------------------------
function patch_proc(token,url,str_json)
{
const args = {
data: str_json,
headers: { "Authorization": "Bearer " + token}
}
try
{
client.patch(url, args, function (data, response) {
console.log(data)
})
console.error ("*** 終了 ***")
}
catch (error)
{
console.error ("*** error *** from client.get ***")
console.error (error)
}
}
// ---------------------------------------------------------------
console.error ("*** 開始 ***")
const key_in=process.argv[2]
const population_in=process.argv[3]
console.log (key_in + "\t" + population_in)
const today = new Date ()
var ddx = (1900 + today.getYear ()) + "-" + (today.getMonth () +1)
ddx += "-" + today.getDate ()
console.log(ddx)
const env = process.env
const json_str = fs.readFileSync (env.GOOGLE_APPLICATION_CREDENTIALS,'utf8')
const dict_aa = JSON.parse (json_str)
const project_id = dict_aa['project_id']
console.log(project_id)
const token = get_token.get_token_proc()
const url="https://firestore.googleapis.com/v1/projects/" + project_id + "/databases/(default)/documents/cities/" + key_in
var Client = require('node-rest-client').Client
var client = new Client()
var args = {
headers: { "Authorization": "Bearer " + token}
}
client.get(url, args, function (data, response) {
console.log(data.fields)
data.fields.population.integerValue = population_in
data.fields.date_mod.stringValue = ddx
const str_json = JSON.stringify(data)
patch_proc(token,url,str_json)
})
// ---------------------------------------------------------------
get_token.js はこちら
Node.js の Rest で Cloud Firestore のデータを作成 (Create)
実行コマンド
export NODE_PATH=/usr/lib/node_modules
export GOOGLE_APPLICATION_CREDENTIALS="***.json"
./firestore_update_rest.js t0921 8942700