Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 37 ยท Getting Started

Project Setup and Configuration

Organize a Node.js project for TypeORM or Sequelize: folders, tsconfig decorator flags, per-environment settings, the Sequelize CLI config, and a registry pattern you can run.

A database layer grows fast. A clear folder layout, one place for configuration and correct compiler settings save you hours later. This lesson sets up a project that works for both libraries.

All library code is read-only; the runnable blocks are stand-ins for the configuration and registry logic.

A folder layout that scales

TEXT
src/
  config/
    database.ts        # settings per environment
  db/
    data-source.ts     # TypeORM DataSource (or sequelize.ts)
    migrations/        # chapter 5
  entities/            # TypeORM entities  (models/ for Sequelize)
    user.entity.ts
    post.entity.ts
  repositories/        # query code, one file per aggregate
  services/            # business rules that call repositories
  index.ts

Keep SQL-shaped code in repositories/; controllers and services should not build queries. That separation makes the testing chapter much easier.

TypeScript settings for TypeORM

Decorators need two compiler flags, and reflect-metadata must be imported once before anything else.

JSON
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "strict": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "strictPropertyInitialization": false,
    "outDir": "dist"
  }
}
TypeScript
// src/index.ts - first line
import "reflect-metadata";

strictPropertyInitialization: false (or the ! on each property) stops TypeScript complaining that entity fields are not assigned in a constructor. Newer TypeScript versions also support standard decorators, but TypeORM's decorators still rely on the flags above.

Decorator metadata does not work with some bundlers and transpilers that strip type information (for example plain esbuild or swc without the right plugin). If columns lose their types, check how your build handles emitDecoratorMetadata, or write explicit types: @Column({ type: "varchar" }).

Per-environment configuration

Development, testing and production use different databases. Build the options from one function so the choice is explicit.

Node.js Config per environment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice that synchronize: true appears only in development and tests. In production it can drop or alter columns without review.

Sequelize CLI configuration

Sequelize has a separate CLI (sequelize-cli) for migrations and seeders. It reads .sequelizerc and a config file:

JavaScript
// .sequelizerc
const path = require("path");
module.exports = {
  config: path.resolve("src/config", "cli-config.cjs"),
  "models-path": path.resolve("src/models"),
  "migrations-path": path.resolve("src/db/migrations"),
  "seeders-path": path.resolve("src/db/seeders"),
};
JavaScript
// src/config/cli-config.cjs
module.exports = {
  development: { dialect: "postgres", host: "localhost", database: "shop_dev", username: "app", password: "secret" },
  production:  { use_env_variable: "DATABASE_URL", dialect: "postgres" },
};

use_env_variable reads a full connection URL from the environment, which suits hosting platforms.

TypeORM CLI configuration

The TypeORM CLI needs a file that exports a DataSource. Point to it with -d:

JSON
{
  "scripts": {
    "typeorm": "typeorm-ts-node-commonjs",
    "migration:run": "npm run typeorm -- migration:run -d src/db/data-source.ts"
  }
}

The same DataSource file serves your app and the CLI, so settings never drift apart.

A registry pattern

As models multiply you need a single list. Both libraries accept that list: entities: [User, Post] in TypeORM, and in Sequelize you call each model's init (often from one initModels(sequelize) function so associations can be wired after all models exist). This stand-in shows the idea and one important check: duplicates.

Node.js Model registry

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The two-pass shape (register everything, then associate) is exactly why Sequelize projects have an associate step: a model cannot reference one that is not defined yet.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which compiler flags do TypeORM decorators rely on?

    Both flags are needed so column types can be read from TypeScript metadata.
  2. 2. Where should synchronize: true be used?

    In production it can alter or drop columns without review; use migrations.
  3. 3. What does use_env_variable do in the Sequelize CLI config?

    It names the environment variable that holds the connection string.
  4. 4. Why register all models first and associate them in a second pass?

    Associating after every model exists avoids ordering problems and circular imports.

Summary

  • Keep entities/models, repositories, services and migrations in separate folders.
  • TypeORM needs experimentalDecorators, emitDecoratorMetadata and one import "reflect-metadata".
  • Build connection options per environment; allow synchronize only outside production.
  • The Sequelize CLI reads .sequelizerc; the TypeORM CLI needs a DataSource file passed with -d.
  • Register models in one place and associate them after all are defined.

Next: TypeORM Entities and Columns