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
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReturnTypeDefinition = void 0;
4
+ var ReturnTypeDefinition = /** @class */ (function () {
5
+ function ReturnTypeDefinition(tsType, isArray, isNullable) {
6
+ this.tsType = tsType;
7
+ this.isArray = isArray;
8
+ this.isNullable = isNullable;
9
+ }
10
+ ReturnTypeDefinition.prototype.toString = function () {
11
+ var type = this.tsType;
12
+ if (this.isArray)
13
+ type = "".concat(type, "[]");
14
+ if (this.isNullable)
15
+ type = "".concat(type, " | null");
16
+ return type;
17
+ };
18
+ return ReturnTypeDefinition;
19
+ }());
20
+ exports.ReturnTypeDefinition = ReturnTypeDefinition;
@@ -0,0 +1,2 @@
1
+ import type { FunctionData } from './function-data';
2
+ export declare function shouldIncludeFunction(fd: FunctionData): boolean;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shouldIncludeFunction = shouldIncludeFunction;
4
+ function shouldIncludeFunction(fd) {
5
+ return !!fd.overloads.length;
6
+ }
@@ -0,0 +1,2 @@
1
+ import type { ParsedConfig } from '../config';
2
+ export declare function shouldIncludeOverload(schema: string, name: string, comment: string | null, config: ParsedConfig): boolean;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shouldIncludeOverload = shouldIncludeOverload;
4
+ var shared_1 = require("../shared");
5
+ function shouldIncludeOverload(schema, name, comment, config) {
6
+ var tokens = (0, shared_1.getTokens)(comment);
7
+ /*
8
+ If mode is inclusive, include the overload only if it AND the function
9
+ itself are not marked for exclusion.
10
+ */
11
+ if (config.functions.mode === 'inclusive') {
12
+ var shouldExcludeFunction = config.functions.excludeFunctions.some(function (f) { return f.schema === schema && f.name === name; });
13
+ var shouldExcludeOverload = tokens.some(function (t) { return t === shared_1.Directives.Exclude; });
14
+ return !shouldExcludeFunction && !shouldExcludeOverload;
15
+ }
16
+ /*
17
+ If mode is exclusive, include the overload if either it OR the function
18
+ itself are marked for inclusion.
19
+ */
20
+ var shouldIncludeFunction = config.functions.includeFunctions.some(function (f) { return f.schema === schema && f.name === name; });
21
+ var shouldIncludeOverload = tokens.some(function (t) { return t === shared_1.Directives.Include; });
22
+ return shouldIncludeFunction || shouldIncludeOverload;
23
+ }
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- export { generateTypes } from './generate-types';
2
- export type { IntrospeQLConfig } from './introspeql-config';
1
+ export { introspeql } from './introspeql';
2
+ export { Directives } from './shared';
3
+ export type { IntrospeQLConfig } from './config';
package/dist/index.js CHANGED
@@ -1 +1,7 @@
1
- export { generateTypes } from './generate-types';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Directives = exports.introspeql = void 0;
4
+ var introspeql_1 = require("./introspeql");
5
+ Object.defineProperty(exports, "introspeql", { enumerable: true, get: function () { return introspeql_1.introspeql; } });
6
+ var shared_1 = require("./shared");
7
+ Object.defineProperty(exports, "Directives", { enumerable: true, get: function () { return shared_1.Directives; } });
@@ -0,0 +1,7 @@
1
+ import { type IntrospeQLConfig } from './config';
2
+ /**
3
+ * Reads information about schemas, tables, columns, functions, and enums from
4
+ * a PostgreSQL database and generates a TypeScript file containing type
5
+ * definitions for those objects.
6
+ */
7
+ export declare function introspeql(config: IntrospeQLConfig): Promise<string>;
@@ -0,0 +1,161 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
+ return new (P || (P = Promise))(function (resolve, reject) {
38
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
42
+ });
43
+ };
44
+ var __generator = (this && this.__generator) || function (thisArg, body) {
45
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
46
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
47
+ function verb(n) { return function (v) { return step([n, v]); }; }
48
+ function step(op) {
49
+ if (f) throw new TypeError("Generator is already executing.");
50
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
51
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
52
+ if (y = 0, t) op = [op[0] & 2, t.value];
53
+ switch (op[0]) {
54
+ case 0: case 1: t = op; break;
55
+ case 4: _.label++; return { value: op[1], done: false };
56
+ case 5: _.label++; y = op[1]; op = [0]; continue;
57
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
58
+ default:
59
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
60
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
61
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
62
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
63
+ if (t[2]) _.ops.pop();
64
+ _.trys.pop(); continue;
65
+ }
66
+ op = body.call(thisArg, _);
67
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
68
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
69
+ }
70
+ };
71
+ Object.defineProperty(exports, "__esModule", { value: true });
72
+ exports.introspeql = introspeql;
73
+ var promises_1 = require("fs/promises");
74
+ var fs_1 = require("fs");
75
+ var path = __importStar(require("path"));
76
+ var config_1 = require("./config");
77
+ var schemas_1 = require("./schemas");
78
+ var pg_1 = require("pg");
79
+ var shared_1 = require("./shared");
80
+ /**
81
+ * Reads information about schemas, tables, columns, functions, and enums from
82
+ * a PostgreSQL database and generates a TypeScript file containing type
83
+ * definitions for those objects.
84
+ */
85
+ function introspeql(config) {
86
+ return __awaiter(this, void 0, void 0, function () {
87
+ var parsedConfig, client, schemas, schemaDefinitions, typeDefinitionFileContents, e_1;
88
+ return __generator(this, function (_a) {
89
+ switch (_a.label) {
90
+ case 0:
91
+ parsedConfig = config_1.introspeqlConfigSchema.parse(config);
92
+ client = new pg_1.Client(parsedConfig.dbConnectionParams || parsedConfig.dbConnectionString);
93
+ _a.label = 1;
94
+ case 1:
95
+ _a.trys.push([1, 6, 7, 9]);
96
+ return [4 /*yield*/, client.connect()];
97
+ case 2:
98
+ _a.sent();
99
+ return [4 /*yield*/, (0, schemas_1.readSchemaData)(client, parsedConfig)];
100
+ case 3:
101
+ schemas = _a.sent();
102
+ schemaDefinitions = Object.values(schemas)
103
+ .sort(function (a, b) {
104
+ return (0, shared_1.convertPGIdentifierToTSIdentifier)(a.name).localeCompare((0, shared_1.convertPGIdentifierToTSIdentifier)(b.name));
105
+ })
106
+ .map(function (schemaData) {
107
+ return schemas_1.SchemaDefinitionFactory.createSchemaDefinition(schemaData, parsedConfig);
108
+ });
109
+ typeDefinitionFileContents = createTypeDefinitionFileContents(schemaDefinitions, parsedConfig);
110
+ if (!config.writeToDisk) return [3 /*break*/, 5];
111
+ return [4 /*yield*/, writeTypeDefinitionsFile(typeDefinitionFileContents, config.outFile)];
112
+ case 4:
113
+ _a.sent();
114
+ _a.label = 5;
115
+ case 5: return [2 /*return*/, typeDefinitionFileContents];
116
+ case 6:
117
+ e_1 = _a.sent();
118
+ console.error(e_1);
119
+ return [3 /*break*/, 9];
120
+ case 7: return [4 /*yield*/, client.end()];
121
+ case 8:
122
+ _a.sent();
123
+ return [7 /*endfinally*/];
124
+ case 9: return [2 /*return*/];
125
+ }
126
+ });
127
+ });
128
+ }
129
+ function createTypeDefinitionFileContents(schemaDefinitions, config) {
130
+ var fileContents = schemaDefinitions
131
+ .map(function (schemaDef) { return schemaDef.toString(); })
132
+ .join('\n\n');
133
+ if (config.header) {
134
+ var header = config.header;
135
+ while (!header.endsWith('\n\n')) {
136
+ header += '\n';
137
+ }
138
+ fileContents = header + fileContents;
139
+ }
140
+ return fileContents;
141
+ }
142
+ function writeTypeDefinitionsFile(fileContents, outFile) {
143
+ return __awaiter(this, void 0, void 0, function () {
144
+ var directoryPath;
145
+ return __generator(this, function (_a) {
146
+ switch (_a.label) {
147
+ case 0:
148
+ directoryPath = path.dirname(outFile);
149
+ if (!!(0, fs_1.existsSync)(directoryPath)) return [3 /*break*/, 2];
150
+ return [4 /*yield*/, (0, promises_1.mkdir)(directoryPath)];
151
+ case 1:
152
+ _a.sent();
153
+ _a.label = 2;
154
+ case 2: return [4 /*yield*/, (0, promises_1.writeFile)(outFile, fileContents, 'utf-8')];
155
+ case 3:
156
+ _a.sent();
157
+ return [2 /*return*/];
158
+ }
159
+ });
160
+ });
161
+ }
@@ -0,0 +1,11 @@
1
+ import { ColumnDefinition } from './column-definition';
2
+ export declare abstract class AbstractRelationDefinition {
3
+ protected pgRelationName: string;
4
+ protected columns: ColumnDefinition[];
5
+ protected comment?: string;
6
+ protected abstract relationType: string;
7
+ constructor(pgRelationName: string, columns: ColumnDefinition[], comment?: string);
8
+ toString(): string;
9
+ protected createColumnNamesUnion(): string;
10
+ protected createRowTypeDefinition(): string;
11
+ }
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AbstractRelationDefinition = void 0;
4
+ var shared_1 = require("../shared");
5
+ var AbstractRelationDefinition = /** @class */ (function () {
6
+ function AbstractRelationDefinition(pgRelationName, columns, comment) {
7
+ this.pgRelationName = pgRelationName;
8
+ this.columns = columns;
9
+ this.comment = comment;
10
+ }
11
+ AbstractRelationDefinition.prototype.toString = function () {
12
+ var tsNamespaceName = (0, shared_1.convertPGIdentifierToTSIdentifier)(this.pgRelationName);
13
+ var columnNames = this.createColumnNamesUnion();
14
+ var rowType = this.createRowTypeDefinition();
15
+ var stringified = "export namespace ".concat(tsNamespaceName, " {\n export const PG").concat((0, shared_1.capitalize)(this.relationType), "Name = '").concat(this.pgRelationName, "';\n\n").concat((0, shared_1.indent)(columnNames, 2), "\n\n").concat((0, shared_1.indent)(rowType, 2), "\n}");
16
+ if (this.comment) {
17
+ stringified = this.comment + '\n' + stringified;
18
+ }
19
+ return stringified;
20
+ };
21
+ AbstractRelationDefinition.prototype.createColumnNamesUnion = function () {
22
+ if (this.columns.length === 0) {
23
+ return 'export type ColumnNames = never;';
24
+ }
25
+ var columnNames = this.columns
26
+ .map(function (columnDefinition) {
27
+ return "'".concat(columnDefinition.pgColumnName, "'");
28
+ })
29
+ .join(' |\n');
30
+ return "export type ColumnNames = |\n".concat((0, shared_1.indent)(columnNames, 2), ";");
31
+ };
32
+ AbstractRelationDefinition.prototype.createRowTypeDefinition = function () {
33
+ if (this.columns.length === 0) {
34
+ return 'export interface RowType {}';
35
+ }
36
+ var columnDefinitions = this.columns
37
+ .map(function (columnDefinition) { return columnDefinition.toString(); })
38
+ .join('\n');
39
+ return "export interface RowType {\n".concat((0, shared_1.indent)(columnDefinitions, 2), "\n}");
40
+ };
41
+ return AbstractRelationDefinition;
42
+ }());
43
+ exports.AbstractRelationDefinition = AbstractRelationDefinition;
@@ -0,0 +1,14 @@
1
+ import { z } from 'zod';
2
+ export declare const columnDataSchema: z.ZodObject<{
3
+ name: z.ZodString;
4
+ type: z.ZodObject<{
5
+ oid: z.ZodCoercedNumber<unknown>;
6
+ schema: z.ZodString;
7
+ name: z.ZodString;
8
+ isEnum: z.ZodBoolean;
9
+ numDimensions: z.ZodNumber;
10
+ isNullable: z.ZodBoolean;
11
+ }, z.core.$strip>;
12
+ comment: z.ZodNullable<z.ZodString>;
13
+ }, z.core.$strip>;
14
+ export type ColumnData = z.infer<typeof columnDataSchema>;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.columnDataSchema = void 0;
4
+ var zod_1 = require("zod");
5
+ exports.columnDataSchema = zod_1.z.object({
6
+ name: zod_1.z.string(),
7
+ type: zod_1.z.object({
8
+ oid: zod_1.z.coerce.number(),
9
+ schema: zod_1.z.string(),
10
+ name: zod_1.z.string(),
11
+ isEnum: zod_1.z.boolean(),
12
+ numDimensions: zod_1.z.number(),
13
+ isNullable: zod_1.z.boolean(),
14
+ }),
15
+ comment: zod_1.z.string().nullable(),
16
+ });
@@ -0,0 +1,8 @@
1
+ import type { ColumnTypeDefinition } from './column-type-definition';
2
+ export declare class ColumnDefinition {
3
+ readonly pgColumnName: string;
4
+ private typeDefinition;
5
+ private comment?;
6
+ constructor(pgColumnName: string, typeDefinition: ColumnTypeDefinition, comment?: string);
7
+ toString(): string;
8
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ColumnDefinition = void 0;
4
+ var ColumnDefinition = /** @class */ (function () {
5
+ function ColumnDefinition(pgColumnName, typeDefinition, comment) {
6
+ this.pgColumnName = pgColumnName;
7
+ this.typeDefinition = typeDefinition;
8
+ this.comment = comment;
9
+ }
10
+ ColumnDefinition.prototype.toString = function () {
11
+ var stringified = "['".concat(this.pgColumnName, "']: ").concat(this.typeDefinition.toString(), ";");
12
+ if (this.comment) {
13
+ stringified = this.comment + '\n' + stringified;
14
+ }
15
+ return stringified;
16
+ };
17
+ return ColumnDefinition;
18
+ }());
19
+ exports.ColumnDefinition = ColumnDefinition;
@@ -0,0 +1,7 @@
1
+ export declare class ColumnTypeDefinition {
2
+ protected tsType: string;
3
+ protected numDimensions: number;
4
+ protected isNullable: boolean;
5
+ constructor(tsType: string, numDimensions: number, isNullable: boolean);
6
+ toString(): string;
7
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ColumnTypeDefinition = void 0;
4
+ var ColumnTypeDefinition = /** @class */ (function () {
5
+ function ColumnTypeDefinition(tsType, numDimensions, isNullable) {
6
+ this.tsType = tsType;
7
+ this.numDimensions = numDimensions;
8
+ this.isNullable = isNullable;
9
+ }
10
+ ColumnTypeDefinition.prototype.toString = function () {
11
+ var type = this.tsType;
12
+ type += '[]'.repeat(this.numDimensions);
13
+ if (this.isNullable)
14
+ type = "".concat(type, " | null");
15
+ return type;
16
+ };
17
+ return ColumnTypeDefinition;
18
+ }());
19
+ exports.ColumnTypeDefinition = ColumnTypeDefinition;
@@ -0,0 +1,11 @@
1
+ export * from './abstract-relation-definition';
2
+ export * from './column-data';
3
+ export * from './column-definition';
4
+ export * from './column-type-definition';
5
+ export * from './materialized-view-definition';
6
+ export * from './read-column-data';
7
+ export * from './read-relation-data';
8
+ export * from './relation-data';
9
+ export * from './should-include-relation';
10
+ export * from './table-definition';
11
+ export * from './view-definition';
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./abstract-relation-definition"), exports);
18
+ __exportStar(require("./column-data"), exports);
19
+ __exportStar(require("./column-definition"), exports);
20
+ __exportStar(require("./column-type-definition"), exports);
21
+ __exportStar(require("./materialized-view-definition"), exports);
22
+ __exportStar(require("./read-column-data"), exports);
23
+ __exportStar(require("./read-relation-data"), exports);
24
+ __exportStar(require("./relation-data"), exports);
25
+ __exportStar(require("./should-include-relation"), exports);
26
+ __exportStar(require("./table-definition"), exports);
27
+ __exportStar(require("./view-definition"), exports);
@@ -0,0 +1,4 @@
1
+ import { AbstractRelationDefinition } from './abstract-relation-definition';
2
+ export declare class MaterializedViewDefinition extends AbstractRelationDefinition {
3
+ protected relationType: string;
4
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.MaterializedViewDefinition = void 0;
19
+ var abstract_relation_definition_1 = require("./abstract-relation-definition");
20
+ var MaterializedViewDefinition = /** @class */ (function (_super) {
21
+ __extends(MaterializedViewDefinition, _super);
22
+ function MaterializedViewDefinition() {
23
+ var _this = _super !== null && _super.apply(this, arguments) || this;
24
+ _this.relationType = 'materializedView';
25
+ return _this;
26
+ }
27
+ return MaterializedViewDefinition;
28
+ }(abstract_relation_definition_1.AbstractRelationDefinition));
29
+ exports.MaterializedViewDefinition = MaterializedViewDefinition;
@@ -0,0 +1,3 @@
1
+ import { type ColumnData } from './column-data';
2
+ import type { Client } from 'pg';
3
+ export declare function readColumnData(client: Client, tableOID: number): Promise<ColumnData[]>;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.readColumnData = readColumnData;
40
+ var column_data_1 = require("./column-data");
41
+ function readColumnData(client, tableOID) {
42
+ return __awaiter(this, void 0, void 0, function () {
43
+ var result, rows, columnData;
44
+ return __generator(this, function (_a) {
45
+ switch (_a.label) {
46
+ case 0: return [4 /*yield*/, client.query("\nSELECT jsonb_build_object(\n 'name',\n a.attname,\n 'type',\n jsonb_build_object(\n 'oid',\n\t CASE WHEN a.attndims > 0 THEN (\n SELECT base_type.oid\n\t FROM pg_catalog.pg_type AS array_type, pg_catalog.pg_type as base_type\n\t WHERE array_type.oid = a.atttypid AND base_type.typarray = array_type.oid\n ) ELSE a.atttypid END,\n\t 'schema',\n\t (\n\t SELECT nspname \n\t FROM pg_catalog.pg_namespace\n\t WHERE oid = t.typnamespace\n \t),\n\t 'name',\n\t CASE WHEN a.attndims > 0 THEN (\n SELECT base_type.typname\n\t FROM pg_catalog.pg_type AS array_type, pg_catalog.pg_type as base_type\n\t WHERE array_type.oid = a.atttypid AND base_type.typarray = array_type.oid\n ) ELSE t.typname END,\n 'isEnum',\n\t CASE WHEN a.attndims > 0 THEN (\n SELECT t2.typtype = 'e' AS is_enum\n\t FROM pg_catalog.pg_type AS t1, pg_catalog.pg_type as t2\n\t INNER JOIN pg_catalog.pg_namespace AS n ON t2.typnamespace = n.oid\n\t WHERE t1.oid = a.atttypid AND t2.typarray = t1.oid\n ) ELSE t.typtype = 'e' END,\n\t 'numDimensions',\n\t a.attndims,\n\t 'isNullable',\n\t NOT a.attnotnull\n ),\n 'comment',\n col_description(a.attrelid, a.attnum)\n) FROM pg_catalog.pg_class AS c\nINNER JOIN pg_catalog.pg_namespace AS n ON c.relnamespace = n.oid\nINNER JOIN pg_catalog.pg_attribute AS a ON c.oid = a.attrelid\nINNER JOIN pg_catalog.pg_type AS t ON a.atttypid = t.oid\nWHERE c.oid = $1\nAND a.attnum >= 1;", [tableOID])];
47
+ case 1:
48
+ result = _a.sent();
49
+ rows = result.rows.map(function (r) { return r.jsonb_build_object; });
50
+ columnData = column_data_1.columnDataSchema.array().parse(rows);
51
+ return [2 /*return*/, columnData];
52
+ }
53
+ });
54
+ });
55
+ }
@@ -0,0 +1,4 @@
1
+ import { type RelationData } from './relation-data';
2
+ import type { Client } from 'pg';
3
+ import type { ParsedConfig } from '../config';
4
+ export declare function readRelationData(relationType: 'table' | 'view' | 'materializedView', client: Client, config: ParsedConfig): Promise<RelationData[]>;
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
+ if (ar || !(i in from)) {
41
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
+ ar[i] = from[i];
43
+ }
44
+ }
45
+ return to.concat(ar || Array.prototype.slice.call(from));
46
+ };
47
+ Object.defineProperty(exports, "__esModule", { value: true });
48
+ exports.readRelationData = readRelationData;
49
+ var relation_data_1 = require("./relation-data");
50
+ var should_include_relation_1 = require("./should-include-relation");
51
+ function readRelationData(relationType, client, config) {
52
+ return __awaiter(this, void 0, void 0, function () {
53
+ var relkind, schemaPlaceholders, query, parameters, result, relationData;
54
+ return __generator(this, function (_a) {
55
+ switch (_a.label) {
56
+ case 0:
57
+ relkind = relationType === 'table' ? 'r'
58
+ : relationType === 'view' ? 'v'
59
+ : 'm';
60
+ schemaPlaceholders = config.schemas
61
+ .map(function (_, i) { return "$".concat(i + 1); })
62
+ .join(', ');
63
+ query = "\nSELECT \n c.oid AS oid, \n n.nspname AS schema, \n c.relname AS name,\n obj_description(c.oid, 'pg_class') AS comment\nFROM pg_catalog.pg_class AS c\nINNER JOIN pg_catalog.pg_namespace AS n ON c.relnamespace = n.oid\nWHERE c.relkind = '".concat(relkind, "' AND n.nspname IN (").concat(schemaPlaceholders, ");\n");
64
+ parameters = __spreadArray([], config.schemas, true);
65
+ return [4 /*yield*/, client.query(query, parameters)];
66
+ case 1:
67
+ result = _a.sent();
68
+ relationData = relation_data_1.relationDataSchema
69
+ .array()
70
+ .parse(result.rows)
71
+ .filter(function (_a) {
72
+ var name = _a.name, schema = _a.schema, comment = _a.comment;
73
+ return (0, should_include_relation_1.shouldIncludeRelation)({
74
+ name: name,
75
+ schema: schema,
76
+ comment: comment,
77
+ mode: config.tables.mode,
78
+ exceptions: config.tables.mode === 'inclusive' ?
79
+ config.tables.excludeTables
80
+ : config.tables.includeTables,
81
+ });
82
+ });
83
+ return [2 /*return*/, relationData];
84
+ }
85
+ });
86
+ });
87
+ }
@@ -0,0 +1,8 @@
1
+ import { z } from 'zod';
2
+ export declare const relationDataSchema: z.ZodObject<{
3
+ oid: z.ZodNumber;
4
+ schema: z.ZodString;
5
+ name: z.ZodString;
6
+ comment: z.ZodNullable<z.ZodString>;
7
+ }, z.core.$strip>;
8
+ export type RelationData = z.infer<typeof relationDataSchema>;