Skip to content

Commit

Permalink
add user model scaffold, sharing dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Sep 8, 2024
1 parent 8626969 commit 183c79a
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 31 deletions.
3 changes: 2 additions & 1 deletion api/nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"collection": "@nestjs/schematics",
"compilerOptions": {
"deleteOutDir": true
}
},
"entryFile": "api/src/main"
}
5 changes: 3 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"start:prod": "node dist/api/src/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest --config ./test/jest-config.json -i --detectOpenHandles"
},
Expand All @@ -24,7 +24,8 @@
"pg": "^8.12.0",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"typeorm": "^0.3.20"
"typeorm": "catalog:",
"class-transformer": "catalog:"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand Down
5 changes: 3 additions & 2 deletions api/src/modules/config/app-config.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import { DatabaseModule } from '@api/modules/config/database/database.module';
ConfigModule.forRoot({
isGlobal: true,
cache: true,
// TODO: This is a bit ugly, we should find a way to make this more elegant
envFilePath: [
join(
__dirname,
`../../../../shared/config/.env.${process.env.NODE_ENV}`,
`../../../../../../shared/config/.env.${process.env.NODE_ENV}`,
),
join(__dirname, '../../../../shared/config/.env'),
join(__dirname, '../../../../../../shared/config/.env'),
],
}),
],
Expand Down
6 changes: 4 additions & 2 deletions api/src/modules/config/app-config.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { join } from 'path';
import { DATABASE_ENTITIES } from '@shared/entities/database.entities';

@Injectable()
export class ApiConfigService {
Expand All @@ -9,6 +9,8 @@ export class ApiConfigService {
/**
* @note We could abstract this to a data layer access config specific class within database module, as well for other configs when the thing gets more complex.
* we could also abstract the underlying engine type, which is now set in the main app module
*
* @note: Maybe it's a good idea to move the datasource config to shared folder, to be used potentially for a e2e test agent
*/
getDatabaseConfig() {
return {
Expand All @@ -17,7 +19,7 @@ export class ApiConfigService {
username: this.configService.get('DB_USERNAME'),
password: this.configService.get('DB_PASSWORD'),
database: this.configService.get('DB_NAME'),
entities: [join(__dirname, '**', '*.entity.{ts,js}')],
entities: DATABASE_ENTITIES,
synchronize: true,
ssl: this.isProduction()
? { require: true, rejectUnauthorized: false }
Expand Down
10 changes: 10 additions & 0 deletions api/src/modules/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { UsersService } from './users.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from '@shared/entities/users/user.entity';

@Module({
imports: [TypeOrmModule.forFeature([User])],
providers: [UsersService],
})
export class UsersModule {}
4 changes: 4 additions & 0 deletions api/src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class UsersService {}
63 changes: 40 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ packages:
catalog:
pg: "^8.12.0"
typeorm: "^0.3.20"
zod: "^3.23.8"
zod: "^3.23.8"
class-transformer: "^0.5.1"
3 changes: 3 additions & 0 deletions shared/entities/database.entities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { User } from "@shared/entities/users/user.entity";

export const DATABASE_ENTITIES = [User];
24 changes: 24 additions & 0 deletions shared/entities/users/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
Column,
CreateDateColumn,
Entity,
OneToMany,
PrimaryGeneratedColumn,
} from "typeorm";
import { Exclude } from "class-transformer";

@Entity({ name: "users" })
export class User {
@PrimaryGeneratedColumn("uuid")
id: string;

@Column({ unique: true })
email: string;

@Column()
@Exclude()
password: string;

@CreateDateColumn({ name: "created_at" })
createdAt: Date;
}
3 changes: 3 additions & 0 deletions shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"pg": "catalog:",
"typeorm": "catalog:",
"zod": "catalog:"
},
"dependencies": {
"class-transformer": "catalog:"
}
}

0 comments on commit 183c79a

Please sign in to comment.