nestjs-typeorm3-kit 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +138 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -104,6 +104,144 @@ import { DefRepositoryModule } from "nestjs-typeorm3-kit";
|
|
|
104
104
|
export class PrimaryRepoModule {}
|
|
105
105
|
```
|
|
106
106
|
|
|
107
|
+
#### Use @DefTransaction
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { Injectable } from "@nestjs/common";
|
|
111
|
+
import { DefTransaction, InjectRepo } from "nestjs-typeorm3-kit";
|
|
112
|
+
import { PhotoRepo } from "~/domains/primary/photo/photo.repo";
|
|
113
|
+
import { BookRepo } from "~/domains/primary/book/book.repo";
|
|
114
|
+
|
|
115
|
+
const PRIMARY_CONNECTION = "default";
|
|
116
|
+
const SECONDARY_CONNECTION = "secondary_db";
|
|
117
|
+
@Injectable()
|
|
118
|
+
export class DemoService {
|
|
119
|
+
constructor(
|
|
120
|
+
readonly photoRepo: PhotoRepo,
|
|
121
|
+
@InjectRepo(BookRepo, PRIMARY_CONNECTION)
|
|
122
|
+
readonly bookRepo: BookRepo
|
|
123
|
+
) {}
|
|
124
|
+
|
|
125
|
+
@DefTransaction()
|
|
126
|
+
create(body: any) {
|
|
127
|
+
return this.photoRepo.save(body);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// secondary connection
|
|
132
|
+
@Injectable()
|
|
133
|
+
export class DemoService {
|
|
134
|
+
constructor(
|
|
135
|
+
@InjectRepo(ExampleRepo, SECONDARY_CONNECTION)
|
|
136
|
+
readonly exampleRepo: ExampleRepo,
|
|
137
|
+
@InjectRepo(DataLogRepo, SECONDARY_CONNECTION)
|
|
138
|
+
readonly dataLogRepo: DataLogRepo
|
|
139
|
+
) {}
|
|
140
|
+
|
|
141
|
+
@DefTransaction({ connectionName: SECONDARY_CONNECTION })
|
|
142
|
+
create(body: any) {
|
|
143
|
+
return this.photoRepo.save(body);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
#### Use wraper Controller and ChildModule
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import { DefController } from "nestjs-typeorm3-kit";
|
|
152
|
+
|
|
153
|
+
@Controller("demo")
|
|
154
|
+
export class DemoController {
|
|
155
|
+
constructor(private readonly demoService: DemoService) {}
|
|
156
|
+
|
|
157
|
+
@DefGet()
|
|
158
|
+
list(@Query() query: any) {
|
|
159
|
+
return this.demoService.list(query);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
@DefPost()
|
|
163
|
+
create(@Body() body: any) {
|
|
164
|
+
return this.demoService.create(body);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// child module suport prefix route and API TAG swagger
|
|
169
|
+
@ChildModule({
|
|
170
|
+
prefix: REFIX_MODULE.client,
|
|
171
|
+
imports: [PrimaryRepoModule, SecondaryRepoModule],
|
|
172
|
+
providers: [DemoService, ExampleService],
|
|
173
|
+
controllers: [ExampleController, DemoController],
|
|
174
|
+
})
|
|
175
|
+
export class ClientModule implements NestModule {
|
|
176
|
+
configure(consumer: MiddlewareConsumer) {}
|
|
177
|
+
}
|
|
178
|
+
// setup swagger beautiful in main.ts
|
|
179
|
+
// main.ts
|
|
180
|
+
import { NestFactory } from "@nestjs/core";
|
|
181
|
+
import { AppModule } from "./app.module";
|
|
182
|
+
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
|
|
183
|
+
import { getFromContainer, MetadataStorage } from "class-validator";
|
|
184
|
+
import { SchemasObject } from "@nestjs/swagger/dist/interfaces/open-api-spec.interface";
|
|
185
|
+
|
|
186
|
+
import { validationMetadatasToSchemas } from "class-validator-jsonschema";
|
|
187
|
+
import { setupTransactionContext } from "nestjs-typeorm3-kit";
|
|
188
|
+
import { INestApplication } from "@nestjs/common";
|
|
189
|
+
|
|
190
|
+
const configSwagger = (app: INestApplication) => {
|
|
191
|
+
const options = new DocumentBuilder()
|
|
192
|
+
.setTitle("SWAGGER_TITLE")
|
|
193
|
+
.setDescription("SWAGGER_DESCRIPTION")
|
|
194
|
+
.setVersion("SWAGGER_VERSION")
|
|
195
|
+
.addSecurity("bearer", {
|
|
196
|
+
type: "http",
|
|
197
|
+
scheme: "bearer",
|
|
198
|
+
bearerFormat: "JWT",
|
|
199
|
+
})
|
|
200
|
+
.build();
|
|
201
|
+
|
|
202
|
+
const document = SwaggerModule.createDocument(app as any, options, {
|
|
203
|
+
// extraModels: [PageResponse]
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// Creating all the swagger schemas based on the class-validator decorators
|
|
207
|
+
const metadatas = (getFromContainer(MetadataStorage) as any)
|
|
208
|
+
.validationMetadatas;
|
|
209
|
+
const targetSchemas = document.components.schemas || {};
|
|
210
|
+
const schemasBinding = validationMetadatasToSchemas(metadatas) || {};
|
|
211
|
+
|
|
212
|
+
Object.keys(schemasBinding).forEach((key) => {
|
|
213
|
+
const value = schemasBinding[key] as SchemasObject;
|
|
214
|
+
if (!targetSchemas[key]) {
|
|
215
|
+
Object.assign(targetSchemas, { key: value });
|
|
216
|
+
} else {
|
|
217
|
+
const targetValue = targetSchemas[key] as SchemasObject;
|
|
218
|
+
|
|
219
|
+
Object.assign(targetValue.properties, value.properties);
|
|
220
|
+
targetValue.required = value.required;
|
|
221
|
+
Object.assign(targetSchemas, { key: targetValue });
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
document.components.schemas = Object.assign({}, targetSchemas);
|
|
225
|
+
SwaggerModule.setup("swagger", app as any, document);
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
async function bootstrap() {
|
|
229
|
+
setupTransactionContext();
|
|
230
|
+
const app = await NestFactory.create(AppModule);
|
|
231
|
+
const port = process.env.PORT || 3000;
|
|
232
|
+
app.enableCors({ origin: "*" });
|
|
233
|
+
configSwagger(app);
|
|
234
|
+
await app.listen(port);
|
|
235
|
+
console.log(
|
|
236
|
+
`Server start on port ${port}. Open http://localhost:${port} to see results`
|
|
237
|
+
);
|
|
238
|
+
console.log(`API DOCUMENT Open http://localhost:${port}/swagger`);
|
|
239
|
+
console.log(`API DOCUMENT JSON Open http://localhost:${port}/swagger-json`);
|
|
240
|
+
console.log("TIMEZONE: ", process.env.TZ);
|
|
241
|
+
}
|
|
242
|
+
bootstrap();
|
|
243
|
+
```
|
|
244
|
+
|
|
107
245
|
#### Example
|
|
108
246
|
|
|
109
247
|
[Example](./example)
|