Node DDD Boilerplate
RESTful api with Domain Driven Design
nvm
, node v12.8.0
or LTS
version of node installedyarn
- npm install -g yarn
..npmrc
. By default, npm
doesn’t save installed dependencies to package.json (and you should always track your dependencies!).If you encounter installation issues with bcrypt
you need to install node-gyp first.
If you update your node version to latest and you encountered compatibility issues:
pm2
runnign pm2 list
(if there is kill the process)npm rebuild bcrypt --update-binary
Prerequisites
$ docker-compose up -d
Access http://localhost:<PORT>/api/<VERSION>
and you're ready to go!
yarn docker:db:reset
- reset and run all migrations and seeders.yarn docker:db:refresh
- reset and run all migrations.yarn docker:db:refresh-test
- reset and run all migrations for testyarn docker:test
- refreshes test database and run unit and black-box testing....will add more
git clone --depth=1 https://github.com/joshuaalpuerto/node-ddd-boilerplate.git
yarn db:refresh
yarn start
http://localhost:<PORT>/api/<VERSION>
and you're ready to go!
$ npm install -g standard # JavaScript Standard Style
$ npm install -g babel-eslint # required by StandardJs
$ npm install -g snazzy # Format JavaScript Standard Style as beautiful output
$ npm install -g sequelize-cli # CLI for Sequelize
node_ddd
and node_ddd_test
for test enviroment.postgresql.conf
configuration file of your Postgresql installation.Follow the the steps in the next section to enable Posgresql SSL connections.
$ psql
psql (9.6.0)
Type "help" for help.
$ CREATE DATABASE node_ddd;
$ CREATE DATABASE node_ddd_test;
sequelize
and sequelize-cli
as ORM and data migration tool
can change easily to diffrent ORM and migration tool.
camelCase
should never be used. This leaves snake_case
and kebab-case
, I prefer snake_case
for file.yarn start
- start the Node-DDD API Boilerplate for productionyarn start:dev
- start the Node-DDD API Boilerplate locally/developmentyarn start:cc
- start codecrumbs
will give you quick overview the structure of the projectyarn test
- run Unit testsyarn db:reset
- run all migrations and seeds.yarn db:refresh
- run all migrations.yarn lint
- lint codebase using JavaScript Standard Styleyarn lint:fix
- fix code according to JS Standard Styleyarn migrate
- apply db changes using migration scriptyarn add <package-name>
- add a new package to package.jsonyarn remove <package-name>
- remove package from package.jsonnpx sequelize model:create --name newmodel
--attributes "id:integer, title:string - create a new modelSequelize is used to define mappings between models and database tables. It will automatically add the attributes created_at
and updated_at
to the tables created. However for consistency for our naming we change this to createdAt
and updatedAt
. This will cause issue when using model so we have to add this on config:
module.exports = function (sequelize, DataTypes) {
const User = sequelize.define('users', {
...
}, {
timestamps: false, // Add this
})
}
Basic commands
$ sequelize db:migrate Run pending migrations.
$ sequelize db:migrate:old_schema Update legacy migration table
$ sequelize db:migrate:undo Revert the last migration run.
$ sequelize db:migrate:undo:all Revert all migrations ran.
$ sequelize db:seed Run seeders.
$ sequelize db:seed:undo Deletes data from the database.
$ sequelize db:seed:undo:all Deletes data from the database.
$ sequelize model:create --name modelname --attributes "text:text, url:string" # create model
$ sequelize seed:create # create seeder
If you did not install your sequelize-cli globally you can run this commands by
npx
IMPORTANT: as of 6/23/17
the model file created with the sequelize db:model
command still initializes a model with an empty classMethods
object with an associate
property in the options passed to sequelize.define
method. You cannot define associations this way anymore as of Sequelize v4.0.0-1. This tripped me up for a hot second because the generated model file did not reflect this change, and the fact that support for the old way had been removed is seemingly buried in the changelogs. Don’t make the same mistake I did and be super confused for too long about why associations aren’t working.
//old way that will be included in your generated model file
module.exports = function(sequelize, DataTypes) {
var nameofmodel = sequelize.define('nameofmodel', {
...model attributes
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
})
return nameofmodel
};
//The right way to set associations in model files
module.exports = function(sequelize, DataTypes) {
var nameofmodel = sequelize.define('nameofmodel', {
...model attributes
});
nameofmodel.associate = function (models) {
// associations can be defined here
};
return nameofmodel
}
For reference, see: https://github.com/sequelize/cli
Proxy
Adding pre-commit
to your project can be helpful to encourage consistency and quality of your code repository.
git
. It will ensure that your npm test
(or other specified scripts) passes before you can commit your changes. This all conveniently configured in your package.json
.(
, [
, or `
if (condition) { ... }
function name (arg) { ... }
===
instead of ==
– but obj == null
is allowed to check null || undefined
.err
function parameterwindow
– except document
and navigator
are okay
open
, length
,
event
, and name
.This boilerplate is open to suggestions and contributions, documentation contributions are also important! :)
This boilerplate is forked and modified from node-api-boilerplate - Talysson de Oliveira Cassiano :clap:
MIT License - fork, modify and use however you want.