This commit is contained in:
pincman 2024-05-08 21:06:43 +08:00
parent fa01d3c04c
commit 99401e713c
14 changed files with 629 additions and 656 deletions

View File

@ -7,6 +7,7 @@
"license": "UNLICENSED",
"scripts": {
"cli": "nest",
"db": "mikro-orm",
"dev": "pnpm start:dev",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
@ -22,13 +23,17 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@mikro-orm/cli": "^6.2.5",
"@mikro-orm/core": "^6.2.5",
"@mikro-orm/migrations": "^6.2.5",
"@mikro-orm/nestjs": "^5.2.3",
"@mikro-orm/postgresql": "^6.2.5",
"@mikro-orm/seeder": "^6.2.5",
"@nestjs/common": "^10.3.8",
"@nestjs/core": "^10.3.8",
"@nestjs/platform-fastify": "^10.3.8",
"fastify": "^4.26.2",
"lodash": "^4.17.21",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"uuid": "^9.0.1"
@ -41,6 +46,7 @@
"@swc/cli": "^0.3.12",
"@swc/core": "^1.5.3",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.17.1",
"@types/node": "^20.12.8",
"@types/supertest": "^6.0.2",
"@types/uuid": "^9.0.8",
@ -71,5 +77,12 @@
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
},
"mikro-orm": {
"useTsNode": true,
"configPaths": [
"./src/config/database.config.ts",
"./dist/config/database.config.js"
]
}
}

View File

@ -1,24 +1,19 @@
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { PostgreSqlDriver } from '@mikro-orm/postgresql';
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ContentModule } from './modules/content/content.module';
import { UserModule } from './modules/user/user.module';
import { database } from './config/database.config';
import { moduleImports } from './module.list';
@Module({
imports: [
MikroOrmModule.forRoot({
host: '127.0.0.1',
user: 'postgres',
password: '123456',
dbName: '3rapp',
driver: PostgreSqlDriver,
autoLoadEntities: true,
...database(),
registerRequestContext: true,
// autoLoadEntities: true,
}),
UserModule,
ContentModule,
...moduleImports,
],
controllers: [AppController],
providers: [AppService],

View File

@ -0,0 +1,20 @@
import { Migrator } from '@mikro-orm/migrations';
import { defineConfig, Utils } from '@mikro-orm/postgresql';
import { moduleImports } from '@/module.list';
import { getEntities } from '@/modules/database/helpers';
export const database = () =>
defineConfig({
host: '127.0.0.1',
user: 'postgres',
password: '123456',
dbName: '3rapp',
entities: getEntities(moduleImports),
extensions: [Migrator, SeedManager],
migrations: {
tableName: 'migrations',
path: Utils.detectTsNode() ? 'src/database/migrations' : 'dist/database/migrations',
},
});
export default database();

View File

@ -0,0 +1,90 @@
{
"namespaces": ["public"],
"name": "public",
"tables": [
{
"columns": {
"id": {
"name": "id",
"type": "uuid",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "uuid"
},
"title": {
"name": "title",
"type": "varchar(255)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "string"
},
"body": {
"name": "body",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"keywords": {
"name": "keywords",
"type": "text[]",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "array"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"mappedType": "datetime"
}
},
"name": "3rapp_posts",
"schema": "public",
"indexes": [
{
"columnNames": ["title"],
"composite": false,
"keyName": "3rapp_posts_title_index",
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "3rapp_posts_pkey",
"columnNames": ["id"],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {},
"nativeEnums": {}
}
],
"nativeEnums": {}
}

View File

@ -0,0 +1,14 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20240508080237 extends Migration {
async up(): Promise<void> {
this.addSql(
'create table "3rapp_posts" ("id" uuid not null, "title" varchar(255) not null, "body" text not null, "keywords" text[] null, "created_at" timestamptz not null, "updated_at" timestamptz not null, constraint "3rapp_posts_pkey" primary key ("id"));',
);
this.addSql('create index "3rapp_posts_title_index" on "3rapp_posts" ("title");');
}
async down(): Promise<void> {
this.addSql('drop table if exists "3rapp_posts" cascade;');
}
}

View File

@ -15,8 +15,9 @@ async function bootstrap() {
// 设置全局访问前缀
app.setGlobalPrefix('api');
// 启动后的输出
await app.listen(3100, () => {
console.log('api: http://localhost:3100');
await app.listen(3001, () => {
console.log();
console.log('api: http://localhost:3001/api');
});
}
bootstrap();

View File

@ -0,0 +1,6 @@
import { ModuleMetadata } from '@nestjs/common';
import { ContentModule } from './modules/content/content.module';
import { UserModule } from './modules/user/user.module';
export const moduleImports: ModuleMetadata['imports'] = [UserModule, ContentModule];

View File

@ -1,4 +1,11 @@
import { AnyEntity, EntityName } from '@mikro-orm/core';
import { Module } from '@nestjs/common';
import { PostEntity } from './entities/post.entity';
@Module({})
export class ContentModule {}
export class ContentModule {
static entities(): EntityName<AnyEntity>[] {
return [PostEntity];
}
}

View File

@ -1,8 +1,27 @@
import { Entity, PrimaryKey } from '@mikro-orm/core';
import { ArrayType, Entity, EntityRepositoryType, PrimaryKey, Property } from '@mikro-orm/core';
import { v4 } from 'uuid';
@Entity()
import { PostRepository } from '../repositories/post.repository';
@Entity({ repository: () => PostRepository, tableName: '3rapp_posts' })
export class PostEntity {
@PrimaryKey()
[EntityRepositoryType]?: PostRepository;
@PrimaryKey({ type: 'uuid' })
id = v4();
@Property({ index: true })
title: string;
@Property({ type: 'text', lazy: true })
body: string;
@Property({ type: ArrayType, nullable: true })
keywords?: string[];
@Property()
createdAt = new Date();
@Property({ onUpdate: () => new Date() })
updatedAt = new Date();
}

View File

@ -8,5 +8,7 @@ export class PostService {
constructor(protected postRepo: PostRepository) {}
@CreateRequestContext<PostService>((t) => t.postRepo)
async doSomething() {}
async doSomething() {
console.log('test');
}
}

View File

@ -0,0 +1,8 @@
import { ModuleMetadata } from '@nestjs/common';
import { isFunction } from 'lodash';
export const getEntities = (modules: ModuleMetadata['imports'] = []) =>
modules
.map((m) => ('entities' in m && isFunction(m.entities) ? m.entities() : []))
.reduce((o, n) => [...o, ...n], []);
export const resolveConfig = () => {};

View File

@ -6,6 +6,7 @@
"dev": "turbo dev",
"api:dev": "turbo dev --filter=api",
"api:cli": "turbo cli --cwd=./apps/api --",
"db": "turbo db --cwd=./apps/api --",
"admin:dev": "turbo dev --filter=admin --filter=api",
"web:dev": "turbo dev --filter=web --filter=api",
"lint": "turbo lint",

File diff suppressed because it is too large Load Diff

View File

@ -23,6 +23,10 @@
},
"@3rapp/admin#dev": {
"dependsOn": ["@3rapp/utils#build"]
},
"@3rapp/api#db": {
"cache": false,
"persistent": true
}
}
}