introspeql 0.0.4 → 1.1.0

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.
Files changed (134) hide show
  1. package/README.md +313 -83
  2. package/dist/comments/comment-converter.d.ts +54 -0
  3. package/dist/comments/comment-converter.js +185 -0
  4. package/dist/comments/index.d.ts +2 -0
  5. package/dist/comments/index.js +18 -0
  6. package/dist/comments/prettify-comment.d.ts +1 -0
  7. package/dist/comments/prettify-comment.js +18 -0
  8. package/dist/config/connection-options.d.ts +14 -0
  9. package/dist/config/connection-options.js +42 -0
  10. package/dist/config/create-relation-options.d.ts +22 -0
  11. package/dist/config/create-relation-options.js +60 -0
  12. package/dist/config/default-type-mappings.d.ts +13 -0
  13. package/dist/config/default-type-mappings.js +16 -0
  14. package/dist/config/entity-data.d.ts +6 -0
  15. package/dist/config/entity-data.js +8 -0
  16. package/dist/config/function-options.d.ts +23 -0
  17. package/dist/config/function-options.js +74 -0
  18. package/dist/config/general-options.d.ts +7 -0
  19. package/dist/config/general-options.js +75 -0
  20. package/dist/config/index.d.ts +123 -0
  21. package/dist/config/index.js +43 -0
  22. package/dist/config/materialized-view-options.d.ts +24 -0
  23. package/dist/config/materialized-view-options.js +5 -0
  24. package/dist/config/output-options.d.ts +8 -0
  25. package/dist/config/output-options.js +40 -0
  26. package/dist/config/table-options.d.ts +24 -0
  27. package/dist/config/table-options.js +5 -0
  28. package/dist/config/view-options.d.ts +24 -0
  29. package/dist/config/view-options.js +5 -0
  30. package/dist/enums/enum-data.d.ts +9 -0
  31. package/dist/enums/enum-data.js +11 -0
  32. package/dist/enums/enum-definition.d.ts +7 -0
  33. package/dist/enums/enum-definition.js +24 -0
  34. package/dist/enums/index.d.ts +4 -0
  35. package/dist/enums/index.js +20 -0
  36. package/dist/enums/is-enum-replaced-with-custom-type.d.ts +3 -0
  37. package/dist/enums/is-enum-replaced-with-custom-type.js +9 -0
  38. package/dist/enums/read-enum-data.d.ts +3 -0
  39. package/dist/enums/read-enum-data.js +57 -0
  40. package/dist/functions/function-data.d.ts +26 -0
  41. package/dist/functions/function-data.js +27 -0
  42. package/dist/functions/function-definition.d.ts +7 -0
  43. package/dist/functions/function-definition.js +21 -0
  44. package/dist/functions/index.d.ts +9 -0
  45. package/dist/functions/index.js +25 -0
  46. package/dist/functions/overload-type-definition.d.ts +8 -0
  47. package/dist/functions/overload-type-definition.js +21 -0
  48. package/dist/functions/parameter-type-definition-builder.d.ts +16 -0
  49. package/dist/functions/parameter-type-definition-builder.js +41 -0
  50. package/dist/functions/parameter-type-definition.d.ts +9 -0
  51. package/dist/functions/parameter-type-definition.js +28 -0
  52. package/dist/functions/read-function-data.d.ts +4 -0
  53. package/dist/functions/read-function-data.js +93 -0
  54. package/dist/functions/return-type-definition.d.ts +7 -0
  55. package/dist/functions/return-type-definition.js +20 -0
  56. package/dist/functions/should-include-function.d.ts +2 -0
  57. package/dist/functions/should-include-function.js +6 -0
  58. package/dist/functions/should-include-overload.d.ts +2 -0
  59. package/dist/functions/should-include-overload.js +23 -0
  60. package/dist/index.d.ts +3 -2
  61. package/dist/index.js +7 -1
  62. package/dist/introspeql.d.ts +7 -0
  63. package/dist/introspeql.js +161 -0
  64. package/dist/relations/abstract-relation-definition.d.ts +11 -0
  65. package/dist/relations/abstract-relation-definition.js +43 -0
  66. package/dist/relations/column-data.d.ts +14 -0
  67. package/dist/relations/column-data.js +16 -0
  68. package/dist/relations/column-definition.d.ts +8 -0
  69. package/dist/relations/column-definition.js +19 -0
  70. package/dist/relations/column-type-definition.d.ts +7 -0
  71. package/dist/relations/column-type-definition.js +19 -0
  72. package/dist/relations/index.d.ts +11 -0
  73. package/dist/relations/index.js +27 -0
  74. package/dist/relations/materialized-view-definition.d.ts +4 -0
  75. package/dist/relations/materialized-view-definition.js +29 -0
  76. package/dist/relations/read-column-data.d.ts +3 -0
  77. package/dist/relations/read-column-data.js +55 -0
  78. package/dist/relations/read-relation-data.d.ts +4 -0
  79. package/dist/relations/read-relation-data.js +87 -0
  80. package/dist/relations/relation-data.d.ts +8 -0
  81. package/dist/relations/relation-data.js +10 -0
  82. package/dist/relations/should-include-relation.d.ts +10 -0
  83. package/dist/relations/should-include-relation.js +24 -0
  84. package/dist/relations/table-definition.d.ts +4 -0
  85. package/dist/relations/table-definition.js +29 -0
  86. package/dist/relations/view-definition.d.ts +4 -0
  87. package/dist/relations/view-definition.js +29 -0
  88. package/dist/schemas/index.d.ts +3 -0
  89. package/dist/schemas/index.js +19 -0
  90. package/dist/schemas/read-schema-data.d.ts +17 -0
  91. package/dist/schemas/read-schema-data.js +219 -0
  92. package/dist/schemas/schema-definition-factory.d.ts +15 -0
  93. package/dist/schemas/schema-definition-factory.js +177 -0
  94. package/dist/schemas/schema-definition.d.ts +13 -0
  95. package/dist/schemas/schema-definition.js +52 -0
  96. package/dist/shared/capitalize.d.ts +1 -0
  97. package/dist/shared/capitalize.js +6 -0
  98. package/dist/shared/convert-pg-identifier-to-ts-identifier.d.ts +19 -0
  99. package/dist/shared/convert-pg-identifier-to-ts-identifier.js +55 -0
  100. package/dist/shared/directives.d.ts +12 -0
  101. package/dist/shared/directives.js +16 -0
  102. package/dist/shared/get-tokens.d.ts +1 -0
  103. package/dist/shared/get-tokens.js +7 -0
  104. package/dist/shared/indent.d.ts +1 -0
  105. package/dist/shared/indent.js +9 -0
  106. package/dist/shared/index.d.ts +6 -0
  107. package/dist/shared/index.js +22 -0
  108. package/dist/shared/parsing-error.d.ts +3 -0
  109. package/dist/shared/parsing-error.js +26 -0
  110. package/dist/types/index.d.ts +1 -0
  111. package/dist/types/index.js +17 -0
  112. package/dist/types/lookup-type.d.ts +15 -0
  113. package/dist/types/lookup-type.js +25 -0
  114. package/package.json +32 -17
  115. package/dist/append-schema.d.ts +0 -8
  116. package/dist/append-schema.js +0 -96
  117. package/dist/generate-types.d.ts +0 -8
  118. package/dist/generate-types.js +0 -110
  119. package/dist/introspect-columns.d.ts +0 -21
  120. package/dist/introspect-columns.js +0 -53
  121. package/dist/introspect-enum.d.ts +0 -20
  122. package/dist/introspect-enum.js +0 -24
  123. package/dist/introspect-procedures.d.ts +0 -53
  124. package/dist/introspect-procedures.js +0 -129
  125. package/dist/introspect-tables.d.ts +0 -19
  126. package/dist/introspect-tables.js +0 -43
  127. package/dist/introspeql-config.d.ts +0 -40
  128. package/dist/introspeql-config.js +0 -104
  129. package/dist/prepare-data-for-writing.d.ts +0 -38
  130. package/dist/prepare-data-for-writing.js +0 -145
  131. package/dist/snake-case-to-pascal-case.d.ts +0 -8
  132. package/dist/snake-case-to-pascal-case.js +0 -17
  133. package/dist/write-header.d.ts +0 -2
  134. package/dist/write-header.js +0 -10
