Monthly Archives: February 2015

csmsc – Create SailsJS Model and Controller w Node Command Line

Published a node command module to automate the creation of Models and Controllers for a SailsJS api.


This is a node.js command line utility that creates SailsJS base Models and Controllers for API use.


This utility is available on npm. Globally install it by using the -g flag:

npm install -g csmsc


To use it, cd into a project directory, and run csmsc with -n and -f args to represent model name and properties respectively.


Make a model and controller for ‘modelName’ with properties ‘primaryproperty’, ‘anotherproperty’ and ‘aThirdproperty’:

csmsc -n ModelName -f firstprop,secondprop,thirdprop

csmsc -n ModelName -f title,url,subtitle,text,author -c tags -m company


SailsJS expects controllers and models to be created using Pascal Case please appropriately name Models with the first character uppercase to ensure compatibility.

csmsc will write the model into the api/models/ folder. csmsc will write the controller into the api/controllers/ folder.

Existing files will be over written.

Model properties are string by default, modify as needed.

MongoDb will add the following

_id as a unique identifier.
createdAt date created.
updatedAt date last updated.

csmsc will add the following

createdBy user that created the record.
updatedBy user that last updated the record.

createdBy and updatedBy values must be passed to the api or will default to ‘admin’

Additional Notes

GENERATE model and Sails controller for SailsJS api

-n is the name of the model and controller.
-f is a list of comma separated fields
-c is a list of comma separated collections (one to many)
-m is a list of comma separated models (one to one)

the first field in the field list is considered the primary field.

CREATE Sails model and Sails controller EXAMPLE FROM COMMAND LINE

csmsc -n NewModel -f one,two,three 

MODEL – Returns model definition for frontend Validation Error Checking


DISPLAYORDER – Returns list of fields for ordered Dynamic view presentation


GET – Returns ID AND PRIMARY fields for dropdowns / suggestive lists


mongodb’s auto generated id will be used as the key primaryFild will be the value for that key

SEARCH – Returns results after searching primary or specified field for string


Search string follows the slash

Field string is optional and follows ?field=

Using a search string without a field name will search the primary field / node of the object.

Using a search string with a field name specified will search the specified field / node for the string provided.







Implementation is not complete, this is a placeholder.

Publishing a Node Module

I created and published a command line tool to generate my Models and Controllers for a SailsJS API.

The process is the same as writing anything for node but requires the following at the top of the main script.

#!/usr/bin/env node

And the following addition in the package.json file.

"preferGlobal": true,
"bin": {
"commandlineCommand": "./bin/mainScript.js"

Commit the project to github or another repository and publish to using the following commands.

npm set "Your Name"
npm set ""
npm set ""
npm adduser

npm publish ./