🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

mobx-state-tree-firebase

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-state-tree-firebase

Firebase Integration for mobx-state-tree

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

Mobx-state-tree firebase model

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 -

Node (v6+)
npm (v3+)

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 mst-firebase --save

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 'FirebaseModel';

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

import { findById } from 'FirebaseModel';
import { findAllWhere } from 'FirebaseModel';

Next you have to "inherit" all the properties of the FirebaseModel to your mobx-state-tree model. An example for this would be -

const Post = FirebaseBaseModel.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.

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

FirstPost.save();

And voila! A new post is added!

  • To update a model -
const FirstPost = Post.create({
  title: 'Hey!',
  description: 'updated?',
  _id: "-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.
  • 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));
});
  • 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

Keywords

Firebase

FAQs

Package last updated on 10 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts