pulyaevskiy / firebase-functions-interop

Firebase Functions Interop Library for Dart.
BSD 3-Clause "New" or "Revised" License
191 stars 52 forks source link

Feature to export group of functions #46

Open m1-nann opened 5 years ago

m1-nann commented 5 years ago

Could we add feature to support adding function into a newObject and then to functions exports.

Let's say if I want to deploy two function functionA and functionB in group alphabet this way:

firebase deploy --only functions:alphabet

at the moment, I would set it up like this

/// index.dart
void main(){
  /// some functionA functionB
  var o = newObject();
  setProperty(
      o,
      'functionA',
      functions.database
          .ref('entries/{user_id}/functionATrigger')
          .onWrite(functionA));
  setProperty(
      o,
      'functionB',
      functions.database
          .ref('entries/{user_id}/functionBTrigger')
          .onWrite(functionB));
  functions['alphabet'] = o;
}
pulyaevskiy commented 5 years ago

I was not aware that it's possible to do this way. Could you point me at the docs in JS SDK where this is mentioned?

I'd be curious to see if this works for HTTPS triggers as well (I'd assume not).

m1-nann commented 5 years ago

Found it (couldn't this morning): https://firebase.google.com/docs/cli/#deploy_specific_functions

for HTTPS, It does actually, because the actually function name when deployed is converted to 'alphabet-functionA' (with the dash) (assuming that was your concern)

pulyaevskiy commented 5 years ago

Ok, thanks. Good to know, it might be something recent they added as I don't remember this details the last time a read this doc.

I'd probably approach it by adding a new FunctionGroup class where users can register their functions and then using it to register the whole group. E.g.:

// This is pseudo code
class FunctionGroup {
  final Object _group = newObject();
  operator []=(String key, dynamic function) {
    setProperty(_group, key, function);
  }
}

// ...

class FirebaseFunctions {
  // ...
  operator []=(String key, dynamic function) {
    if (function is FunctionGroup) {
      setExport(key, function._group);
    } else {
      setExport(key, function);
    }
  }
}
m1-nann commented 5 years ago

that sounds good. I also suggest to have and FirebaseFunction base class (or , so we can use IsFirebaseFunction instead of dynamic. Even if it's empty, still better for type check.

/// as Base
abstract class FirebaseFunction {}
class DatabaseFunctions extends FirebaseFunction {
  final js.FirebaseFunctions _functions;
  DatabaseFunctions._(this._functions);

  /// Returns reference builder for specified [path] in Realtime Database.
  RefBuilder ref(String path) =>
      new RefBuilder._(_functions.database.ref(path));
}

/// OR as interface
class IsFirebaseFunction {}
class DatabaseFunctions implements IsFirebaseFunction {...}

What do you think?