Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

cevr/firespace

Repository files navigation

 

A simple space for Firebase

 

Introduction

Firespace is for developers who want to quickly develop applications using Firebase, without all of the overhead. Currently only targeting the firebase real time database. 1kb gzipped!

npm install @cvr/firespace
yarn add @cvr/firespace

Step 1 - Set the firebase config in your root file

import { setConfig } from '@cvr/firespace';

setConfig({
    apiKey: '<APIKEY>',
    databaseURL: '<DATABASEURL>',
});

// you can also extend it
import 'firebase/auth';

const firebase = setConfig({
    apiKey: 'xx',
    databaseURL: 'xx',
    authDomain: 'xx',
});

const auth = firebase.auth();

Step 2 - Use it

import { useSpace } from '@cvr/firespace';

export default function App() {
    const [todos, space] = useSpace('todos');

    return (
        <div>
            <h1>Todo</h1>
            <AddTodo space={space} />
            <Todos todos={todos} space={space} />
        </div>
    );
}

function AddTodo({ space }) {
    const [text, setText] = useState('');
    const handleAddClick = async () => {
        if (text) {
            await space.add({ text, done: false });
            setText('');
        }
    };
    return <input value={text} onChange={e => setText(e.target.value)} placeholder="What to do next" />;
}

function Todos({ todos, space }) {
    const todosList = Object.entries(todos || {});
    return (
        <div>
            {todosList.map(([id, todo]) => (
                <Todo key={id} id={id} todo={todo} space={space} />
            ))}
        </div>
    );
}

function Todo({ todo, id, space }) {
    return (
        <div onClick={() => space.update(id, { done: !todo.done })}>
            <span>{todo.text}</span>
            <button
                onClick={e => {
                    e.stopPropagation();
                    space.delete(id);
                }}
            >
                delete
            </button>
        </div>
    );
}

Simple Api

import { useSpace } from '@cvr/firespace';

function Component() {
    const [todos, space] = useSpace('todos', ref => {
      //optionally, return custom ref
      return ref.orderByValue()
    });

    const id = await space.add({ text: 'Install it', done: false });
    await space.update(id, { done: true });
    await space.delete(id);
    space.loading;
    space.error;
}

Try it

Edit Firespace Example

About

A small and simple library for the firebase realtime database

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published