@@ -1,24 +0,0 @@
1
- import { z } from 'zod';
2
- /**
3
- * Reads metadata for the provided enum.
4
- *
5
- * @param client
6
- * @param data
7
- * @returns A {@link Promise}<{@link EnumData}[]>
8
- */
9
- export async function introspectEnum(client, data) {
10
- const query = `
11
- SELECT ARRAY(SELECT enumlabel FROM pg_enum WHERE enumtypid = $1)::text[] AS enum_values;
12
- `;
13
- const parameters = [data.id];
14
- const result = await client.query(query, parameters);
15
- const { enum_values: enumValues } = z
16
- .object({
17
- enum_values: z.string().array(),
18
- })
19
- .parse(result.rows[0]);
20
- return {
21
- ...data,
22
- values: enumValues,
23
- };
24
- }
@@ -1,53 +0,0 @@
1
- import { z } from 'zod';
2
- import type { Client } from 'pg';
3
- import type { ParsedIntrospeQLConfig } from './introspeql-config';
4
- declare const procedureDataSchema: z.ZodObject<{
5
- id: z.ZodNumber;
6
- schema: z.ZodString;
7
- name: z.ZodString;
8
- arg_names: z.ZodPipe<z.ZodNullable<z.ZodArray<z.ZodString>>, z.ZodTransform<string[], string[] | null>>;
9
- arg_types: z.ZodArray<z.ZodObject<{
10
- id: z.ZodCoercedNumber<unknown>;
11
- schema: z.ZodString;
12
- name: z.ZodString;
13
- is_array: z.ZodBoolean;
14
- is_enum: z.ZodBoolean;
15
- }, z.core.$strip>>;
16
- return_type: z.ZodObject<{
17
- id: z.ZodCoercedNumber<unknown>;
18
- schema: z.ZodString;
19
- name: z.ZodString;
20
- is_array: z.ZodBoolean;
21
- is_enum: z.ZodBoolean;
22
- }, z.core.$strip>;
23
- }, z.core.$strip>;
24
- export type ProcedureData = z.infer<typeof procedureDataSchema>;
25
- /**
26
- * Reads information about procedures in the provided schema(s), including their
27
- * ids, names, schema names, argument names, argument types, and return type.
28
- *
29
- * @param client
30
- * @param config
31
- * @returns A {@link Promise}<{@link ProcedureData}[]>
32
- */
33
- export declare function introspectProcedures(client: Client, config: ParsedIntrospeQLConfig): Promise<{
34
- id: number;
35
- schema: string;
36
- name: string;
37
- arg_names: string[];
38
- arg_types: {
39
- id: number;
40
- schema: string;
41
- name: string;
42
- is_array: boolean;
43
- is_enum: boolean;
44
- }[];
45
- return_type: {
46
- id: number;
47
- schema: string;
48
- name: string;
49
- is_array: boolean;
50
- is_enum: boolean;
51
- };
52
- }[]>;
53
- export {};
@@ -1,129 +0,0 @@
1
- import { z } from 'zod';
2
- const typeSchema = z.object({
3
- /** The OID of the type. */
4
- id: z.coerce.number(),
5
- /** The name of the schema in which the type was defined. */
6
- schema: z.string(),
7
- /**
8
- * The name of the type, or the name of the type of each element if the type
9
- * is an array.
10
- */
11
- name: z.string(),
12
- /** Whether or not the type is an array. */
13
- is_array: z.boolean(),
14
- /**
15
- * Whether or not the type (or the type of each element if the type is an
16
- * array) is an enum.
17
- */
18
- is_enum: z.boolean(),
19
- });
20
- const procedureDataSchema = z.object({
21
- /** The OID of the procedure. */
22
- id: z.number(),
23
- /** The name of the schema in which the procedure was defined. */
24
- schema: z.string(),
25
- /** The name of the procedure. */
26
- name: z.string(),
27
- /**
28
- * An array containing the names of the procedure's arguments. Note that this
29
- * array can be empty even for procedures with arguments if those arguments
30
- * are unnamed.
31
- */
32
- arg_names: z
33
- .string()
34
- .array()
35
- .nullable()
36
- .transform(v => (v ? v : [])),
37
- /**
38
- * An array containing type information for each of the procedure's
39
- * arguments.
40
- */
41
- arg_types: typeSchema.array(),
42
- /**
43
- * Type information for the procedure's return type.
44
- */
45
- return_type: typeSchema,
46
- });
47
- /**
48
- * Reads information about procedures in the provided schema(s), including their
49
- * ids, names, schema names, argument names, argument types, and return type.
50
- *
51
- * @param client
52
- * @param config
53
- * @returns A {@link Promise}<{@link ProcedureData}[]>
54
- */
55
- export async function introspectProcedures(client, config) {
56
- const schemaPlaceholders = config.schemas
57
- .map((_, i) => `$${i + 1}`)
58
- .join(', ');
59
- let query = `
60
- SELECT
61
- p.oid AS id,
62
- n.nspname AS schema,
63
- p.proname AS name,
64
- p.proargnames AS arg_names,
65
- ARRAY(
66
- SELECT jsonb_build_object(
67
- 'id',
68
- t.oid,
69
- 'schema',
70
- n.nspname,
71
- 'name',
72
- t.typname,
73
- 'is_array',
74
- EXISTS(SELECT 1 FROM pg_catalog.pg_type AS t WHERE t.typarray = pt.*),
75
- 'is_enum',
76
- t.typtype = 'e'
77
- ) FROM UNNEST(p.proargtypes) AS pt
78
- INNER JOIN pg_catalog.pg_type as t
79
- ON
80
- CASE
81
- WHEN EXISTS(SELECT 1 FROM pg_catalog.pg_type AS t WHERE t.typarray = pt.*)
82
- THEN pt.* = t.typarray
83
- ELSE pt.* = t.oid
84
- END
85
- INNER JOIN pg_catalog.pg_namespace AS n
86
- ON t.typnamespace = n.oid
87
- ) AS arg_types,
88
- (
89
- SELECT jsonb_build_object(
90
- 'id',
91
- t.oid,
92
- 'schema',
93
- n.nspname,
94
- 'name',
95
- t.typname,
96
- 'is_array',
97
- EXISTS(SELECT 1 FROM pg_catalog.pg_type AS t WHERE t.typarray = p.prorettype),
98
- 'is_enum',
99
- t.typtype = 'e'
100
- ) FROM pg_catalog.pg_type AS t
101
- INNER JOIN pg_catalog.pg_namespace as n
102
- ON t.typnamespace = n.oid
103
- WHERE
104
- CASE
105
- WHEN EXISTS(SELECT 1 FROM pg_catalog.pg_type AS t WHERE t.typarray = p.prorettype)
106
- THEN t.typarray = p.prorettype
107
- ELSE t.oid = p.prorettype
108
- END
109
- ) AS return_type
110
- FROM pg_catalog.pg_proc AS p
111
- INNER JOIN pg_catalog.pg_namespace AS n ON p.pronamespace = n.oid
112
- WHERE n.nspname IN (${schemaPlaceholders})
113
- `;
114
- const parameters = [...config.schemas];
115
- if (config.ignoreProcedures.length > 0) {
116
- let offset = schemaPlaceholders.length;
117
- const filters = [];
118
- for (const procedureToIgnore of config.ignoreProcedures) {
119
- filters.push(`(n.nspname = $${offset++} AND p.proname = $${offset++})`);
120
- parameters.push(procedureToIgnore.schema);
121
- parameters.push(procedureToIgnore.name);
122
- }
123
- query += `AND NOT (${filters.join(' OR\n')})`;
124
- }
125
- query += ';';
126
- const result = await client.query(query, parameters);
127
- const procedureData = procedureDataSchema.array().parse(result.rows);
128
- return procedureData;
129
- }
@@ -1,19 +0,0 @@
1
- import { z } from 'zod';
2
- import type { Client } from 'pg';
3
- import type { ParsedIntrospeQLConfig } from './introspeql-config';
4
- declare const tableDataSchema: z.ZodObject<{
5
- id: z.ZodNumber;
6
- schema: z.ZodString;
7
- name: z.ZodString;
8
- }, z.core.$strip>;
9
- export type TableData = z.infer<typeof tableDataSchema>;
10
- /**
11
- * Returns information about tables in the provided schema(s), including their
12
- * ids, names, and schema names.
13
- *
14
- * @param client
15
- * @param config
16
- * @returns A {@link Promise}<{@link TableData}[]>
17
- */
18
- export declare function introspectTables(client: Client, config: ParsedIntrospeQLConfig): Promise<TableData[]>;
19
- export {};
@@ -1,43 +0,0 @@
1
- import { z } from 'zod';
2
- const tableDataSchema = z.object({
3
- /** The OID of the table. */
4
- id: z.number(),
5
- /** The name of the schema containing the table. */
6
- schema: z.string(),
7
- /** The name of the table. */
8
- name: z.string(),
9
- });
10
- /**
11
- * Returns information about tables in the provided schema(s), including their
12
- * ids, names, and schema names.
13
- *
14
- * @param client
15
- * @param config
16
- * @returns A {@link Promise}<{@link TableData}[]>
17
- */
18
- export async function introspectTables(client, config) {
19
- const schemaPlaceholders = config.schemas
20
- .map((_, i) => `$${i + 1}`)
21
- .join(', ');
22
- let query = `
23
- SELECT c.oid AS id, n.nspname AS schema, c.relname AS name
24
- FROM pg_catalog.pg_class AS c
25
- INNER JOIN pg_catalog.pg_namespace AS n ON c.relnamespace = n.oid
26
- WHERE c.relkind = 'r' AND n.nspname IN (${schemaPlaceholders})
27
- `;
28
- const parameters = [...config.schemas];
29
- if (config.ignoreTables.length > 0) {
30
- let offset = schemaPlaceholders.length;
31
- const filters = [];
32
- for (const tableToIgnore of config.ignoreTables) {
33
- filters.push(`(n.nspname = $${offset++} AND c.relname = $${offset++})`);
34
- parameters.push(tableToIgnore.schema);
35
- parameters.push(tableToIgnore.name);
36
- }
37
- query += `AND NOT (${filters.join(' OR\n')})`;
38
- }
39
- query += ';';
40
- const result = await client.query(query, parameters);
41
- const tableData = tableDataSchema.array().parse(result.rows);
42
- return tableData;
43
- }
@@ -1,40 +0,0 @@
1
- import { z } from 'zod';
2
- declare const introspeqlConfig: z.ZodIntersection<z.ZodUnion<readonly [z.ZodObject<{
3
- dbConnectionString: z.ZodString;
4
- }, z.core.$strip>, z.ZodObject<{
5
- dbConnectionParams: z.ZodObject<{
6
- user: z.ZodOptional<z.ZodString>;
7
- password: z.ZodOptional<z.ZodString>;
8
- host: z.ZodOptional<z.ZodString>;
9
- port: z.ZodOptional<z.ZodNumber>;
10
- database: z.ZodOptional<z.ZodString>;
11
- }, z.core.$strip>;
12
- }, z.core.$strip>]>, z.ZodObject<{
13
- outFile: z.ZodString;
14
- schemas: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodString>>>;
15
- header: z.ZodOptional<z.ZodString>;
16
- types: z.ZodPipe<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>, z.ZodTransform<{
17
- 'pg_catalog.bool': string;
18
- 'pg_catalog.int2': string;
19
- 'pg_catalog.int4': string;
20
- 'pg_catalog.float4': string;
21
- 'pg_catalog.float8': string;
22
- 'pg_catalog.json': string;
23
- 'pg_catalog.jsonb': string;
24
- 'pg_catalog.date': string;
25
- 'pg_catalog.timestamp': string;
26
- 'pg_catalog.timestamptz': string;
27
- 'pg_catalog.void': string;
28
- }, Record<string, string> | undefined>>;
29
- ignoreTables: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodObject<{
30
- schema: z.ZodString;
31
- name: z.ZodString;
32
- }, z.core.$strip>>>>;
33
- ignoreProcedures: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodObject<{
34
- schema: z.ZodString;
35
- name: z.ZodString;
36
- }, z.core.$strip>>>>;
37
- }, z.core.$strip>>;
38
- type IntrospeQLConfig = z.input<typeof introspeqlConfig>;
39
- type ParsedIntrospeQLConfig = z.infer<typeof introspeqlConfig>;
40
- export { introspeqlConfig, type IntrospeQLConfig, type ParsedIntrospeQLConfig };
@@ -1,104 +0,0 @@
1
- import { z } from 'zod';
2
- const databaseConnectionConfig = z.union([
3
- /**
4
- * A PostgreSQL connection string specifying the database to connect to.
5
- * Useful if you are already storing such a string as an environment variable.
6
- */
7
- z.object({
8
- dbConnectionString: z.string(),
9
- }),
10
- /**
11
- * An object containing the components of a PostgreSQL connection string that
12
- * together specify the database to connect to. Useful if you are already
13
- * storing such values as individual environment variables.
14
- */
15
- z.object({
16
- dbConnectionParams: z.object({
17
- user: z.string().optional(),
18
- password: z.string().optional(),
19
- host: z.string().optional(),
20
- port: z.number().optional(),
21
- database: z.string().optional(),
22
- }),
23
- }),
24
- ]);
25
- const entityData = z.object({
26
- schema: z.string(),
27
- name: z.string(),
28
- });
29
- const otherConfig = z.object({
30
- /**
31
- * The file to which TypeScript types will be written.
32
- */
33
- outFile: z.string(),
34
- /**
35
- * The database schemas from which types should be generated.
36
- */
37
- schemas: z
38
- .string()
39
- .array()
40
- .nonempty({
41
- message: 'Please provide at least one schema.',
42
- })
43
- .optional()
44
- .default(() => ['public']),
45
- /**
46
- * An optional header that will be written to the generated type definition
47
- * file before the types. Useful when you want to define or import types
48
- * directly in the generated file.
49
- */
50
- header: z.string().optional(),
51
- /**
52
- * Custom type mappings. Default settings align with transformations applied
53
- * by node-postgres. To override these settings for a type, provide the
54
- * name of the schema in which the type is defined and the name of the type
55
- * (separated by a dot) as the key, and a string representation of the
56
- * desired type as the value.
57
- *
58
- * @example
59
- * ```
60
- * {
61
- * types: {
62
- * "pg_catalog.int8" : "bigint"
63
- * }
64
- * }
65
- * ```
66
- */
67
- types: z
68
- .record(z.string(), z.string())
69
- .optional()
70
- .transform(t => ({
71
- 'pg_catalog.bool': 'boolean',
72
- 'pg_catalog.int2': 'number',
73
- 'pg_catalog.int4': 'number',
74
- 'pg_catalog.float4': 'number',
75
- 'pg_catalog.float8': 'number',
76
- 'pg_catalog.json': 'object',
77
- 'pg_catalog.jsonb': 'object',
78
- 'pg_catalog.date': 'Date',
79
- 'pg_catalog.timestamp': 'Date',
80
- 'pg_catalog.timestamptz': 'Date',
81
- 'pg_catalog.void': 'void',
82
- ...t,
83
- })),
84
- /**
85
- * An array of tables that should NOT be included in the generated type
86
- * definitions. Useful for ignoring tables such as `spatial_ref_sys`
87
- * when using PostGIS, or tables containing information about
88
- * migrations.
89
- */
90
- ignoreTables: entityData
91
- .array()
92
- .optional()
93
- .default(() => []),
94
- /**
95
- * An array of procedures that should NOT be included in the generated type
96
- * definitions.
97
- */
98
- ignoreProcedures: entityData
99
- .array()
100
- .optional()
101
- .default(() => []),
102
- });
103
- const introspeqlConfig = z.intersection(databaseConnectionConfig, otherConfig);
104
- export { introspeqlConfig };
@@ -1,38 +0,0 @@
1
- import type { EnumData } from './introspect-enum';
2
- import type { TableData } from './introspect-tables';
3
- import type { ColumnData } from './introspect-columns';
4
- import type { ProcedureData } from './introspect-procedures';
5
- import type { ParsedIntrospeQLConfig } from './introspeql-config';
6
- export interface Schema {
7
- formattedName: string;
8
- rawName: string;
9
- enums: Array<{
10
- name: string;
11
- values: string[];
12
- }>;
13
- tables: Array<{
14
- formattedName: string;
15
- rawName: string;
16
- columnNames: Record<string, string>;
17
- rowType: Record<string, string>;
18
- }>;
19
- procedures: Array<{
20
- formattedName: string;
21
- rawName: string;
22
- argNames: string[];
23
- argTypes: string[];
24
- returnType: string;
25
- }>;
26
- }
27
- /**
28
- * Organizes data by schema, table, and procedure, and formats it in
29
- * preparation for type generation.
30
- *
31
- * @param enumDataObjects
32
- * @param tableDataObjects
33
- * @param columnDataObjectsByTableId
34
- * @param procedureDataObjects
35
- * @param config
36
- * @returns An array of {@link Schema} objects.
37
- */
38
- export declare function prepareDataForWriting(enumDataObjects: EnumData[], tableDataObjects: TableData[], columnDataObjectsByTableId: Record<number, ColumnData[]>, procedureDataObjects: ProcedureData[], config: ParsedIntrospeQLConfig): Schema[];
@@ -1,145 +0,0 @@
1
- import { snakeCaseToPascalCase } from './snake-case-to-pascal-case';
2
- /**
3
- * Organizes data by schema, table, and procedure, and formats it in
4
- * preparation for type generation.
5
- *
6
- * @param enumDataObjects
7
- * @param tableDataObjects
8
- * @param columnDataObjectsByTableId
9
- * @param procedureDataObjects
10
- * @param config
11
- * @returns An array of {@link Schema} objects.
12
- */
13
- export function prepareDataForWriting(enumDataObjects, tableDataObjects, columnDataObjectsByTableId, procedureDataObjects, config) {
14
- const schemas = [];
15
- prepareEnums(enumDataObjects, schemas, config);
16
- prepareTables(tableDataObjects, columnDataObjectsByTableId, schemas, config);
17
- prepareProcedures(procedureDataObjects, schemas, config);
18
- return schemas;
19
- }
20
- function prepareEnums(enumDataObjects, schemas, config) {
21
- for (const enumDataObj of enumDataObjects) {
22
- // If the enum is overridden by configuration options, skip it.
23
- const typeMappingKey = `${enumDataObj.schema}.${enumDataObj.name}`;
24
- if (typeMappingKey in config.types)
25
- continue;
26
- const schema = findOrInsertSchema(schemas, enumDataObj.schema);
27
- const formattedEnumName = snakeCaseToPascalCase(enumDataObj.name);
28
- schema.enums.push({
29
- name: formattedEnumName,
30
- values: enumDataObj.values,
31
- });
32
- }
33
- }
34
- function prepareTables(tableDataObjects, columnDataObjectsByTableId, schemas, config) {
35
- for (const tableDataObj of tableDataObjects) {
36
- const schema = findOrInsertSchema(schemas, tableDataObj.schema);
37
- const tableData = {
38
- formattedName: snakeCaseToPascalCase(tableDataObj.name),
39
- rawName: tableDataObj.name,
40
- columnNames: columnDataObjectsByTableId[tableDataObj.id].reduce((acc, current) => {
41
- const formattedColumnName = snakeCaseToPascalCase(current.column_name);
42
- acc[formattedColumnName] = current.column_name;
43
- return acc;
44
- }, {}),
45
- rowType: columnDataObjectsByTableId[tableDataObj.id].reduce((acc, current) => {
46
- let type;
47
- const typeMappingKey = `${current.column_type_schema}.${current.column_type}`;
48
- if (typeMappingKey in config.types) {
49
- type = config.types[typeMappingKey];
50
- }
51
- else if (current.is_enum) {
52
- const formattedEnumName = snakeCaseToPascalCase(current.column_type);
53
- type = `Enums.${formattedEnumName}`;
54
- if (current.column_type_schema !== schema.rawName) {
55
- const formattedEnumSchemaName = snakeCaseToPascalCase(current.column_type_schema);
56
- type = `${formattedEnumSchemaName}.${type}`;
57
- }
58
- }
59
- else {
60
- type = 'string';
61
- }
62
- type += '[]'.repeat(current.num_dimensions);
63
- if (current.nullable) {
64
- type += ' | null';
65
- }
66
- acc[current.column_name] = type;
67
- return acc;
68
- }, {}),
69
- };
70
- schema.tables.push(tableData);
71
- }
72
- }
73
- function prepareProcedures(procedureDataObjects, schemas, config) {
74
- for (const procDataObj of procedureDataObjects) {
75
- const schema = findOrInsertSchema(schemas, procDataObj.schema);
76
- const extantProcedureDeclarations = schema.procedures.filter(p => p.rawName === procDataObj.name).length;
77
- const procData = {
78
- formattedName: snakeCaseToPascalCase(procDataObj.name) +
79
- (extantProcedureDeclarations > 0 ?
80
- '_' + extantProcedureDeclarations
81
- : ''),
82
- rawName: procDataObj.name,
83
- argNames: procDataObj.arg_names,
84
- argTypes: procDataObj.arg_types.map(a => {
85
- let type;
86
- const typeMappingKey = `${a.schema}.${a.name}`;
87
- if (typeMappingKey in config.types) {
88
- type = config.types[typeMappingKey];
89
- }
90
- else if (a.is_enum) {
91
- const formattedEnumName = snakeCaseToPascalCase(a.name);
92
- type = `Enums.${formattedEnumName}`;
93
- if (a.schema !== schema.rawName) {
94
- const formattedEnumSchemaName = snakeCaseToPascalCase(a.schema);
95
- type = `${formattedEnumSchemaName}.${type}`;
96
- }
97
- }
98
- else {
99
- type = 'string';
100
- }
101
- if (a.is_array) {
102
- type += '[]';
103
- }
104
- return type;
105
- }),
106
- returnType: (() => {
107
- let type;
108
- const typeMappingKey = `${procDataObj.return_type.schema}.${procDataObj.return_type.name}`;
109
- if (typeMappingKey in config.types) {
110
- type = config.types[typeMappingKey];
111
- }
112
- else if (procDataObj.return_type.is_enum) {
113
- const formattedEnumName = snakeCaseToPascalCase(procDataObj.return_type.name);
114
- type = `Enums.${formattedEnumName}`;
115
- if (procDataObj.return_type.schema !== schema.rawName) {
116
- const formattedEnumSchemaName = snakeCaseToPascalCase(procDataObj.return_type.schema);
117
- type = `${formattedEnumSchemaName}.${type}`;
118
- }
119
- }
120
- else {
121
- type = 'string';
122
- }
123
- if (procDataObj.return_type.is_array) {
124
- type += '[]';
125
- }
126
- return type;
127
- })(),
128
- };
129
- schema.procedures.push(procData);
130
- }
131
- }
132
- function findOrInsertSchema(schemas, rawName) {
133
- let schema = schemas.find(s => s.rawName === rawName);
134
- if (schema)
135
- return schema;
136
- schema = {
137
- formattedName: snakeCaseToPascalCase(rawName),
138
- rawName,
139
- enums: [],
140
- tables: [],
141
- procedures: [],
142
- };
143
- schemas.push(schema);
144
- return schema;
145
- }
@@ -1,8 +0,0 @@
1
- /**
2
- * Converts snake_case to PascalCase, preserving one leading underscore, if
3
- * present.
4
- *
5
- * @param str - A snake_case string.
6
- * @returns A PascalCase string.
7
- */
8
- export declare function snakeCaseToPascalCase(str: string): string;
@@ -1,17 +0,0 @@
1
- /**
2
- * Converts snake_case to PascalCase, preserving one leading underscore, if
3
- * present.
4
- *
5
- * @param str - A snake_case string.
6
- * @returns A PascalCase string.
7
- */
8
- export function snakeCaseToPascalCase(str) {
9
- let formatted = str
10
- .split('_')
11
- .map(s => s.slice(0, 1).toUpperCase() + s.slice(1))
12
- .join('');
13
- if (str.startsWith('_')) {
14
- formatted = '_' + formatted;
15
- }
16
- return formatted;
17
- }
@@ -1,2 +0,0 @@
1
- import type { ParsedIntrospeQLConfig } from './introspeql-config';
2
- export declare function writeHeader(outputPath: string, config: ParsedIntrospeQLConfig): void;
@@ -1,10 +0,0 @@
1
- import fs from 'node:fs';
2
- export function writeHeader(outputPath, config) {
3
- const comment = `/* This file was generated by IntrospeQL. Do not edit manually. */\n\n`;
4
- const header = config.header ?
5
- comment +
6
- config.header +
7
- '\n'.repeat(config.header.endsWith('\n') ? 1 : 2)
8
- : comment;
9
- fs.writeFileSync(outputPath, header, 'utf-8');
10
- }