Giter Site home page Giter Site logo

mobx-state-tree-firebase's Introduction

[NOT BEING MAINTAINED]

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 spiralis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mobx-state-tree-firebase's Issues

FireStore + MST

Hi @sanketsahusoft you talked about a MST+firestore module lib in the works. How do i access it

Add reason for being unmaintained

Hi; I just came across this project. Understood it's unmaintained, if you don't mind, could you add a small blurb about why to the readme? E.g. I'm primarily interested if:

a) is this unmaintained b/c the approach didn't work out and is fundamentally flawed?
b) is this unmaintained b/c there is another library that does the same thing but better, if so what?
c) is this unmaintained solely b/c the author doesn't have time/moved on to other technologies, but the existing code might be worth using+updating?

Thanks!

Typescript model types?

I see that the type is lost with the FirebaseModel construct. When I call actions( self => ... then the self variable is any.

Any plans on defining types for this so that the object structure declared in the props method keeps it's original types?

Issue running `npm run build`

When I run the build command as given in the title, I am getting an issue with the minification. The project was created using create-react-app with typescript: create-react-app mst-todo --scripts-version=react-scripts-ts

npm run-script build

> [email protected] build D:\src\INTS\mst-todo
> react-scripts-ts build

Creating an optimized production build...
Warning: The 'no-use-before-declare' rule requires type information.
ts-loader: Using [email protected] and D:\src\INTS\mst-todo\tsconfig.json
Failed to compile.

Failed to minify the code from this file:

        ./node_modules/mobx-state-tree-firebase/dist/index.js:11

Any ideas?

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.