Skip to content

Commit

Permalink
feat(cli): expose maxWorkerThreads configuration variable
Browse files Browse the repository at this point in the history
  • Loading branch information
adelsz committed Sep 26, 2023
1 parent 263bd61 commit 2410738
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
19 changes: 10 additions & 9 deletions docs-new/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>` | 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
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const TransformCodec = t.union([TSTransformCodec, SQLTransformCodec]);
export type TransformConfig = t.TypeOf<typeof TransformCodec>;

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]),
Expand Down Expand Up @@ -78,6 +80,7 @@ export interface ParsedConfig {
port: number;
ssl?: tls.ConnectionOptions | boolean;
};
maxWorkerThreads: number | undefined;
failOnError: boolean;
camelCaseColumnNames: boolean;
hungarianNotation: boolean;
Expand Down Expand Up @@ -172,6 +175,7 @@ export function parseConfig(
};

const {
maxWorkerThreads,
db = defaultDBConfig,
dbUrl: configDbUri,
transforms,
Expand Down Expand Up @@ -224,5 +228,6 @@ export function parseConfig(
camelCaseColumnNames: camelCaseColumnNames ?? false,
hungarianNotation: hungarianNotation ?? true,
typesOverrides: parsedTypesOverrides,
maxWorkerThreads,
};
}
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand Down

0 comments on commit 2410738

Please sign in to comment.