mirror of
https://github.com/aditama-labs/nest-autocrud.git
synced 2024-11-22 03:16:21 +00:00
wip: process for typeorm
This commit is contained in:
parent
a74cb24e9f
commit
291e656757
@ -1,7 +0,0 @@
|
|||||||
import { ConfigurableModuleBuilder } from '@nestjs/common';
|
|
||||||
import { TypeORMModuleOptions } from './interfaces';
|
|
||||||
|
|
||||||
export const { ConfigurableModuleClass } =
|
|
||||||
new ConfigurableModuleBuilder<TypeORMModuleOptions>()
|
|
||||||
.setClassMethodName('forRoot')
|
|
||||||
.build();
|
|
@ -1,2 +1,3 @@
|
|||||||
export const TYPEORM_DATASOURCE = 'TYPEORM_DATASOURCE';
|
export const TYPEORM_DATASOURCE = 'TYPEORM_DATASOURCE';
|
||||||
export const TYPEORM_REPOSITORY = 'TYPEORM_REPOSITORY';
|
export const TYPEORM_REPOSITORY = 'TYPEORM_REPOSITORY';
|
||||||
|
export const TYPEORM_WHERE_CLAUSE = 'TYPEORM_WHERE_CLAUSE';
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
export interface TypeORMModuleOptions {
|
import { EntityTarget, FindOptionsWhere, ObjectLiteral } from "typeorm";
|
||||||
entity;
|
|
||||||
|
export interface TypeORMModuleOptions<T extends ObjectLiteral> {
|
||||||
|
entity: EntityTarget<T>;
|
||||||
synchronize?: boolean;
|
synchronize?: boolean;
|
||||||
entities: string[];
|
entities: string[];
|
||||||
|
uniqueWhereClause: FindOptionsWhere<T>;
|
||||||
}
|
}
|
||||||
|
13
libs/typeorm/src/processes/create.process.ts
Normal file
13
libs/typeorm/src/processes/create.process.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { CreateProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||||
|
import { TypeORMProcess } from './typeorm.process';
|
||||||
|
|
||||||
|
export class TypeORMCreateProcess<T>
|
||||||
|
extends TypeORMProcess<T>
|
||||||
|
implements CreateProcess
|
||||||
|
{
|
||||||
|
public payload;
|
||||||
|
|
||||||
|
async process() {
|
||||||
|
this.result = await this.service.getRepository().save(this.payload);
|
||||||
|
}
|
||||||
|
}
|
14
libs/typeorm/src/processes/delete.process.ts
Normal file
14
libs/typeorm/src/processes/delete.process.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { DeleteProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||||
|
import { TypeORMProcess } from './typeorm.process';
|
||||||
|
|
||||||
|
export class TypeORMDeleteProcess<T>
|
||||||
|
extends TypeORMProcess<T>
|
||||||
|
implements DeleteProcess
|
||||||
|
{
|
||||||
|
public identityData;
|
||||||
|
public identityKey: string = 'id';
|
||||||
|
|
||||||
|
async process() {
|
||||||
|
this.result = await this.service.getRepository().delete(this.uniqueWhereClause);
|
||||||
|
}
|
||||||
|
}
|
7
libs/typeorm/src/processes/index.ts
Normal file
7
libs/typeorm/src/processes/index.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export * from './create.process';
|
||||||
|
export * from './delete.process';
|
||||||
|
export * from './list.process';
|
||||||
|
export * from './pagination.process';
|
||||||
|
export * from './read.process';
|
||||||
|
export * from './update.process';
|
||||||
|
|
8
libs/typeorm/src/processes/list.process.ts
Normal file
8
libs/typeorm/src/processes/list.process.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import { ListProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||||
|
import { TypeORMProcess } from './typeorm.process';
|
||||||
|
|
||||||
|
export class TypeORMListProcess<T> extends TypeORMProcess<T> implements ListProcess {
|
||||||
|
async process() {
|
||||||
|
this.result = await this.service.getRepository().find();
|
||||||
|
}
|
||||||
|
}
|
20
libs/typeorm/src/processes/pagination.process.ts
Normal file
20
libs/typeorm/src/processes/pagination.process.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { PaginationProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||||
|
import { IPaginationParam } from '@aditama-labs/nest-autocrud/skeleton/src/interfaces/pagination-param.interface';
|
||||||
|
import { TypeORMProcess } from './typeorm.process';
|
||||||
|
|
||||||
|
export class TypeORMPaginationProcess<T>
|
||||||
|
extends TypeORMProcess<T>
|
||||||
|
implements PaginationProcess
|
||||||
|
{
|
||||||
|
params: IPaginationParam;
|
||||||
|
|
||||||
|
async process() {
|
||||||
|
const { page, limit } = this.params;
|
||||||
|
const skip = (page - 1) * limit;
|
||||||
|
|
||||||
|
this.result = await this.service.getRepository().find({
|
||||||
|
skip,
|
||||||
|
take: limit,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
13
libs/typeorm/src/processes/read.process.ts
Normal file
13
libs/typeorm/src/processes/read.process.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { ReadProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||||
|
import { TypeORMProcess } from './typeorm.process';
|
||||||
|
|
||||||
|
export class TypeORMReadProcess<T> extends TypeORMProcess<T> implements ReadProcess {
|
||||||
|
public identityData;
|
||||||
|
public identityKey: string = 'id';
|
||||||
|
|
||||||
|
async process() {
|
||||||
|
this.result = await this.service.getRepository().findOne({
|
||||||
|
where: this.uniqueWhereClause,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
16
libs/typeorm/src/processes/typeorm.process.ts
Normal file
16
libs/typeorm/src/processes/typeorm.process.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { DefaultProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||||
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
|
import { TypeORMService } from '../typeorm.service';
|
||||||
|
import { TYPEORM_WHERE_CLAUSE } from '../constants';
|
||||||
|
import { FindOptionsWhere } from 'typeorm';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class TypeORMProcess<T> extends DefaultProcess {
|
||||||
|
constructor(
|
||||||
|
public service: TypeORMService<T>,
|
||||||
|
@Inject(TYPEORM_WHERE_CLAUSE)
|
||||||
|
public uniqueWhereClause: FindOptionsWhere<T>,
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
}
|
17
libs/typeorm/src/processes/update.process.ts
Normal file
17
libs/typeorm/src/processes/update.process.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { UpdateProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||||
|
import { TypeORMProcess } from './typeorm.process';
|
||||||
|
|
||||||
|
export class TypeORMUpdateProcess<T>
|
||||||
|
extends TypeORMProcess<T>
|
||||||
|
implements UpdateProcess
|
||||||
|
{
|
||||||
|
public identityData;
|
||||||
|
public identityKey: string = 'id';
|
||||||
|
public payload;
|
||||||
|
|
||||||
|
async process() {
|
||||||
|
this.result = await this.service
|
||||||
|
.getRepository()
|
||||||
|
.update(this.uniqueWhereClause, this.payload);
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,12 @@
|
|||||||
import { DynamicModule, Module } from '@nestjs/common';
|
import { DynamicModule, Module } from '@nestjs/common';
|
||||||
import { TypeormService } from './typeorm.service';
|
import { DataSource, ObjectLiteral } from 'typeorm';
|
||||||
|
import {
|
||||||
|
TYPEORM_DATASOURCE,
|
||||||
|
TYPEORM_REPOSITORY,
|
||||||
|
TYPEORM_WHERE_CLAUSE,
|
||||||
|
} from './constants';
|
||||||
import { TypeORMModuleOptions } from './interfaces';
|
import { TypeORMModuleOptions } from './interfaces';
|
||||||
import { ConfigurableModuleClass } from './config.module-definition';
|
import { TypeORMService } from './typeorm.service';
|
||||||
import { DataSource } from 'typeorm';
|
|
||||||
import { TYPEORM_DATASOURCE, TYPEORM_REPOSITORY } from './constants';
|
|
||||||
|
|
||||||
const getDatabaseCredential = (
|
const getDatabaseCredential = (
|
||||||
synchronize?: boolean,
|
synchronize?: boolean,
|
||||||
@ -44,13 +47,13 @@ const getDatabaseCredential = (
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@Module({
|
@Module({})
|
||||||
providers: [TypeormService],
|
export class TypeORMModule {
|
||||||
exports: [TypeormService],
|
static forRoot<T extends ObjectLiteral>(
|
||||||
})
|
options: TypeORMModuleOptions<T>,
|
||||||
export class TypeORMModule<T> extends ConfigurableModuleClass {
|
): DynamicModule {
|
||||||
static forRoot(options: TypeORMModuleOptions): DynamicModule {
|
|
||||||
let providers = [
|
let providers = [
|
||||||
|
TypeORMService,
|
||||||
{
|
{
|
||||||
provide: TYPEORM_DATASOURCE,
|
provide: TYPEORM_DATASOURCE,
|
||||||
useFactory: async () => {
|
useFactory: async () => {
|
||||||
@ -69,8 +72,15 @@ export class TypeORMModule<T> extends ConfigurableModuleClass {
|
|||||||
];
|
];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...super.forRoot(options),
|
module: TypeORMModule,
|
||||||
providers,
|
providers: [
|
||||||
|
...providers,
|
||||||
|
{
|
||||||
|
provide: TYPEORM_WHERE_CLAUSE,
|
||||||
|
useValue: options.uniqueWhereClause,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
exports: providers,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,13 @@ import { Repository } from 'typeorm';
|
|||||||
import { TYPEORM_REPOSITORY } from './constants';
|
import { TYPEORM_REPOSITORY } from './constants';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TypeormService<T> {
|
export class TypeORMService<T> {
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(TYPEORM_REPOSITORY)
|
@Inject(TYPEORM_REPOSITORY)
|
||||||
private repository: Repository<T>,
|
private repository: Repository<T>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
public getRepository(): Repository<T>{
|
||||||
|
return this.repository;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,43 +1,11 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { SimpleModule } from './example/simple/simple.module';
|
import { CustomModule } from './example/prisma/custom/custom.module';
|
||||||
import { CustomModule } from './example/custom/custom.module';
|
import { SimpleModule } from './example/prisma/simple/simple.module';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
||||||
|
|
||||||
const getDatabaseCredential = (): any => {
|
|
||||||
const url = new URL(process.env.DATABASE_URL);
|
|
||||||
|
|
||||||
// Extract the necessary components
|
|
||||||
let protocol;
|
|
||||||
switch(url.protocol){
|
|
||||||
case 'postgresql':
|
|
||||||
protocol = 'postgres';
|
|
||||||
break;
|
|
||||||
case 'mysql':
|
|
||||||
protocol = 'mysql';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
const username = url.username;
|
|
||||||
const password = url.password;
|
|
||||||
const host = url.hostname;
|
|
||||||
const port = url.port;
|
|
||||||
const databaseName = url.pathname.substring(1); // Remove the leading slash
|
|
||||||
return {
|
|
||||||
type: protocol,
|
|
||||||
host: host,
|
|
||||||
port: port,
|
|
||||||
username: username,
|
|
||||||
password: password,
|
|
||||||
database: databaseName,
|
|
||||||
entities: [],
|
|
||||||
synchronize: true,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
SimpleModule,
|
SimpleModule,
|
||||||
CustomModule,
|
CustomModule,
|
||||||
TypeOrmModule.forRoot(getDatabaseCredential()),
|
|
||||||
],
|
],
|
||||||
controllers: [],
|
controllers: [],
|
||||||
providers: [],
|
providers: [],
|
||||||
|
@ -11,6 +11,7 @@ export class CustomReadProcess extends PrismaReadProcess {
|
|||||||
console.log(await this.prisma.user.findMany());
|
console.log(await this.prisma.user.findMany());
|
||||||
console.log('The ID requested in path parameter', this.identityData);
|
console.log('The ID requested in path parameter', this.identityData);
|
||||||
}
|
}
|
||||||
|
|
||||||
async after(): Promise<any> {
|
async after(): Promise<any> {
|
||||||
this.customResult = {
|
this.customResult = {
|
||||||
...super.output(),
|
...super.output(),
|
Loading…
Reference in New Issue
Block a user