项目作者: mksolemn

项目描述 :
Firebase/firestore store snippets
高级语言: JavaScript
项目地址: git://github.com/mksolemn/firebase-firestore-snippets.git
创建时间: 2018-03-17T12:39:51Z
项目社区:https://github.com/mksolemn/firebase-firestore-snippets

开源协议:

下载


firebase-firestore-snippets

required packages

  1. npm install --save @google-cloud/firestore
  2. npm i --save lodash

1. Firestore nodejs deduper - remove duplicates from firestore

Same method could be user for firestore or any Array of data.

Example: clearing duplicates from database by document field

Examples shows how to remove duplicates in Array - full firestore implementation - HERE

  1. // clear_dups.js
  2. dupeArray.map((val, indexOut) => {
  3. const tempObj = val;
  4. console.log('Original: ', val.title, ' :id: ', val.dupeId);
  5. dupeArray.map((inVal, indexIn) => {
  6. if (val.dupeId !== inVal.dupeId) { // IF YOU WANT TO KEEP ORIGINAL DATABASE ITEM
  7. // this will make sure that one item is left and the rest removed
  8. // change title & user.facebook_post_link to any desired field or add more fields...
  9. if (val.[YOUR-FIELD-VALUE] === inVal.[YOUR-FIELD-VALUE]) {
  10. firestore.doc('scraped_posts/' + inVal.dupeId).delete().then(() => {
  11. console.log('Found duplicate: ', inVal.[YOUR-FIELD-VALUE], ' :id: ', inVal.dupeId);
  12. });
  13. }
  14. }
  15. })
  16. })

Run deduper

  1. node deduper-run

2. Firestore delete by title or ID - delete single document

Example: remove single document from firestore by ID or title

Examples shows how to remove single document by ID or title - full firestore implementation - HERE

You can change title field to any other field you may need to delete by.

  1. // delete_single.js
  2. function deleteById(postID){
  3. if (postID) {
  4. firestore.doc('scraped_posts/' + postID).delete().then(() => {
  5. console.log('Delete post: ', postID, ' :id: ', postID, ' -DELETED');
  6. });
  7. }
  8. }
  9. function deleteByTitle(postTitle) {
  10. if (postTitle) {
  11. firestore.collection('scraped_posts').where('title', '==', postTitle)
  12. .get()
  13. .then(querySnapshot => {
  14. deleteById( querySnapshot.docs[0].id);
  15. })
  16. }
  17. }

Run document delete

  1. // delete by id
  2. node run_delete --id 'id of property'
  3. // delete by title
  4. node run_delete --title 'Title of preperty'