README
Firebase Database Modeler upgrades your Realtime Database to a whole new level!
Full and awesome Typescript support!
Supports firebase, firebase-admin and react-native-firebase packages.
README still being improved. Not a focus right now, as I am using this package in a full time real project development.
Instalation
npm install --save firebase-database-modeler
// or
yarn add firebase-database-modeler
Usage
import { _, _$, _root, modelerSetDefaultDatabase } from 'firebase-database-modeler';
// There are multiple ways of setting up the database depending of the firebase package
// you are using (firebase, firebase-admin or react-native-firebase).
// Read their docs to see how to get the firebase.database().
const database = firebase.database()
modelerSetDefaultDatabase(database);
const stores = _('stores', {
$storeId: _$({
name: _<string>('n'), // The DB property key can be different from the model property key!
rating: _<number>('rating'),
open: _<boolean>('open'),
optionalProp: _<number | null>('oP'), // You can tag the model property as optional by adding `| null`.
users: _('users', {
$userId: _$({
name: _<string>('name')
})
})
})
})
const root = _root({
stores
})
async function createStore(storeId: string, userId: string, userName: string) {
// Typescript IntelliSense will fully guide you to build the object!
// In _set(), all the model properties are required.
await stores.$storeId._set({
name: 'Cool Store', // In the model declaration, we've set this name property
// to have the key 'n' in the DB. _set() automatically converts this!
rating: 4.2,
open: true,
users: {
[userId]: {
name: userName
};
}
}, storeId) // This storeId variable will be used as the $storeId path segment
}
async function setStoreName(storeId: string, newName: string) {
// Typescript will complain if the _set() first argument is not a string, in this case.
await stores.$storeId._set(newName, storeId)
}
// The type of this function will be the store model type! This package
// automatically converts the model schema to the DB schema!
async function getStore(storeId: string) {
return await stores.$storeId._onceVal('value', storeId)
}
API
Functions
modelerSetDefaultDatabase (database: Database) => void
Sets the default database that will be used by all Realtime Database operations you may call using your Model.
You do not need to use this if you are passing the database to the _root()
or ._ref()
based functions.
_ (key: string, children?: Node) => Node
Creates a Node. First parameter is the Node key: the name of it in the database.
The second parameter allows Node nesting.
You may pass a type to it.
const root = _('/', {
first: _('1st'),
second: _('second', {
nested: _<string>('stuff'),
}),
});
database.second.nested._key(); // = 'stuff'
_$ (key: string, children?: Node) => Node
Creates a Variable Node. It's the same as calling _('