Skip to content

Commit

Permalink
Merge pull request #7534 from ever-co/fix/knex-orm-support
Browse files Browse the repository at this point in the history
[Fix] Knex ORM Module Support
  • Loading branch information
evereq authored Feb 15, 2024
2 parents 1f192b3 + f828020 commit 2d4e7ed
Show file tree
Hide file tree
Showing 22 changed files with 508 additions and 245 deletions.
3 changes: 3 additions & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"compile": "tsc -p tsconfig.build.json"
},
"dependencies": {
"@mikro-orm/nestjs": "^5.2.3",
"@nestjs/common": "^10.3.0",
"@nestjs/typeorm": "^10.0.1",
"apollo-server-core": "^3.10.1",
"slugify": "^1.6.5"
},
Expand Down
7 changes: 2 additions & 5 deletions packages/config/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ let mikroOrmConnectionConfig: MikroOrmModuleOptions;
const dbPoolSize = process.env.DB_POOL_SIZE ? parseInt(process.env.DB_POOL_SIZE) : 80;
const dbConnectionTimeout = process.env.DB_CONNECTION_TIMEOUT ? parseInt(process.env.DB_CONNECTION_TIMEOUT) : 5000; // 5 seconds default
const idleTimeoutMillis = process.env.DB_IDLE_TIMEOUT ? parseInt(process.env.DB_IDLE_TIMEOUT) : 10000; // 10 seconds
const dbSlowQueryLoggingTimeout = process.env.DB_SLOW_QUERY_LOGGING_TIMEOUT
? parseInt(process.env.DB_SLOW_QUERY_LOGGING_TIMEOUT)
: 10000; // 10 seconds default
const dbSlowQueryLoggingTimeout = process.env.DB_SLOW_QUERY_LOGGING_TIMEOUT ? parseInt(process.env.DB_SLOW_QUERY_LOGGING_TIMEOUT) : 10000; // 10 seconds default

console.log('DB Pool Size: ' + dbPoolSize);
console.log('DB Connection Timeout: ' + dbConnectionTimeout);
Expand Down Expand Up @@ -209,8 +207,7 @@ switch (dbType) {
break;

case DatabaseTypeEnum.betterSqlite3:
const betterSqlitePath =
process.env.DB_PATH || path.join(process.cwd(), ...['apps', 'api', 'data'], 'gauzy.sqlite3');
const betterSqlitePath = process.env.DB_PATH || path.join(process.cwd(), ...['apps', 'api', 'data'], 'gauzy.sqlite3');
console.log('Better Sqlite DB Path: ' + betterSqlitePath);

// MikroORM DB Config
Expand Down
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
"jimp": "^0.22.7",
"jsonwebtoken": "^9.0.0",
"kafkajs": "^1.14.0",
"knex": "^2.4.0",
"mjml": "^4.14.1",
"mkdirp": "^3.0.1",
"moment": "^2.29.4",
Expand All @@ -151,6 +152,7 @@
"multer-storage-cloudinary": "^4.0.0",
"mysql2": "^3.6.5",
"nats": "^2.6.1",
"nest-knexjs": "^0.0.21",
"nestjs-i18n": "^10.4.0",
"node-fetch": "^2.6.7",
"nodemailer": "^6.4.11",
Expand Down
15 changes: 13 additions & 2 deletions packages/core/src/core/crud/crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,25 @@ export abstract class CrudService<T extends BaseEntity> implements ICrudService<
) { }

/**
* Alias (default we used table name) for pagination crud
* Get the table name from the repository metadata.
* @returns {string} The table name.
*/
protected get tableName(): string {
return this.repository.metadata.tableName;
}

/**
* Get the alias for pagination CRUD operations.
* By default, it uses the table name.
* @returns {string} The alias.
*/
protected get alias(): string {
return this.repository.metadata.tableName;
}

/**
* Returns the ORM type.
* Get the ORM type.
* @returns {MultiORM} The ORM type.
*/
protected get ormType(): MultiORM {
return ormType;
Expand Down
32 changes: 32 additions & 0 deletions packages/core/src/database/database.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Global, Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { KnexModule } from 'nest-knexjs';
import { ConfigModule, ConfigService } from '@gauzy/config';

/**
Expand Down Expand Up @@ -38,6 +39,37 @@ import { ConfigModule, ConfigService } from '@gauzy/config';
const { dbConnectionOptions } = configService.config;
return dbConnectionOptions;
}
}),
/**
* Configure the Knex.js module for the application using asynchronous options.
*/
KnexModule.forRootAsync({
useFactory: () => ({
config: {
client: 'pg', // Database client (PostgreSQL in this case)
useNullAsDefault: true, // Specify whether to use null as the default for unspecified fields
connection: {
host: process.env.DB_HOST || 'localhost', // Database host (default: localhost)
port: process.env.DB_PORT ? parseInt(process.env.DB_PORT, 10) : 5432, // Database port (default: 5432)
database: process.env.DB_NAME || 'postgres', // Database name (default: postgres)
user: process.env.DB_USER || 'postgres', // Database username (default: postgres)
password: process.env.DB_PASS || 'root', // Database password (default: root)
},
// Connection pool settings
pool: {
min: 10, // Minimum number of connections in the pool
max: process.env.DB_POOL_SIZE ? parseInt(process.env.DB_POOL_SIZE) : 80, // Maximum number of connections in the pool
// Number of milliseconds a client must sit idle in the pool
// before it is disconnected from the backend and discarded
idleTimeoutMillis: process.env.DB_IDLE_TIMEOUT ? parseInt(process.env.DB_IDLE_TIMEOUT) : 10000, // 10 seconds
// Connection timeout - number of milliseconds to wait before timing out
// when connecting a new client
acquireTimeoutMillis: process.env.DB_CONNECTION_TIMEOUT ? parseInt(process.env.DB_CONNECTION_TIMEOUT) : 5000, // 5 seconds default
},
},
retryAttempts: 100, // Number of retry attempts in case of connection failures
retryDelay: 3000, // Delay between retry attempts in milliseconds
}),
})
],
exports: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import { TenantIssueTypeBulkCreateCommand } from '../tenant-issue-type-bulk-crea
import { IssueTypeService } from '../../issue-type.service';

@CommandHandler(TenantIssueTypeBulkCreateCommand)
export class TenantIssueTypeBulkCreateHandler
implements ICommandHandler<TenantIssueTypeBulkCreateCommand>
{
constructor(private readonly issueTypeService: IssueTypeService) {}
export class TenantIssueTypeBulkCreateHandler implements ICommandHandler<TenantIssueTypeBulkCreateCommand> {

constructor(
private readonly issueTypeService: IssueTypeService
) { }

/**
*
* @param command
* @returns
*/
public async execute(
command: TenantIssueTypeBulkCreateCommand
): Promise<IIssueType[]> {
const { tenants } = command;

// Create issue types of the tenant.
return await this.issueTypeService.bulkCreateTenantsIssueTypes(tenants);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ export class IssueTypeController extends CrudFactory<
async findAllIssueTypes(
@Query() params: IssueTypeQuerDTO
): Promise<IPagination<IIssueType>> {
return await this.issueTypeService.findAllIssueTypes(params);
return await this.issueTypeService.fetchAll(params);
}
}
Loading

0 comments on commit 2d4e7ed

Please sign in to comment.