npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mobx-state-tree-firebase

v1.0.0

Published

Firebase Integration for mobx-state-tree

Downloads

7

Readme

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.

  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 = Post.create({
  title: 'Hey!',
  description: 'updated?',
  _id: "-Kw4VJxqhKSiA3r2K8ue" 
});

FirstPost.save();
  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