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

marsdb-react

v0.1.4

Published

Declarative data-binding for React based on MarsDB

Downloads

11

Readme

MarsDB-React

Build Status npm version Coverage Status Dependency Status

Declarative data-binding for React based on MarsDB, inspired by Relay, Redux, Flux and Mithril.

  • Declarative: Never again communicate with your data store using an imperative API. Simply declare your data requirements with flexible MarsDB cursors
  • Colocation: Queries live next to the views that rely on them, so you can easily reason about your app.
  • Mongo-like queries/mutations: Work with the data as always, MarsDB takes care of the rest.

Example

Basic concepts

MarsDB-React uses Relay-like concept of data reuirements declaration. Just make a component and create a data container below based on the component.

import React from 'react';
import ReactDOM from 'react-dom';
import { createContainer, DataManagerContainer } from 'marsdb-react';
import Collection from 'marsdb';

const MessageModel = new Collection('messages');

class HelloWorld extends React.Component {
  handleClickAddMessage = () => {
    MessageModel.insert({ text: this.state.text });
    this.setState({ text: '' });
  };

  handleChangeText = (e) => {
    this.setState({ text: e.target.value });
  };

  handleClickMoreLimit = () => {
    const { limit } = this.props.variables;
    limit(10 + limit());
  };

  render() {
    const { messages, variables } = this.props;
    const { limit } = variables;
    return (
      <div>
        <div>
          <h3>Add a message</h3>
          <input
            placeholder="Message text"
            value={this.state.text}
            onChange={this.handleChangeText}
          />
          <button onClick={this.handleClickAddMessage}>Say "hello"</button>
        </div>
        <div>
          <h3>Messages (with limit: {limit()})</h3>
          <div><button onClick={this.handleClickMoreLimit}>Limit +10</button></div>
          {messages().map(m => (
            <p key={m()._id}>"Hello" with message: {m().text}!</p>
          ))}
        </div>
      </div>
    );
  }
}

HelloWorld = createContainer(HelloWorld, {
  initialVariables: {
    limit: 2
  },
  fragments: {
    messages: ({limit}) => MessageModel.find().limit(limit())
  }
});

ReactDOM.render(document.body, (
  <DataManagerContainer
    component={HelloWorld}
    renderLoading(() => <span>Loading...</span>)
  />
))

As you can see, data declaration uses the same fields, that Relay use. But it's plain javascript! There is some things, that should be noticed:

  • Variables in fragment function and in a component, data from props in a component – all is a getter-setter property functions. By calling limit() it returns current value, by calling limit(10) it sets new one and returns 10.
  • Each property have a version variable, that changed when new value is set. It can be used in shouldComponentUpdate
  • As we noticed above, each data property is a getter-setter property function. To access a messages list you need to call messages() that returns current list of messages. Each message of the list is also a proprty function!
  • Each change of the model automatically triggers re-rendering of the component with new data. No subsribers, no listeners, no special "mutation" logic. Just insert/update.
  • Data container can be rendered by <DataManagerContainer> component. It resolves all data requests and show a component only when all data received and ready to show.
  • To use MarsDB-React with React Router use MarsDB-React-Router
  • For more complex example, see TodoMVC
  • If you have any questions, please ask me by issue

TodoMVC

The repository comes with an implementation of TodoMVC. To try it out:

git clone https://github.com/c58/marsdb-react.git
cd marsdb-react/examples/todomvc && npm install
npm start

Then, just point your browser at http://localhost:3000.

Contributing

I’m waiting for your pull requests and issues. Don’t forget to execute gulp lint before requesting. Accepted only requests without errors.

License

See License