Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Database Access with TypeORM | TypeScript Mastery lesson | BraveProgrammer
  1. Courses
  2. /
  3. TypeScript Mastery

Lesson 37 of 56

·Typescript

Database Access with TypeORM

25 minutes

Course progress66%

Install

npm i typeorm reflect-metadata sqlite3
npm i -D @types/node

Add import 'reflect-metadata'; once.

Entity definition

import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

()
   {
  ()
  id!: ;

  ({ :  })
  name!: ;

  ({ : , :  })
  ?: ;
}
@Entity
export
class
User
@PrimaryGeneratedColumn
number
@Column
length
100
string
@Column
type
'int'
nullable
true
age
number

DataSource (connection) setup

import { DataSource } from 'typeorm';
import { User } from './entity/User';

export const AppDataSource = new DataSource({
  type: 'sqlite',
  database: 'dev.db',
  synchronize: true, // auto‑create tables (dev only)
  entities: [User],
});

await AppDataSource.initialize();

Repository usage

const repo = AppDataSource.getRepository(User);
await repo.save({ name: 'Alice', age: 30 });
const all = await repo.find();

Info

synchronize: true is convenient for prototypes but should be disabled in production.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from TypeScript MasteryBrowse activities
Previous lessonValidation with `class-validator` & DecoratorsContinue to lesson 38Testing with Jest & `ts-jest`Back to TypeScript Mastery curriculum