Foundry Node.js SDK
This SDK lets you write
Table of contents
Installation
$ npm install --save @foundryapp/foundry-backend
Example
- Add Foundry backend SDK to your Cloud Functions project
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const foundry = require('@foundryapp/foundry-backend').firebase;
admin.initializeApp();
foundry.users.add([
{
id: 'user-id-1',
data: { email: 'user@email.com' },
},
]);
foundry.firestore.collection('posts').addDocs([
{
id: 'post-doc-id-1',
data: {
ownerId: 'user-id-1',
content: 'Hello World!',
},
},
]);
const createPost = foundry.functions.httpsCallable.register('createPost');
createPost.triggerAsUser('user-id-1').onCall({
data: {
content: 'Content of a new post',
},
});
exports.createPost = functions.https.onCall(async (data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('permission-denied', 'User isn\'t authenticted');
}
const { uid } = context.auth;
await admin.firestore().collection('posts').add({
ownerId: uid,
content: data.content,
});
});
const getPosts = foundry.functions.httpsCallable.register('getPosts');
getPosts.triggerAsUser('user-id-1').onCall();
exports.getPosts = functions.https.onCall(async (data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('permission-denied', 'User isn\'t authenticted');
}
const { uid } = context.auth;
const posts = await admin.firestore().collection('posts').where('ownerId', '==', uid).get();
return posts.docs.map(d => d.data());
});
- Start Foundry CLI in the same directory where are your Cloud Functions
$ foundry go
Now every time you save your local code files Foundry will trigger your cloud functions as you specified in the code.
The output from Foundry in your terminal will look like this:
[1] createPost
response => {
data: null,
status: 200,
statusText: 'OK'
}
[1] getPosts
response => {
data: [
{
ownerId: 'user-id-1',
content: 'Content of a new post'
},
{
ownerId: 'user-id-1',
content: 'Hello World!'
}
],
status: 200,
statusText: 'OK'
}
Usage
Fill in emulated Auth users
foundry.users.add([
{
id: 'user-id-1',
data: { email: 'user@email.com' },
},
]);
foundry.users.copyFromProdByCount(5);
foundry.users.copyFromProdById(['user-prod-id-1', 'user-prod-id-2']);
Fill in emulated Firestore
foundry.firestore.collection('posts').addDocs([
{
id: 'post-doc-id-1',
data: {
ownerId: 'user-id-1',
content: 'Hello World!',
},
},
]);
foundry.firestore.collection('posts').copyDocsFromProdByCount(5);
foundry.firestore.collection('posts').copyDocsFromProdById(['prod-doc-id-1', 'prod-doc-id-2']);
Fill in emulated RealtimeDB
foundry.database.ref('posts').addChildren([
{
key: 'post-1',
data: {
ownerId: 'user-id-1',
content: 'Hello World!',
},
},
]);
foundry.database.ref('posts').copyAllChildrenFromProd();
foundry.database.ref('posts').copyChildrenFromProdByKey(['prod-key-1', 'prod-key-2']);
Register function
const funcFirestore = firebase.functions.firestore.register('funcFirestore');
const funcDatabase = firebase.functions.database.register('funcDatabase');
const funcAuth = firebase.functions.auth.register('funcAuth');
const funcHttps = firebase.functions.https.register('funcHttps');
const funcHttpsCallable = firebase.functions.httpsCallable.register('funcHttpsCallable');
const func = firebase.functions.firestore.get('functionName');
Describe function triggers
Firestore function
funcFirestore.trigger().onCreate({
collection: 'posts',
id: 'post-doc-id-1',
data: {
content: 'My post content',
},
});
funcFirestore.trigger().onDelete('posts', 'post-doc-id-1');
funcFirestore.trigger().onUpdate({
collection: 'posts',
id: 'post-doc-id-1',
data: {
content: 'New content',
},
});
funcFirestore.triggerWithProdData().onCreate('posts', 'prod-post-doc-id');
RealtimeDB function
funcDatabase.trigger().onCreate({
refPath: 'ref/path',
data: { ... },
});
funcDatabase.trigger().onDelete('ref/path');
funcDatabase.trigger().onUpdate({
refPath: 'ref/path',
data: { ... },
});
funcDatabase.triggerWithProdData().onCreate('re/path/in/production');
Auth function
funcAuth.trigger().onCreate({
uid: 'user-id-1',
data: { email: 'user@email.com' },
});
funcAuth.trigger().onDelete('user-id');
funcAuth.triggerWithProdData().onCreate('prod-user-id');
Https function
funcHttps.trigger().get({
route: '/api/user',
data: { ... },
});
funcHttps.trigger().post({ ... });
funcHttps.trigger().put({ ... });
funcHttps.trigger().delete({ ... });
funcHttps.trigger().options({ ... });
Https Callable function
funcHttpsCallable.triggerAsUser('user-id-1').onCall({
});
funcHttpsCallable.trigger().onCall({
});