true-pg 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -51,13 +51,13 @@ Example config file:
51
51
 
52
52
  ## Configuration Options
53
53
 
54
- | Option | Description | Default |
55
- | ------------------ | -------------------------------------------------------- | ----------------------------- |
56
- | `uri` | PostgreSQL connection URI | Required, or connectionConfig |
57
- | `connectionConfig` | PostgreSQL connection config object | Required, or uri |
58
- | `out` | Output directory for generated files | `"models"` |
59
- | `adapters` | Adapters to use (e.g. `kysely`, `zod`) | `"kysely"` |
60
- | `defaultSchema` | Default schema to use (Kysely schema will be unprefixed) | `"public"` |
54
+ | Option | Description | Default |
55
+ | --------------- | -------------------------------------------------------- | ------------------- |
56
+ | `uri` | PostgreSQL connection URI | Required, or config |
57
+ | `config` | Knex connection config object | Required, or uri |
58
+ | `out` | Output directory for generated files | `"models"` |
59
+ | `adapters` | Adapters to use (e.g. `kysely`, `zod`) | `"kysely"` |
60
+ | `defaultSchema` | Default schema to use (Kysely schema will be unprefixed) | `"public"` |
61
61
 
62
62
  ## Customising Code Generation
63
63
 
package/lib/bin.js CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env node
2
2
  import mri from "mri";
3
- import { existsSync, readFileSync } from "fs";
4
3
  import { generate, adapters } from "./index.js";
5
4
  const args = process.argv.slice(2);
