From 241073841454bd82621473bba805defd2465f8f3 Mon Sep 17 00:00:00 2001 From: Adel Salakh Date: Tue, 26 Sep 2023 17:28:37 +0200 Subject: [PATCH] feat(cli): expose maxWorkerThreads configuration variable --- docs-new/docs/cli.md | 19 ++++++++++--------- packages/cli/src/config.ts | 5 +++++ packages/cli/src/index.ts | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/docs-new/docs/cli.md b/docs-new/docs/cli.md index e215f1b9..0ba766a1 100644 --- a/docs-new/docs/cli.md +++ b/docs-new/docs/cli.md @@ -84,24 +84,25 @@ Configuration file can be also be written in CommonJS format and default exporte ### Configuration file format -| Name | Type | Description | -|-------------------------|---------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `transforms` | `Transform[]` | An array of transforms to apply to the files. | -| `srcDir` | `string` | Directory to scan or watch for query files. | -| `db` | `DatabaseConfig` | A database config. | +| Name | Type | Description | +|-------------------------|--------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `transforms` | `Transform[]` | An array of transforms to apply to the files. | +| `srcDir` | `string` | Directory to scan or watch for query files. | +| `db` | `DatabaseConfig` | A database config. | | `failOnError?` | `boolean` | Whether to fail on a file processing error and abort generation. **Default:** `false` | | `dbUrl?` | `string` | A connection string to the database. Example: `postgres://user:password@host/database`. Overrides (merged) with `db` config. | | `camelCaseColumnNames?` | `boolean` | Whether to convert column names to camelCase. _Note that this only coverts the types. You need to do this at runtime independently using a library like `pg-camelcase`_. | | `typesOverrides?` | `Record` | A map of type overrides. Similarly to `camelCaseColumnNames`, this only affects the types. _You need to do this at runtime independently using a library like `pg-types`._ | +| `maxWorkerThreads` | `number` | The maximum number of worker threads to use for type generation. **The default is based on the number of available CPUs.** | Fields marked with `?` are optional. #### Transform -| Name | Type | Description | -|----------------|----------|---------------------------------------------------------------------------------------------------| -| `mode` | `string` | The mode to use. Can be `sql` or `ts`. | -| `include` | `string` | A glob pattern to match files to process. Example: `"**/*.sql"`. | +| Name | Type | Description | +|----------------|----------|-----------------------------------------------------------------------------------------------------------------------------------------| +| `mode` | `string` | The mode to use. Can be `sql` or `ts`. | +| `include` | `string` | A glob pattern to match files to process. Example: `"**/*.sql"`. | | `emitTemplate` | `string` | A template to use for the output file name. See [Customizing generated file paths](#customizing-generated-file-paths) for more details. | #### DatabaseConfig diff --git a/packages/cli/src/config.ts b/packages/cli/src/config.ts index 7a73eae1..fd753139 100644 --- a/packages/cli/src/config.ts +++ b/packages/cli/src/config.ts @@ -35,6 +35,8 @@ const TransformCodec = t.union([TSTransformCodec, SQLTransformCodec]); export type TransformConfig = t.TypeOf; const configParser = t.type({ + // maximum number of worker threads to use for the codegen worker pool + maxWorkerThreads: t.union([t.number, t.undefined]), transforms: t.array(TransformCodec), srcDir: t.string, failOnError: t.union([t.boolean, t.undefined]), @@ -78,6 +80,7 @@ export interface ParsedConfig { port: number; ssl?: tls.ConnectionOptions | boolean; }; + maxWorkerThreads: number | undefined; failOnError: boolean; camelCaseColumnNames: boolean; hungarianNotation: boolean; @@ -172,6 +175,7 @@ export function parseConfig( }; const { + maxWorkerThreads, db = defaultDBConfig, dbUrl: configDbUri, transforms, @@ -224,5 +228,6 @@ export function parseConfig( camelCaseColumnNames: camelCaseColumnNames ?? false, hungarianNotation: hungarianNotation ?? true, typesOverrides: parsedTypesOverrides, + maxWorkerThreads, }; } diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 3d066bd4..76e22bb2 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -29,7 +29,7 @@ class FileProcessor { constructor(private readonly config: ParsedConfig) { this.pool = new WorkerPool({ filename: new URL('./worker.js', import.meta.url).href, - maxThreads: 8, + maxThreads: config.maxWorkerThreads, workerData: config, }); console.log(`Using a pool of ${this.pool.threads.length} threads.`);