1 | # fakeStoreJs :construction: [![Build Status](https://travis-ci.org/FabienGreard/fakeStoreJs.svg?branch=master)](https://travis-ci.org/FabienGreard/fakeStoreJs)[![install size](https://packagephobia.now.sh/badge?p=fakestorejs)](https://packagephobia.now.sh/result?p=fakestorejs)[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/FabienGreard/fakeStoreJs.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/FabienGreard/fakeStoreJs/context:javascript)[![devDependencies Status](https://david-dm.org/FabienGreard/fakeStoreJs/dev-status.svg)](https://david-dm.org/FabienGreard/fakeStoreJs?type=dev)
|
2 |
|
3 | fakeStoreJs make mocking easy, quickly create a CRUD access to any object
|
4 |
|
5 | - Create multiple store in less than a heartbeat ! :hearts:
|
6 | - Come with a unique id attribution ! :boom:
|
7 | - Extends CRUD method using resolvers ! :unlock:
|
8 | - Persistent data ! :new:
|
9 | - Easy to use ! 🔥
|
10 |
|
11 | If something doesn’t work, please [file an issue](https://github.com/FabienGreard/fakeStoreJs/issues/new) :bug:.
|
12 |
|
13 | ## QuickStart :rocket:
|
14 |
|
15 | ### Install
|
16 |
|
17 | ```sh
|
18 | $ npm install fakestorejs or yarn install fakestorejs
|
19 | ```
|
20 |
|
21 | ### Use it Now !
|
22 |
|
23 | Start importing `createStore` from fakeStoreJs.
|
24 |
|
25 | ```javascript
|
26 | const createStore = require('fakestorejs');
|
27 | ```
|
28 |
|
29 | Create a store from any object.
|
30 |
|
31 | ```javascript
|
32 | const store = createStore({
|
33 | book: {
|
34 | data: [
|
35 | { title: 'Speaking JavaScript', author: 'Dr. Axel Raushmayer' },
|
36 | { title: 'Effective JavaScript', author: 'David Herman' },
|
37 | { title: 'Eloquent Javascript', author: 'Marijin Haverbeke' },
|
38 | { title: 'You-Dont-Know-JS', author: 'Kyle Simpson' }
|
39 | ]
|
40 | }
|
41 | });
|
42 | ```
|
43 |
|
44 | :tada: Start using it :tada:
|
45 |
|
46 | ```javascript
|
47 | store.book.get();
|
48 | // { sucess: true, data: [ { uid: "000000" author: "Speaking JavaScript", title: "Dr. Axel Raushmayer" }, ...] }
|
49 | ```
|
50 |
|
51 | ## Usage
|
52 |
|
53 | ### Requirements
|
54 |
|
55 | fakeStoreJs need an object with at least on key.
|
56 | 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.
|
57 |
|
58 | ```javascript
|
59 | const store = createStore({
|
60 | dragon: {
|
61 | data: [], // can be empty or fill with any object
|
62 | schema: function Dragon() {}, // deprecated use of anonymous function
|
63 | options: { useSchema: true } // Must be specified or it will create a schema from the data given see next example (schemaless)
|
64 | }
|
65 | });
|
66 | ```
|
67 |
|
68 | Or
|
69 |
|
70 | ```javascript
|
71 | const store = createStore({
|
72 | dragon: {
|
73 | data: [{ name: 'Frizzly', type: 'Ice' }] // Must have at least one object inside the data field
|
74 | }
|
75 | });
|
76 | ```
|
77 |
|
78 | Let's now have a deeper look at what are schema.
|
79 |
|
80 | ### Schema
|
81 |
|
82 | A schema is the 'constructor' used by fakestorejs to create new object.
|
83 |
|
84 | 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 :
|
85 |
|
86 | ```javascript
|
87 | const store = createStore({
|
88 | user: {
|
89 | data: [],
|
90 | schema: function User({ firstname, lastname }) {
|
91 | this.firstname = firstname;
|
92 | this.lastname = lastname;
|
93 | this.username = `${firstname[0]}.${${lastname}}` // Usualy schema are used to create 'calculated' properties otherwise use fakeStoreJs schemaless strategy
|
94 | },
|
95 | options: {
|
96 | useSchema: true
|
97 | }
|
98 | }
|
99 | });
|
100 | ```
|
101 |
|
102 | ### Schemaless strategy
|
103 |
|
104 | Most of the time when mocking data you don't need complexity properties like in the schema model, this is the schemaless fakeStoreJs strategy.
|
105 |
|
106 | ```javascript
|
107 | const store = createStore({
|
108 | user: {
|
109 | data: [{ firstname: 'David', lastname: 'Herman' }] // fakeStoreJs will automatically create a schema that take every key from your first object inside your data array
|
110 | }
|
111 | });
|
112 | ```
|
113 |
|
114 | ### Methods
|
115 |
|
116 | fakeStoreJs comes with embedded crud like method :
|
117 | However you can override them and or create new one using [resolvers](https://github.com/FabienGreard/fakeStoreJs#Resolvers) !
|
118 |
|
119 | | Method | Parameters | sucess | error |
|
120 | | -------- | ------------------------ | --------------------------------- | ---------------------------------- |
|
121 | | post() | obj: Object | { sucess: Boolean, data: Object } | { sucess: Boolean, error: String } |
|
122 | | get() | None | { sucess: Boolean, data: Object } | { sucess: Boolean, error: String } |
|
123 | | put() | uid: String, obj: Object | { sucess: Boolean, data: Object } | { sucess: Boolean, error: String } |
|
124 | | delete() | uid: String | { sucess: Boolean} | { sucess: Boolean, error: String } |
|
125 |
|
126 | FakeStoreJs will add a unique identifier(uid) for each item.
|
127 |
|
128 | #### Resolvers
|
129 |
|
130 | Resolvers allow custom methods by adding a key inside your object call `resolvers` :
|
131 |
|
132 | ```javascript
|
133 | const store = createStore({
|
134 | book: {
|
135 | data: [
|
136 | { title: 'Speaking JavaScript', author: 'Dr. Axel Raushmayer' },
|
137 | { title: 'Effective JavaScript', author: 'David Herman' },
|
138 | { title: 'Eloquent Javascript', author: 'Marijin Haverbeke' },
|
139 | { title: 'You-Dont-Know-JS', author: 'Kyle Simpson' }
|
140 | ],
|
141 | resolvers: {
|
142 | // Add your own methods !!
|
143 | getById: function(uid) {
|
144 | // do not use arrow function
|
145 | const item = this.collection.find(item => item.uid === uid);
|
146 | return item
|
147 | ? { sucess: true, data: item }
|
148 | : { sucess: false, error: 'couldnt match the uid' };
|
149 | },
|
150 | multiplePost: function(arrayOfObj) {
|
151 | let error = false;
|
152 | const collectionPreviousState = this.collection;
|
153 |
|
154 | for (let [i, obj] of arrayOfObj.entries()) {
|
155 | try {
|
156 | obj = this.Book(obj); // use of the schema context, 'collection': book with 'schema': Book
|
157 | this.collection = [...this.collection, obj];
|
158 | } catch (e) {
|
159 | error = { sucess: false, error: e };
|
160 | break;
|
161 | }
|
162 | arrayOfObj[i] = obj;
|
163 | }
|
164 |
|
165 | if (error) {
|
166 | this.collection = collectionPreviousState;
|
167 | return error;
|
168 | } else return { sucess: true, data: arrayOfObj };
|
169 | }
|
170 | }
|
171 | }
|
172 | });
|
173 | ```
|
174 |
|
175 | fakeStoreJs bind the resolvers with a neat context : `{ collection: Array, schema: Function }` where :
|
176 |
|
177 | - `collection` is the table from your store(database).
|
178 | - `schema` is your schema from the `createStore()`.
|
179 |
|
180 | Nb: `schema` will always be your collection name capitalized.
|
181 |
|
182 | example: `book` schema will be `Book`
|
183 |
|
184 | ### Options
|
185 |
|
186 | It is possible to add options to fakeStoreJs using the key : `options` :
|
187 |
|
188 | ```javascript
|
189 | const store = createStore({
|
190 | book: {
|
191 | data: [
|
192 | { title: 'Speaking JavaScript', author: 'Dr. Axel Raushmayer' },
|
193 | { title: 'Effective JavaScript', author: 'David Herman' },
|
194 | { title: 'Eloquent Javascript', author: 'Marijin Haverbeke' },
|
195 | { title: 'You-Dont-Know-JS', author: 'Kyle Simpson' }
|
196 | ],
|
197 | schema: function Book({ author, title }) {
|
198 | this.author = author;
|
199 | this.title = title;
|
200 | },
|
201 | options: {
|
202 | idLabel: 'id',
|
203 | useSchema: true
|
204 | }
|
205 | }
|
206 | });
|
207 | ```
|
208 |
|
209 | | Method | Type | informations | Default |
|
210 | | --------------- | ------- | -------------------------------------------------------------- | ------- |
|
211 | | idLabel | String | Use as 'key name' for the generate identifier | 'uid' |
|
212 | | useSchema | Boolean | Switch beetween embedded schema constructor or your own schema | false |
|
213 | | isPersistent | Boolean | Keep the data even after a restart | false |
|
214 | | isDataDeletable | Boolean | Delete the initial data from the data field | false |
|
215 |
|
216 | ## Contributing
|
217 |
|
218 | This project welcome any new contribution.
|