firebase_node 0.1.2+3

  • Readme
  • Changelog
  • Example
  • Installing
  • new63

Pub Version Gitter

Wrapper for firebase functions and firebase admin to run in Node.js based on package:firebase and package:firebase_functions_interop.

Note: This package is in active development and may contains bugs. Contribution is highly appreciated.

Usage #

create project #

mkdir example
cd example
firebase init functions
mkdir functions/node

example/functions/node/main.dart #

import 'package:firebase_node/admin.dart';
import 'package:firebase_node/functions.dart';

void main() {
  functions['onCreateUser'] = functions.auth.user().onCreate(onCreateUser);

  functions['onDeleteUser'] = functions.auth.user().onDelete(onDeleteUser);

  functions['storageFunctions'] =
      functions.storage.bucket('my-bucket').object().onFinalize(onFinalize);

  // non-Future function
  functions['documentUpdate'] =
      functions.firestore.document('my-document').onUpdate((change, context) {
    print(context.auth.uid);
    return 0;
  });
}

final app = initializeApp();

//Future function must have literal
Future<int> onDeleteUser(UserRecord user, EventContext context) async {
  final ref = app.firestore().collection('userdata').doc(user.uid);
  await ref.delete();
  return 0; //Every function must return value so that Firebase functions know when the function end.
}

Future<int> onFinalize(ObjectMetadata metadata, EventContext context) async {
  final ref = app.firestore().collection('tracker').doc('my-bucket');
  final fieldsAndValues = [
    FieldPath('file', metadata.name),
    metadata.size,
    'file_count',
    FieldValue.increment(1),
  ];
  await ref.update(fieldsAndValues: fieldsAndValues);
  return 0;
}

Future<int> onCreateUser(UserRecord user, EventContext context) async {
  final ref = app.firestore().collection('userdata').doc(user.uid);
  final data = {
    'name': user.displayName,
    'email': user.email,
    'timestamp': FieldValue.serverTimestamp(),
  };
  await ref.set(data);
  return 0;
}

example/functions/build.yaml #

targets:
  $default:
    builders:
      firebase_node|nodejs_builder:
        enabled: true
        options:
          # node_modules is optional to require any node modules
          node_modules:
            # This wil output
            # 'const someModule = require("module-name")'
            # 'self.someModule = someModule'
            # So if you have custom js interop you can use node module @JS('someModule')
            someModule: module-name
          dart2js_args:
            - -O1

example/functions/pubspec.yaml #

name: firebase_node_example

environment:
  sdk: '>=2.8.1 <3.0.0'

dependencies:
  firebase_node:

dev_dependencies:
  build_node_compilers:
  build_runner:

example/functions/package.json #

{
  "name": "firebase_node_example",
  "engines": {
    "node": "8"
  },
  "main": "build/node/main.dart.node.js",
  "dependencies": {
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1"
  }
}

example/firebase.json #

{
  "functions": {
    "ignore": ["node_modules", ".dart_tool", "packages"]
  }
}

compile #

cd functions
pub run build_runner build --output build
firebase deploy

Project Structure #

example/
├─firebase.json
└─functions/
  ├─lib/
  ├─node/
  │ └─main.dart
  ├─package.json
  ├─build.yaml
  └─pubspec.yaml

Progress #

firebase_node.admin #

  • [x] app
  • [x] auth
  • [x] credential
  • [x] database
  • [x] firestore
  • [x] instance id
  • [x] machine learning
  • [x] messaging
  • [x] project management
  • [x] remote config
  • [x] security rules
  • [ ] storage

firebase_node.functions #

  • [x] analytics
  • [x] auth
  • [x] crashlytics
  • [x] database
  • [x] firestore
  • [x] https
  • [x] pubsub
  • [x] remote config
  • [x] storage
  • [x] test lab

Features and bugs #

Please file feature requests and bugs at the issue tracker.

0.1.2+3 #

  • Change Dart SDK to 2.8.1

0.1.2+2 #

  • Fix typo

0.1.2 #

  • Add some docs
  • Add some admin storage features
  • Fixed some issues

0.1.1 #

  • Some admin storage features completed
  • Change factory constructor to static if it can return null

0.1.0 #

  • Some bug fixes
  • Admin Storage on progress

0.0.4 #

  • Implement getAll in firebase
  • Fix firebase transaction get
  • Add transaction getDocument and getQuery

0.0.3 #

  • Create custom builder that allow to require NodeJS modules
  • Fix update methods in Firestore classes

0.0.2 #

  • Fix admin.initializeApp() on null options
  • Add example
  • Fix pedantic and pana

0.0.1 #

  • Initial version

