Reputation: 101
I'm trying to create something with the new standard so sails, can someone explain me this, would you like to understand more about sails with action2, can they replace the controllers? how do I save, in this case, a simple user?
erros:
Upvotes: 1
Views: 967
Reputation: 21
I'll give you an example, and we can work from there.
// api/controllers/user/create.js
module.exports = {
// Necessary Documentation
friendlyName: 'User creation',
description: 'Creates an user',
// This will validate that every argument needed is received
// from req.body, req.params or req.query (when comparing to a Express controller)
// It also validates presence if `required` is true and correct typing
inputs: {
password: {
description: 'Password',
type: 'string',
required: true
},
password_confirmation: {
description: 'Password confirmation',
type: 'string',
required: true
},
email: {
description: 'The email of the user to create',
type: 'string',
required: true
},
},
// Since an action2 will be parsed to a Node machine, we need to specify an output
// These will work very similar to callbacks with no error parameter
exits: {
// This is the name of a function we will have to use later
created: {
description: 'Created',
// But this, is the name of the response Sails will invoke
// You'll have to provide your own custom response:
// https://sailsjs.com/documentation/concepts/extending-sails/custom-responses
// I made something silly down there.
responseType: 'created'
},
badRequest: {
description: 'Invalid data',
//This one, as well as ´ok´ are always available
responseType: 'badRequest'
},
},
// This is your procedure, you can see this as the code you'd put in your controller
// `this.req` will have your req object, but you cannot access `res`
fn: async function create(inputs, exits){
// We can validate that the passwords match
if (inputs.password !== inputs.password_confirmation){
// In case of failure, we answer with a badRequest response
return exits.badRequest({errors: ['Password mismatch']});
}
//This is an async function, so we can use await and treat Promises as sync
try{
// We create here our user. 'inputs' is an object that contains all parameters
// described before. Namely: password, password_confirmation and email
//I'm just using a placeholder with Sequelize.js syntax.
const user = await User.create(inputs);
// Finally if all went well, we can answer the request with an ok response
return exits.created(user);
}catch(error){
// In case of error, it must be a validation from DB.
// Maybe not, but it's- an example, so let's say it is.
// I highly recommend you to make a better error handler than this.
return exits.badRequest(error);
}
}
};
// api/responses/created.js
module.exports = function created(optionalData = null) {
const statusCodeToSet = 201;
return this.res.status(statusCodeToSet).send(optionalData);
};
// config/routes.js
module.exports= {
"POST /user": "UserController.create",
}
// Finally, you can POST a JSON to /user
{
"email": "[email protected]",
"password": "12345678",
"password_confirmation": "12345678"
}
// Also, if we had in our routes something like this:
module.exports= {
"POST api/v:version/user": "UserController.create",
}
// We could add in our inputs object in the action this:
version: {
type: 'number',
required: true
}
// And we would have our version number in
// `inputs.version` inside our `fn` action
Upvotes: 1