Skip to content

Commit

Permalink
more docs and mirage diff explanations to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Nov 29, 2020
1 parent c77bc38 commit 1d4a33a
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,59 @@ export default MemServer;
This is basically a superior mirage.js API & implementation. Also check the tests...
### Memserver serializer API:
Memserver serializer is very straight-forward, performant and functional/explicit. We have two ways to serialize model
data, it is up to you the developer if you want to serialize it in a custom format(for example JSONAPI) by adding a new
static method(`static customSerializer(modelOrArray) {}`) on the model:
Memserver serializer API:
```js
import Model from 'memserver/model';

class User extends Model {
}

const user = User.find(1);

const serializedUserForEndpoint = User.serializer(user); // or User.serialize(user);

const users = User.findAll({ active: true });

const serializedUsersForEndpoint = User.serializer(users); // or users.map((user) => User.serialize(user));
```
Custom serializers:
```js
import Model from 'memserver/model';

class User extends Model {
static customSerializer(modelObjectOrArray) {
if (Array.isArray(objectOrArray)) {
return modelObjectOrArray.map((object) => this.serialize(object));
}

return this.customSerialize(objectOrArray);
}

static customSerialize(object) {
return Object.assign({}, object, {
newKey: 'something'
});
}
}

const user = User.find(1);

const serializedUserForEndpoint = User.customSerializer(user); // or User.customSerialize(user);

const users = User.findAll({ active: true });

const serializedUsersForEndpoint = User.customSerializer(users); // or users.map((user) => User.customSerialize(user));
```
### Why this is superior to Mirage?
- Data stored as pure JS objects and their Model module methods provides a functional way to work on the data.
Expand Down Expand Up @@ -226,6 +279,10 @@ typechecking/annotated validations on model properties. Due to unresolved nature
relationship settings. Instead users should set the related foreign keys and change that value to relationship
primary key just as it would have been done on a SQL database.
- No implicit/custom serializer abstraction/API. Memserver model serializer is more sane than mirage. It makes it easier
and more functional to create your own serializers since all `MemserverModel.serializer(modelOrArray)` does is, it embeds
defined `embedReferences`s and sets undefined or null values to null in the resulted objectOrArray.
- route shorthands accept the model definition to execute default behavior: `this.post('/users', User)` doesn't need to dasherize,
underscore or do any other string manipulation to get the reference model definition. It also returns correct default
http status code based on the HTTP verb, ex. HTTP POST returns 201 Created just like mirage.
Expand Down

0 comments on commit 1d4a33a

Please sign in to comment.