introspeql 0.0.4 → 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.
- package/README.md +286 -85
- package/dist/comments/comment-converter.d.ts +54 -0
- package/dist/comments/comment-converter.js +185 -0
- package/dist/comments/index.d.ts +2 -0
- package/dist/comments/index.js +18 -0
- package/dist/comments/prettify-comment.d.ts +1 -0
- package/dist/comments/prettify-comment.js +18 -0
- package/dist/config/connection-options.d.ts +14 -0
- package/dist/config/connection-options.js +42 -0
- package/dist/config/default-type-mappings.d.ts +13 -0
- package/dist/config/default-type-mappings.js +16 -0
- package/dist/config/entity-data.d.ts +6 -0
- package/dist/config/entity-data.js +8 -0
- package/dist/config/function-options.d.ts +23 -0
- package/dist/config/function-options.js +74 -0
- package/dist/config/general-options.d.ts +7 -0
- package/dist/config/general-options.js +75 -0
- package/dist/config/index.d.ts +71 -0
- package/dist/config/index.js +29 -0
- package/dist/config/output-options.d.ts +8 -0
- package/dist/config/output-options.js +40 -0
- package/dist/config/table-options.d.ts +21 -0
- package/dist/config/table-options.js +48 -0
- package/dist/enums/enum-data.d.ts +9 -0
- package/dist/enums/enum-data.js +11 -0
- package/dist/enums/enum-definition.d.ts +7 -0
- package/dist/enums/enum-definition.js +24 -0
- package/dist/enums/index.d.ts +4 -0
- package/dist/enums/index.js +20 -0
- package/dist/enums/is-enum-replaced-with-custom-type.d.ts +3 -0
- package/dist/enums/is-enum-replaced-with-custom-type.js +9 -0
- package/dist/enums/read-enum-data.d.ts +3 -0
- package/dist/enums/read-enum-data.js +57 -0
- package/dist/functions/function-data.d.ts +26 -0
- package/dist/functions/function-data.js +27 -0
- package/dist/functions/function-definition.d.ts +7 -0
- package/dist/functions/function-definition.js +21 -0
- package/dist/functions/index.d.ts +9 -0
- package/dist/functions/index.js +25 -0
- package/dist/functions/overload-type-definition.d.ts +8 -0
- package/dist/functions/overload-type-definition.js +21 -0
- package/dist/functions/parameter-type-definition-builder.d.ts +16 -0
- package/dist/functions/parameter-type-definition-builder.js +41 -0
- package/dist/functions/parameter-type-definition.d.ts +9 -0
- package/dist/functions/parameter-type-definition.js +28 -0
- package/dist/functions/read-function-data.d.ts +4 -0
- package/dist/functions/read-function-data.js +93 -0
- package/dist/functions/return-type-definition.d.ts +7 -0
- package/dist/functions/return-type-definition.js +20 -0
- package/dist/functions/should-include-function.d.ts +2 -0
- package/dist/functions/should-include-function.js +6 -0
- package/dist/functions/should-include-overload.d.ts +2 -0
- package/dist/functions/should-include-overload.js +23 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +7 -1
- package/dist/introspeql.d.ts +7 -0
- package/dist/introspeql.js +161 -0
- package/dist/schemas/index.d.ts +3 -0
- package/dist/schemas/index.js +19 -0
- package/dist/schemas/read-schema-data.d.ts +15 -0
- package/dist/schemas/read-schema-data.js +182 -0
- package/dist/schemas/schema-definition-factory.d.ts +15 -0
- package/dist/schemas/schema-definition-factory.js +167 -0
- package/dist/schemas/schema-definition.d.ts +11 -0
- package/dist/schemas/schema-definition.js +38 -0
- package/dist/shared/convert-pg-identifier-to-ts-identifier.d.ts +19 -0
- package/dist/shared/convert-pg-identifier-to-ts-identifier.js +57 -0
- package/dist/shared/directives.d.ts +12 -0
- package/dist/shared/directives.js +16 -0
- package/dist/shared/get-tokens.d.ts +1 -0
- package/dist/shared/get-tokens.js +7 -0
- package/dist/shared/indent.d.ts +1 -0
- package/dist/shared/indent.js +9 -0
- package/dist/shared/index.d.ts +5 -0
- package/dist/shared/index.js +21 -0
- package/dist/shared/parsing-error.d.ts +3 -0
- package/dist/shared/parsing-error.js +26 -0
- package/dist/tables/column-data.d.ts +14 -0
- package/dist/tables/column-data.js +16 -0
- package/dist/tables/column-definition.d.ts +8 -0
- package/dist/tables/column-definition.js +19 -0
- package/dist/tables/column-type-definition.d.ts +7 -0
- package/dist/tables/column-type-definition.js +19 -0
- package/dist/tables/index.d.ts +8 -0
- package/dist/tables/index.js +24 -0
- package/dist/tables/read-column-data.d.ts +3 -0
- package/dist/tables/read-column-data.js +55 -0
- package/dist/tables/read-table-data.d.ts +4 -0
- package/dist/tables/read-table-data.js +84 -0
- package/dist/tables/should-include-table.d.ts +10 -0
- package/dist/tables/should-include-table.js +24 -0
- package/dist/tables/table-data.d.ts +8 -0
- package/dist/tables/table-data.js +10 -0
- package/dist/tables/table-definition.d.ts +10 -0
- package/dist/tables/table-definition.js +43 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +17 -0
- package/dist/types/lookup-type.d.ts +15 -0
- package/dist/types/lookup-type.js +25 -0
- package/package.json +32 -17
- package/dist/append-schema.d.ts +0 -8
- package/dist/append-schema.js +0 -96
- package/dist/generate-types.d.ts +0 -8
- package/dist/generate-types.js +0 -110
- package/dist/introspect-columns.d.ts +0 -21
- package/dist/introspect-columns.js +0 -53
- package/dist/introspect-enum.d.ts +0 -20
- package/dist/introspect-enum.js +0 -24
- package/dist/introspect-procedures.d.ts +0 -53
- package/dist/introspect-procedures.js +0 -129
- package/dist/introspect-tables.d.ts +0 -19
- package/dist/introspect-tables.js +0 -43
- package/dist/introspeql-config.d.ts +0 -40
- package/dist/introspeql-config.js +0 -104
- package/dist/prepare-data-for-writing.d.ts +0 -38
- package/dist/prepare-data-for-writing.js +0 -145
- package/dist/snake-case-to-pascal-case.d.ts +0 -8
- package/dist/snake-case-to-pascal-case.js +0 -17
- package/dist/write-header.d.ts +0 -2
- package/dist/write-header.js +0 -10
|
@@ -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,19 @@
|
|
|
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("./read-schema-data"), exports);
|
|
18
|
+
__exportStar(require("./schema-definition"), exports);
|
|
19
|
+
__exportStar(require("./schema-definition-factory"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Client } from 'pg';
|
|
2
|
+
import { type EnumData } from '../enums';
|
|
3
|
+
import { type FunctionData } from '../functions';
|
|
4
|
+
import { type ColumnData, type TableData } from '../tables';
|
|
5
|
+
import type { ParsedConfig } from '../config';
|
|
6
|
+
export interface TableDataWithColumns extends TableData {
|
|
7
|
+
columns: ColumnData[];
|
|
8
|
+
}
|
|
9
|
+
export interface SchemaData {
|
|
10
|
+
name: string;
|
|
11
|
+
enums: EnumData[];
|
|
12
|
+
functions: FunctionData[];
|
|
13
|
+
tables: TableDataWithColumns[];
|
|
14
|
+
}
|
|
15
|
+
export declare function readSchemaData(client: Client, parsedConfig: ParsedConfig): Promise<Record<string, SchemaData>>;
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
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);
|
|
24
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
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;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.readSchemaData = readSchemaData;
|
|
51
|
+
var enums_1 = require("../enums");
|
|
52
|
+
var functions_1 = require("../functions");
|
|
53
|
+
var tables_1 = require("../tables");
|
|
54
|
+
function readSchemaData(client, parsedConfig) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
56
|
+
var schemas, functions, _i, functions_2, f, _a, _b, overload, _c, _d, paramType, tables, _e, tables_2, t, columns, tableDataWithColumns, _f, columns_1, column;
|
|
57
|
+
return __generator(this, function (_g) {
|
|
58
|
+
switch (_g.label) {
|
|
59
|
+
case 0:
|
|
60
|
+
schemas = {};
|
|
61
|
+
return [4 /*yield*/, (0, functions_1.readFunctionData)(client, parsedConfig)];
|
|
62
|
+
case 1:
|
|
63
|
+
functions = _g.sent();
|
|
64
|
+
_i = 0, functions_2 = functions;
|
|
65
|
+
_g.label = 2;
|
|
66
|
+
case 2:
|
|
67
|
+
if (!(_i < functions_2.length)) return [3 /*break*/, 11];
|
|
68
|
+
f = functions_2[_i];
|
|
69
|
+
registerDBObject(f, schemas);
|
|
70
|
+
_a = 0, _b = f.overloads;
|
|
71
|
+
_g.label = 3;
|
|
72
|
+
case 3:
|
|
73
|
+
if (!(_a < _b.length)) return [3 /*break*/, 10];
|
|
74
|
+
overload = _b[_a];
|
|
75
|
+
_c = 0, _d = overload.paramTypes;
|
|
76
|
+
_g.label = 4;
|
|
77
|
+
case 4:
|
|
78
|
+
if (!(_c < _d.length)) return [3 /*break*/, 7];
|
|
79
|
+
paramType = _d[_c];
|
|
80
|
+
return [4 /*yield*/, readAndRegisterTypeIfEnum(paramType, client, parsedConfig, schemas)];
|
|
81
|
+
case 5:
|
|
82
|
+
_g.sent();
|
|
83
|
+
_g.label = 6;
|
|
84
|
+
case 6:
|
|
85
|
+
_c++;
|
|
86
|
+
return [3 /*break*/, 4];
|
|
87
|
+
case 7: return [4 /*yield*/, readAndRegisterTypeIfEnum(overload.returnType, client, parsedConfig, schemas)];
|
|
88
|
+
case 8:
|
|
89
|
+
_g.sent();
|
|
90
|
+
_g.label = 9;
|
|
91
|
+
case 9:
|
|
92
|
+
_a++;
|
|
93
|
+
return [3 /*break*/, 3];
|
|
94
|
+
case 10:
|
|
95
|
+
_i++;
|
|
96
|
+
return [3 /*break*/, 2];
|
|
97
|
+
case 11: return [4 /*yield*/, (0, tables_1.readTableData)(client, parsedConfig)];
|
|
98
|
+
case 12:
|
|
99
|
+
tables = _g.sent();
|
|
100
|
+
_e = 0, tables_2 = tables;
|
|
101
|
+
_g.label = 13;
|
|
102
|
+
case 13:
|
|
103
|
+
if (!(_e < tables_2.length)) return [3 /*break*/, 19];
|
|
104
|
+
t = tables_2[_e];
|
|
105
|
+
return [4 /*yield*/, (0, tables_1.readColumnData)(client, t.oid)];
|
|
106
|
+
case 14:
|
|
107
|
+
columns = _g.sent();
|
|
108
|
+
tableDataWithColumns = __assign(__assign({}, t), { columns: columns });
|
|
109
|
+
registerDBObject(tableDataWithColumns, schemas);
|
|
110
|
+
_f = 0, columns_1 = columns;
|
|
111
|
+
_g.label = 15;
|
|
112
|
+
case 15:
|
|
113
|
+
if (!(_f < columns_1.length)) return [3 /*break*/, 18];
|
|
114
|
+
column = columns_1[_f];
|
|
115
|
+
return [4 /*yield*/, readAndRegisterTypeIfEnum(column.type, client, parsedConfig, schemas)];
|
|
116
|
+
case 16:
|
|
117
|
+
_g.sent();
|
|
118
|
+
_g.label = 17;
|
|
119
|
+
case 17:
|
|
120
|
+
_f++;
|
|
121
|
+
return [3 /*break*/, 15];
|
|
122
|
+
case 18:
|
|
123
|
+
_e++;
|
|
124
|
+
return [3 /*break*/, 13];
|
|
125
|
+
case 19: return [2 /*return*/, schemas];
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
function registerDBObject(dbObject, schemas) {
|
|
131
|
+
if (!(dbObject.schema in schemas)) {
|
|
132
|
+
schemas[dbObject.schema] = {
|
|
133
|
+
name: dbObject.schema,
|
|
134
|
+
enums: [],
|
|
135
|
+
functions: [],
|
|
136
|
+
tables: [],
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
if (isEnumData(dbObject)) {
|
|
140
|
+
schemas[dbObject.schema].enums.push(dbObject);
|
|
141
|
+
}
|
|
142
|
+
else if (isFunctionData(dbObject)) {
|
|
143
|
+
schemas[dbObject.schema].functions.push(dbObject);
|
|
144
|
+
}
|
|
145
|
+
else if (isTableDataWithColumns(dbObject)) {
|
|
146
|
+
schemas[dbObject.schema].tables.push(dbObject);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function isEnumData(dbObject) {
|
|
150
|
+
return enums_1.enumDataSchema.safeParse(dbObject).success;
|
|
151
|
+
}
|
|
152
|
+
function isFunctionData(dbObject) {
|
|
153
|
+
return functions_1.functionDataSchema.safeParse(dbObject).success;
|
|
154
|
+
}
|
|
155
|
+
function isTableDataWithColumns(dbObject) {
|
|
156
|
+
return (tables_1.tableDataSchema.safeParse(dbObject).success &&
|
|
157
|
+
'columns' in dbObject &&
|
|
158
|
+
tables_1.columnDataSchema.array().safeParse(dbObject.columns).success);
|
|
159
|
+
}
|
|
160
|
+
function readAndRegisterTypeIfEnum(maybeEnum, client, parsedConfig, schemas) {
|
|
161
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
162
|
+
var enumData;
|
|
163
|
+
return __generator(this, function (_a) {
|
|
164
|
+
switch (_a.label) {
|
|
165
|
+
case 0:
|
|
166
|
+
if (!(maybeEnum.isEnum &&
|
|
167
|
+
!isEnumDefinedInConfig(maybeEnum.schema, maybeEnum.name, parsedConfig) &&
|
|
168
|
+
!(maybeEnum.schema in schemas &&
|
|
169
|
+
schemas[maybeEnum.schema].enums.some(function (e) { return e.name === maybeEnum.name; })))) return [3 /*break*/, 2];
|
|
170
|
+
return [4 /*yield*/, (0, enums_1.readEnumData)(client, maybeEnum.oid)];
|
|
171
|
+
case 1:
|
|
172
|
+
enumData = _a.sent();
|
|
173
|
+
registerDBObject(enumData, schemas);
|
|
174
|
+
_a.label = 2;
|
|
175
|
+
case 2: return [2 /*return*/];
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
function isEnumDefinedInConfig(schema, name, parsedConfig) {
|
|
181
|
+
return "".concat(schema, ".").concat(name) in parsedConfig.types;
|
|
182
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { SchemaDefinition } from './schema-definition';
|
|
2
|
+
import type { SchemaData } from './read-schema-data';
|
|
3
|
+
import type { ParsedConfig } from '../config';
|
|
4
|
+
export declare class SchemaDefinitionFactory {
|
|
5
|
+
static createSchemaDefinition(data: SchemaData, config: ParsedConfig): SchemaDefinition;
|
|
6
|
+
private static validateDBObjectNames;
|
|
7
|
+
private static createSortedEnumDefinitions;
|
|
8
|
+
private static sortDBObjectData;
|
|
9
|
+
private static createEnumDefinitions;
|
|
10
|
+
private static shouldCopyDBObjectComment;
|
|
11
|
+
private static createSortedTableDefinitions;
|
|
12
|
+
private static createTableDefinitions;
|
|
13
|
+
private static createSortedFunctionDefinitions;
|
|
14
|
+
private static createFunctionDefinitions;
|
|
15
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SchemaDefinitionFactory = void 0;
|
|
4
|
+
var schema_definition_1 = require("./schema-definition");
|
|
5
|
+
var enums_1 = require("../enums");
|
|
6
|
+
var tables_1 = require("../tables");
|
|
7
|
+
var functions_1 = require("../functions");
|
|
8
|
+
var types_1 = require("../types");
|
|
9
|
+
var comments_1 = require("../comments");
|
|
10
|
+
var shared_1 = require("../shared");
|
|
11
|
+
var SchemaDefinitionFactory = /** @class */ (function () {
|
|
12
|
+
function SchemaDefinitionFactory() {
|
|
13
|
+
}
|
|
14
|
+
SchemaDefinitionFactory.createSchemaDefinition = function (data, config) {
|
|
15
|
+
this.validateDBObjectNames(data.enums);
|
|
16
|
+
this.validateDBObjectNames(data.tables);
|
|
17
|
+
this.validateDBObjectNames(data.functions);
|
|
18
|
+
var enumDefinitions = this.createSortedEnumDefinitions(data, config);
|
|
19
|
+
var tableDefinitions = this.createSortedTableDefinitions(data, config);
|
|
20
|
+
var functionDefinitions = this.createSortedFunctionDefinitions(data, config);
|
|
21
|
+
return new schema_definition_1.SchemaDefinition(data.name, enumDefinitions, tableDefinitions, functionDefinitions);
|
|
22
|
+
};
|
|
23
|
+
SchemaDefinitionFactory.validateDBObjectNames = function (dbObjects) {
|
|
24
|
+
var seen = new Set();
|
|
25
|
+
for (var _i = 0, dbObjects_1 = dbObjects; _i < dbObjects_1.length; _i++) {
|
|
26
|
+
var dbObject = dbObjects_1[_i];
|
|
27
|
+
var formattedName = (0, shared_1.convertPGIdentifierToTSIdentifier)(dbObject.name);
|
|
28
|
+
if (seen.has(formattedName)) {
|
|
29
|
+
throw new shared_1.ParsingError('Duplicate identifier: ' + formattedName);
|
|
30
|
+
}
|
|
31
|
+
seen.add(formattedName);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
SchemaDefinitionFactory.createSortedEnumDefinitions = function (data, config) {
|
|
35
|
+
var sortedEnums = this.sortDBObjectData(data.enums);
|
|
36
|
+
return this.createEnumDefinitions(sortedEnums, config);
|
|
37
|
+
};
|
|
38
|
+
SchemaDefinitionFactory.sortDBObjectData = function (objectData) {
|
|
39
|
+
return objectData.toSorted(function (a, b) {
|
|
40
|
+
return (0, shared_1.convertPGIdentifierToTSIdentifier)(a.name).localeCompare((0, shared_1.convertPGIdentifierToTSIdentifier)(b.name));
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
SchemaDefinitionFactory.createEnumDefinitions = function (enums, config) {
|
|
44
|
+
var _this = this;
|
|
45
|
+
var enumDefinitions = enums.map(function (enumData) {
|
|
46
|
+
var comment = '';
|
|
47
|
+
if (_this.shouldCopyDBObjectComment(enumData, config)) {
|
|
48
|
+
try {
|
|
49
|
+
comment = comments_1.CommentConverter.convertComment(enumData.comment);
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
if (e instanceof shared_1.ParsingError) {
|
|
53
|
+
console.warn(e.message);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
throw e;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return new enums_1.EnumDefinition(enumData.name, enumData.values, comment);
|
|
61
|
+
});
|
|
62
|
+
return enumDefinitions;
|
|
63
|
+
};
|
|
64
|
+
SchemaDefinitionFactory.shouldCopyDBObjectComment = function (objectData, config) {
|
|
65
|
+
if (!objectData.comment)
|
|
66
|
+
return false;
|
|
67
|
+
var commentTokens = (0, shared_1.getTokens)(objectData.comment);
|
|
68
|
+
return ((config.copyComments &&
|
|
69
|
+
!commentTokens.includes(shared_1.Directives.DisableTSDocComments)) ||
|
|
70
|
+
(!config.copyComments &&
|
|
71
|
+
commentTokens.includes(shared_1.Directives.EnableTSDocComments)));
|
|
72
|
+
};
|
|
73
|
+
SchemaDefinitionFactory.createSortedTableDefinitions = function (data, config) {
|
|
74
|
+
var sortedTables = this.sortDBObjectData(data.tables);
|
|
75
|
+
return this.createTableDefinitions(sortedTables, config);
|
|
76
|
+
};
|
|
77
|
+
SchemaDefinitionFactory.createTableDefinitions = function (tables, config) {
|
|
78
|
+
var _this = this;
|
|
79
|
+
var tableDefinitions = tables.map(function (tableData) {
|
|
80
|
+
var shouldCopyTableComment = _this.shouldCopyDBObjectComment(tableData, config);
|
|
81
|
+
var comment = '';
|
|
82
|
+
if (shouldCopyTableComment) {
|
|
83
|
+
try {
|
|
84
|
+
comment = comments_1.CommentConverter.convertComment(tableData.comment);
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
if (e instanceof shared_1.ParsingError) {
|
|
88
|
+
console.warn(e.message);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
throw e;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/*
|
|
96
|
+
By default, comments on a column are copied if the parent table's
|
|
97
|
+
comments should be copied. This can be overridden with directives
|
|
98
|
+
applied to the column-level comment directly.
|
|
99
|
+
*/
|
|
100
|
+
var shouldCopyColumnComment = function (comment) {
|
|
101
|
+
if (!comment)
|
|
102
|
+
return false;
|
|
103
|
+
var tokens = (0, shared_1.getTokens)(comment);
|
|
104
|
+
return ((shouldCopyTableComment &&
|
|
105
|
+
!tokens.includes(shared_1.Directives.DisableTSDocComments)) ||
|
|
106
|
+
(!shouldCopyTableComment &&
|
|
107
|
+
tokens.includes(shared_1.Directives.EnableTSDocComments)));
|
|
108
|
+
};
|
|
109
|
+
var columnDefinitions = tableData.columns.map(function (columnData) {
|
|
110
|
+
var tsType = (0, types_1.lookupType)(columnData.type, tableData.schema, config);
|
|
111
|
+
var typeDefinition = new tables_1.ColumnTypeDefinition(tsType, columnData.type.numDimensions, columnData.type.isNullable);
|
|
112
|
+
var comment = '';
|
|
113
|
+
if (shouldCopyColumnComment(columnData.comment)) {
|
|
114
|
+
try {
|
|
115
|
+
comment = comments_1.CommentConverter.convertComment(columnData.comment);
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
if (e instanceof shared_1.ParsingError) {
|
|
119
|
+
console.warn(e.message);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
throw e;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return new tables_1.ColumnDefinition(columnData.name, typeDefinition, comment);
|
|
127
|
+
});
|
|
128
|
+
return new tables_1.TableDefinition(tableData.name, columnDefinitions, comment);
|
|
129
|
+
});
|
|
130
|
+
return tableDefinitions;
|
|
131
|
+
};
|
|
132
|
+
SchemaDefinitionFactory.createSortedFunctionDefinitions = function (data, config) {
|
|
133
|
+
var sortedFunctionData = this.sortDBObjectData(data.functions);
|
|
134
|
+
return this.createFunctionDefinitions(sortedFunctionData, config);
|
|
135
|
+
};
|
|
136
|
+
SchemaDefinitionFactory.createFunctionDefinitions = function (functions, config) {
|
|
137
|
+
return functions.map(function (functionData) {
|
|
138
|
+
var overloadDefinitions = functionData.overloads.map(function (overloadData) {
|
|
139
|
+
var overloadCommentTokens = (0, shared_1.getTokens)(overloadData.comment);
|
|
140
|
+
var nullableArgs = (config.functions.nullableArgs &&
|
|
141
|
+
!overloadCommentTokens.includes(shared_1.Directives.DisableNullableArgs)) ||
|
|
142
|
+
(!config.functions.nullableArgs &&
|
|
143
|
+
overloadCommentTokens.includes(shared_1.Directives.EnableNullableArgs));
|
|
144
|
+
var parameterTypeDefs = overloadData.paramTypes.map(function (paramType) {
|
|
145
|
+
var tsType = (0, types_1.lookupType)(paramType, functionData.schema, config);
|
|
146
|
+
return functions_1.ParameterTypeDefinitionBuilder.getBuilder()
|
|
147
|
+
.withTSType(tsType)
|
|
148
|
+
.withIsArray(paramType.isArray)
|
|
149
|
+
.withIsNullable(nullableArgs)
|
|
150
|
+
.withIsVariadic(paramType.isVariadic)
|
|
151
|
+
.withIsOptional(paramType.isOptional)
|
|
152
|
+
.build();
|
|
153
|
+
});
|
|
154
|
+
var returnType = (0, types_1.lookupType)(overloadData.returnType, functionData.schema, config);
|
|
155
|
+
var nullableReturnType = (config.functions.nullableReturnTypes &&
|
|
156
|
+
!overloadCommentTokens.includes(shared_1.Directives.DisableNullableReturnTypes)) ||
|
|
157
|
+
(!config.functions.nullableReturnTypes &&
|
|
158
|
+
overloadCommentTokens.includes(shared_1.Directives.EnableNullableReturnTypes));
|
|
159
|
+
var returnTypeDefinition = new functions_1.ReturnTypeDefinition(returnType, overloadData.returnType.isArray, nullableReturnType);
|
|
160
|
+
return new functions_1.OverloadTypeDefinition(parameterTypeDefs, returnTypeDefinition);
|
|
161
|
+
});
|
|
162
|
+
return new functions_1.FunctionDefinition(functionData.name, overloadDefinitions);
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
return SchemaDefinitionFactory;
|
|
166
|
+
}());
|
|
167
|
+
exports.SchemaDefinitionFactory = SchemaDefinitionFactory;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EnumDefinition } from '../enums';
|
|
2
|
+
import { FunctionDefinition } from '../functions';
|
|
3
|
+
import { TableDefinition } from '../tables';
|
|
4
|
+
export declare class SchemaDefinition {
|
|
5
|
+
protected pgSchemaName: string;
|
|
6
|
+
protected enumDefinitions: EnumDefinition[];
|
|
7
|
+
protected tableDefinitions: TableDefinition[];
|
|
8
|
+
protected functionDefinitions: FunctionDefinition[];
|
|
9
|
+
constructor(pgSchemaName: string, enumDefinitions: EnumDefinition[], tableDefinitions: TableDefinition[], functionDefinitions: FunctionDefinition[]);
|
|
10
|
+
toString(): string;
|
|
11
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SchemaDefinition = void 0;
|
|
4
|
+
var shared_1 = require("../shared");
|
|
5
|
+
var SchemaDefinition = /** @class */ (function () {
|
|
6
|
+
function SchemaDefinition(pgSchemaName, enumDefinitions, tableDefinitions, functionDefinitions) {
|
|
7
|
+
this.pgSchemaName = pgSchemaName;
|
|
8
|
+
this.enumDefinitions = enumDefinitions;
|
|
9
|
+
this.tableDefinitions = tableDefinitions;
|
|
10
|
+
this.functionDefinitions = functionDefinitions;
|
|
11
|
+
}
|
|
12
|
+
SchemaDefinition.prototype.toString = function () {
|
|
13
|
+
var tsSchemaName = (0, shared_1.convertPGIdentifierToTSIdentifier)(this.pgSchemaName);
|
|
14
|
+
var enums = this.enumDefinitions
|
|
15
|
+
.map(function (enumDef) { return enumDef.toString(); })
|
|
16
|
+
.join('\n\n');
|
|
17
|
+
var tables = this.tableDefinitions
|
|
18
|
+
.map(function (tableDef) { return tableDef.toString(); })
|
|
19
|
+
.join('\n\n');
|
|
20
|
+
var functions = this.functionDefinitions
|
|
21
|
+
.map(function (funcDef) { return funcDef.toString(); })
|
|
22
|
+
.join('\n\n');
|
|
23
|
+
var result = "export namespace ".concat(tsSchemaName, " {\n export const PGSchemaName = '").concat(this.pgSchemaName, "';");
|
|
24
|
+
if (enums.length) {
|
|
25
|
+
result += "\n\n export namespace Enums {\n".concat((0, shared_1.indent)(enums, 4), "\n }");
|
|
26
|
+
}
|
|
27
|
+
if (tables.length) {
|
|
28
|
+
result += "\n\n export namespace Tables {\n".concat((0, shared_1.indent)(tables, 4), "\n }");
|
|
29
|
+
}
|
|
30
|
+
if (functions.length) {
|
|
31
|
+
result += "\n\n export namespace Functions {\n".concat((0, shared_1.indent)(functions, 4), "\n }");
|
|
32
|
+
}
|
|
33
|
+
result += '\n}';
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
36
|
+
return SchemaDefinition;
|
|
37
|
+
}());
|
|
38
|
+
exports.SchemaDefinition = SchemaDefinition;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a PostgreSQL identifier to a valid TypeScript identifier.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* In PostgreSQL, if an identifier is enclosed in double quotes, it can include
|
|
6
|
+
* a wider selection of characters than are valid in TypeScript.
|
|
7
|
+
* Additionally, while it is idiomatic to use snake_case in PostgreSQL, it is
|
|
8
|
+
* more commonplace to use PascalCase for TypeScript types. Therefore, the
|
|
9
|
+
* following rules are followed when converting identifiers:
|
|
10
|
+
*
|
|
11
|
+
* - If the PostgreSQL identifier begins with underscore(s), these are
|
|
12
|
+
* preserved.
|
|
13
|
+
* - Otherwise, only alphanumeric and $ characters are copied.
|
|
14
|
+
* - All non-letter characters are treated as separators, except for
|
|
15
|
+
* apostrophes, which are simply discarded.
|
|
16
|
+
* - Each word (i.e. the letters between separators) is capitalized.
|
|
17
|
+
* - If the result begins with a number, it is prefaced with an underscore.
|
|
18
|
+
*/
|
|
19
|
+
export declare function convertPGIdentifierToTSIdentifier(identifier: string): string;
|