The Users resource Trainjs scaffolding is generated by passing the scaffold
command to the trainjs generate
script. The argument of the scaffold
command is the singular version of the resource name (in this case, User), together with optional parameters for the data model’s attributes:
~/toy_app $ trainjs generate scaffold User name:string email:string
create .sequelizerc
create app/controllers/users_controller.js
create app/models
create app/models/user.js
create config/database.json
create db
create db/migrate
create db/migrate/20151229204512_create_users.js
create public/assets/stylesheets/form-for.css
create public/assets/stylesheets/scaffolds.css
create public/controllers
create public/controllers/users_controller.js
create public/directives/alert.js
create public/helpers
create public/helpers/alert.js
create public/partials/controller_name
create public/partials/users/form.html
create public/partials/users/index.html
create public/partials/users/show.html
create public/services
create public/services/user.js
trainjs scaffolding use sqlite3 by default
package.json
{
"name" : "toy_app" ,
"version" : "1.0.0" ,
"dependencies" : {
"angular-resource" : "1.5.0" ,
"angular-form-for" : "4.1.10" ,
"sqlite3" : "*" ,
"sequelize" : "*" ,
"body-parser" : "*" ,
"connect" : "*" ,
"angular" : "1.5.0" ,
"angular-ui-router" : "0.2.18"
}
}
If you want to use another database you can edit package.json
file and config/database.json
file
{
"development" : {
"dialect" : "sqlite" ,
"storage" : "db/development.sqlite3"
},
"test" : {
"dialect" : "sqlite" ,
"storage" : "db/test.sqlite3"
},
"production" : {
"dialect" : "sqlite" ,
"storage" : "db/production.sqlite3"
}
}
Install all modules listed as dependencies in package.json
~/toy_app $ npm install
To update users
data in the database using sequelize-cli
sudo npm install -g sequelize-cli
Run pending migrations.
~/toy_app $ sequelize db:migrate
Loaded configuration file "config/database.json" .
Using environment "development" .
Using gulpfile /usr/lib/node_modules/sequelize-cli/lib/gulpfile.js
Starting 'db:migrate' ...
== 20151229204512_create_users: migrating =======
== 20151229204512_create_users: migrated ( 0.297s)
The trainjs routes, with a rule for the Users resource.
config/routes.js
module . exports = [
{ resources : 'users' },
];
The Users controller in schematic form.
app/controllers/users_controller.js
function UsersController () {
this . index = function ( req , res , next ) {
var users = ModelSync ( User . findAll () );
res . end ( JSON . stringify ( users ));
};
this . create = function ( req , res , next ) {
var user = ModelSync ( User . create ( req . body ) );
res . end ( JSON . stringify ( user ));
};
this . show = function ( req , res , next ) {
var user = ModelSync ( User . findById ( req . params . id ) );
res . end ( JSON . stringify ( user ));
};
this . update = function ( req , res , next ) {
var user = ModelSync ( User . findById ( req . params . id ) );
user . update ( req . body ). then ( function ( _user ) {
res . end ( JSON . stringify ( _user ));
})
};
this . destroy = function ( req , res , next ) {
var user = ModelSync ( User . findById ( req . params . id ) );
user . destroy ();
res . end ();
};
}
module . exports = UsersController ;
We’ll learn more about HTTP request methods.
The User model for the demo application.
app/models/user.js
var Sequelize = require ( 'sequelize' );
var sequelize = CONFIG . database ;
var User = sequelize . define ( 'user' , {
name : {
type : Sequelize . STRING ,
},
email : {
type : Sequelize . STRING ,
},
}, {
freezeTableName : true // Model tableName will be the same as the model name
});
module . exports = User ;
The view for the user index.
public/partials/users/index.html
<p notice id= "notice" ></p>
<h1> Listing Users</h1>
<table>
<thead>
<tr>
<th> Name</th>
<th> Email</th>
<th colspan= "3" ></th>
</tr>
</thead>
<tbody>
<tr ng-repeat= "user in users" >
<td> {{ user.name }}</td>
<td> {{ user.email }}</td>
<td><a href ui-sref= "user_detail({id: user.id})" > Show</a></td>
<td><a href ui-sref= "user_form({id: user.id})" > Edit</a></td>
<td><a href ng-click= "deleteUser(user.id)" > Delete</a></td>
</tr>
</tbody>
</table>
<br>
<a href ui-sref= "user_form({id: 'new'})" > New User</a>
Next, we can the local web server using trainjs server
(or trainjs s
).
~/toy_app $ trainjs server
=> Server running at http://0.0.0.0:1337
=> Ctrl-C to shutdown server
We go to http://0.0.0.0:1337/#/users for listing all users