Skip to content

Commit a9bf1eb

Browse files
committed
Improve options
1 parent 268311d commit a9bf1eb

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DynamicModule, Module, Provider } from '@nestjs/common';
2+
import { WorkerUtilsOptions } from 'graphile-worker';
23
import { GraphileWorkerService } from './graphile-worker.service';
34

45
export const GRAPHILE_WORKER_TOKEN = Symbol.for('NestJsGraphileWorker');
@@ -8,16 +9,24 @@ export const GRAPHILE_WORKER_TOKEN = Symbol.for('NestJsGraphileWorker');
89
exports: [GraphileWorkerService],
910
})
1011
export class GraphileWorkerModule {
11-
/**
12-
*
13-
* @param connectionString `${env.TYPEORM_CONNECTION}://${env.TYPEORM_USERNAME}:${env.TYPEORM_PASSWORD}@${env.TYPEORM_HOST}/${env.TYPEORM_DATABASE}`
14-
}
15-
* @returns
16-
*/
17-
static forRoot(connectionString: string): DynamicModule {
12+
static forRoot(options: WorkerUtilsOptions): DynamicModule;
13+
static forRoot(connectionString: string): DynamicModule;
14+
static forRoot(connectionStringOrOptions: unknown): DynamicModule {
15+
let options: WorkerUtilsOptions = {};
16+
17+
if (typeof connectionStringOrOptions === 'string') {
18+
options.connectionString = connectionStringOrOptions;
19+
} else if (isWorkerUtilsOptions(connectionStringOrOptions)) {
20+
options = connectionStringOrOptions;
21+
} else {
22+
throw Error(
23+
'Cannot detect type of option provided for `GraphileWorkerModule.forRoot()`',
24+
);
25+
}
26+
1827
const graphileWorkerService: Provider = {
1928
provide: GraphileWorkerService,
20-
useValue: new GraphileWorkerService(connectionString),
29+
useValue: new GraphileWorkerService(options),
2130
};
2231

2332
return {
@@ -28,3 +37,7 @@ export class GraphileWorkerModule {
2837
};
2938
}
3039
}
40+
41+
function isWorkerUtilsOptions(options: unknown): options is WorkerUtilsOptions {
42+
return (options as WorkerUtilsOptions).connectionString !== undefined;
43+
}

libs/graphile-worker/src/graphile-worker.service.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,16 @@ export class GraphileWorkerService {
1212
private readonly logger = new Logger(GraphileWorkerService.name);
1313
private isMigrationDone: boolean;
1414

15-
constructor(private readonly connectionString: string) {}
15+
constructor(private readonly options: WorkerUtilsOptions) {}
1616

1717
async quickAddJob(
18-
options: WorkerUtilsOptions,
1918
identifier: string,
2019
payload?: unknown,
2120
spec?: TaskSpec,
2221
): Promise<Job> {
2322
await this.runMigrations();
2423

25-
const job = await quickAddJob(
26-
{
27-
connectionString: this.connectionString,
28-
...options,
29-
},
30-
identifier,
31-
payload,
32-
spec,
33-
);
24+
const job = await quickAddJob(this.options, identifier, payload, spec);
3425

3526
this.logger.debug(`quickAddJob add job #${job.id}`);
3627

@@ -42,7 +33,7 @@ export class GraphileWorkerService {
4233
return;
4334
}
4435

45-
await runMigrations({ connectionString: this.connectionString });
36+
await runMigrations(this.options);
4637
this.logger.debug('Run migrations');
4738
this.isMigrationDone = true;
4839
}

src/app.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ export class AppController {
88
@Post()
99
@HttpCode(201)
1010
async addJob() {
11-
await this.graphileWorker.quickAddJob({}, 'test', { hello: 'world' });
11+
await this.graphileWorker.quickAddJob('test', { hello: 'world' });
1212
}
1313
}

src/app.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { AppController } from './app.controller';
44

55
@Module({
66
imports: [
7-
GraphileWorkerModule.forRoot(
8-
'postgres://example:password@postgres/example',
9-
),
7+
GraphileWorkerModule.forRoot({
8+
connectionString: 'postgres://example:password@postgres/example',
9+
}),
1010
],
1111
controllers: [AppController],
1212
providers: [],

0 commit comments

Comments
 (0)