GithubHelp home page GithubHelp logo

maximegris / typescript-express-sequelize Goto Github PK

View Code? Open in Web Editor NEW
174.0 11.0 53.0 120 KB

Easily bootstrap your NodeJS project with Express, Sequelize and Typescript :+1:

License: Other

JavaScript 44.91% TypeScript 55.09%
typescript sequelize express nodejs

typescript-express-sequelize's Introduction

Logo

Build Status License

Introduction

Easily bootstrap your Typescript project with NodeJS + Express + Sequelize ORM. ❤️

Installation

Run one of the command below

npm install
npm install -g yarn
yarn

The build tasks use Gulp tasks runner. Typescript is transpiled to Javascript in the /build directory. This sample use PostgreSQL database but you can easily change it and use your favorite relational database (npm or yarn command) :

npm install --save mysql // For both mysql and mariadb dialects
npm install --save sqlite3
npm install --save tedious // MSSQL

Configure your database

Sequelize configuration and entities can be found in /Src/sqlz directory.

Directory Description
config Your database configuration.
migrations Your database migrations scripts. Keep this files in Javascript and run sequelize db:migrate to migrate your database schema.
models Sequelize entities.

First, define your database schema in config/config.json file. Use Sequelize CLI to initialize your database.

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 !

Run the project

npm start

Your web server is now exposed on http://localhost:3000

GET /api/languages

curl -X GET -H 'Content-Type: application/json' http://localhost:3000/api/languages

POST /api/languages

curl -X POST -H 'Content-Type: application/json' -d '{"label":"French","name":"fr"}' http://localhost:3000/api/languages

GET /api/appusers

curl -X GET -H 'Content-Type: application/json' http://localhost:3000/api/appusers

POST /api/appusers

curl -X POST -H 'Content-Type: application/json' -d '{"email":"[email protected]","pwd":"something"}' http://localhost:3000/api/appusers

Build

npm run build

Lint your code before you commit!

In a collaborative project, it's always a pain when you have to work on files not correctly formatted. Now before each commit, yout typescript files are linted based on your tsconfig.json > .editorconfig > tslint.json files.

λ git commit -m "Example precommit"

> husky - npm run -s precommit

25l[14:22:30] Running tasks for src/**/*.ts [started]
[14:22:30] prettify [started]
[14:22:31] prettify [completed]
[14:22:31] git add [started]
[14:22:31] git add [completed]
[14:22:31] Running tasks for src/**/*.ts [completed]
25h25h[master 23c4321] Example precommit
 1 file changed, 1 insertion(+), 1 deletion(-)

By the way you can also run the command with a npm script

npm run prettify

Debug with Typescript and VSCode

Add breakpoints to your Typescript source code and launch in VSCode the "Debug Typescript" task. When you'll access to an endpoint, you will be able to debug directly in your Typescript's files.

Questions and issues

The github issue tracker is only for bug reports and feature requests.

Roadmap

  • Add Sequelize Typescript example with association
  • Manage multiple database configuration with NODE_ENV
  • Add Swagger API Framework

typescript-express-sequelize's People

Contributors

kant avatar maximegris avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

typescript-express-sequelize's Issues

Debug failed

When trying to use debugger like the instructions:
debug failed - "Could not find the task build"

[Tips] Import/Export in one line

Currently you do :

import * as LanguageGet from './languages.get'
import * as LanguagePost from './languages.post'

export { LanguageGet, LanguagePost }

You can do

export * as LanguageGet from './languages.get'
export * as LanguagePost from './languages.post'

So you win a line :)
You should also also export default your functions (when you only have one in file), so you save you from writing * as in your import/export.

Btw thank you for your work

How to define instance methods in models files.?

Hey,
I was going through your example of sequelize models. I am unable to understand how to write instance methods that require querying other models.
I have a doubt while writing the instance methods on a model. Let us assume there is a user model and a user has the option to create todos. Now a user can have many todos. How to write a function in the user models that returns the number of todos a user has. Example

instanceMethods: {
      countTodos: function() {
          // returns total number of todos of a user.
      }
    }

sync with database

I configured database config.json file, but sequelize tables appusers and languages are not creating, I need to write sequelize.sync() in src/sql/models/_index.ts. Is it an issue or am I missing something?

Build fails

There is an error in the compile:typescript phase while running npm run start

Screenshot 2023-04-26 at 11 49 13

Screenshot 2023-04-26 at 11 49 40

How can this be fixed?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.