Skip to content

Instantly share code, notes, and snippets.

@ra9r
Last active June 14, 2019 18:50
Show Gist options
  • Save ra9r/291da389a1354dc837a9fea57c9a3e25 to your computer and use it in GitHub Desktop.
Save ra9r/291da389a1354dc837a9fea57c9a3e25 to your computer and use it in GitHub Desktop.
Store a DocumentReference #firestore #nodejs
const admin = require('firebase-admin');
let serviceAccount = require('./service_key.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
let db = admin.firestore();
async function main() {
console.log('Saving thingA')
await db.collection('Things').doc('thingA').set({
name: 'This is thing A',
parent: null,
}, { merge: true });
await db.doc('Things/thingA/doodads/doodad1').set({
name: 'This is thing A doodad 1'
});
await db.doc('Things/thingA/doodads/doodad2').set({
name: 'This is thing A doodad 2'
});
console.log('Saving thingB')
await db.collection('Things').doc('thingB').set({
name: 'This is thing A',
parent: db.doc('Things/thingA')
}, { merge: true });
await db.collection('OtherThings').doc('otherThingA').set({
name: 'This is other thing A',
doodads: [
db.doc('Things/thingA/doodads/doodad1'),
db.doc('Things/thingA/doodads/doodad2'),
]
}, { merge: true });
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment