Proof of concept

This commit is contained in:
Adam Cooper 2023-10-18 18:11:32 -04:00
parent 3e535f8805
commit 5ca0a076e2
10 changed files with 41 additions and 30 deletions

View file

@ -1,24 +0,0 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { Cat } from "./src/cats/cats.entity";
@Injectable()
export class CatsService {
constructor(
@InjectRepository(Cat)
private catsRepository: Repository<Cat>,
) {}
findAll(): Promise<Cat[]> {
return this.catsRepository.find();
}
findOne(id: number): Promise<Cat | null> {
return this.catsRepository.findOneBy({ id });
}
async remove(id: number): Promise<void> {
await this.catsRepository.delete(id)
}
}

15
package-lock.json generated
View file

@ -11,6 +11,7 @@
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/devtools-integration": "^0.1.5",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/typeorm": "^10.0.0",
"pg": "^8.11.3",
@ -1639,6 +1640,20 @@
}
}
},
"node_modules/@nestjs/devtools-integration": {
"version": "0.1.5",
"resolved": "https://registry.npmjs.org/@nestjs/devtools-integration/-/devtools-integration-0.1.5.tgz",
"integrity": "sha512-iwxvfxa3kzOulG/Fje82Ww80OVsA8rzYR+UAEKrdGExRRGQRGQ0+bHfG0XLlPG3XIuM5J2MRMo7x1bN8ZIs9Gg==",
"dependencies": {
"chalk": "^4.1.2",
"node-fetch": "^2.6.9"
},
"peerDependencies": {
"@nestjs/common": "^9.3.7 || ^10.0.0",
"@nestjs/core": "^9.3.7 || ^10.0.0",
"reflect-metadata": "^0.1.12"
}
},
"node_modules/@nestjs/platform-express": {
"version": "10.2.5",
"resolved": "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-10.2.5.tgz",

View file

@ -8,6 +8,7 @@
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"typeorm": "typeorm-ts-node-esm",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
@ -22,6 +23,7 @@
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/devtools-integration": "^0.1.5",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/typeorm": "^10.0.0",
"pg": "^8.11.3",

View file

@ -1,17 +1,21 @@
import { Module } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm'; // DEBUG: Is this necessary? Docs unclear.
import { DevtoolsModule } from '@nestjs/devtools-integration';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Cat } from './cats/cats.entity';
import { CatsModule } from './cats/cats.module';
console.debug(`[AppModule] Starting...`)
let dbConfig: TypeOrmModuleOptions;
if ( process.env.NODE_ENV === 'production' ) {
dbConfig = {
type: 'sqlite',
database: 'database/database_development.sqlite3'
database: 'database/database_development.sqlite3',
entities: [Cat],
}
} else {
dbConfig = {
@ -22,9 +26,12 @@ if ( process.env.NODE_ENV === 'production' ) {
}
@Module({
imports: [TypeOrmModule.forRoot(dbConfig), CatsModule],
imports: [
TypeOrmModule.forRoot(dbConfig),
DevtoolsModule.register({ http: process.env.NODE_ENV !== 'production' }),
CatsModule
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

View file

@ -4,10 +4,13 @@ import { Cat } from "./cats.entity";
@Controller('cats')
export class CatsController {
constructor(private catsService: CatsService) {};
constructor(private catsService: CatsService) {
console.debug("[cats controller] Starting...");
}
@Get()
findAll(): Promise<Cat[]> {
console.debug("[ctrl findAll] Starting...");
return this.catsService.findAll();
}

View file

@ -1,5 +1,7 @@
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";
console.debug('[cats entity] Starting...')
@Entity()
export class Cat {
@PrimaryGeneratedColumn()

View file

@ -4,6 +4,8 @@ import { CatsController } from "./cats.controller";
import { CatsService } from "./cats.service";
import { Cat } from "./cats.entity";
console.debug("[cats module] Starting...");
@Module({
imports: [TypeOrmModule.forFeature([Cat])],
providers: [CatsService],

View file

@ -8,9 +8,12 @@ export class CatsService {
constructor(
@InjectRepository(Cat)
private catsRepository: Repository<Cat>,
) {}
) {
console.debug("[cats service] Starting...");
}
findAll(): Promise<Cat[]> {
console.log("[svc findAll] Starting...")
return this.catsRepository.find();
}

View file

@ -2,7 +2,8 @@ import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
console.debug("[bootstrap] Starting...")
const app = await NestFactory.create(AppModule, { snapshot: true });
await app.listen(3000);
}
bootstrap();