├── .editorconfig ├── .github └── FUNDING.yml ├── .gitignore ├── .sequelizerc ├── .travis.yml ├── .vscode └── launch.json ├── LICENSE.md ├── README.md ├── _config.yml ├── gulpfile.js ├── package.json ├── sequelize.png ├── src ├── dao │ ├── _index.ts │ ├── appusers.ts │ └── languages.ts ├── endpoints │ ├── _index.ts │ ├── appusers │ │ ├── _index.ts │ │ ├── appusers.get.ts │ │ └── appusers.post.ts │ └── languages │ │ ├── _index.ts │ │ ├── languages.get.ts │ │ └── languages.post.ts ├── global.d.ts ├── routes │ ├── _index.ts │ ├── appusers.ts │ └── languages.ts ├── server.ts ├── sqlz │ ├── config │ │ └── config.json │ ├── migrations │ │ ├── 20170305162303-create-language.js │ │ └── 20170305171003-create-app-user.js │ └── models │ │ ├── _index.ts │ │ ├── appuser.ts │ │ └── language.ts └── test.ts ├── test └── index.ts ├── tsconfig.json └── tslint.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | indent_style = space 9 | indent_size = 2 10 | end_of_line = lf 11 | charset = utf-8 12 | trim_trailing_whitespace = true 13 | insert_final_newline = true -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: maximegris # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: maximegris # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | build 3 | coverage 4 | docs 5 | node_modules 6 | package-lock.json 7 | yarn-lock.json 8 | -------------------------------------------------------------------------------- /.sequelizerc: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | 3 | module.exports = { 4 | "config": path.resolve('./src/sqlz/config', 'config.json'), 5 | "models-path": path.resolve('./src/sqlz/models'), 6 | "seeders-path": path.resolve('./src/sqlz/seeders'), 7 | "migrations-path": path.resolve('./src/sqlz/migrations') 8 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | - "10" 5 | install: 6 | - npm install 7 | script: 8 | - npm run build 9 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "Debug Typescript", 6 | "type": "node", 7 | "request": "launch", 8 | "program": "${workspaceRoot}/src/server.ts", 9 | "stopOnEntry": false, 10 | "args": [], 11 | "cwd": "${workspaceRoot}", 12 | "preLaunchTask": "build", 13 | "runtimeExecutable": null, 14 | "runtimeArgs": [ 15 | "--nolazy" 16 | ], 17 | "env": { 18 | "NODE_ENV": "development" 19 | }, 20 | "console": "internalConsole", 21 | "sourceMaps": true, 22 | "outDir": "${workspaceRoot}/build", 23 | "restart": true 24 | } 25 | ] 26 | } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Apache License 2 | ============== 3 | 4 | _Version 2.0, January 2004_ 5 | _<>_ 6 | 7 | ### Terms and Conditions for use, reproduction, and distribution 8 | 9 | #### 1. Definitions 10 | 11 | “License” shall mean the terms and conditions for use, reproduction, and 12 | distribution as defined by Sections 1 through 9 of this document. 13 | 14 | “Licensor” shall mean the copyright owner or entity authorized by the copyright 15 | owner that is granting the License. 16 | 17 | “Legal Entity” shall mean the union of the acting entity and all other entities 18 | that control, are controlled by, or are under common control with that entity. 19 | For the purposes of this definition, “control” means **(i)** the power, direct or 20 | indirect, to cause the direction or management of such entity, whether by 21 | contract or otherwise, or **(ii)** ownership of fifty percent (50%) or more of the 22 | outstanding shares, or **(iii)** beneficial ownership of such entity. 23 | 24 | “You” (or “Your”) shall mean an individual or Legal Entity exercising 25 | permissions granted by this License. 26 | 27 | “Source” form shall mean the preferred form for making modifications, including 28 | but not limited to software source code, documentation source, and configuration 29 | files. 30 | 31 | “Object” form shall mean any form resulting from mechanical transformation or 32 | translation of a Source form, including but not limited to compiled object code, 33 | generated documentation, and conversions to other media types. 34 | 35 | “Work” shall mean the work of authorship, whether in Source or Object form, made 36 | available under the License, as indicated by a copyright notice that is included 37 | in or attached to the work (an example is provided in the Appendix below). 38 | 39 | “Derivative Works” shall mean any work, whether in Source or Object form, that 40 | is based on (or derived from) the Work and for which the editorial revisions, 41 | annotations, elaborations, or other modifications represent, as a whole, an 42 | original work of authorship. For the purposes of this License, Derivative Works 43 | shall not include works that remain separable from, or merely link (or bind by 44 | name) to the interfaces of, the Work and Derivative Works thereof. 45 | 46 | “Contribution” shall mean any work of authorship, including the original version 47 | of the Work and any modifications or additions to that Work or Derivative Works 48 | thereof, that is intentionally submitted to Licensor for inclusion in the Work 49 | by the copyright owner or by an individual or Legal Entity authorized to submit 50 | on behalf of the copyright owner. For the purposes of this definition, 51 | “submitted” means any form of electronic, verbal, or written communication sent 52 | to the Licensor or its representatives, including but not limited to 53 | communication on electronic mailing lists, source code control systems, and 54 | issue tracking systems that are managed by, or on behalf of, the Licensor for 55 | the purpose of discussing and improving the Work, but excluding communication 56 | that is conspicuously marked or otherwise designated in writing by the copyright 57 | owner as “Not a Contribution.” 58 | 59 | “Contributor” shall mean Licensor and any individual or Legal Entity on behalf 60 | of whom a Contribution has been received by Licensor and subsequently 61 | incorporated within the Work. 62 | 63 | #### 2. Grant of Copyright License 64 | 65 | Subject to the terms and conditions of this License, each Contributor hereby 66 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 67 | irrevocable copyright license to reproduce, prepare Derivative Works of, 68 | publicly display, publicly perform, sublicense, and distribute the Work and such 69 | Derivative Works in Source or Object form. 70 | 71 | #### 3. Grant of Patent License 72 | 73 | Subject to the terms and conditions of this License, each Contributor hereby 74 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 75 | irrevocable (except as stated in this section) patent license to make, have 76 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where 77 | such license applies only to those patent claims licensable by such Contributor 78 | that are necessarily infringed by their Contribution(s) alone or by combination 79 | of their Contribution(s) with the Work to which such Contribution(s) was 80 | submitted. If You institute patent litigation against any entity (including a 81 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a 82 | Contribution incorporated within the Work constitutes direct or contributory 83 | patent infringement, then any patent licenses granted to You under this License 84 | for that Work shall terminate as of the date such litigation is filed. 85 | 86 | #### 4. Redistribution 87 | 88 | You may reproduce and distribute copies of the Work or Derivative Works thereof 89 | in any medium, with or without modifications, and in Source or Object form, 90 | provided that You meet the following conditions: 91 | 92 | * **(a)** You must give any other recipients of the Work or Derivative Works a copy of 93 | this License; and 94 | * **(b)** You must cause any modified files to carry prominent notices stating that You 95 | changed the files; and 96 | * **(c)** You must retain, in the Source form of any Derivative Works that You distribute, 97 | all copyright, patent, trademark, and attribution notices from the Source form 98 | of the Work, excluding those notices that do not pertain to any part of the 99 | Derivative Works; and 100 | * **(d)** If the Work includes a “NOTICE” text file as part of its distribution, then any 101 | Derivative Works that You distribute must include a readable copy of the 102 | attribution notices contained within such NOTICE file, excluding those notices 103 | that do not pertain to any part of the Derivative Works, in at least one of the 104 | following places: within a NOTICE text file distributed as part of the 105 | Derivative Works; within the Source form or documentation, if provided along 106 | with the Derivative Works; or, within a display generated by the Derivative 107 | Works, if and wherever such third-party notices normally appear. The contents of 108 | the NOTICE file are for informational purposes only and do not modify the 109 | License. You may add Your own attribution notices within Derivative Works that 110 | You distribute, alongside or as an addendum to the NOTICE text from the Work, 111 | provided that such additional attribution notices cannot be construed as 112 | modifying the License. 113 | 114 | You may add Your own copyright statement to Your modifications and may provide 115 | additional or different license terms and conditions for use, reproduction, or 116 | distribution of Your modifications, or for any such Derivative Works as a whole, 117 | provided Your use, reproduction, and distribution of the Work otherwise complies 118 | with the conditions stated in this License. 119 | 120 | #### 5. Submission of Contributions 121 | 122 | Unless You explicitly state otherwise, any Contribution intentionally submitted 123 | for inclusion in the Work by You to the Licensor shall be under the terms and 124 | conditions of this License, without any additional terms or conditions. 125 | Notwithstanding the above, nothing herein shall supersede or modify the terms of 126 | any separate license agreement you may have executed with Licensor regarding 127 | such Contributions. 128 | 129 | #### 6. Trademarks 130 | 131 | This License does not grant permission to use the trade names, trademarks, 132 | service marks, or product names of the Licensor, except as required for 133 | reasonable and customary use in describing the origin of the Work and 134 | reproducing the content of the NOTICE file. 135 | 136 | #### 7. Disclaimer of Warranty 137 | 138 | Unless required by applicable law or agreed to in writing, Licensor provides the 139 | Work (and each Contributor provides its Contributions) on an “AS IS” BASIS, 140 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 141 | including, without limitation, any warranties or conditions of TITLE, 142 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are 143 | solely responsible for determining the appropriateness of using or 144 | redistributing the Work and assume any risks associated with Your exercise of 145 | permissions under this License. 146 | 147 | #### 8. Limitation of Liability 148 | 149 | In no event and under no legal theory, whether in tort (including negligence), 150 | contract, or otherwise, unless required by applicable law (such as deliberate 151 | and grossly negligent acts) or agreed to in writing, shall any Contributor be 152 | liable to You for damages, including any direct, indirect, special, incidental, 153 | or consequential damages of any character arising as a result of this License or 154 | out of the use or inability to use the Work (including but not limited to 155 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or 156 | any and all other commercial damages or losses), even if such Contributor has 157 | been advised of the possibility of such damages. 158 | 159 | #### 9. Accepting Warranty or Additional Liability 160 | 161 | While redistributing the Work or Derivative Works thereof, You may choose to 162 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or 163 | other liability obligations and/or rights consistent with this License. However, 164 | in accepting such obligations, You may act only on Your own behalf and on Your 165 | sole responsibility, not on behalf of any other Contributor, and only if You 166 | agree to indemnify, defend, and hold each Contributor harmless for any liability 167 | incurred by, or claims asserted against, such Contributor by reason of your 168 | accepting any such warranty or additional liability. 169 | 170 | _END OF TERMS AND CONDITIONS_ 171 | 172 | ### APPENDIX: How to apply the Apache License to your work 173 | 174 | To apply the Apache License to your work, attach the following boilerplate 175 | notice, with the fields enclosed by brackets `[]` replaced with your own 176 | identifying information. (Don't include the brackets!) The text should be 177 | enclosed in the appropriate comment syntax for the file format. We also 178 | recommend that a file or class name and description of purpose be included on 179 | the same “printed page” as the copyright notice for easier identification within 180 | third-party archives. 181 | 182 | Copyright [yyyy] [name of copyright owner] 183 | 184 | Licensed under the Apache License, Version 2.0 (the "License"); 185 | you may not use this file except in compliance with the License. 186 | You may obtain a copy of the License at 187 | 188 | http://www.apache.org/licenses/LICENSE-2.0 189 | 190 | Unless required by applicable law or agreed to in writing, software 191 | distributed under the License is distributed on an "AS IS" BASIS, 192 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 193 | See the License for the specific language governing permissions and 194 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Logo](./sequelize.png)](http://docs.sequelizejs.com) 2 | 3 | [![Build Status](https://travis-ci.org/maximegris/typescript-express-sequelize.svg?branch=master)](https://travis-ci.org/maximegris/typescript-express-sequelize) 4 | [![License](https://img.shields.io/badge/license-Apache2-blue.svg?style=flat)](https://github.com/maximegris/typescript-express-sequelize/blob/master/LICENSE.md) 5 | 6 | # Introduction 7 | 8 | Easily bootstrap your Typescript project with NodeJS + Express + Sequelize ORM. :heart: 9 | 10 | ## Installation 11 | 12 | Run one of the command below 13 | 14 | ```bash 15 | npm install 16 | ``` 17 | 18 | ```bash 19 | npm install -g yarn 20 | yarn 21 | ``` 22 | 23 | The build tasks use **Gulp tasks runner**. Typescript is transpiled to Javascript in the /build directory. 24 | This sample use PostgreSQL database but you can easily change it and use your favorite relational database (npm or yarn command) : 25 | ```bash 26 | npm install --save mysql // For both mysql and mariadb dialects 27 | npm install --save sqlite3 28 | npm install --save tedious // MSSQL 29 | ``` 30 | 31 | ## Configure your database 32 | 33 | Sequelize configuration and entities can be found in /Src/sqlz directory. 34 | 35 | | Directory | Description | 36 | |---|---| 37 | | config | Your database configuration. | 38 | | migrations | Your database migrations scripts. Keep this files in Javascript and run sequelize db:migrate to migrate your database schema. | 39 | | models | Sequelize entities. | 40 | 41 | First, define your database schema in config/config.json file. 42 | Use [Sequelize CLI](http://docs.sequelizejs.com/en/v3/docs/migrations/) to initialize your database. 43 | 44 | In models/ directory, the index.ts file define the DbConnection interface. When you create a new Sequelize entity, add its reference in this interface to fully use Typescript's superpower ! 45 | 46 | ## Run the project 47 | 48 | ```bash 49 | npm start 50 | ``` 51 | 52 | Your web server is now exposed on http://localhost:3000 53 | 54 | ### GET /api/languages 55 | curl -X GET -H 'Content-Type: application/json' http://localhost:3000/api/languages 56 | 57 | ### POST /api/languages 58 | curl -X POST -H 'Content-Type: application/json' -d '{"label":"French","name":"fr"}' http://localhost:3000/api/languages 59 | 60 | ### GET /api/appusers 61 | curl -X GET -H 'Content-Type: application/json' http://localhost:3000/api/appusers 62 | 63 | ### POST /api/appusers 64 | curl -X POST -H 'Content-Type: application/json' -d '{"email":"foo@bar.com","pwd":"something"}' http://localhost:3000/api/appusers 65 | 66 | ## Build 67 | 68 | ```bash 69 | npm run build 70 | ``` 71 | 72 | ## Lint your code before you commit! 73 | 74 | In a collaborative project, it's always a pain when you have to work on files not correctly formatted. 75 | Now before each commit, yout typescript files are linted based on your tsconfig.json > .editorconfig > tslint.json files. 76 | 77 | ```bash 78 | λ git commit -m "Example precommit" 79 | 80 | > husky - npm run -s precommit 81 | 82 | 25l[14:22:30] Running tasks for src/**/*.ts [started] 83 | [14:22:30] prettify [started] 84 | [14:22:31] prettify [completed] 85 | [14:22:31] git add [started] 86 | [14:22:31] git add [completed] 87 | [14:22:31] Running tasks for src/**/*.ts [completed] 88 | 25h25h[master 23c4321] Example precommit 89 | 1 file changed, 1 insertion(+), 1 deletion(-) 90 | ``` 91 | 92 | By the way you can also run the command with a npm script 93 | 94 | ```bash 95 | npm run prettify 96 | ``` 97 | 98 | ## Debug with Typescript and VSCode 99 | 100 | Add breakpoints to your Typescript source code and launch in VSCode the "Debug Typescript" task. 101 | When you'll access to an endpoint, you will be able to debug directly in your Typescript's files. 102 | 103 | ## Questions and issues 104 | 105 | The [github issue tracker](https://github.com/maximegris/typescript-express-sequelize/issues) is **_only_** for bug reports and feature requests. 106 | 107 | ## Roadmap 108 | - [x] Add Sequelize Typescript example with association 109 | - [x] Manage multiple database configuration with NODE_ENV 110 | - [ ] Add Swagger API Framework 111 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-architect -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | let gulp = require("gulp"); 3 | let sourcemaps = require("gulp-sourcemaps"); 4 | let typescript = require("gulp-typescript"); 5 | let nodemon = require("gulp-nodemon"); 6 | let tslint = require("gulp-tslint"); 7 | let rimraf = require("rimraf"); 8 | let typedoc = require("gulp-typedoc"); 9 | let mocha = require("gulp-mocha"); 10 | let istanbul = require("gulp-istanbul"); 11 | let plumber = require("gulp-plumber"); 12 | let remapIstanbul = require("remap-istanbul/lib/gulpRemapIstanbul"); 13 | 14 | const CLEAN_BUILD = "clean:build"; 15 | const CLEAN_COVERAGE = "clean:coverage"; 16 | const CLEAN_DOC = "clean:doc"; 17 | const TSLINT = "tslint"; 18 | const COMPILE_TYPESCRIPT = "compile:typescript"; 19 | const COPY_STATIC_FILES = "copy:static"; 20 | const BUILD = "build"; 21 | const GENERATE_DOC = "generate:doc"; 22 | const PRETEST = "pretest"; 23 | const RUN_TESTS = "run:tests"; 24 | const TEST = "test"; 25 | const REMAP_COVERAGE = "remap:coverage"; 26 | 27 | const TS_SRC_GLOB = "./src/**/*.ts"; 28 | const TS_TEST_GLOB = "./test/**/*.ts"; 29 | const JS_TEST_GLOB = "./build/**/*.js"; 30 | const JS_SRC_GLOB = "./build/**/*.js"; 31 | const TS_GLOB = [TS_SRC_GLOB]; 32 | const STATIC_FILES = ['./src/**/*.json'] 33 | 34 | const tsProject = typescript.createProject("tsconfig.json"); 35 | 36 | // Removes the ./build directory with all its content. 37 | gulp.task(CLEAN_BUILD, function (callback) { 38 | rimraf("./build", callback); 39 | }); 40 | 41 | // Removes the ./coverage directory with all its content. 42 | gulp.task(CLEAN_COVERAGE, function (callback) { 43 | rimraf("./coverage", callback); 44 | }); 45 | 46 | // Removes the ./docs directory with all its content. 47 | gulp.task(CLEAN_DOC, function (callback) { 48 | rimraf("./docs", callback); 49 | }); 50 | 51 | // Checks all *.ts-files if they are conform to the rules specified in tslint.json. 52 | gulp.task(TSLINT, function () { 53 | return gulp.src(TS_GLOB) 54 | .pipe(tslint({ formatter: "verbose" })) 55 | .pipe(tslint.report({ 56 | // set this to true, if you want the build process to fail on tslint errors. 57 | emitError: false 58 | })); 59 | }); 60 | 61 | // Compiles all *.ts-files to *.js-files. 62 | gulp.task(COPY_STATIC_FILES, function () { 63 | return gulp.src(STATIC_FILES) 64 | .pipe(gulp.dest("build")); 65 | }); 66 | 67 | gulp.task(COMPILE_TYPESCRIPT, function () { 68 | return gulp.src(TS_GLOB) 69 | .pipe(sourcemaps.init()) 70 | .pipe(tsProject()) 71 | .pipe(sourcemaps.write(".", { sourceRoot: "../src" })) 72 | .pipe(gulp.dest("build")); 73 | }); 74 | 75 | // Runs all required steps for the build in sequence. 76 | gulp.task(BUILD, gulp.series(CLEAN_BUILD, TSLINT, COMPILE_TYPESCRIPT, COPY_STATIC_FILES)); 77 | 78 | // Generates a documentation based on the code comments in the *.ts files. 79 | gulp.task(GENERATE_DOC, gulp.series(CLEAN_DOC, function () { 80 | return gulp.src(TS_SRC_GLOB) 81 | .pipe(typedoc({ 82 | out: "docs", 83 | target: "es5", 84 | name: "Express + Sequelize", 85 | module: "commonjs", 86 | readme: "readme.md", 87 | includeDeclarations: true, 88 | version: true 89 | })) 90 | })); 91 | 92 | // Sets up the istanbul coverage 93 | gulp.task(PRETEST, function () { 94 | return gulp.src(JS_SRC_GLOB) 95 | .pipe(sourcemaps.init({ loadMaps: true })) 96 | .pipe(istanbul({ includeUntested: true })) 97 | .pipe(istanbul.hookRequire()) 98 | }); 99 | 100 | // Run the tests via mocha and generate a istanbul json report. 101 | gulp.task(RUN_TESTS, function (callback) { 102 | let mochaError; 103 | gulp.src(JS_TEST_GLOB) 104 | .pipe(plumber()) 105 | .pipe(mocha({ reporter: "spec" })) 106 | .on("error", function (err) { 107 | mochaError = err; 108 | }) 109 | .pipe(istanbul.writeReports({ 110 | reporters: ["json"] 111 | })) 112 | .on("end", function () { 113 | callback(mochaError); 114 | }); 115 | }); 116 | 117 | // Remap Coverage to *.ts-files and generate html, text and json summary 118 | gulp.task(REMAP_COVERAGE, function () { 119 | return gulp.src("./coverage/coverage-final.json") 120 | .pipe(remapIstanbul({ 121 | // basePath: ".", 122 | fail: true, 123 | reports: { 124 | "html": "./coverage", 125 | "json": "./coverage", 126 | "text-summary": null, 127 | "lcovonly": "./coverage/lcov.info" 128 | } 129 | })) 130 | .pipe(gulp.dest("coverage")) 131 | .on("end", function () { 132 | console.log("--> For a more detailed report, check the ./coverage directory <--") 133 | }); 134 | }); 135 | 136 | // Runs all required steps for testing in sequence. 137 | gulp.task(TEST, gulp.series(BUILD, CLEAN_COVERAGE, PRETEST, RUN_TESTS, REMAP_COVERAGE)); 138 | 139 | // Runs the build task and starts the server every time changes are detected. 140 | gulp.task("watch", gulp.series(BUILD, function () { 141 | return nodemon({ 142 | ext: "ts json", 143 | script: "build/server.js", 144 | watch: ["src/*", "test/*"], 145 | legacyWatch: true, 146 | tasks: [BUILD] 147 | }); 148 | })); 149 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typescript-express-sequelize", 3 | "version": "2.1.0", 4 | "description": "Sample project with Express + Sequelize + Typescript", 5 | "homepage": "https://github.com/maximegris/typescript-express-sequelize", 6 | "author": { 7 | "name": "Maxime GRIS", 8 | "email": "maxime.gris@gmail.com" 9 | }, 10 | "main": "build/src/server.js", 11 | "keywords": [ 12 | "Node", 13 | "Express", 14 | "Typescript", 15 | "Sequelize" 16 | ], 17 | "scripts": { 18 | "build": "gulp build", 19 | "doc": "gulp generate:doc", 20 | "start": "cross-env NODE_ENV=development gulp watch", 21 | "start:prod": "cross-env NODE_ENV=production gulp watch", 22 | "run:test": "cross-env NODE_ENV=test gulp test", 23 | "tslint": "gulp tslint", 24 | "prettify": "./node_modules/.bin/tsfmt -r --baseDir ./src", 25 | "sqlz:migrate": "./node_modules/.bin/sequelize db:migrate", 26 | "sqlz:undo": "./node_modules/.bin/sequelize db:migrate:undo", 27 | "sqlz:new": "./node_modules/.bin/sequelize migration:create" 28 | }, 29 | "lint-staged": { 30 | "src/**/*.ts": [ 31 | "npm run prettify" 32 | ] 33 | }, 34 | "dependencies": { 35 | "body-parser": "1.19.0", 36 | "cors": "2.8.5", 37 | "cross-env": "7.0.0", 38 | "express": "4.17.1", 39 | "express-boom": "3.0.0", 40 | "morgan": "1.9.1", 41 | "pg": "7.18.2", 42 | "pg-hstore": "2.3.3", 43 | "sequelize": "5.21.5", 44 | "uuid": "3.4.0", 45 | "winston": "3.2.1" 46 | }, 47 | "devDependencies": { 48 | "chai": "4.2.0", 49 | "extendify": "1.0.0", 50 | "glob": "7.1.6", 51 | "gulp": "4.0.2", 52 | "gulp-istanbul": "1.1.3", 53 | "gulp-json-refs": "0.1.1", 54 | "gulp-mocha": "7.0.2", 55 | "gulp-nodemon": "2.4.2", 56 | "gulp-plumber": "1.2.1", 57 | "gulp-sourcemaps": "2.6.5", 58 | "gulp-tslint": "8.1.4", 59 | "gulp-typedoc": "2.2.4", 60 | "gulp-typescript": "5.0.1", 61 | "husky": "4.2.3", 62 | "lint-staged": "10.0.7", 63 | "remap-istanbul": "0.13.0", 64 | "rimraf": "3.0.2", 65 | "sequelize-cli": "5.5.1", 66 | "tslint": "6.0.0", 67 | "typedoc": "0.16.10", 68 | "typescript": "3.8.2", 69 | "typescript-formatter": "7.2.2" 70 | }, 71 | "engines": { 72 | "node": ">=4.0.0" 73 | }, 74 | "license": "SEE LICENSE IN LICENSE.md", 75 | "husky": { 76 | "hooks": { 77 | "pre-commit": "lint-staged" 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /sequelize.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maximegris/typescript-express-sequelize/d341ea1ac4521d67ca09cb4d4b13eb7678add08f/sequelize.png -------------------------------------------------------------------------------- /src/dao/_index.ts: -------------------------------------------------------------------------------- 1 | import * as LanguagesDao from './languages' 2 | import * as AppUserDao from './appusers' 3 | 4 | export { LanguagesDao } 5 | export { AppUserDao } 6 | -------------------------------------------------------------------------------- /src/dao/appusers.ts: -------------------------------------------------------------------------------- 1 | import * as uuid from 'uuid' 2 | import { AppUser } from './../sqlz/models/appuser' 3 | import { Language } from '../sqlz/models/language' 4 | 5 | export function create(appUser: any): Promise { 6 | 7 | return Language.findOne({ 8 | where: { name: 'fr' } 9 | }) 10 | .then(language => { 11 | return AppUser 12 | .create({ 13 | id: uuid.v1(), 14 | email: appUser.email, 15 | pwd: appUser.pwd, 16 | languageId: language.get('id') 17 | }) 18 | }) 19 | } 20 | 21 | export function findAll(): Promise { 22 | return AppUser 23 | .findAll({ include: [{ all: true }] }) 24 | } 25 | 26 | export function login(appUser: any): Promise { 27 | return AppUser 28 | .findOne({ 29 | where: { 30 | email: appUser.email, 31 | pwd: appUser.pwd 32 | }, 33 | include: [Language] 34 | }) 35 | } 36 | -------------------------------------------------------------------------------- /src/dao/languages.ts: -------------------------------------------------------------------------------- 1 | import * as uuid from 'uuid' 2 | import { Language } from './../sqlz/models/language' 3 | 4 | export function create(language: any): Promise { 5 | return Language 6 | .create({ 7 | id: uuid.v1(), 8 | label: language.label, 9 | name: language.name 10 | }) 11 | } 12 | 13 | export function findAll(): Promise { 14 | return Language 15 | .findAll() 16 | } 17 | -------------------------------------------------------------------------------- /src/endpoints/_index.ts: -------------------------------------------------------------------------------- 1 | import * as LanguageController from './languages/_index' 2 | import * as AppUserController from './appusers/_index' 3 | 4 | export { LanguageController, AppUserController } 5 | -------------------------------------------------------------------------------- /src/endpoints/appusers/_index.ts: -------------------------------------------------------------------------------- 1 | import * as AppUserGet from './appusers.get' 2 | import * as AppUserPost from './appusers.post' 3 | 4 | export { AppUserGet, AppUserPost } 5 | -------------------------------------------------------------------------------- /src/endpoints/appusers/appusers.get.ts: -------------------------------------------------------------------------------- 1 | import { Request, Response } from 'express' 2 | import { AppUserDao } from '../../dao/_index' 3 | 4 | export function list(req: Request, res: Response) { 5 | return AppUserDao 6 | .findAll() 7 | .then(appusers => res.status(200).send(appusers)) 8 | .catch(error => res.boom.badRequest(error)) 9 | } 10 | -------------------------------------------------------------------------------- /src/endpoints/appusers/appusers.post.ts: -------------------------------------------------------------------------------- 1 | import { Request, Response } from 'express' 2 | import { AppUserDao } from '../../dao/_index' 3 | 4 | export function create(req: Request, res: Response) { 5 | 6 | req.checkBody('pwd', 'Password is required').notEmpty() 7 | req.checkBody('email', 'Email is required').notEmpty() 8 | req.checkBody('email', 'A valid email is required').isEmail() 9 | 10 | req.getValidationResult() 11 | .then(function(result) { 12 | if (result.isEmpty()) { 13 | return AppUserDao.create(req.body) 14 | .then(appuser => res.status(201).send(appuser)) 15 | .catch(error => res.boom.badRequest(error)) 16 | } else { 17 | res.boom.badRequest('Validation errors', result.mapped()) 18 | } 19 | }) 20 | } 21 | 22 | export function login(req: Request, res: Response) { 23 | 24 | req.checkBody('pwd', 'Password is required').notEmpty() 25 | req.checkBody('email', 'Email is required').notEmpty() 26 | req.checkBody('email', 'A valid email is required').isEmail() 27 | 28 | req.getValidationResult() 29 | .then(function(result) { 30 | if (result.isEmpty()) { 31 | return AppUserDao.login(req.body) 32 | } else { 33 | res.boom.badRequest('Validation errors', result.mapped()) 34 | } 35 | }) 36 | .then(appuser => res.status(200).send(appuser)) 37 | .catch(error => res.boom.badRequest(error)) 38 | } 39 | -------------------------------------------------------------------------------- /src/endpoints/languages/_index.ts: -------------------------------------------------------------------------------- 1 | import * as LanguageGet from './languages.get' 2 | import * as LanguagePost from './languages.post' 3 | 4 | export { LanguageGet, LanguagePost } 5 | -------------------------------------------------------------------------------- /src/endpoints/languages/languages.get.ts: -------------------------------------------------------------------------------- 1 | import { Request, Response } from 'express' 2 | import { LanguagesDao } from '../../dao/_index' 3 | 4 | export function list(req: Request, res: Response) { 5 | return LanguagesDao 6 | .findAll() 7 | .then(languages => res.status(200).send(languages)) 8 | .catch(error => res.boom.badRequest(error)) 9 | } 10 | -------------------------------------------------------------------------------- /src/endpoints/languages/languages.post.ts: -------------------------------------------------------------------------------- 1 | import { Request, Response } from 'express' 2 | import { LanguagesDao } from '../../dao/_index' 3 | 4 | export function create(req: Request, res: Response) { 5 | return LanguagesDao.create(req.body) 6 | .then(language => res.status(201).send(language)) 7 | .catch(error => res.boom.badRequest(error)) 8 | } 9 | -------------------------------------------------------------------------------- /src/global.d.ts: -------------------------------------------------------------------------------- 1 | declare namespace Express { 2 | export interface Response { 3 | boom: any 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /src/routes/_index.ts: -------------------------------------------------------------------------------- 1 | import * as winston from 'winston' 2 | import { Express, Request, Response } from 'express' 3 | import * as LanguagesRoutes from './languages' 4 | import * as AppUserRoutes from './appusers' 5 | 6 | export function initRoutes(app: Express) { 7 | winston.log('info', '--> Initialisations des routes') 8 | 9 | app.get('/api', (req: Request, res: Response) => res.status(200).send({ 10 | message: 'server is running!' 11 | })) 12 | 13 | LanguagesRoutes.routes(app) 14 | AppUserRoutes.routes(app) 15 | 16 | app.all('*', (req: Request, res: Response) => res.boom.notFound()) 17 | } 18 | -------------------------------------------------------------------------------- /src/routes/appusers.ts: -------------------------------------------------------------------------------- 1 | import { Express } from 'express' 2 | import { AppUserController } from '../endpoints/_index' 3 | 4 | export function routes(app: Express) { 5 | 6 | app.get('/api/appUsers', AppUserController.AppUserGet.list) 7 | app.post('/api/appUsers', AppUserController.AppUserPost.create) 8 | app.post('/api/appUsers/login', AppUserController.AppUserPost.login) 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/routes/languages.ts: -------------------------------------------------------------------------------- 1 | import { Express } from 'express' 2 | import { LanguageController } from '../endpoints/_index' 3 | 4 | export function routes(app: Express) { 5 | 6 | app.get('/api/languages', LanguageController.LanguageGet.list) 7 | app.post('/api/languages', LanguageController.LanguagePost.create) 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/server.ts: -------------------------------------------------------------------------------- 1 | import * as express from 'express' 2 | import * as winston from 'winston' 3 | import * as boom from 'express-boom' 4 | import * as morgan from 'morgan' 5 | import * as cors from 'cors' 6 | import { json, urlencoded } from 'body-parser' 7 | import { Express } from 'express' 8 | import * as routes from './routes/_index' 9 | 10 | const PORT: number = 3000 11 | 12 | /** 13 | * Root class of your node server. 14 | * Can be used for basic configurations, for instance starting up the server or registering middleware. 15 | */ 16 | export class Server { 17 | 18 | private app: Express 19 | 20 | constructor() { 21 | this.app = express() 22 | 23 | // Express middleware 24 | this.app.use(cors({ 25 | optionsSuccessStatus: 200 26 | })) 27 | this.app.use(urlencoded({ 28 | extended: true 29 | })) 30 | this.app.use(json()) 31 | this.app.use(boom()) 32 | this.app.use(morgan('combined')) 33 | this.app.listen(PORT, () => { 34 | winston.log('info', '--> Server successfully started at port %d', PORT) 35 | }) 36 | routes.initRoutes(this.app) 37 | } 38 | 39 | getApp() { 40 | return this.app 41 | } 42 | } 43 | new Server() 44 | -------------------------------------------------------------------------------- /src/sqlz/config/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "development": { 3 | "username": "postgres", 4 | "password": "diapa", 5 | "database": "app-dev", 6 | "host": "127.0.0.1", 7 | "port": 5432, 8 | "dialect": "postgres", 9 | "logging": true, 10 | "force": true, 11 | "timezone": "+00:00" 12 | }, 13 | "test": { 14 | "username": "postgres", 15 | "password": "diapa", 16 | "database": "app-test", 17 | "host": "127.0.0.1", 18 | "port": 5432, 19 | "dialect": "postgres", 20 | "logging": true, 21 | "force": true, 22 | "timezone": "+00:00" 23 | }, 24 | "production": { 25 | "username": "postgres", 26 | "password": "postgres", 27 | "database": "app", 28 | "host": "127.0.0.1", 29 | "port": 5432, 30 | "dialect": "postgres", 31 | "logging": true, 32 | "force": true, 33 | "timezone": "+00:00" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/sqlz/migrations/20170305162303-create-language.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | up: (queryInterface, Sequelize) => 3 | queryInterface.createTable('Languages', { 4 | id: { 5 | allowNull: false, 6 | type: Sequelize.UUID, 7 | defaultValue: Sequelize.UUIDV1, 8 | primaryKey: true 9 | }, 10 | label: { 11 | allowNull: false, 12 | type: Sequelize.STRING(255) 13 | }, 14 | name: { 15 | allowNull: false, 16 | type: Sequelize.STRING(50), 17 | unique: true 18 | }, 19 | createdAt: { 20 | allowNull: false, 21 | type: Sequelize.DATE 22 | }, 23 | updatedAt: { 24 | allowNull: false, 25 | type: Sequelize.DATE 26 | } 27 | }) 28 | , 29 | down: (queryInterface, Sequelize) => queryInterface.dropTable('Languages') 30 | } 31 | -------------------------------------------------------------------------------- /src/sqlz/migrations/20170305171003-create-app-user.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | up: (queryInterface, Sequelize) => 3 | queryInterface.createTable('AppUsers', { 4 | id: { 5 | allowNull: false, 6 | type: Sequelize.UUID, 7 | defaultValue: Sequelize.UUIDV1, 8 | primaryKey: true 9 | }, 10 | email: { 11 | allowNull: false, 12 | type: Sequelize.STRING(50), 13 | unique: true 14 | }, 15 | pwd: { 16 | allowNull: false, 17 | type: Sequelize.STRING(255) 18 | }, 19 | createdAt: { 20 | allowNull: false, 21 | type: Sequelize.DATE 22 | }, 23 | updatedAt: { 24 | allowNull: false, 25 | type: Sequelize.DATE 26 | }, 27 | languageId: { 28 | type: Sequelize.UUID, 29 | allowNull: false, 30 | onDelete: 'CASCADE', 31 | references: { 32 | model: 'Languages', 33 | key: 'id', 34 | as: 'languageId', 35 | } 36 | } 37 | }) 38 | , 39 | down: (queryInterface, Sequelize) => queryInterface.dropTable('AppUsers') 40 | } 41 | -------------------------------------------------------------------------------- /src/sqlz/models/_index.ts: -------------------------------------------------------------------------------- 1 | import { Sequelize } from 'sequelize' 2 | 3 | const config = require('../config/config.json') 4 | 5 | const dbConfig = config[process.env.NODE_ENV] 6 | const sequelize = new Sequelize( 7 | dbConfig['database'], 8 | dbConfig['username'], 9 | dbConfig['password'], 10 | dbConfig 11 | ) 12 | 13 | export default sequelize 14 | -------------------------------------------------------------------------------- /src/sqlz/models/appuser.ts: -------------------------------------------------------------------------------- 1 | import { Model, STRING, UUID, Deferrable } from 'sequelize' 2 | import sequelize from './_index' 3 | import { Language } from './language' 4 | 5 | export class AppUser extends Model { 6 | 7 | } 8 | 9 | export class AppUserModel { 10 | id: string 11 | name: string 12 | pwd: string 13 | createdAt: Date 14 | updatedAt: Date 15 | } 16 | 17 | AppUser.init( 18 | { 19 | email: STRING(50), 20 | pwd: STRING(50) 21 | }, 22 | { sequelize, modelName: 'AppUser' } 23 | ) 24 | 25 | AppUser.belongsTo(Language, { 26 | foreignKey: 'languageId' 27 | }) 28 | -------------------------------------------------------------------------------- /src/sqlz/models/language.ts: -------------------------------------------------------------------------------- 1 | import { Model, STRING, UUID } from 'sequelize' 2 | import sequelize from './_index' 3 | import { AppUser } from './appuser' 4 | 5 | export class Language extends Model { 6 | } 7 | 8 | export class LanguageModel { 9 | id: string 10 | label: string 11 | name: string 12 | createdAt: Date 13 | updatedAt: Date 14 | } 15 | 16 | Language.init( 17 | { 18 | label: STRING(255), 19 | name: STRING(50) 20 | }, 21 | { sequelize, modelName: 'Language' } 22 | ) 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This is only for test demonstration purposes, remove this as soon as you implemented your own tests. 2 | export function addTwo(input: number): number { 3 | input += 2 4 | return input 5 | } 6 | -------------------------------------------------------------------------------- /test/index.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai' 2 | import { addTwo } from '../src/test' 3 | 4 | describe('Testing demo function', () => { 5 | it('Should add two to input', () => { 6 | let input: number = 5 7 | let result = addTwo(input) 8 | expect(result).to.eq(input + 2) 9 | }) 10 | }) 11 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "target": "es6", 5 | "module": "commonjs", 6 | "declaration": false, 7 | "noImplicitAny": false, 8 | "removeComments": true, 9 | "noLib": false, 10 | "emitDecoratorMetadata": true, 11 | "experimentalDecorators": true, 12 | "sourceMap": true, 13 | "typeRoots": [ 14 | "node_modules/@types" 15 | ], 16 | "lib": [ 17 | "es2017", 18 | "es2016", 19 | "es2015", 20 | "dom" 21 | ] 22 | }, 23 | "include": [ 24 | "src/**/*" 25 | ], 26 | "exclude": [ 27 | "node_modules" 28 | ] 29 | } 30 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "class-name": true, 4 | "comment-format": [ 5 | true, 6 | "check-space" 7 | ], 8 | "indent": [ 9 | true, 10 | "spaces" 11 | ], 12 | "no-duplicate-variable": true, 13 | "no-eval": true, 14 | "no-internal-module": true, 15 | "no-trailing-whitespace": true, 16 | "no-unsafe-finally": true, 17 | "no-var-keyword": true, 18 | "one-line": [ 19 | true, 20 | "check-open-brace", 21 | "check-whitespace" 22 | ], 23 | "quotemark": [ 24 | true, 25 | "single" 26 | ], 27 | "semicolon": [ 28 | true, 29 | "never" 30 | ], 31 | "triple-equals": [ 32 | true, 33 | "allow-null-check" 34 | ], 35 | "typedef-whitespace": [ 36 | true, 37 | { 38 | "call-signature": "nospace", 39 | "index-signature": "nospace", 40 | "parameter": "nospace", 41 | "property-declaration": "nospace", 42 | "variable-declaration": "nospace" 43 | } 44 | ], 45 | "variable-name": [ 46 | true, 47 | "ban-keywords" 48 | ], 49 | "whitespace": [ 50 | true, 51 | "check-branch", 52 | "check-decl", 53 | "check-operator", 54 | "check-separator", 55 | "check-type" 56 | ] 57 | } 58 | } --------------------------------------------------------------------------------