Skip to content

Commit

Permalink
[RT] v0.2.0 (#15)
Browse files Browse the repository at this point in the history
* refactored response files into separate files

* adds the hability to define custom routes through the faux object (#14)

* added api.prefix to the config and added prefix to routes (#16)

* added relationship routes

* added relationship routes (#17)

* removed unecessary Faux.js file

* removed test file

* removed test file

* fixed unparsed id on hasMany delete route
  • Loading branch information
olavoasantos committed Aug 31, 2018
1 parent 38625e2 commit af0ce18
Show file tree
Hide file tree
Showing 32 changed files with 493 additions and 310 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ Network Trash Folder
Temporary Items
.apdisk

node_modules
node_modules
/test.js
8 changes: 0 additions & 8 deletions Faux.js

This file was deleted.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ const UserModel = {
// Generate attribute routes (e.g. /users/1/email)
// Bool
attributeRoutes: true,
// Generate relationship routes (e.g. /users/1/profile)
// Bool
relationshipRoutes: true,
// Protect attributes (dont send it nor create attribute routes)
// Array [(Column)<Strings>]
protected: ['password'],
Expand Down
2 changes: 2 additions & 0 deletions config/base.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
'api.prefix': '/',

'auth.namespace': '/',
'token.header': 'Authorization',

Expand Down
2 changes: 1 addition & 1 deletion database/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Table {
const entry = {
...oldData,
...data,
id,
id: parseInt(id),
updated_at: (new Date).getTime()
};

Expand Down
90 changes: 87 additions & 3 deletions generators/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ const Database = require('../database');
const App = require('../server');
const Auth = require('../auth');
const Config = require('../config');
const { Routes, CreateRoute, CreateAttributeRoute } = require('../routes');
const {
Routes,
CreateRoute,
CreateAttributeRoute,
CreateRelationshipRoute
} = require('../routes');

/**
* Generator
Expand Down Expand Up @@ -51,8 +56,87 @@ const Generator = Model => {
if (Model.attributeRoutes) {
Model.columns.forEach(column => {
if (!Model.protected.includes(column)) {
CreateAttributeRoute('show', 'get', `${Model.route}/:id/${column}`, Model);
CreateAttributeRoute('update', 'patch', `${Model.route}/:id/${column}`, Model);
CreateAttributeRoute(
'show',
'get',
`${Model.route}/:id/${column}`,
Model
);
CreateAttributeRoute(
'update',
'patch',
`${Model.route}/:id/${column}`,
Model
);
}
});
}

if (Model.relationshipRoutes) {
Object.keys(Model.hasOne).forEach(relationship => {
if (!Model.protected.includes(relationship)) {
const Relationship = Config.get('models')[relationship];
CreateRelationshipRoute(
'hasOneIndexResponse',
'get',
`${Model.route}/:id/${relationship.toLowerCase()}`,
Model,
Relationship
);
CreateRelationshipRoute(
'hasOneStoreResponse',
'post',
`${Model.route}/:id/${relationship.toLowerCase()}`,
Model,
Relationship
);
CreateRelationshipRoute(
'hasOneUpdateResponse',
'patch',
`${Model.route}/:id/${relationship.toLowerCase()}/:relationshipId`,
Model,
Relationship
);
CreateRelationshipRoute(
'hasOneDeleteResponse',
'delete',
`${Model.route}/:id/${relationship.toLowerCase()}/:relationshipId`,
Model,
Relationship
);
}
});
Object.keys(Model.hasMany).forEach(relationship => {
if (!Model.protected.includes(relationship)) {
const Relationship = Config.get('models')[relationship];
CreateRelationshipRoute(
'hasManyIndexResponse',
'get',
`${Model.route}/:id/${relationship.toLowerCase()}`,
Model,
Relationship
);
CreateRelationshipRoute(
'hasManyStoreResponse',
'post',
`${Model.route}/:id/${relationship.toLowerCase()}`,
Model,
Relationship
);
CreateRelationshipRoute(
'hasManyUpdateResponse',
'patch',
`${Model.route}/:id/${relationship.toLowerCase()}/:relationshipId`,
Model,
Relationship
);
CreateRelationshipRoute(
'hasManyDeleteResponse',
'delete',
`${Model.route}/:id/${relationship.toLowerCase()}/:relationshipId`,
Model,
Relationship
);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion helpers/getDataFromBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const getDataFromBody = (columns, body, shouldIncludeNull = true) =>
columns.reduce((data, column) => {
if (shouldIncludeNull || body[column]) {
data[column] = isNaN(parseFloat(body[column])) ? body[column] : parseInt(body[column]);
data[column] = body[column];
}
return data;
}, {});
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { Config, Start, Register } = require('./Faux');
const { Start, Register, App, Config } = require('./main');

/** Base faux wrapper */
const faux = {
config: Config,
register: Register,
start: Start,
route: App,
}

module.exports = faux;
4 changes: 3 additions & 1 deletion main/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Start = require('./Start');
const Register = require('./Register');
const App = require('../server');
const Config = require('../config');

module.exports = { Start, Register };
module.exports = { Config, App, Start, Register };
49 changes: 39 additions & 10 deletions model/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Model {
this.columns = [];
this.factory = () => ({});
this.attributeRoutes = false;
this.relationshipRoutes = false;
this.protected = [];
this.middlewares = [];
this.encrypt = [];
Expand All @@ -28,15 +29,30 @@ class Model {

hydrate(ModelSchema) {
this.name = Validate.name(ModelSchema.name);
this.route = Validate.route(ModelSchema.route);
this.route = this.generateRoute(Validate.route(ModelSchema.route));
this.columns = Validate.columns(ModelSchema.columns);
Object.keys(ModelSchema).forEach(property => {
if (check.isSet(Validate[property])) {
if (
!['name', 'route', 'columns'].includes(property) &&
check.isSet(Validate[property])
) {
this[property] = Validate[property](ModelSchema[property]);
}
});
}

generateRoute(route) {
const prefix = Config.get('api.prefix')
.replace(/ +/g, '-')
.replace(/^\/|\/$/g, '')
.toLowerCase();
const uri = route
.replace(/ +/g, '-')
.replace(/^\/|\/$/g, '')
.toLowerCase();
return `/${prefix}/${uri}`.replace(/\/\//g, '/');
}

validate(data) {
return Object.keys(this.validation).reduce((errors, column) => {
const rule = this.validation[column];
Expand All @@ -60,16 +76,25 @@ class Model {
const eagerLoad = check.isObject(relationship)
? relationship
: {
model: relationship,
model: relationship
};

let relationshipModel;
if (this.hasOne[relationship.model]) {
relationshipModel = Config.get('models')[relationship.model];
return relationshipModel.database.where(this.hasOne[relationship.model], model_id);
if (this.hasOne[eagerLoad.model]) {
relationshipModel = Config.get('models')[eagerLoad.model];
return relationshipModel.database.where(
this.hasOne[eagerLoad.model],
model_id
);
}
if (this.hasMany[relationship.model]) {
relationshipModel = Config.get('models')[relationship.model];
return relationshipModel.database.whereAll(this.hasMany[relationship.model], model_id) || [];
if (this.hasMany[eagerLoad.model]) {
relationshipModel = Config.get('models')[eagerLoad.model];
return (
relationshipModel.database.whereAll(
this.hasMany[eagerLoad.model],
model_id
) || []
);
}
}

Expand All @@ -78,7 +103,11 @@ class Model {
this.eagerLoad.forEach(relationship => {
const relationshipData = this.loadRelationship(row.id, relationship);
if (relationshipData) {
row[relationship.model.toLowerCase()] = relationshipData;
row[
relationship.model
? relationship.model.toLowerCase()
: relationship.toLowerCase()
] = relationshipData;
}
});
}
Expand Down
8 changes: 8 additions & 0 deletions model/ValidateSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ module.exports = (() => {
return attributeRoutes;
},

relationshipRoutes: relationshipRoutes => {
if (!check.isBool(relationshipRoutes)) {
error(`The ${name} model's relationshipRoutes should be a boolean.`);
}

return relationshipRoutes;
},

factory: modelFactory => {
if (!check.isFunction(modelFactory)) {
error(`The ${name} model's factory should be a function.`);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "faux-call",
"version": "0.1.4",
"version": "0.2.0",
"description": "Simple mock server for your convenience and testing",
"main": "index.js",
"keywords": [
Expand Down
Loading

0 comments on commit af0ce18

Please sign in to comment.