firebase-collection
v1.5.1
Published
Firebase Realtime Database Collection Modeling for JavaScript
Downloads
26
Maintainers
Readme
firebase-collection
A simple Firebase modeling library for managing collections with schema validation.
Installation
Install the package using npm:
npm install firebase-collection
Initialization
Initialize Firebase using the init function from the package
// commonjs
const firebase = require("firebase-collection");
// module
import firebase from "firebase-collection";
firebase.init({
url: "your_firebase_realtime_database_url",
service: "service_account ( type Object )"
})
Creating a Collection
Create a collection by instantiating the Collection class
const users = new firebase.Collection('users', {
// Your collection schema
name: String,
age: Number
});
CRUD Operations
Perform CRUD operations on the collection
// Create a new record
const newUser = await users.create({ name: 'John Doe', age: 25 });
// Find all records
const allUsers = await users.findAll();
// Find a record by ID
const userById = await users.findById(newUser.__id);
// Find a record based on a filter
const filteredUser = await users.find({ name: 'John Doe' });
// Update a record by ID
await users.update(newUser.__id, { age: 26 });
// Delete a record by ID
await users.delete(newUser.__id);
// Delete a collection
await users.useDestroy();
Export
// commonjs
const { useExports } = require("firebase-collection");
// module
import { useExports } from "firebase-collection";
// use fs and path
useExports().then( metadata => {
fs.writeFileSync( path.join( __dirname, 'export.json' ), metadata, { encoding:"utf8" });
})
License
This project is licensed under the MIT License - see the LICENSE file for details.