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

@besync/graphstore

v1.0.10

Published

mobx persistent stores for graph databases like firebase

Downloads

18

Readme

Graphstore

An observable persistent real-time store based on Mobx

About

GraphStore is an observable persistent real-time store for React based on Mobx. It allows one to

Installation

npm install graphstore --save

Usage: Get from store

import { observer, autorun } from 'mobx';
import { Model, stores, GraphStoreMemory } from '@besync/graphstore';
import { mockData } from '@besync/graphstore-test-mockdata';

Model.setDefaultStore(new GraphStoreMemory(mockData()));

let user = stores.UserStore.getbyId(userId)};
let posts = user.userPosts;

The user object returned is actually an observable object that will stay up to date whenever the state changes (whether within the application or directly on the back end store).

By accessing the userPosts property, GraphStore will automatically "lazy-connect" to the database to get the associated UserPosts for this User. Again the posts object in this case is an observable collection that will stay up to date with any added, deleted, or updates posts.

Usage: Business Logic / UI

GraphStore objects are just MobX observables. So in a React @obseserver render() method or in a console application autorun they just stay up to date. They also contain a useful loading property.

autorun(() => {
  console.log(
    !(user.loading || users.userPosts.loading)
      ? `${user.name} has ${posts.length} posts`
      : 'loading from database'
  )
})

Usage: Render (React)

The following class will automatically subscribe to the database starting with the first render, and will automatically release the subscription when the component is no longer in scope. The render() will be recalled whenever any of the data changes (from the database or from other views).

@observer
class UserView extends Component {

  constructor(props) {
    super(props);
    this.state =  { user: User = stores.UserStore.getbyId(props.userId)};
  }

  render() {
        return (user.loading) ? <div>Loading<div> :
        <div>
        Hello this.state.user.username;
        </div>
    }
}

Usage: Update/Delete actions

GraphStore keeps the backend persistent store up to date with any state changes. Just remember to wrap them in MobX actions if using strict mode.

@action function update() {
  userPosts[1].delete();
  user.name = "GraphStore Expert";
}

Usage: Create actions

GraphStore uses push on a collection to create new documents. Just remember to wrap them in MobX actions so that you can add all the primary keys etc before the back end store is updated.

@action function create() {
  var newPost = userPosts.push();
  newPost.id = "-myNewPost";
}

Example input schema ()

We include some utility code generators (see packages/graphstore-dev) to generate a GraphQl schema from backend databases such as Firebase. These schema are in turn converted into TypeScript code automatically, and used by the library. An example of the input schema is as follows (typically code generated for large existing databases, but can be manually post-edited).

Note that we use graphql for the schema with custom decorators simply as a lingua franca; it does not imply that the server or the client libraries use GraphQL (although they certainly can be!)

type User @connector(byId: "(id) => getDocument('users/${id}')") {
  id: UserId! @primary
  email: String!
  profile_picture: String!
  username: String!
  chats: [Chat] @resolver(get: "ChatsForUser(id)")
  logs: [Log] @resolver(get: "LogsForUser(id)")
  userPosts: [UserPost] @resolver(get: "UserPostsForUser(id)")
}

Back end integrations

Currently the library has connectors for the Firebase realtime database and for a JSON-based in memory store.

License

Apache 2.0