firebase_node 0.0.3+1 firebase_node: ^0.0.3+1 copied to clipboard
Wrapper for firebase functions and firebase admin to run in Node.js.
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 my_project
cd my_project
firebase init functions
mkdir functions/node
my_project/functions/node/main.dart #
import 'package:firebase_node/functions.dart';
import 'package:firebase_node/admin.dart' as admin;
void main() {
functions['onCreateUser'] =
functions.auth.user().onCreate((user, context) async {
final app = await admin.initializeApp();
final ref = app.firestore().collection('userdata').doc(user.uid);
await ref.set({
'name': user.displayName,
'email': user.email,
'timestamp': admin.FieldValue.serverTimestamp(),
});
});
functions['onDeleteUser'] = functions.auth.user().onDelete(onDeleteUser);
functions['storageFunctions'] = functions.storage
.bucket('my-bucket')
.object()
.onFinalize((metadata, context) async {
final app = admin.initializeApp();
final ref = app.firestore().collection('tracker').doc('my-bucket');
final fileName = metadata.name;
final fileSize = metadata.size;
final fileId = metadata.id;
final timestamp =
admin.Timestamp.fromDateTime(DateTime.parse(metadata.updated));
final data = {
'file.$fileId': {
'name': fileName,
'size': fileSize,
'timestamp': timestamp,
},
'file_count': admin.FieldValue.increment(1),
};
await ref.update(data: data);
});
}
Future<void> onDeleteUser(UserRecord user, EventContext context) async {
final app = await admin.initializeApp();
final ref = app.firestore().collection('userdata').doc(user.uid);
await ref.delete();
}
my_project/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
# We need to override build_node_compilers to use nodejs_builder
build_node_compilers|entrypoint:
enabled: false
my_project/functions/pubspec.yaml #
name: my_project
environment:
sdk: '>=2.8.1 <3.0.0'
dependencies:
firebase_node:
dev_dependencies:
build_node_compilers:
build_runner:
my_project/functions/package.json #
{
"name": "functions",
"engines": {
"node": "8"
},
"main": "build/node/main.dart.js",
"dependencies": {
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1"
}
}
my_project/firebase.json #
{
"functions": {
"ignore": ["node_modules", ".dart_tool", "packages"]
}
}
compile #
cd functions
pub run build_runner build --output build
firebase deploy
Project Structure #
my_project/
├─firebase.json
└─functions/
├─lib/
├─node/
│ └─main.dart
├─package.json
├─build.yaml
└─pubspec.yaml
Progress #
firebase_node.admin #
- app
- auth
- credential
- database
- firestore
- instance id
- machine learning
- messaging
- project management
- remote config
- security rules
- storage
firebase_node.functions #
- analytics
- auth
- crashlytics
- database
- firestore
- https
- pubsub
- remote config
- storage
- test lab
Features and bugs #
Please file feature requests and bugs at the issue tracker.