@everlast-brands/collection
v2.2.1
Published
A collection is used to interact with an apps data on firebase. They provide a simple API to CRUD custom datastructures.
Downloads
7
Readme
Everlast Brands - Collections
Collections are a middleware between firebase firestore and backend services. They make getting CRUD applications up and running very fast, however, they are extremely tightly coupled to google firestore.
Installation
yarn add @everlast-brands/collection
// or
npm install @everlast-brands/collection
Setup
import express from "express";
import * as admin from "firebase-admin";
import {Collection} from "@everlast-brands/collection";
// Configure firebase firestore
admin.initializeApp({
databaseURL: process.env.DATABASE_URL,
projectId: process.env.PROJECT_ID,
storageBucket: process.env.STORAGE_BUCKET,
});
const DB = admin.firestore();
DB.settings({ ignoreUndefinedProperties: true });
const app = express();
// add collections to the server
app.use((req, res, next) => {
req.collections = {
user: new Collection('users', DB),
messages: new Collection('messages', DB),
// ...other collections
}
})
// ...rest of app
Usage
app.post("/users", async (req, res) => {
const userdata = req.body.user;
const result = await req.collections.user.create(userdata);
// if successfull send success
// if error send failure
})