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

fakestorejs

v0.4.5

Published

FakeStoreJs make mocking easy, quickly create a CRUD acess to any object

Downloads

82

Readme

fakeStoreJs :construction: Build Statusinstall sizeLanguage grade: JavaScriptdevDependencies Status

fakeStoreJs make mocking easy, quickly create a CRUD access to any object

  • Create multiple store in less than a heartbeat ! :hearts:
  • Come with a unique id attribution ! :boom:
  • Extends CRUD method using resolvers ! :unlock:
  • Persistent data ! :new:
  • Easy to use ! 🔥

If something doesn’t work, please file an issue :bug:.

QuickStart :rocket:

Install

$ npm install fakestorejs or yarn install fakestorejs

Use it Now !

Start importing createStore from fakeStoreJs.

const createStore = require('fakestorejs');

Create a store from any object.

const store = createStore({
  book: {
    data: [
      { title: 'Speaking JavaScript', author: 'Dr. Axel Raushmayer' },
      { title: 'Effective JavaScript', author: 'David Herman' },
      { title: 'Eloquent Javascript', author: 'Marijin Haverbeke' },
      { title: 'You-Dont-Know-JS', author: 'Kyle Simpson' }
    ]
  }
});

:tada: Start using it :tada:

store.book.get();
// { sucess: true, data: [ { uid: "000000" author: "Speaking JavaScript", title: "Dr. Axel Raushmayer" }, ...] }

Usage

Requirements

fakeStoreJs need an object with at least on key. Each key represent a collection name (or table name) and they must provide an array of data or a schema, look at the example below.

const store = createStore({
  dragon: {
    data: [], // can be empty or fill with any object
    schema: function Dragon() {}, // deprecated use of anonymous function
    options: { useSchema: true } // Must be specified or it will create a schema from the data given see next example (schemaless)
  }
});

Or

const store = createStore({
  dragon: {
    data: [{ name: 'Frizzly', type: 'Ice' }] // Must have at least one object inside the data field
  }
});

Let's now have a deeper look at what are schema.

Schema

A schema is the 'constructor' used by fakestorejs to create new object.

Example : You want to create a store of users, each user should have a username build from its lastname and firstname, you need to specified it :

const store = createStore({
  user: {
    data: [],
    schema: function User({ firstname, lastname }) {
      this.firstname = firstname;
      this.lastname = lastname;
      this.username = `${firstname[0]}.${${lastname}}` // Usualy schema are used to create 'calculated' properties otherwise use fakeStoreJs schemaless strategy
    },
    options: {
      useSchema: true
    }
  }
});

Schemaless strategy

Most of the time when mocking data you don't need complexity properties like in the schema model, this is the schemaless fakeStoreJs strategy.

const store = createStore({
  user: {
    data: [{ firstname: 'David', lastname: 'Herman' }] // fakeStoreJs will automatically create a schema that take every key from your first object inside your data array
  }
});

Methods

fakeStoreJs comes with embedded crud like method : However you can override them and or create new one using resolvers !

| Method | Parameters | sucess | error | | -------- | ------------------------ | --------------------------------- | ---------------------------------- | | post() | obj: Object | { sucess: Boolean, data: Object } | { sucess: Boolean, error: String } | | get() | None | { sucess: Boolean, data: Object } | { sucess: Boolean, error: String } | | put() | uid: String, obj: Object | { sucess: Boolean, data: Object } | { sucess: Boolean, error: String } | | delete() | uid: String | { sucess: Boolean} | { sucess: Boolean, error: String } |

FakeStoreJs will add a unique identifier(uid) for each item.

Resolvers

Resolvers allow custom methods by adding a key inside your object call resolvers :

const store = createStore({
  book: {
    data: [
      { title: 'Speaking JavaScript', author: 'Dr. Axel Raushmayer' },
      { title: 'Effective JavaScript', author: 'David Herman' },
      { title: 'Eloquent Javascript', author: 'Marijin Haverbeke' },
      { title: 'You-Dont-Know-JS', author: 'Kyle Simpson' }
    ],
    resolvers: {
      // Add your own methods !!
      getById: function(uid) {
        // do not use arrow function
        const item = this.collection.find(item => item.uid === uid);
        return item
          ? { sucess: true, data: item }
          : { sucess: false, error: 'couldnt match the uid' };
      },
      multiplePost: function(arrayOfObj) {
        let error = false;
        const collectionPreviousState = this.collection;

        for (let [i, obj] of arrayOfObj.entries()) {
          try {
            obj = this.Book(obj); // use of the schema context, 'collection': book with 'schema': Book
            this.collection = [...this.collection, obj];
          } catch (e) {
            error = { sucess: false, error: e };
            break;
          }
          arrayOfObj[i] = obj;
        }

        if (error) {
          this.collection = collectionPreviousState;
          return error;
        } else return { sucess: true, data: arrayOfObj };
      }
    }
  }
});

fakeStoreJs bind the resolvers with a neat context : { collection: Array, schema: Function } where :

  • collection is the table from your store(database).
  • schema is your schema from the createStore().

Nb: schema will always be your collection name capitalized.

example: book schema will be Book

Options

It is possible to add options to fakeStoreJs using the key : options :

const store = createStore({
  book: {
    data: [
      { title: 'Speaking JavaScript', author: 'Dr. Axel Raushmayer' },
      { title: 'Effective JavaScript', author: 'David Herman' },
      { title: 'Eloquent Javascript', author: 'Marijin Haverbeke' },
      { title: 'You-Dont-Know-JS', author: 'Kyle Simpson' }
    ],
    schema: function Book({ author, title }) {
      this.author = author;
      this.title = title;
    },
    options: {
      idLabel: 'id',
      useSchema: true
    }
  }
});

| Method | Type | informations | Default | | --------------- | ------- | -------------------------------------------------------------- | ------- | | idLabel | String | Use as 'key name' for the generate identifier | 'uid' | | useSchema | Boolean | Switch beetween embedded schema constructor or your own schema | false | | isPersistent | Boolean | Keep the data even after a restart | false | | isDataDeletable | Boolean | Delete the initial data from the data field | false |

Contributing

This project welcome any new contribution.