const admin = require("firebase-admin"); | |
const { GeoFirestore, GeoCollectionReference } = require("geofirestore"); | |
/** | |
* Generate new private key by selecting a project from below: | |
* https://console.firebase.google.com/u/0/project/_/settings/serviceaccounts/adminsdk | |
*/ | |
const SERVICE_ACCOUNT = require("./serviceAccountKey.json"); | |
/** | |
* Set the collection to update here. | |
*/ | |
const COLLECTION_NAME = "restaurants"; | |
/** | |
* If you use a custom key, define it here, otherwise leave this. | |
*/ | |
const CUSTOM_KEY = "coordinates"; | |
admin.initializeApp({ | |
credential: admin.credential.cert(SERVICE_ACCOUNT), | |
databaseURL: "https://" + SERVICE_ACCOUNT["project_id"] + ".firebaseio.com", | |
}); | |
const firestore = admin.firestore(); | |
const geofirestore = new GeoFirestore(firestore); | |
const collection = firestore.collection(COLLECTION_NAME); | |
const geocollection = new GeoCollectionReference(collection); | |
let totalUpdated = 0; | |
function updateCollection(query) { | |
return new Promise((resolve, reject) => | |
deleteQueryBatch(query.limit(500), resolve, reject) | |
); | |
} | |
function deleteQueryBatch(query, resolve, reject) { | |
query | |
.get() | |
.then((snapshot) => { | |
// When there are no documents left, we are done | |
if (snapshot.size === 0) { | |
console.log(`DONE, UPDATED ${totalUpdated}`); | |
return 0; | |
} | |
console.log( | |
`UPDATING ${snapshot.size} GEODOCUMENTS, ALREADY UPDATED ${totalUpdated}` | |
); | |
// Update documents in a batch | |
const batch = geofirestore.batch(); | |
snapshot.docs.forEach((doc) => { | |
const geodoc = geocollection.doc(doc.id); | |
batch.set(geodoc, doc.data().d, { customKey: CUSTOM_KEY }); | |
}); | |
return batch.commit().then(() => snapshot.size); | |
}) | |
.catch((e) => { | |
console.warn( | |
"This script was unable to convert your collection for GeoFirestore v4.", | |
e.details | |
); | |
return 0; | |
}) | |
.then((numUpdated) => { | |
totalUpdated += numUpdated; | |
if (numUpdated === 0) { | |
resolve(); | |
return; | |
} | |
process.nextTick(() => deleteQueryBatch(query, resolve, reject)); | |
}) | |
.catch((err) => reject(err)); | |
} | |
(async () => { | |
await updateCollection(collection.orderBy("g").orderBy("l").orderBy("d")); | |
})(); |
This comment has been minimized.
This comment has been minimized.
@junkycoder good catch! I just fixed it. |
This comment has been minimized.
This comment has been minimized.
How would I use something like this in React? |
This comment has been minimized.
This comment has been minimized.
I gave it a shot in my react app. I had to change admin to firebase.initializeApp(firebaseConfig); |
This comment has been minimized.
This comment has been minimized.
This isn't designed to work as part of an app as it needs unfettered access to your collections. This should be run as a node script with the admin sdk. |
This comment has been minimized.
This comment has been minimized.
Yeah. I got that now. I have a node backend I'm going to try and use this
for.
I did a hack to get 4.x working in my app but boy is it ugly.
Thanks
…On Mon, Sep 7, 2020 at 12:07 PM Michael Solati ***@***.***> wrote:
*@MichaelSolati* commented on this gist.
------------------------------
This isn't designed to work as part of an app as it needs unfettered
access to your collections. This should be run as a node script with the
admin sdk.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/874543594145fa29691b883835a73460#gistcomment-3444762>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACQHNB34TO6XR77DFSVWSBTSEUALZANCNFSM4QQPDWPA>
.
|
This comment has been minimized.
This comment has been minimized.
Does that script need to run everytime your geocollection updates or adds
data?
thanks
…On Mon, Sep 7, 2020 at 12:18 PM BP ***@***.***> wrote:
Yeah. I got that now. I have a node backend I'm going to try and use this
for.
I did a hack to get 4.x working in my app but boy is it ugly.
Thanks
On Mon, Sep 7, 2020 at 12:07 PM Michael Solati ***@***.***>
wrote:
> *@MichaelSolati* commented on this gist.
> ------------------------------
>
> This isn't designed to work as part of an app as it needs unfettered
> access to your collections. This should be run as a node script with the
> admin sdk.
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <https://gist.github.com/874543594145fa29691b883835a73460#gistcomment-3444762>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ACQHNB34TO6XR77DFSVWSBTSEUALZANCNFSM4QQPDWPA>
> .
>
|
This comment has been minimized.
This comment has been minimized.
You'd run the script once per collection in order to update it. After that you should use the 4+ library to add/set/anything rather than 3/2/1. |
This comment has been minimized.
This comment has been minimized.
you mean @firebase/firestore because I already have the latest version
https://www.npmjs.com/package/@firebase/firestore
geofirestore: v4.3.0
…On Mon, Sep 7, 2020 at 2:24 PM Michael Solati ***@***.***> wrote:
*@MichaelSolati* commented on this gist.
------------------------------
You'd run the script once per collection in order to update it. After that
you should use the 4+ library to add/set/anything rather than 3/2/1.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/874543594145fa29691b883835a73460#gistcomment-3444877>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACQHNBYNAGZOJUIDHBFJRJLSEUQPRANCNFSM4QQPDWPA>
.
|
This comment has been minimized.
This comment has been minimized.
This script was designed to migrate collections created be geofirestore 3/2/1 to be compatible for 4. So if you used v3 before you'd run the script then use v4 of geofirestore. |
This comment has been minimized.
This comment has been minimized.
Hi, I am using 3.4.1. currently. Are there any major improvements in 4.0. Also I am using this in google cloud functions in firebase project. Can you tell me how to run this script? Regards, |
This comment has been minimized.
This comment has been minimized.
There are no major improvements in performance, just restructured data. As far as running the script I'd advise you go through the code above and make sure it makes sense for you. I don't want to be responsible for accidentally corrupting your data, so please read it and make sure you want to execute it. This is not code you'd run on a cloud function, it's something you'd run locally. |
This comment has been minimized.
This comment has been minimized.
@MichaelSolati Thank you! Also, I am getting empty results when I use where condition as below I thought "notifications" field needs to be in all firestore documents of a collection. so updated all documents with "notifications" field but still the query returns empty set of documents. Can you please let me know if it is possible to filter the query results using where clause? Regards, |
This comment has been minimized.
This comment has been minimized.
I think you can delete the line #20 |
This comment has been minimized.
It works! ... in my case :)
Just had problem to run script by pure Node, but it is simple to rewrite imports to require
and remove type annotations like
: admin.firestore.Query
and: Promise<any>
.