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
package/package.json CHANGED
@@ -1,37 +1,52 @@
1
1
  {
2
2
  "name": "introspeql",
3
- "version": "0.0.3",
4
- "description": "Easy Postgres to TypeScript type generation.",
5
- "exports": {
6
- ".": "./dist/index.js"
7
- },
8
- "types": "./dist/index.d.ts",
9
- "scripts": {
10
- "format": "npx prettier . --write",
11
- "test": "echo \"Error: no test specified\" && exit 1",
12
- "build": "tsc"
13
- },
3
+ "version": "1.0.0",
4
+ "description": "IntrospeQL reads information about the schemas, tables, columns, functions, and enums in your PostgreSQL database and produces a TypeScript file detailing type information for each database object.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
14
7
  "files": [
15
8
  "dist",
16
9
  "README.md"
17
10
  ],
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "format": "npx prettier . --write",
14
+ "lint": "npx eslint --fix",
15
+ "test": "vitest --no-watch"
16
+ },
18
17
  "keywords": [
19
18
  "PostgreSQL",
20
19
  "Postgres",
21
20
  "TypeScript"
22
21
  ],
23
22
  "author": "Joseph Dvorak",
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "https://github.com/dvorakjt/introspeql"
26
+ },
24
27
  "license": "MIT",
25
- "type": "module",
28
+ "type": "commonjs",
26
29
  "devDependencies": {
27
- "@types/node": "^24.5.2",
28
- "prettier": "^3.6.2",
29
- "tsx": "^4.20.6",
30
- "typescript": "^5.9.2"
30
+ "@eslint/js": "^9.39.2",
31
+ "@eslint/json": "^0.14.0",
32
+ "@eslint/markdown": "^7.5.1",
33
+ "@faker-js/faker": "^10.1.0",
34
+ "@types/node": "^24.7.0",
35
+ "@types/pg": "^8.15.5",
36
+ "dotenv": "^17.2.3",
37
+ "eslint": "^9.39.2",
38
+ "globals": "^16.5.0",
39
+ "nanoid": "^5.1.6",
40
+ "pgmock": "^1.0.3",
41
+ "prettier": "^3.7.4",
42
+ "typescript": "^5.9.3",
43
+ "typescript-eslint": "^8.51.0",
44
+ "vitest": "^3.2.4"
31
45
  },
32
46
  "dependencies": {
33
- "@types/pg": "^8.15.5",
47
+ "@prettier/sync": "^0.6.1",
34
48
  "pg": "^8.16.3",
35
- "zod": "^4.1.11"
49
+ "prettier-plugin-jsdoc": "^1.8.0",
50
+ "zod": "^4.1.12"
36
51
  }
37
52
  }
