introspeql 0.0.3 → 1.0.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 (120) hide show
  1. package/README.md +286 -85
  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/default-type-mappings.d.ts +13 -0
  11. package/dist/config/default-type-mappings.js +16 -0
  12. package/dist/config/entity-data.d.ts +6 -0
  13. package/dist/config/entity-data.js +8 -0
  14. package/dist/config/function-options.d.ts +23 -0
  15. package/dist/config/function-options.js +74 -0
  16. package/dist/config/general-options.d.ts +7 -0
  17. package/dist/config/general-options.js +75 -0
  18. package/dist/config/index.d.ts +71 -0
  19. package/dist/config/index.js +29 -0
  20. package/dist/config/output-options.d.ts +8 -0
  21. package/dist/config/output-options.js +40 -0
  22. package/dist/config/table-options.d.ts +21 -0
  23. package/dist/config/table-options.js +48 -0
  24. package/dist/enums/enum-data.d.ts +9 -0
  25. package/dist/enums/enum-data.js +11 -0
  26. package/dist/enums/enum-definition.d.ts +7 -0
  27. package/dist/enums/enum-definition.js +24 -0
  28. package/dist/enums/index.d.ts +4 -0
  29. package/dist/enums/index.js +20 -0
  30. package/dist/enums/is-enum-replaced-with-custom-type.d.ts +3 -0
  31. package/dist/enums/is-enum-replaced-with-custom-type.js +9 -0
  32. package/dist/enums/read-enum-data.d.ts +3 -0
  33. package/dist/enums/read-enum-data.js +57 -0
  34. package/dist/functions/function-data.d.ts +26 -0
  35. package/dist/functions/function-data.js +27 -0
  36. package/dist/functions/function-definition.d.ts +7 -0
  37. package/dist/functions/function-definition.js +21 -0
  38. package/dist/functions/index.d.ts +9 -0
  39. package/dist/functions/index.js +25 -0
  40. package/dist/functions/overload-type-definition.d.ts +8 -0
  41. package/dist/functions/overload-type-definition.js +21 -0
  42. package/dist/functions/parameter-type-definition-builder.d.ts +16 -0
  43. package/dist/functions/parameter-type-definition-builder.js +41 -0
  44. package/dist/functions/parameter-type-definition.d.ts +9 -0
  45. package/dist/functions/parameter-type-definition.js +28 -0
  46. package/dist/functions/read-function-data.d.ts +4 -0
  47. package/dist/functions/read-function-data.js +93 -0
  48. package/dist/functions/return-type-definition.d.ts +7 -0
  49. package/dist/functions/return-type-definition.js +20 -0
  50. package/dist/functions/should-include-function.d.ts +2 -0
  51. package/dist/functions/should-include-function.js +6 -0
  52. package/dist/functions/should-include-overload.d.ts +2 -0
  53. package/dist/functions/should-include-overload.js +23 -0
  54. package/dist/index.d.ts +3 -2
  55. package/dist/index.js +7 -1
  56. package/dist/introspeql.d.ts +7 -0
  57. package/dist/introspeql.js +161 -0
  58. package/dist/schemas/index.d.ts +3 -0
  59. package/dist/schemas/index.js +19 -0
  60. package/dist/schemas/read-schema-data.d.ts +15 -0
  61. package/dist/schemas/read-schema-data.js +182 -0
  62. package/dist/schemas/schema-definition-factory.d.ts +15 -0
  63. package/dist/schemas/schema-definition-factory.js +167 -0
  64. package/dist/schemas/schema-definition.d.ts +11 -0
  65. package/dist/schemas/schema-definition.js +38 -0
  66. package/dist/shared/convert-pg-identifier-to-ts-identifier.d.ts +19 -0
  67. package/dist/shared/convert-pg-identifier-to-ts-identifier.js +57 -0
  68. package/dist/shared/directives.d.ts +12 -0
  69. package/dist/shared/directives.js +16 -0
  70. package/dist/shared/get-tokens.d.ts +1 -0
  71. package/dist/shared/get-tokens.js +7 -0
  72. package/dist/shared/indent.d.ts +1 -0
  73. package/dist/shared/indent.js +9 -0
  74. package/dist/shared/index.d.ts +5 -0
  75. package/dist/shared/index.js +21 -0
  76. package/dist/shared/parsing-error.d.ts +3 -0
  77. package/dist/shared/parsing-error.js +26 -0
  78. package/dist/tables/column-data.d.ts +14 -0
  79. package/dist/tables/column-data.js +16 -0
  80. package/dist/tables/column-definition.d.ts +8 -0
  81. package/dist/tables/column-definition.js +19 -0
  82. package/dist/tables/column-type-definition.d.ts +7 -0
  83. package/dist/tables/column-type-definition.js +19 -0
  84. package/dist/tables/index.d.ts +8 -0
  85. package/dist/tables/index.js +24 -0
  86. package/dist/tables/read-column-data.d.ts +3 -0
  87. package/dist/tables/read-column-data.js +55 -0
  88. package/dist/tables/read-table-data.d.ts +4 -0
  89. package/dist/tables/read-table-data.js +84 -0
  90. package/dist/tables/should-include-table.d.ts +10 -0
  91. package/dist/tables/should-include-table.js +24 -0
  92. package/dist/tables/table-data.d.ts +8 -0
  93. package/dist/tables/table-data.js +10 -0
  94. package/dist/tables/table-definition.d.ts +10 -0
  95. package/dist/tables/table-definition.js +43 -0
  96. package/dist/types/index.d.ts +1 -0
  97. package/dist/types/index.js +17 -0
  98. package/dist/types/lookup-type.d.ts +15 -0
  99. package/dist/types/lookup-type.js +25 -0
  100. package/package.json +33 -18
  101. package/dist/append-schema.d.ts +0 -8
  102. package/dist/append-schema.js +0 -96
  103. package/dist/generate-types.d.ts +0 -8
  104. package/dist/generate-types.js +0 -111
  105. package/dist/introspect-columns.d.ts +0 -21
  106. package/dist/introspect-columns.js +0 -53
  107. package/dist/introspect-enum.d.ts +0 -20
  108. package/dist/introspect-enum.js +0 -24
  109. package/dist/introspect-procedures.d.ts +0 -53
  110. package/dist/introspect-procedures.js +0 -129
  111. package/dist/introspect-tables.d.ts +0 -19
  112. package/dist/introspect-tables.js +0 -43
  113. package/dist/introspeql-config.d.ts +0 -40
  114. package/dist/introspeql-config.js +0 -104
  115. package/dist/prepare-data-for-writing.d.ts +0 -38
  116. package/dist/prepare-data-for-writing.js +0 -145
  117. package/dist/snake-case-to-pascal-case.d.ts +0 -8
  118. package/dist/snake-case-to-pascal-case.js +0 -17
  119. package/dist/write-header.d.ts +0 -2
  120. package/dist/write-header.js +0 -10
@@ -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
- }