example/readme.md

Project Structure #

example/
├─README.md
├─firebase.json
└─functions/
  ├─lib/
  ├─node/
  │ └─main.dart
  ├─package.json
  ├─build.yaml
  └─pubspec.yaml

Compile #

firebase init functions
cd functions
pub get
pub run build_runner build --output build
firebase deploy

example/functions/node/main.dart #

import 'package:firebase_node/admin.dart';
import 'package:firebase_node/functions.dart';

void main() {
  functions['onCreateUser'] = functions.auth.user().onCreate(onCreateUser);

  functions['onDeleteUser'] = functions.auth.user().onDelete(onDeleteUser);

  functions['storageFunctions'] =
      functions.storage.bucket('my-bucket').object().onFinalize(onFinalize);

  // non-Future function
  functions['documentUpdate'] =
      functions.firestore.document('my-document').onUpdate((change, context) {
    print(context.auth.uid);
    return 0;
  });
}

final app = initializeApp();

//Future function must have literal
Future<int> onDeleteUser(UserRecord user, EventContext context) async {
  final ref = app.firestore().collection('userdata').doc(user.uid);
  await ref.delete();
  return 0; //Every function must return value so that Firebase functions know when the function end.
}

Future<int> onFinalize(ObjectMetadata metadata, EventContext context) async {
  final ref = app.firestore().collection('tracker').doc('my-bucket');
  final fieldsAndValues = [
    FieldPath('file', metadata.name),
    metadata.size,
    'file_count',
    FieldValue.increment(1),
  ];
  await ref.update(fieldsAndValues: fieldsAndValues);
  return 0;
}

Future<int> onCreateUser(UserRecord user, EventContext context) async {
  final ref = app.firestore().collection('userdata').doc(user.uid);
  final data = {
    'name': user.displayName,
    'email': user.email,
    'timestamp': FieldValue.serverTimestamp(),
  };
  await ref.set(data);
  return 0;
}

example/functions/build.yaml #

targets:
  $default:
    builders:
      firebase_node|nodejs_builder:
        enabled: true
        options:
          # node_modules is optional to require any node modules
          node_modules:
            # This wil output
            # 'const someModule = require("module-name")'
            # 'self.someModule = someModule'
            # So if you have custom js interop you can use node module @JS('someModule')
            someModule: module-name
          dart2js_args:
            - -O1

example/functions/pubspec.yaml #

name: firebase_node_example

environment:
  sdk: '>=2.8.1 <3.0.0'

dependencies:
  firebase_node:

dev_dependencies:
  build_node_compilers:
  build_runner:

example/functions/package.json #

{
  "name": "firebase_node_example",
  "engines": {
    "node": "8"
  },
  "main": "build/node/main.dart.node.js",
  "dependencies": {
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1"
  }
}

example/firebase.json #

{
  "functions": {
    "ignore": ["node_modules", ".dart_tool", "packages"]
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  firebase_node: ^0.1.2+3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:firebase_node/admin.dart';
import 'package:firebase_node/builder.dart';
import 'package:firebase_node/functions.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
34
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
63
Learn more about scoring.

We analyzed this package on May 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.1
  • pana: 0.13.8-dev

Maintenance issues and suggestions

No valid SDK. (-20 points)

The analysis could not detect a valid SDK that can use this package.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.8.1 <3.0.0
analyzer ^0.39.8 0.39.8
build ^1.3.0 1.3.0
build_modules ^2.10.0 2.10.0
build_web_compilers ^2.11.0 2.11.0
js ^0.6.1+1 0.6.1+1
meta ^1.1.8 1.1.8
node_interop ^1.1.1 1.1.1
node_io ^1.1.1 1.1.1
node_preamble ^1.4.8 1.4.9
path ^1.7.0 1.7.0
scratch_space ^0.0.4+2 0.0.4+2
Transitive dependencies
_fe_analyzer_shared 3.0.0
archive 2.0.13
args 1.6.0
async 2.4.1
bazel_worker 0.1.23+1
build_config 0.4.2
charcode 1.1.3
checked_yaml 1.0.2
collection 1.14.12
convert 2.1.1
crypto 2.1.5
csslib 0.16.1
fixnum 0.10.11
glob 1.2.0
graphs 0.2.0
html 0.14.0+3
json_annotation 3.0.1
logging 0.11.4
package_config 1.9.3
pedantic 1.9.0
pool 1.4.0
protobuf 1.0.1
pub_semver 1.4.4
pubspec_parse 0.1.5
source_maps 0.10.9
source_span 1.7.0
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+15
yaml 2.2.1
Dev dependencies
effective_dart ^1.2.1