@@ -1,8 +0,0 @@
1
- import type { Schema } from './prepare-data-for-writing';
2
- /**
3
- * Appends a namespace representing the given schema to the provided output file.
4
- *
5
- * @param outputPath
6
- * @param schema
7
- */
8
- export declare function appendSchema(outputPath: string, schema: Schema): void;
@@ -1,96 +0,0 @@
1
- import fs from 'node:fs';
2
- /**
3
- * Appends a namespace representing the given schema to the provided output file.
4
- *
5
- * @param outputPath
6
- * @param schema
7
- */
8
- export function appendSchema(outputPath, schema) {
9
- let schemaTypeDefinitions = `export namespace ${schema.formattedName} {\n`;
10
- schemaTypeDefinitions += ` export const SchemaName = '${schema.rawName}';\n`;
11
- if (schema.enums.length > 0) {
12
- schemaTypeDefinitions = appendEnums(schemaTypeDefinitions, schema);
13
- }
14
- if (schema.tables.length > 0) {
15
- schemaTypeDefinitions = appendTables(schemaTypeDefinitions, schema);
16
- }
17
- if (schema.procedures.length > 0) {
18
- schemaTypeDefinitions = appendProcedures(schemaTypeDefinitions, schema);
19
- }
20
- schemaTypeDefinitions += `};`;
21
- fs.appendFileSync(outputPath, schemaTypeDefinitions, 'utf-8');
22
- }
23
- function appendEnums(typeDefs, schema) {
24
- typeDefs += '\n' + ' '.repeat(2) + 'export namespace Enums {';
25
- for (const e of schema.enums) {
26
- typeDefs += '\n' + ' '.repeat(4) + `export enum ${e.name} {\n`;
27
- typeDefs += e.values.map(v => ' '.repeat(6) + `${v} = '${v}',\n`).join('');
28
- typeDefs += ' '.repeat(4) + '};\n';
29
- }
30
- typeDefs += ' '.repeat(2) + `};\n`;
31
- return typeDefs;
32
- }
33
- function appendTables(typeDefs, schema) {
34
- typeDefs += '\n' + ' '.repeat(2) + 'export namespace Tables {';
35
- for (const t of schema.tables) {
36
- typeDefs +=
37
- '\n' + ' '.repeat(4) + `export namespace ${t.formattedName} {\n`;
38
- typeDefs += ' '.repeat(6) + `export const TableName = '${t.rawName}';\n`;
39
- if (Object.entries(t.columnNames).length > 0) {
40
- typeDefs += '\n' + ' '.repeat(6) + 'export enum ColumnNames {\n';
41
- for (const [formattedName, rawName] of Object.entries(t.columnNames)) {
42
- typeDefs += ' '.repeat(8) + `${formattedName} = '${rawName}',\n`;
43
- }
44
- typeDefs += ' '.repeat(6) + '};\n';
45
- }
46
- if (Object.entries(t.rowType).length > 0) {
47
- typeDefs += '\n' + ' '.repeat(6) + 'export interface RowType {\n';
48
- for (const [rawName, type] of Object.entries(t.rowType)) {
49
- const columnName = Object.entries(t.columnNames).find(entry => {
50
- return entry[1] === rawName;
51
- })[0];
52
- typeDefs += ' '.repeat(8) + `[ColumnNames.${columnName}]: ${type};\n`;
53
- }
54
- typeDefs += ' '.repeat(6) + '};\n';
55
- }
56
- typeDefs += ' '.repeat(4) + '};\n';
57
- }
58
- typeDefs += ' '.repeat(2) + '};\n';
59
- return typeDefs;
60
- }
61
- function appendProcedures(typeDefs, schema) {
62
- typeDefs += '\n' + ' '.repeat(2) + 'export namespace Procedures {';
63
- for (const proc of schema.procedures) {
64
- typeDefs +=
65
- '\n' + ' '.repeat(4) + `export namespace ${proc.formattedName} {\n`;
66
- typeDefs +=
67
- ' '.repeat(6) + `export const ProcedureName = '${proc.rawName}';\n`;
68
- typeDefs +=
69
- '\n' +
70
- ' '.repeat(6) +
71
- `export const ArgNames = [\n` +
72
- proc.argNames
73
- .map(n => {
74
- return ' '.repeat(8) + `'${n}',\n`;
75
- })
76
- .join('') +
77
- ' '.repeat(6) +
78
- '] as const;\n';
79
- typeDefs +=
80
- '\n' +
81
- ' '.repeat(6) +
82
- `export type ArgTypes = [\n` +
83
- proc.argTypes
84
- .map(t => {
85
- return ' '.repeat(8) + t + ',\n';
86
- })
87
- .join('') +
88
- ' '.repeat(6) +
89
- '];\n';
90
- typeDefs +=
91
- '\n' + ' '.repeat(6) + `export type ReturnType = ${proc.returnType};\n`;
92
- typeDefs += ' '.repeat(4) + '};\n';
93
- }
94
- typeDefs += ' '.repeat(2) + '};\n';
95
- return typeDefs;
96
- }
@@ -1,8 +0,0 @@
1
- import { type IntrospeQLConfig } from './introspeql-config';
2
- /**
3
- * Reads table, function, and enum metadata from a database and writes
4
- * corresponding TypeScript type definitions to the provided output path.
5
- *
6
- * @param config
7
- */
8
- export declare function generateTypes(config: IntrospeQLConfig): Promise<void>;
@@ -1,111 +0,0 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- import { Client } from 'pg';
4
- import { introspectTables } from './introspect-tables';
5
- import { introspectColumns } from './introspect-columns';
6
- import { introspectProcedures } from './introspect-procedures';
7
- import { introspectEnum, } from './introspect-enum';
8
- import { writeHeader } from './write-header';
9
- import { prepareDataForWriting } from './prepare-data-for-writing';
10
- import { appendSchema } from './append-schema';
11
- import { introspeqlConfig } from './introspeql-config';
12
- /**
13
- * Reads table, function, and enum metadata from a database and writes
14
- * corresponding TypeScript type definitions to the provided output path.
15
- *
16
- * @param config
17
- */
18
- export async function generateTypes(config) {
19
- const parsedConfig = introspeqlConfig.parse(config);
20
- const client = 'dbConnectionString' in parsedConfig ?
21
- new Client({
22
- connectionString: parsedConfig.dbConnectionString,
23
- })
24
- : new Client({
25
- ...parsedConfig.dbConnectionParams,
26
- });
27
- try {
28
- await client.connect();
29
- }
30
- catch (e) {
31
- throw new Error('Failed to connect to the database.', { cause: e });
32
- }
33
- // Read table data
34
- let tableDataObjects;
35
- try {
36
- tableDataObjects = await introspectTables(client, parsedConfig);
37
- }
38
- catch (e) {
39
- throw new Error('Failed to introspect tables.', { cause: e });
40
- }
41
- // Read column data and update enums object when enum types are found
42
- const columnDataObjectsByTableId = {};
43
- const partialEnumDataObjects = [];
44
- for (const tableDataObj of tableDataObjects) {
45
- try {
46
- const columnDataObjects = await introspectColumns(client, tableDataObj.id);
47
- columnDataObjectsByTableId[tableDataObj.id] = columnDataObjects;
48
- for (const columnDataObj of columnDataObjects) {
49
- if (columnDataObj.is_enum &&
50
- !partialEnumDataObjects.find(d => d.id === columnDataObj.column_type_id)) {
51
- partialEnumDataObjects.push({
52
- id: columnDataObj.column_type_id,
53
- schema: columnDataObj.column_type_schema,
54
- name: columnDataObj.column_type,
55
- });
56
- }
57
- }
58
- }
59
- catch (e) {
60
- throw new Error('Failed to introspect columns.', { cause: e });
61
- }
62
- }
63
- // Read procedure data and update enums object when enum types are found
64
- let procedureDataObjects;
65
- try {
66
- procedureDataObjects = await introspectProcedures(client, parsedConfig);
67
- }
68
- catch (e) {
69
- throw new Error('Failed to introspect procedures.', { cause: e });
70
- }
71
- for (const procedureDataObject of procedureDataObjects) {
72
- for (const argType of procedureDataObject.arg_types) {
73
- if (argType.is_enum &&
74
- !partialEnumDataObjects.find(d => d.id === argType.id)) {
75
- partialEnumDataObjects.push({
76
- id: argType.id,
77
- schema: argType.schema,
78
- name: argType.name,
79
- });
80
- }
81
- }
82
- if (procedureDataObject.return_type.is_enum &&
83
- !partialEnumDataObjects.find(d => d.id === procedureDataObject.return_type.id)) {
84
- partialEnumDataObjects.push({
85
- id: procedureDataObject.return_type.id,
86
- schema: procedureDataObject.return_type.schema,
87
- name: procedureDataObject.return_type.name,
88
- });
89
- }
90
- }
91
- // Read enum metadata for all enums that were featured in tables or procedures
92
- const enumDataObjects = [];
93
- for (const partialEnumDataObj of partialEnumDataObjects) {
94
- try {
95
- const enumData = await introspectEnum(client, partialEnumDataObj);
96
- enumDataObjects.push(enumData);
97
- }
98
- catch (e) {
99
- throw new Error('Failed to introspect enum.', { cause: e });
100
- }
101
- }
102
- await client.end();
103
- const outDir = path.dirname(config.outFile);
104
- if (!fs.existsSync(outDir)) {
105
- fs.mkdirSync(outDir, { recursive: true });
106
- }
107
- if (!fs)
108
- writeHeader(parsedConfig.outFile, parsedConfig);
109
- const schemas = prepareDataForWriting(enumDataObjects, tableDataObjects, columnDataObjectsByTableId, procedureDataObjects, parsedConfig);
110
- schemas.forEach(schema => appendSchema(parsedConfig.outFile, schema));
111
- }
@@ -1,21 +0,0 @@
1
- import { z } from 'zod';
2
- import type { Client } from 'pg';
3
- declare const columnDataSchema: z.ZodObject<{
4
- column_name: z.ZodString;
5
- column_type_schema: z.ZodString;
6
- column_type_id: z.ZodNumber;
7
- column_type: z.ZodString;
8
- is_enum: z.ZodBoolean;
9
- num_dimensions: z.ZodNumber;
10
- nullable: z.ZodBoolean;
11
- }, z.core.$strip>;
12
- export type ColumnData = z.infer<typeof columnDataSchema>;
13
- /**
14
- * Reads metadata for each column of a given table.
15
- *
16
- * @param client
17
- * @param tableId
18
- * @returns A {@link Promise}<{@link ColumnData}[]>
19
- */
20
- export declare function introspectColumns(client: Client, tableId: number): Promise<ColumnData[]>;
21
- export {};
@@ -1,53 +0,0 @@
1
- import { z } from 'zod';
2
- const columnDataSchema = z.object({
3
- column_name: z.string(),
4
- column_type_schema: z.string(),
5
- column_type_id: z.number(),
6
- column_type: z.string(),
7
- is_enum: z.boolean(),
8
- num_dimensions: z.number(),
9
- nullable: z.boolean(),
10
- });
11
- /**
12
- * Reads metadata for each column of a given table.
13
- *
14
- * @param client
15
- * @param tableId
16
- * @returns A {@link Promise}<{@link ColumnData}[]>
17
- */
18
- export async function introspectColumns(client, tableId) {
19
- const result = await client.query(`
20
- SELECT
21
- a.attname AS column_name,
22
- (
23
- SELECT nspname
24
- FROM pg_catalog.pg_namespace
25
- WHERE oid = t.typnamespace
26
- ) AS column_type_schema,
27
- CASE WHEN a.attndims > 0 THEN (
28
- SELECT base_type.oid
29
- FROM pg_catalog.pg_type AS array_type, pg_catalog.pg_type as base_type
30
- WHERE array_type.oid = a.atttypid AND base_type.typarray = array_type.oid
31
- ) ELSE a.atttypid END AS column_type_id,
32
- CASE WHEN a.attndims > 0 THEN (
33
- SELECT base_type.typname
34
- FROM pg_catalog.pg_type AS array_type, pg_catalog.pg_type as base_type
35
- WHERE array_type.oid = a.atttypid AND base_type.typarray = array_type.oid
36
- ) ELSE t.typname END AS column_type,
37
- CASE WHEN a.attndims > 0 THEN (
38
- SELECT t2.typtype = 'e' AS is_enum
39
- FROM pg_catalog.pg_type AS t1, pg_catalog.pg_type as t2
40
- INNER JOIN pg_catalog.pg_namespace AS n ON t2.typnamespace = n.oid
41
- WHERE t1.oid = a.atttypid AND t2.typarray = t1.oid
42
- ) ELSE t.typtype = 'e' END AS is_enum,
43
- a.attndims AS num_dimensions,
44
- NOT a.attnotnull As nullable
45
- FROM pg_catalog.pg_class AS c
46
- INNER JOIN pg_catalog.pg_namespace AS n ON c.relnamespace = n.oid
47
- INNER JOIN pg_catalog.pg_attribute AS a ON c.oid = a.attrelid
48
- INNER JOIN pg_catalog.pg_type AS t ON a.atttypid = t.oid
49
- WHERE c.oid = $1
50
- AND a.attnum >= 1;`, [tableId]);
51
- const columnData = columnDataSchema.array().parse(result.rows);
52
- return columnData;
53
- }
@@ -1,20 +0,0 @@
1
- import type { Client } from 'pg';
2
- export type PartialEnumData = {
3
- /** The OID of the enum. */
4
- id: number;
5
- /** The name of the schema in which the enum was defined. */
6
- schema: string;
7
- /** The name of the enum. */
8
- name: string;
9
- };
10
- export type EnumData = PartialEnumData & {
11
- values: string[];
12
- };
13
- /**
14
- * Reads metadata for the provided enum.
15
- *
16
- * @param client
17
- * @param data
18
- * @returns A {@link Promise}<{@link EnumData}[]>
19
- */
20
- export declare function introspectEnum(client: Client, data: PartialEnumData): Promise<EnumData>;
@@ -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
- }