6
5
  const opts = mri(args, {
@@ -15,7 +14,12 @@ const opts = mri(args, {
15
14
  A: "all-adapters",
16
15
  },
17
16
  });
18
- const help = opts.help || (!opts.config && !opts.uri);
17
+ import { cosmiconfig } from "cosmiconfig";
18
+ const explorer = cosmiconfig("truepg");
19
+ const result = opts.config ? await explorer.load(opts.config) : await explorer.search();
20
+ const config = result?.config;
21
+ console.log(config);
22
+ const help = opts.help || (!config && !opts.uri);
19
23
  if (help) {
20
24
  // if help is triggered unintentionally, it's a user error
21
25
  const type = opts.help ? "log" : "error";
@@ -39,17 +43,6 @@ if (help) {
39
43
  else
40
44
  process.exit(1);
41
45
  }
42
- let configfile = opts.config;
43
- if (!configfile) {
44
- const candidates = [".truepgrc.json", ".config/.truepgrc.json"];
45
- for (const candidate of candidates) {
46
- if (await existsSync(candidate)) {
47
- configfile = candidate;
48
- break;
49
- }
50
- }
51
- }
52
- const config = configfile ? JSON.parse(readFileSync(configfile, "utf-8")) : {};
53
46
  if (opts["all-adapters"]) {
54
47
  opts.adapter = Object.keys(adapters);
55
48
  console.log("Enabling all built-in adapters:", opts.adapter);
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { type TruePGOpts, type createGenerator } from "./types.ts";
2
+ export { config } from "./types.ts";
2
3
  export declare const adapters: Record<string, createGenerator>;
3
- export * from "./consumer.ts";
4
4
  export declare function generate(opts: TruePGOpts, generators?: createGenerator[]): Promise<void>;
package/lib/index.js CHANGED
@@ -3,13 +3,14 @@ import { rm, mkdir, writeFile } from "fs/promises";
3
3
  import { Nodes, allowed_kind_names } from "./types.js";
4
4
  import { existsSync } from "fs";
5
5
  import { join } from "./util.js";
6
+ export { config } from "./types.js";
7
+ // import { Pool } from "pg";
6
8
  import { Kysely } from "./kysely/index.js";
7
9
  import { Zod } from "./zod/index.js";
8
10
  export const adapters = {
9
11
  kysely: Kysely,
10
12
  zod: Zod,
11
13
  };
12
- export * from "./consumer.js";
13
14
  const filter_function = (func, warnings) => {
14
15
  const typesToFilter = [
15
16
  "pg_catalog.trigger",
@@ -80,9 +81,11 @@ const multifile = async (generators, schemas, opts) => {
80
81
  const schemaDir = `${out}/${schema.name}`;
81
82
  // skip functions that cannot be represented in JavaScript
82
83
  schema.functions = schema.functions.filter(f => filter_function(f, warnings));
84
+ let createIndex = false;
83
85
  for (const kind of allowed_kind_names) {
84
86
  if (schema[kind].length < 1)
85
87
  continue;
88
+ createIndex = true;
86
89
  await mkdir(`${schemaDir}/${kind}`, { recursive: true });
87
90
  console.log(" Creating %s:\n", kind);
88
91
  for (const [i, item] of schema[kind].entries()) {
@@ -117,6 +120,8 @@ const multifile = async (generators, schemas, opts) => {
117
120
  await write(kindIndexFilename, kindIndex);
118
121
  console.log(' ✅ Created "%s" %s index: %s\n', schema.name, kind, kindIndexFilename);
119
122
  }
123
+ if (!createIndex)
124
+ continue;
120
125
  const index = join(gens.map(gen => gen.schemaIndex(schema, def_gen)));
121
126
  const indexFilename = `${out}/${schema.name}/index.ts`;
122
127
  await write(indexFilename, index);
@@ -135,10 +140,22 @@ const multifile = async (generators, schemas, opts) => {
135
140
  };
136
141
  export async function generate(opts, generators) {
137
142
  const out = opts.out || "./models";
138
- const conn = opts.uri ?? opts.connectionConfig;
139
- if (!conn)
140
- throw new Error("Either uri or connectionConfig is required. Add either option to your .truepgrc.json.");
141
- const extractor = new Extractor(conn);
143
+ // let pg;
144
+ // if (opts.pg) pg = opts.pg;
145
+ // else if (opts.uri) pg = new Pool({ connectionString: opts.uri });
146
+ // else if (opts.config) pg = new Pool(opts.config);
147
+ // else {
148
+ // console.error(
149
+ // "One of these options are required in your config file: pg, uri, config. See documentation for more information.",
150
+ // );
151
+ // process.exit(1);
152
+ // }
153
+ const config = opts.uri ?? opts.config;
154
+ if (!config) {
155
+ console.error("One of these options are required in your config file: uri, config. See documentation for more information.");
156
+ process.exit(1);
157
+ }
158
+ const extractor = new Extractor(config);
142
159
  const schemas = await extractor.extractSchemas();
143
160
  generators ??= opts.adapters.map(adapter => {
144
161
  const selected = adapters[adapter];
package/lib/types.d.ts CHANGED
@@ -67,11 +67,12 @@ export declare namespace Nodes {
67
67
  export type ConnectionConfig = Exclude<ConstructorParameters<typeof Extractor>[0], string | undefined>;
68
68
  export interface TruePGOpts {
69
69
  uri?: string;
70
- connectionConfig?: ConnectionConfig;
70
+ config?: ConnectionConfig;
71
71
  out: string;
72
72
  adapters: string[];
73
73
  defaultSchema?: string;
74
74
  }
75
+ export declare function config(opts: TruePGOpts): TruePGOpts;
75
76
  export interface CreateGeneratorOpts {
76
77
  defaultSchema?: string;
77
78
  warnings: string[];
package/lib/types.js CHANGED
@@ -1,3 +1,4 @@
1
+ // import type { ClientConfig, Pool, PoolConfig } from "pg";
1
2
  import { dirname, relative } from "node:path";
2
3
  import { join } from "./util.js";
3
4
  // To be updated when we add support for other kinds
@@ -163,5 +164,8 @@ export var Nodes;
163
164
  }
164
165
  Nodes.ImportList = ImportList;
165
166
  })(Nodes || (Nodes = {}));
167
+ export function config(opts) {
168
+ return opts;
169
+ }
166
170
  /* convenience function to create a generator with type inference */
167
171
  export const createGenerator = (generatorCreator) => generatorCreator;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "true-pg",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "module": "lib/index.js",
6
6
  "main": "lib/index.js",
@@ -29,6 +29,7 @@
29
29
  "zod": "^3"
30
30
  },
31
31
  "dependencies": {
32
+ "cosmiconfig": "^9.0.0",
32
33
  "mri": "^1.2.0",
33
34
  "pg-extract": "^0.0.3"
34
35
  }