This commit is contained in:
pincman 2024-05-09 04:34:52 +08:00
parent 011ae4b467
commit 2704a9ee9e
11 changed files with 52 additions and 12 deletions

View File

@ -40,6 +40,7 @@
},
"devDependencies": {
"@3rapp/code-config": "workspace:*",
"@faker-js/faker": "^8.4.1",
"@nestjs/cli": "^10.3.2",
"@nestjs/schematics": "^10.1.1",
"@nestjs/testing": "^10.3.8",

View File

@ -10,7 +10,7 @@ import { moduleImports } from './module.list';
imports: [
MikroOrmModule.forRoot({
...database(),
registerRequestContext: true,
// registerRequestContext: true,
// autoLoadEntities: true,
}),
...moduleImports,

View File

@ -15,6 +15,7 @@ export const database = () =>
entities: getEntities(moduleImports),
extensions: [Migrator, SeedManager],
migrations: {
disableForeignKeys: false,
tableName: 'migrations',
path: Utils.detectTsNode() ? 'src/database/migrations' : 'dist/database/migrations',
},

View File

@ -1,6 +1,6 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20240508080237 extends Migration {
export class Migration20240508201511 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"));',

View File

@ -1,10 +1,10 @@
import type { EntityManager } from '@mikro-orm/core';
import { Seeder } from '@mikro-orm/seeder';
import { PostSeeder } from './PostSeeder';
import { ContentPostSeeder } from './content/PostSeeder';
export class DatabaseSeeder extends Seeder {
async run(em: EntityManager): Promise<void> {
return this.call(em, [PostSeeder]);
return this.call(em, [ContentPostSeeder]);
}
}

View File

@ -1,6 +0,0 @@
import type { EntityManager } from '@mikro-orm/core';
import { Seeder } from '@mikro-orm/seeder';
export class PostSeeder extends Seeder {
async run(em: EntityManager): Promise<void> {}
}

View File

@ -0,0 +1,17 @@
import { Factory } from '@mikro-orm/seeder';
import { PostEntity } from '@/modules/content/entities/post.entity';
import { getFaker } from '@/modules/database/helpers';
export class ContentPostFactory extends Factory<PostEntity> {
model = PostEntity;
definition(): Partial<PostEntity> {
const faker = getFaker();
return {
title: faker.lorem.sentence({ min: 3, max: 9 }),
body: faker.lorem.paragraphs({ min: 1, max: 5 }, '<br/>\n'),
keywords: faker.lorem.words({ min: 0, max: 6 }).split(' '),
};
}
}

View File

@ -0,0 +1,10 @@
import type { EntityManager } from '@mikro-orm/core';
import { Seeder } from '@mikro-orm/seeder';
import { ContentPostFactory } from './PostFactory';
export class ContentPostSeeder extends Seeder {
async run(em: EntityManager): Promise<void> {
new ContentPostFactory(em).create(15);
}
}

View File

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

View File

@ -1,3 +1,4 @@
import { base, zh_CN, en, Faker } from '@faker-js/faker';
import { ModuleMetadata } from '@nestjs/common';
import { isFunction } from 'lodash';
@ -5,4 +6,8 @@ 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 = () => {};
const faker = new Faker({
locale: [zh_CN, en, base],
});
export const getFaker = () => faker;

View File

@ -142,6 +142,9 @@ importers:
'@3rapp/code-config':
specifier: workspace:*
version: link:../../packages/code-config
'@faker-js/faker':
specifier: ^8.4.1
version: 8.4.1
'@nestjs/cli':
specifier: ^10.3.2
version: 10.3.2(@swc/cli@0.3.12(@swc/core@1.5.3(@swc/helpers@0.5.11))(chokidar@3.6.0))(@swc/core@1.5.3(@swc/helpers@0.5.11))
@ -814,6 +817,10 @@ packages:
resolution: {integrity: sha512-Ys+3g2TaW7gADOJzPt83SJtCDhMjndcDMFVQ/Tj9iA1BfJzFKD9mAUXT3OenpuPHbI6P/myECxRJrofUsDx/5g==}
engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
'@faker-js/faker@8.4.1':
resolution: {integrity: sha512-XQ3cU+Q8Uqmrbf2e0cIC/QN43sTBSC8KF12u29Mb47tWrt2hAgBXSgpZMj4Ao8Uk0iJcU99QsOCaIL8934obCg==}
engines: {node: ^14.17.0 || ^16.13.0 || >=18.0.0, npm: '>=6.14.13'}
'@fastify/ajv-compiler@3.5.0':
resolution: {integrity: sha512-ebbEtlI7dxXF5ziNdr05mOY8NnDiPB1XvAlLHctRt/Rc+C3LCOVW5imUVX+mhvUhnNzmPBHewUkOFgGlCxgdAA==}
@ -6130,6 +6137,8 @@ snapshots:
'@eslint/js@8.57.0': {}
'@faker-js/faker@8.4.1': {}
'@fastify/ajv-compiler@3.5.0':
dependencies:
ajv: 8.13.0