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

mobase

v0.1.7

Published

Firebase-to-mobx adapter

Downloads

4

Readme

Mobase.js: Firebase-MobX adapter (a no-painer)

Documentation: https://rasdaniil.gitbooks.io/mobase/content/

mobase helps you to create MobX-powered Firebase-syncronised reactive MobaseStores in a simple and intuitive manner.

It's based on a unidirectional data flow pattern. So the the only way to alternate data in the store is to write it to firebase. Because of the nature of Firebase changes will take effect immediately (don't have to wait until it syncs with the server)

MobaseStore has a MobX map as a collection of elements (items) at the core. It supports most of MobX maps' methods like values(), entries(), size, etc..

It's important that every item had it's own id (any unique field can be an id, see options.idField)

GETTING STARTED

npm install --save mobase

Let's say we expect to have our data in firebase structured as following:

/todos
      -KdqjbX-oyvjM7ftb43K
          id: "-KdqjbX-oyvjM7ftb43K"
          isDone: false
          subtasks
              0: "Subtask #1"
              1: "Subtask #2"
          text: "This is my first todo"
      -KdvJzu-bynbpyMa1fGl
      -KdvRsT9-FeN4vVEGTj0
      // and so on

There are a few expressive ways to define our store:

import {MobaseStore} from 'mobase'

// don't forget to firebase.initializeApp(credentials)

const todoStore = new MobaseStore({
     path: '/todos',
     database: firebase.database(),
     fields: {
         id: observable.ref,
         isDone: {
             modifier: observable.ref,
             default: ''
         },
         subtasks: {
             modifier: observable.shallow,
             default: []
         },
         text: observable.ref
     }
})

Now let's use the store created in a reactive manner with React.

import {observer} from 'mobx-react'

const App = observer( ({todoStore}) => {

    return (
        <main>
            <div>Total: {todoStore.size}</div>

            <ul>
                {todoStore.values().map( item => (
                    <li key={item.id}>
                        {item.text}

                        {item.subtasks.map( (subtask, i) => (
                            <div key={i}>{subtask}</div>
                        )}
                    </li>
                ))}
            </ul>
        </main>
    )

} )

ReactDOM.render(<App todoStore={todoStore} />, document.getElementById('app'));

To add data:

const addTodo = text => {
    todoStore
         .write({ text })
         .then( id => console.log('New item id: %s', id) )
}

To modify existing item:

const completeTodo = todo => {
    todoStore
        .update( {id: todo.id, isDone: true} ) // don't forget to specify id of the object being updated
        .then( id => console.log('Updated successfully, id: %s', id) )
}


const addSubtask = (todo, subtask) => {
    const subtasks = todo.subtasks.slice().concat( subtask ) //new subtasks array

    todoStore
        .update( {id: todo.id, subtasks } )
        .then( id => console.log('Updated successfully, id: %s', id) )
}


const todo = todoStore.values()[0] //getting the first todo from the store
completeTodo(todo)
addSubtask(todo, "Yet another subtask)

To remove an item:


const removeTodo = todo => {
    todoStore
         .delete(todo.id)
         .then( id => console.log('Deleted item %s', id) )
}