Skip to content

A starter project for Sequelize, TypeScript, and Express

Notifications You must be signed in to change notification settings

nealwp/sequelize-sandbox

Repository files navigation

Sequelize Sandbox

This repo is an example of using Sequelize, TypeScript, and Express. Each directory has its own README.md file to serve as documentation.

The goal here is to provide simple examples for using Sequelize, as well as establishing useful patterns to follow in similar projects.

Following the setup will give you a running Express API with a persistent PostgreSQL database via Docker. From there, you can experiment with creating new models, controller methods, routes, etc.

Feel free to fork this repo as a starting point for a project of your own.

Project Navigation

Set up

  1. Clone this repo

    git clone [email protected]:nealwp/sequelize-sandbox.git
  2. Run install

    npm i
  3. Start the local db (requires Docker)

    npm run db:start    # data will persist in Docker volume 'sequelize-pgdata'
  4. Run the dev server

    npm run dev
  5. When you're finished/want to stop the database, run

    npm run db:stop
  • You can import the Postman collection to view and use available endpoints.
  • You can also connect to the database on localhost:5432 via psql or pgAdmin, using the following credentials:
    • User: postgres
    • Password: postgres
    • Database Name: scratch

Data Model

The example code in this project is meant to represent a basic inventory system for a game. There are three models to depict this:

  1. Characters
  2. Inventory
  3. Weapons
erDiagram
    CHARACTER ||--o| INVENTORY : ""
    CHARACTER {
        int id
        string name
        int age
    }
    INVENTORY ||--o{ WEAPON : ""
    INVENTORY ||--o| CHARACTER : ""
    INVENTORY {
        int id
        int characterId
    }
    WEAPON ||--o| INVENTORY : ""
    WEAPON {
        int id
        int inventoryId
        string name
        int damage
        string type
    }

Loading
  • A character can be assigned zero or exactly one inventory
  • An inventory can be assigned to zero or exactly one character
  • A weapon can belong to zero or exactly one inventory
  • An inventory can have zero to any number of weapons
  • Each entity can exist independently of all other entities

The model is deliberately simple, as the goal here is to give concrete examples of "how to do X with Sequelize & TypeScript" in as little code as possible.

Disclaimer

The documentation in this repo is opinionated; you will see things like never do X, always do Y, and there should be Z. I'm well aware that this is not the only way - or even the best way - to logically structure a Sequelize/Express project. However, it is a way that works, and works well enough to justify being opinionated about it.

These opinions may be interpreted as rules. If it proves useful for you to follow them - do so. Should you find yourself in a situation where breaking these "rules" makes more sense than following them - great! Do that instead.

I am confident in your ability to use your noggin.

About

A starter project for Sequelize, TypeScript, and Express

Topics

Resources

Stars

Watchers

Forks