{"id":21560161,"url":"https://github.com/foundryapp/foundry-backend-nodejs","last_synced_at":"2025-10-29T06:13:50.306Z","repository":{"id":143749240,"uuid":"260650307","full_name":"FoundryApp/foundry-backend-nodejs","owner":"FoundryApp","description":"This SDK lets you configure Foundry with your Firebase Cloud Functions","archived":false,"fork":false,"pushed_at":"2020-05-09T12:17:55.000Z","size":72,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-24T11:13:09.026Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://foundryapp.co","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FoundryApp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-02T09:04:41.000Z","updated_at":"2021-04-13T12:24:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"8bcef5a9-58db-46fd-b4da-e97270cd99bc","html_url":"https://github.com/FoundryApp/foundry-backend-nodejs","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FoundryApp%2Ffoundry-backend-nodejs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FoundryApp%2Ffoundry-backend-nodejs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FoundryApp%2Ffoundry-backend-nodejs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FoundryApp%2Ffoundry-backend-nodejs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FoundryApp","download_url":"https://codeload.github.com/FoundryApp/foundry-backend-nodejs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244155640,"owners_count":20407390,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T09:12:59.660Z","updated_at":"2025-10-29T06:13:45.269Z","avatar_url":"https://github.com/FoundryApp.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Foundry Node.js SDK\nThis SDK lets you configure Foundry with your Firebase Cloud Functions\n\n## Table of contents\n- [Installation](#installation)\n- [Example](#example)\n- [Usage](#usage)\n  - [Emulated Auth](#fill-in-emulated-auth-users)\n  - [Emulated Firestore](#fill-in-emulated-firestore)\n  - [Emulated Realtime Database](#fill-in-emulated-realtimedb)\n  - [Register function](#register-function)\n  - [Firestore Cloud Function](#firestore-function)\n  - [RealtimeDB Cloud Function](#realtimedb-function)\n  - [Auth Cloud Function](#auth-function)\n  - [Https Cloud Function](#https-function)\n  - [Https Callable Cloud Function](#https-callable-function)\n\n\n## Installation\n\n```\n$ npm install --save @foundryapp/foundry-backend\n```\n\n## Example\n\n1. Add Foundry backend SDK to your Cloud Functions project\n\n```javascript\nconst functions = require('firebase-functions');\nconst admin = require('firebase-admin');\n// Import Foundry\nconst foundry = require('@foundryapp/foundry-backend').firebase;\n\nadmin.initializeApp();\n\n// Fill in the emulated auth users\nfoundry.users.add([\n  {\n    id: 'user-id-1',\n    data: { email: 'user@email.com' },\n  },\n]);\n\n// Fill in the emulated Firestore\nfoundry.firestore.collection('posts').addDocs([\n  {\n    id: 'post-doc-id-1',\n    data: {\n      ownerId: 'user-id-1',\n      content: 'Hello World!',\n    },\n  },\n]);\n\n/////////\n\n// Register 'myCloudFunc' with Foundry\nconst createPost = foundry.functions.httpsCallable.register('createPost');\n\n// Now specify how Foundry should trigger your function\ncreatePost.triggerAsUser('user-id-1').onCall({ \n  data: {\n    content: 'Content of a new post',    \n  },\n});\n\n// Cloud Function for creating posts\nexports.createPost = functions.https.onCall(async (data, context) =\u003e {\n  if (!context.auth) {\n    throw new functions.https.HttpsError('permission-denied', 'User isn\\'t authenticted');\n  }\n  \n  const { uid } = context.auth;\n  await admin.firestore().collection('posts').add({\n    ownerId: uid,\n    content: data.content,\n  });\n});\n\n/////////\n\nconst getPosts = foundry.functions.httpsCallable.register('getPosts');\ngetPosts.triggerAsUser('user-id-1').onCall();\n\n// Cloud Function for retrieving all user's posts\nexports.getPosts = functions.https.onCall(async (data, context) =\u003e {\n  if (!context.auth) {\n    throw new functions.https.HttpsError('permission-denied', 'User isn\\'t authenticted');\n  }\n  \n  const { uid } = context.auth;\n  const posts = await admin.firestore().collection('posts').where('ownerId', '==', uid).get();\n  return posts.docs.map(d =\u003e d.data());\n});\n```\n\n2. Start Foundry CLI in the same directory where  are your Cloud Functions\n\n```\n$ foundry go\n```\n\nNow every time you save your local code files Foundry will trigger your cloud functions as you specified in the code.\nThe output from Foundry in your terminal will look like this:\n```\n[1] createPost\nresponse =\u003e {\n  data: null,\n  status: 200,\n  statusText: 'OK'\n}\n\n[1] getPosts\nresponse =\u003e {\n  data: [\n    {\n      ownerId: 'user-id-1',\n      content: 'Content of a new post'\n    },\n    {\n      ownerId: 'user-id-1',\n      content: 'Hello World!'\n    }\n  ],\n  status: 200,\n  statusText: 'OK'\n}\n```\n\n## Usage\n\n### Fill in emulated Auth users\n```javascript\n// Specify users explicitely\nfoundry.users.add([\n  {\n    id: 'user-id-1',\n    data: { email: 'user@email.com' },\n  },\n]);\n\n// Copy the first 5 users from your production Firebase app\nfoundry.users.copyFromProdByCount(5);\n\n// Copy users from your production Firebase app by their IDs\nfoundry.users.copyFromProdById(['user-prod-id-1', 'user-prod-id-2']);\n```\n\n### Fill in emulated Firestore\n```javascript\n// Specify documents explicitely\nfoundry.firestore.collection('posts').addDocs([\n  {\n    id: 'post-doc-id-1',\n    data: {\n      ownerId: 'user-id-1',\n      content: 'Hello World!',\n    },\n  },\n]);\n\n// Copy documents from the production\n\n// Takes the first 5 docs from collection 'posts' in the production\nfoundry.firestore.collection('posts').copyDocsFromProdByCount(5); \n// Copy docs from the production by their IDs\nfoundry.firestore.collection('posts').copyDocsFromProdById(['prod-doc-id-1', 'prod-doc-id-2']); \n```\n\n### Fill in emulated RealtimeDB\n```javascript\n// Specify refs explicitely\nfoundry.database.ref('posts').addChildren([\n  {\n    key: 'post-1',\n    data: {\n      ownerId: 'user-id-1',\n      content: 'Hello World!',\n    },\n  },\n]);\n\n// Copy directly from the production\nfoundry.database.ref('posts').copyAllChildrenFromProd();\nfoundry.database.ref('posts').copyChildrenFromProdByKey(['prod-key-1', 'prod-key-2']);\n```\n\n### Register function\n```javascript\n// The name under which your cloud function is registered\n// with Foundry must be the same under which you export\n// your cloud function\n\nconst funcFirestore = firebase.functions.firestore.register('funcFirestore');\nconst funcDatabase = firebase.functions.database.register('funcDatabase');\nconst funcAuth = firebase.functions.auth.register('funcAuth');\nconst funcHttps = firebase.functions.https.register('funcHttps');\nconst funcHttpsCallable = firebase.functions.httpsCallable.register('funcHttpsCallable');\n\n// To get a reference of a previously registered function\nconst func = firebase.functions.firestore.get('functionName');\n```\n\n### Describe function triggers\n\n#### Firestore function\n```javascript\n// New document will be created in the emulated Firestore that will trigger the function\nfuncFirestore.trigger().onCreate({\n  collection: 'posts',\n  id: 'post-doc-id-1',\n  data: {\n    content: 'My post content',\n  },\n});\n\n// Document with the ID 'post-doc-id-1' in the collection 'posts' \n// will be deleted from the emulated Firestore\nfuncFirestore.trigger().onDelete('posts', 'post-doc-id-1');\n\n// Specified document in the emulated Firestore will be updated\nfuncFirestore.trigger().onUpdate({\n  collection: 'posts',\n  id: 'post-doc-id-1',\n  data: {\n    content: 'New content',\n  },\n});\n\n// Trigger the function by copying a specified document to the emulated Firestore\nfuncFirestore.triggerWithProdData().onCreate('posts', 'prod-post-doc-id');\n```\n\n#### RealtimeDB function\n```javascript\n// Creates a new reference in the emulated RealtimeDB and triggers the function\nfuncDatabase.trigger().onCreate({\n  refPath: 'ref/path',\n  data: { ... },\n});\n\n// Deletes an existing reference in the emulated RealtimeDB and triggers the function\nfuncDatabase.trigger().onDelete('ref/path');\n\n// Updates an existing reference in the emulated RealtimeDB and triggers the function\nfuncDatabase.trigger().onUpdate({\n  refPath: 'ref/path',\n  data: { ... },\n});\n\n// Copies a reference from the production to the emulated RealtimeDB and triggers the function\nfuncDatabase.triggerWithProdData().onCreate('re/path/in/production');\n```\n\n#### Auth function\n```javascript\n// Creates a new user in the emulated Auth users and triggers the function\nfuncAuth.trigger().onCreate({\n  uid: 'user-id-1',\n  data: { email: 'user@email.com' },\n});\n\n// Creates an existing user in the emulated Auth users and triggers the function\nfuncAuth.trigger().onDelete('user-id');\n\n// Copies an existing user from the production to the emulated Auth users and triggers the function\nfuncAuth.triggerWithProdData().onCreate('prod-user-id');\n```\n\n#### Https function\n```javascript\n// Send a GET http request to the function with the specified data in body\nfuncHttps.trigger().get({\n  // Optional field to specify URL route, for example https://example.com/api/user\n  // if not specified the value '/' is used\n  route: '/api/user',\n  data: { ... },\n});\n\n// Following HTTP methods are supported\nfuncHttps.trigger().post({ ... });\nfuncHttps.trigger().put({ ... });\nfuncHttps.trigger().delete({ ... });\nfuncHttps.trigger().options({ ... });\n```\n\n#### Https Callable function\n```javascript\n// Triggers the function as an authenticated user 'user-id-1' with the specified payload\n// User with the specified ID must be present in the emulated Auth\nfuncHttpsCallable.triggerAsUser('user-id-1').onCall({\n  // Payload\n});\n\n// Triggers the function as an non-authenticated (!) user with the specified payload\nfuncHttpsCallable.trigger().onCall({\n  // Payload\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoundryapp%2Ffoundry-backend-nodejs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffoundryapp%2Ffoundry-backend-nodejs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoundryapp%2Ffoundry-backend-nodejs/lists"}