Sequelize-Json-Serializer
A simple and fast convention based library for serializing sequelize models to JSON.
Basic Usage
conse Sequelize = ; const Serialzer = ; const User = sequelize; const UserSchema = fields: 'username' 'email' 'phone' 'country' ; Serializer;
Somewhere in your application:
router;
Another Basic Example
const UserSchema = fields: '*' // Include all fields; ... const payload = Serializer; /* Response Body... { "id" : "2", "username": "user360", "email": "test@live.com", "phone": "02XXXXXXXX", "country": "Ghana" } */
The default convention ensures the primary key attribute is included by default. To prevent this behavior, set excludePK option in the schema to true .
const UserSchema = fields: '*' // Include all fields options: excludePK: true // ignores all primary keys ; ... const payload = Serializer; /* Response Body... { "username": "user360", "email": "test@live.com", "phone": "02XXXXXXXX", "country": "Ghana" } */
The serialize method works for both arrays and sequelize models
const UserSchema = fields: 'username' 'email' 'phone' 'country' options: excludePK: true // Exclude primary key 'id' ... /* Response Body... { "username": "user360", "email": "test@live.com", "phone": "02XXXXXXXX", "country": "Ghana" } */
Formatting fields
Formatting can be applied to defined fields in schema to alter the output of values. See how
const UserSchema = fields: 'username' 'email' 'phone' 'country' options: { return value ? null : value; }
Renaming Fields
Fields can be renamed by setting the fields to an object following the syntax
[raw_field] : [new_name]
Rename Field Example
const UserSchema = fields: ... 'username': 'user_name' 'phone': 'phone_number' 'email': '' // no renaming done here ; User;
Serializer Options
You can control how models are serialized by passing an options argument to the serializer
Option | Description |
---|---|
serializer | Indicates the type of serializer to use. Possible values include 'model' and 'raw'. Model is only supported at the moment |
emptyPolicy | Defines the policy that is used when a field is not available or found during serialization. Possible values include SET_NULL, SET_UNDEFINED and IGNORE (default). |
includeAll | Controls whether to automatically serialize all include properties. Default is false |
### Set options
const Serializer = ; Serializer
Including Other Defined Schemas
You can mix a schema with others by specifying them in the include section.
Example
const Sequelize = ; const sequelize = /*setup db connection*/ const Serializer = ; const Teacher = sequelize; const TeacherSchema = fields: 'name' ; const School = sequelize; const SchoolSchema = fields: 'name' 'location' 'courses' include: 'teacher': Teacher // Simplified syntax options: // Define formatting for single properties. Only works on fields... propertyFormat: { return value; // always trim school name } { return value; } { return value ? value : } // Define User schema const UserSchema = fields: 'username' 'email' 'phone' 'country' include: // define property 'school' on output model 'school': model: School // the sequelize model. field: 'schools' // the raw field name in users model association: 'many' // [single] or [many] ; // Define school schema Serializer; // Define teacher schema Serializer; // Define user schema Serializer; // Usage -------------------------------------------- // ### Single Entity User
Schema Tags
Schema tags allow defining multiple schema for a single model. It can be useful in scenarios where responses are different for other entities (eg. user account types). Below is a typical example
// #Example const Request = sequelize const User = sequelize;
Defining schema for different user accounts (or roles)
Admin Schema
Here we define the response structure for administrative calls
const AdminRequestSchema = fields: 'type': 'request_type' 'date_created': '' // no renaming.. 'last_updated': '' include: 'sender': model: User field: "user" // association: "single" --- Default is always single // Define schema Serializer; // don't forget to set the last parameter to 'admin'
Regular User Schema
Define response structure of regular user calls
const RegularRequestSchema = fields: 'type': 'request_type' 'name': 'sender_name' // Define schema for regular users Serializer;
Testing schema
We've now defined two schemas for Request model. We can serialize based on user account and prevent eavesdropping on other detailed properties of the sender's user account to a regular user call.
router;