Giter Site home page Giter Site logo

mobx-state-tree-firebase's Introduction

mobx-state-tree-firebase: Firebase integration for your MST models

This package will ease the process of setting up all your CRUD operations in your react-native/react project which uses mobx-state-tree as the state management library.

Prerequisites

You will need a mobx-state-tree model to use this library.

Installing

Installing is pretty straightforward, if you have the prerequisites set up. Just copy the below code to your terminal in your project directory. Note that this has a peer dependency of mobx and firebase (duh!).

npm install mobx-state-tree-firebase --save

or

yarn add mobx-state-tree-firebase

Getting Started

Now that you have installed the package you are all set to start playing with it!

First, import the package to wherever you need to save the MST model.

import { FirebaseModel } from 'mobx-state-tree-firebase';

This will only provide the save/update function, to get all the other functions you can import them seperately like

import { findById } from 'mobx-state-tree-firebase';
import { findAllWhere } from 'mobx-state-tree-firebase';

You can extend your existing MST models with the FirebaseModel like this

import { FirebaseModel } from 'mobx-state-tree-firebase';

const Post = FirebaseModel.named("Post")
  .props({
    _path: "posts",
    title: types.string,
    description: types.string
  })
  .actions((self) => {
    
    return {
      updateTitle(newTitle: string) {
        self.title = newTitle;
      },
    };
  });

Now that Let's go over some common use cases.

  1. To create a new entry -
const firstPost = Post.create({
  title: 'Hey!',
  description: 'Please save this.',
});

firstPost.save();

And voila! A new post is added!

  1. To update a model -
const firstPost = await findById(Post, "-Kw4VJxqhKSiA3r2K8ue");

firstPost.save();

To update an existing post you will have to pass the id in the Model as a private property. Based on this it will create/update a new entry in the firebase model.

  1. To find an entry in your firebase database by it's Id -
findById(
	YourModel,
    IdYouWantToGet)
    .then(success =>
    	//do stuff on success here
	}, error => {
    	//do stuff on error here
    });
    

The first argument is your mobx-state-tree model and the second argument is the Id of the object you are trying to access.

Let us look at an example where we want to fetch a user with a particular user id.

findById(Post, "-Kw0DPoUeMBtsB-ZrZdQ").then(success => {
  console.log("success", getSnapshot(success));
});
  1. To find all entries in your firebase database by a condition -
findAllWhere(
	YourModel,
	PropYouWantToFilterBy,
    OperatorYouWantToFilterBy,
    ValueYouWantToFilterBy)
    .then(success =>
    	//do stuff on success here
	}, error => {
    	//do stuff on error here
    });
);

Note that the first argument is your mobx-state-tree model, second is the column on which you want to add the condition, third is the operator(supported operators right now are =,>,< and between), fourth is the value to filter by.

Let us look at an example where we want to fetch all posts with the title as Lorem ipsum.

findAllWhere(Post, 'title', '=', 'Lorem ipsum').then(success => {
  console.log("success", success);
});

License

This project is licensed under the MIT License - see the LICENSE.md file for details

mobx-state-tree-firebase's People

Contributors

amith92 avatar sanketsahu avatar shivrajkumar avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.