Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Counter SDK for backend services like "node.js" #28

Closed
urkopineda opened this issue Oct 7, 2019 · 7 comments
Closed

Counter SDK for backend services like "node.js" #28

urkopineda opened this issue Oct 7, 2019 · 7 comments
Assignees
Labels
in-progress A fix or resolution is in progress type: feature request New feature or request

Comments

@urkopineda
Copy link

Extension name

This feature request is for extension: \firestore-counter

What feature would you like to see?

Make the sharded-counter.js work not only for the frontend but for backend services like node.js. This requirement includes not to depend on requiring / importing firebase/app and firebase/firestore in the user code, but on the SDK code instead.

How would you use it?

In our case, we use a backend service to make our web site cleaner and lighter without Firebase SDK, so, all the usage of Firebase occurs in the backend. We would like to use the counter so that's why we need to use it on the backend.

@Salakar Salakar added the type: feature request New feature or request label Oct 10, 2019
@lechim
Copy link

lechim commented Oct 17, 2019

Hi I made a quick work around function from the SDK to let this extension work on javascript Firebase functions.

const uuidv4 = require('uuid/v4');
const admin = require('firebase-admin');
const functions = require('firebase-functions');

exports.test = functions.https.onCall(async (data, context) => {
return await increment('visits', 'pages/clicks/test', 1);
});

function increment(field, path, val) {
const db = admin.firestore();
const increment = admin.firestore.FieldValue.increment(val);
const update = field.split(".").reverse().reduce((value, name) => ({[name]: value}), increment);
return doc.collection('_counter_shards_').doc(uuidv4()).set(update, {merge: true});
}

It may not be the best solution yet but works.

@Ehesp Ehesp added the in-review Awaiting review by FE team. label Nov 21, 2019
@Ehesp
Copy link
Member

Ehesp commented Nov 21, 2019

XFN Team: Would this be an additional "client" in the project, or would it be appropriate to adapt the web script to work in a Node environment?

@Ehesp
Copy link
Member

Ehesp commented Nov 21, 2019

Duplicate: #55

@Henry-
Copy link

Henry- commented Nov 21, 2019

I think it would be better to adapt the web script to work in a Node environment.

I have tested the above code, and it triggers the aggregation function every time the "test" onCall function is called.

@voxelbustersold
Copy link

Won't increment provided by admin sdk work for distributed counter tasks?

@karayu karayu added in-progress A fix or resolution is in progress and removed in-review Awaiting review by FE team. labels Jan 13, 2020
@jhuleatt
Copy link
Collaborator

@voxelbusters FieldValue.increment is the right thing for incrementing a value, but it doesn't solve the underlying problem that a Firestore document can only be updated about once per second. More info here.

@jhuleatt
Copy link
Collaborator

We're not planning an official implementation for any other clients at this time, and we've changed the name of the "Web Client SDK" to "client sample" to try to make that a little more clear.

The typescript sample implementation is a good starting place for a node.js implementation of the counter.

In the future, I'd like to see how much of this logic we can move off of the client and into something like a callable function that's installed by the extension itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in-progress A fix or resolution is in progress type: feature request New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants