{"id":19040154,"url":"https://github.com/logisticinfotech/sails1-actions2-examples","last_synced_at":"2025-10-12T06:33:49.870Z","repository":{"id":50455404,"uuid":"161905003","full_name":"logisticinfotech/sails1-actions2-examples","owner":"logisticinfotech","description":"Sails JS Actions2 examples with CRUD and helper function","archived":false,"fork":false,"pushed_at":"2019-01-22T18:06:10.000Z","size":238,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-23T21:06:44.001Z","etag":null,"topics":["action","crud","sails","sails-action2","sails-helper","sails-js-actions2","sails1-action2-demo","sails1-action2-example","sailsjs","sailsjs-action2"],"latest_commit_sha":null,"homepage":"https://www.logisticinfotech.com/blog/sails-js-actions2-example-with-crud/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/logisticinfotech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-15T12:56:14.000Z","updated_at":"2022-12-11T13:11:33.000Z","dependencies_parsed_at":"2022-08-31T03:01:15.112Z","dependency_job_id":null,"html_url":"https://github.com/logisticinfotech/sails1-actions2-examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/logisticinfotech/sails1-actions2-examples","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logisticinfotech%2Fsails1-actions2-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logisticinfotech%2Fsails1-actions2-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logisticinfotech%2Fsails1-actions2-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logisticinfotech%2Fsails1-actions2-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/logisticinfotech","download_url":"https://codeload.github.com/logisticinfotech/sails1-actions2-examples/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logisticinfotech%2Fsails1-actions2-examples/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279010516,"owners_count":26084757,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["action","crud","sails","sails-action2","sails-helper","sails-js-actions2","sails1-action2-demo","sails1-action2-example","sailsjs","sailsjs-action2"],"created_at":"2024-11-08T22:20:54.385Z","updated_at":"2025-10-12T06:33:49.835Z","avatar_url":"https://github.com/logisticinfotech.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Introduction\nThis blog is for Sails JS actions2 examples with crud and helper function, this includes how to use GET POST PUT DELETE with actions2. Example of exits and intercept is included.\n\nyou can check [this blog](https://www.logisticinfotech.com/blog/sails-js-actions2-example-with-crud/) for step by step explaination.\n\n# Steps\n### Install sails js globally\n[npm install sails -g](https://sailsjs.com/get-started)\n\n### Create your app\n##### sails new sails-1-actions-2-examples\nand select type of project (i've choose to empty project)\nafter that just fire '`sails lift`' and browse localhost:1337\nyou can use [nodemon](https://nodemon.io/) for automatic restarting of application.\n\n### lets create simple crud example with action2\nwe will use [sails generate](https://sailsjs.com/documentation/reference/command-line-interface/sails-generate) (sails cli) commands to generate actions,models etc.\n\n##### we should add model default attribute\nopen: /config/models.js file and inside attributes object just add this fields\n ```\n attributes: {\n    createdAt: { type: 'number', autoCreatedAt: true, },\n    updatedAt: { type: 'number', autoUpdatedAt: true, },\n    id: { type: 'number', autoIncrement: true, },\n  },\n ```\n now this fields will add automatically in every model\n we will also uncomment this (development use only)\n =\u003e `migrate: 'alter',` (this will alter our database if needed)\n##### Create our first model with\n=\u003e `sails generate model user`\nnow we will add attributes to our model (/api/models/User.js)\n```\nmodule.exports = {\n  attributes: {\n    firstName: {\n      type: 'string',\n      required: true,\n    },\n    lastName: {\n      type: 'string',\n      required: true,\n    },\n   email: {\n      type: 'string',\n      required: true,\n      unique: true,\n      isEmail: true,\n    },\n    password: {\n      type: 'string',\n      required: true,\n      protect: true\n    },\n  }\n}\n```\n##### now we will generate our first action for create user\n=\u003e `sails generate action api/user/create`\nthis will generate file inside user folder: api/controllers/api/user/create.js\n\n##### we also need declare route inside router.js (config/routes.js)\n=\u003e `'POST /api/v1/user/create': { action: 'api/user/create' },`\n\n##### adding validation with action2\ninstead of using regular res and req in sails v1 we will use inputs and exits\ninside inputs object just add following\n(this validation will return error itself if invalid data received).\n```\ninputs: {\n    firstName: {\n      type: 'string',\n      required: true,\n    },\n    lastName: {\n      type: 'string',\n      required: true,\n    },\n    email: {\n      required: true,\n      unique: true,\n      type: 'string',\n      isEmail: true,\n    },\n    password: {\n      required: true,\n      type: 'string',\n      maxLength: 15,\n      minLength: 6,\n    },\n  },\n```\nyou can also set custom exits (for ex. return invalid with status code 409)\n ```\n exits: {\n    invalid: {\n      statusCode: 409,\n      description: 'firstname, lastname, email and password is required.'\n    },\n },\n```\n\n # Create\n we will use waterline's create with our model name\n we can get parameters value with inputs (for ex.inputs.firstName);\n ```\n fn: async function (inputs, exits) {\n    var userRecord = await User.create({\n        firstName: inputs.firstName,\n        lastName: inputs.lastName,\n        email: inputs.email,\n        password: inputs.password,\n      });\n\n    if (!userRecord) {\n      return exits.invalid({\n        message: 'invalid'\n      });\n    }\n    return exits.success({\n      message: 'User has been created successfully.',\n      data: userRecord\n    });\n  }\n};\n```\n\nif userRecord created successfully we will return success message with user data.\n##### tip: you can intercept response\nif email id is not unique our model driver will return error with code 'E_UNIQUE'\nyou can intercept and send your own message like this\n```\n.intercept('E_UNIQUE', (err) =\u003e {\n        return exits.invalid({\n          message: 'invalid',\n          err: err\n        });\n      })\n      .fetch();\n```\n--------------\n#### I have created a [POSTMAN](https://www.getpostman.com/collections/8ca6ae07e1ceaf68b4aa) for all api calls.\n# Here is our detailed [blog](https://www.logisticinfotech.com/blog/sails-js-actions2-example-with-crud/) regarding Update, View, Delete functions\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flogisticinfotech%2Fsails1-actions2-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flogisticinfotech%2Fsails1-actions2-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flogisticinfotech%2Fsails1-actions2-examples/lists"}