introspeql-kysely 0.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/LICENSE +21 -0
- package/README.md +115 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +21 -0
- package/dist/kysely-type-generator.d.ts +27 -0
- package/dist/kysely-type-generator.js +324 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Joseph Dvorak
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# IntrospeQL-Kysely
|
|
2
|
+
|
|
3
|
+
IntrospeQL-Kysely reads information from your PostgreSQL database and generates
|
|
4
|
+
a type definition file that is compatible with Kysely.
|
|
5
|
+
|
|
6
|
+
## Example Usage
|
|
7
|
+
|
|
8
|
+
```typescript
|
|
9
|
+
// src/model/point.ts
|
|
10
|
+
export interface Point {
|
|
11
|
+
latitude: number;
|
|
12
|
+
longitude: number;
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
// src/scripts/gen-types.ts
|
|
18
|
+
import 'dotenv/config'; // must have dotenv installed
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import {
|
|
21
|
+
introspeqlKysely,
|
|
22
|
+
type IntrospeqlKyselyConfig
|
|
23
|
+
} from 'introspeql-kysely';
|
|
24
|
+
|
|
25
|
+
const outFile = '/model/db.ts';
|
|
26
|
+
|
|
27
|
+
const config: IntrospeqlKyselyConfig = {
|
|
28
|
+
schemas: ['public'], // Add other schemas as necessary
|
|
29
|
+
dbConnectionParams: { // These values should be declared in a .env file
|
|
30
|
+
host: process.env.DB_HOST,
|
|
31
|
+
port: +process.env.DB_PORT!,
|
|
32
|
+
database: process.env.DB_NAME,
|
|
33
|
+
user: process.env.DB_USER,
|
|
34
|
+
password: process.env.DB_PASSWORD,
|
|
35
|
+
},
|
|
36
|
+
writeToDisk: true,
|
|
37
|
+
outFile: path.join(__dirname, '..' + outFile),
|
|
38
|
+
header:
|
|
39
|
+
"import type { Point } from './point';"
|
|
40
|
+
types: {
|
|
41
|
+
'public.geography' : 'Point'
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
genTypes();
|
|
46
|
+
|
|
47
|
+
async function genTypes() {
|
|
48
|
+
await introspeql(config);
|
|
49
|
+
console.log("Type definition file created at " + outFile);
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
// src/main.ts
|
|
55
|
+
import { Kysely, PostgresDialect } from 'kysely';
|
|
56
|
+
import { Pool } from 'pg';
|
|
57
|
+
import { pgFn, type DB } from './model/db';
|
|
58
|
+
|
|
59
|
+
const dialect = new PostgresDialect({
|
|
60
|
+
pool: new Pool({
|
|
61
|
+
host: process.env.DB_HOST,
|
|
62
|
+
port: +process.env.DB_PORT!,
|
|
63
|
+
database: process.env.DB_NAME,
|
|
64
|
+
user: process.env.DB_USER,
|
|
65
|
+
password: process.env.DB_PASSWORD,
|
|
66
|
+
max: 10,
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
const db = new Kysely<DB>({
|
|
71
|
+
dialect
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const result = await db
|
|
75
|
+
.selectFrom('public.locations') // Assumes there is a table called locations
|
|
76
|
+
.select((eb) => [
|
|
77
|
+
pgFn('public.st_distance', [
|
|
78
|
+
eb.ref('coordinates'),
|
|
79
|
+
eb.val({
|
|
80
|
+
latitude: 39.95295623565238,
|
|
81
|
+
longitude: -75.16348330361122
|
|
82
|
+
})
|
|
83
|
+
]).as('distance_to_philadelphia')
|
|
84
|
+
]).execute();
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Further Reading
|
|
88
|
+
|
|
89
|
+
- Kysely: https://kysely.dev/
|
|
90
|
+
- IntrospeQL: https://github.com/dvorakjt/introspeql
|
|
91
|
+
- node-postgres: https://node-postgres.com/
|
|
92
|
+
|
|
93
|
+
## License
|
|
94
|
+
|
|
95
|
+
MIT License
|
|
96
|
+
|
|
97
|
+
Copyright (c) 2026 Joseph Dvorak
|
|
98
|
+
|
|
99
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
100
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
101
|
+
in the Software without restriction, including without limitation the rights
|
|
102
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
103
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
104
|
+
furnished to do so, subject to the following conditions:
|
|
105
|
+
|
|
106
|
+
The above copyright notice and this permission notice shall be included in all
|
|
107
|
+
copies or substantial portions of the Software.
|
|
108
|
+
|
|
109
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
110
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
111
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
112
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
113
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
114
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
115
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.introspeqlKysely = introspeqlKysely;
|
|
15
|
+
var introspeql_1 = require("introspeql");
|
|
16
|
+
var kysely_type_generator_1 = require("./kysely-type-generator");
|
|
17
|
+
function introspeqlKysely(config) {
|
|
18
|
+
return (0, introspeql_1.introspeql)(__assign(__assign({}, config), { createTypeDefinitions: function (schemaDefinitions) {
|
|
19
|
+
return new kysely_type_generator_1.KyselyTypeGenerator(schemaDefinitions).genTypes();
|
|
20
|
+
} }));
|
|
21
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { SchemaDefinition } from "introspeql";
|
|
2
|
+
export declare class KyselyTypeGenerator {
|
|
3
|
+
private schemaDefinitions;
|
|
4
|
+
private static readonly KYSELY_GENERATED_TYPE;
|
|
5
|
+
private static readonly KYSELY_GENERATED_ALWAYS_TYPE;
|
|
6
|
+
private kyselyImports;
|
|
7
|
+
constructor(schemaDefinitions: SchemaDefinition[]);
|
|
8
|
+
genTypes(customHeader?: string): string;
|
|
9
|
+
private genDBInterfaceNode;
|
|
10
|
+
private createRelationProperty;
|
|
11
|
+
private createRelationColumnProperty;
|
|
12
|
+
private createColumnTypeNode;
|
|
13
|
+
private genFunctionTypeNodes;
|
|
14
|
+
private getOverloads;
|
|
15
|
+
private getOptionalParameterPermutations;
|
|
16
|
+
private countVariadicParams;
|
|
17
|
+
private buildConditionalChain;
|
|
18
|
+
private buildParamTypeNode;
|
|
19
|
+
private buildReturnTypeNode;
|
|
20
|
+
private createPgFn;
|
|
21
|
+
private genImportStatements;
|
|
22
|
+
private tsTypeToTypeNode;
|
|
23
|
+
private createEnumTypeNode;
|
|
24
|
+
private applyNullable;
|
|
25
|
+
private applyTSDocComment;
|
|
26
|
+
private formatOutput;
|
|
27
|
+
}
|
|
@@ -0,0 +1,324 @@
|
|
|
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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
36
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
37
|
+
if (ar || !(i in from)) {
|
|
38
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
39
|
+
ar[i] = from[i];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
43
|
+
};
|
|
44
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
45
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.KyselyTypeGenerator = void 0;
|
|
49
|
+
var typescript_1 = __importStar(require("typescript"));
|
|
50
|
+
var sync_1 = __importDefault(require("@prettier/sync"));
|
|
51
|
+
var KyselyTypeGenerator = /** @class */ (function () {
|
|
52
|
+
function KyselyTypeGenerator(schemaDefinitions) {
|
|
53
|
+
this.schemaDefinitions = schemaDefinitions;
|
|
54
|
+
this.kyselyImports = [
|
|
55
|
+
{
|
|
56
|
+
value: "sql",
|
|
57
|
+
isTypeImport: false,
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
value: "Expression",
|
|
61
|
+
isTypeImport: true,
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
value: "RawBuilder",
|
|
65
|
+
isTypeImport: true,
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
KyselyTypeGenerator.prototype.genTypes = function (customHeader) {
|
|
70
|
+
var db = this.genDBInterfaceNode();
|
|
71
|
+
var fnNodes = this.genFunctionTypeNodes();
|
|
72
|
+
var importStatements = this.genImportStatements();
|
|
73
|
+
var printer = typescript_1.default.createPrinter();
|
|
74
|
+
var kyselyImports = printer.printFile(typescript_1.default.factory.createSourceFile([importStatements], typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.EndOfFileToken), typescript_1.NodeFlags.None));
|
|
75
|
+
var statements = __spreadArray([db], fnNodes, true).map(function (statement) {
|
|
76
|
+
return printer.printFile(typescript_1.default.factory.createSourceFile([statement], typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.EndOfFileToken), typescript_1.NodeFlags.None));
|
|
77
|
+
});
|
|
78
|
+
var src = [kyselyImports];
|
|
79
|
+
if (customHeader) {
|
|
80
|
+
src.push(customHeader.trim());
|
|
81
|
+
}
|
|
82
|
+
src = __spreadArray(__spreadArray([], src, true), statements, true);
|
|
83
|
+
var output = src.join("\n\n");
|
|
84
|
+
return this.formatOutput(output);
|
|
85
|
+
};
|
|
86
|
+
KyselyTypeGenerator.prototype.genDBInterfaceNode = function () {
|
|
87
|
+
var _this = this;
|
|
88
|
+
var relationProperties = this.schemaDefinitions.flatMap(function (schemaDefinition) {
|
|
89
|
+
return __spreadArray(__spreadArray(__spreadArray([], schemaDefinition.tableDefinitions, true), schemaDefinition.viewDefinitions, true), schemaDefinition.materializedViewDefinitions, true).map(function (relationDefinition) {
|
|
90
|
+
return _this.createRelationProperty(schemaDefinition.pgName, relationDefinition);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
var db = typescript_1.default.factory.createInterfaceDeclaration([typescript_1.default.factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword)], "DB", undefined, undefined, relationProperties);
|
|
94
|
+
return db;
|
|
95
|
+
};
|
|
96
|
+
KyselyTypeGenerator.prototype.createRelationProperty = function (schemaName, relationDefinition) {
|
|
97
|
+
var _this = this;
|
|
98
|
+
var relationSignature = typescript_1.default.factory.createPropertySignature(undefined, typescript_1.default.factory.createStringLiteral("".concat(schemaName, ".").concat(relationDefinition.pgName), true), undefined, typescript_1.default.factory.createTypeLiteralNode(relationDefinition.columns.map(function (col) {
|
|
99
|
+
return _this.createRelationColumnProperty(col);
|
|
100
|
+
})));
|
|
101
|
+
relationSignature = this.applyTSDocComment(relationSignature, relationDefinition.tsDocComment);
|
|
102
|
+
return relationSignature;
|
|
103
|
+
};
|
|
104
|
+
KyselyTypeGenerator.prototype.createRelationColumnProperty = function (columnDefinition) {
|
|
105
|
+
var columnSignature = typescript_1.default.factory.createPropertySignature(undefined, typescript_1.default.factory.createStringLiteral(columnDefinition.pgName, true), undefined, this.createColumnTypeNode(columnDefinition.typeDefinition));
|
|
106
|
+
columnSignature = this.applyTSDocComment(columnSignature, columnDefinition.tsDocComment);
|
|
107
|
+
return columnSignature;
|
|
108
|
+
};
|
|
109
|
+
KyselyTypeGenerator.prototype.createColumnTypeNode = function (columnTypeDefinition) {
|
|
110
|
+
var columnType = this.tsTypeToTypeNode(columnTypeDefinition.tsType);
|
|
111
|
+
for (var i = 0; i < columnTypeDefinition.numDimensions; i++) {
|
|
112
|
+
columnType = typescript_1.default.factory.createArrayTypeNode(columnType);
|
|
113
|
+
}
|
|
114
|
+
this.applyNullable(columnType, columnTypeDefinition.isNullable);
|
|
115
|
+
if (columnTypeDefinition.generated === "always") {
|
|
116
|
+
columnType = typescript_1.default.factory.createTypeReferenceNode(KyselyTypeGenerator.KYSELY_GENERATED_ALWAYS_TYPE, [columnType]);
|
|
117
|
+
if (!this.kyselyImports.some(function (i) { return i.value === KyselyTypeGenerator.KYSELY_GENERATED_ALWAYS_TYPE; })) {
|
|
118
|
+
this.kyselyImports.push({
|
|
119
|
+
value: KyselyTypeGenerator.KYSELY_GENERATED_ALWAYS_TYPE,
|
|
120
|
+
isTypeImport: true,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else if (columnTypeDefinition.generated === "by_default") {
|
|
125
|
+
columnType = typescript_1.default.factory.createTypeReferenceNode(KyselyTypeGenerator.KYSELY_GENERATED_TYPE, [columnType]);
|
|
126
|
+
if (!this.kyselyImports.some(function (i) { return i.value === KyselyTypeGenerator.KYSELY_GENERATED_TYPE; })) {
|
|
127
|
+
this.kyselyImports.push({
|
|
128
|
+
value: KyselyTypeGenerator.KYSELY_GENERATED_TYPE,
|
|
129
|
+
isTypeImport: true,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return columnType;
|
|
134
|
+
};
|
|
135
|
+
KyselyTypeGenerator.prototype.genFunctionTypeNodes = function () {
|
|
136
|
+
var _this = this;
|
|
137
|
+
var functionDefinitions = this.schemaDefinitions.flatMap(function (schema) {
|
|
138
|
+
return schema.functionDefinitions.map(function (fn) { return ({
|
|
139
|
+
pgName: "".concat(schema.pgName, ".").concat(fn.pgName),
|
|
140
|
+
overloads: _this.getOverloads(fn),
|
|
141
|
+
}); });
|
|
142
|
+
});
|
|
143
|
+
var pgFnNamesIdentifier = typescript_1.default.factory.createIdentifier("PgFnNames");
|
|
144
|
+
var pgFnNamesType = typescript_1.default.factory.createTypeAliasDeclaration(undefined, pgFnNamesIdentifier, undefined, functionDefinitions.length > 0
|
|
145
|
+
? typescript_1.default.factory.createUnionTypeNode(functionDefinitions.map(function (_a) {
|
|
146
|
+
var pgName = _a.pgName;
|
|
147
|
+
return typescript_1.default.factory.createLiteralTypeNode(typescript_1.default.factory.createStringLiteral(pgName, true));
|
|
148
|
+
}))
|
|
149
|
+
: typescript_1.default.factory.createKeywordTypeNode(typescript_1.default.SyntaxKind.NeverKeyword));
|
|
150
|
+
var pgFnParamsType = typescript_1.default.factory.createTypeAliasDeclaration(undefined, typescript_1.default.factory.createIdentifier("PgFnParams"), [
|
|
151
|
+
typescript_1.default.factory.createTypeParameterDeclaration(undefined, typescript_1.default.factory.createIdentifier("T"), typescript_1.default.factory.createTypeReferenceNode(pgFnNamesIdentifier)),
|
|
152
|
+
], this.buildConditionalChain(typescript_1.default.factory.createTypeReferenceNode("T"), functionDefinitions.map(function (_a) {
|
|
153
|
+
var pgName = _a.pgName, overloads = _a.overloads;
|
|
154
|
+
return ({
|
|
155
|
+
extendsType: typescript_1.default.factory.createLiteralTypeNode(typescript_1.default.factory.createStringLiteral(pgName, true)),
|
|
156
|
+
resultType: typescript_1.default.factory.createUnionTypeNode(overloads.map(function (_a) {
|
|
157
|
+
var parameterDefinitions = _a.parameterDefinitions;
|
|
158
|
+
return typescript_1.default.factory.createTupleTypeNode(parameterDefinitions.map(function (parameterDefinition) {
|
|
159
|
+
return _this.buildParamTypeNode(parameterDefinition);
|
|
160
|
+
}));
|
|
161
|
+
})),
|
|
162
|
+
});
|
|
163
|
+
})));
|
|
164
|
+
var pgFnReturnTypes = typescript_1.default.factory.createTypeAliasDeclaration(undefined, "PgFnReturnTypes", [
|
|
165
|
+
typescript_1.default.factory.createTypeParameterDeclaration(undefined, typescript_1.default.factory.createIdentifier("T"), typescript_1.default.factory.createTypeReferenceNode(pgFnNamesIdentifier)),
|
|
166
|
+
typescript_1.default.factory.createTypeParameterDeclaration(undefined, typescript_1.default.factory.createIdentifier("V"), typescript_1.default.factory.createTypeReferenceNode("PgFnParams", [
|
|
167
|
+
typescript_1.default.factory.createTypeReferenceNode("T"),
|
|
168
|
+
])),
|
|
169
|
+
], this.buildConditionalChain(typescript_1.default.factory.createTypeReferenceNode("T"), functionDefinitions.map(function (_a) {
|
|
170
|
+
var pgName = _a.pgName, overloads = _a.overloads;
|
|
171
|
+
return ({
|
|
172
|
+
extendsType: typescript_1.default.factory.createLiteralTypeNode(typescript_1.default.factory.createStringLiteral(pgName)),
|
|
173
|
+
resultType: _this.buildConditionalChain(typescript_1.default.factory.createTypeReferenceNode("V"), overloads.map(function (_a) {
|
|
174
|
+
var parameterDefinitions = _a.parameterDefinitions, returnTypeDefinition = _a.returnTypeDefinition;
|
|
175
|
+
return ({
|
|
176
|
+
extendsType: typescript_1.default.factory.createTupleTypeNode(parameterDefinitions.map(function (parameterDefinition) {
|
|
177
|
+
return _this.buildParamTypeNode(parameterDefinition);
|
|
178
|
+
})),
|
|
179
|
+
resultType: _this.buildReturnTypeNode(returnTypeDefinition),
|
|
180
|
+
});
|
|
181
|
+
})),
|
|
182
|
+
});
|
|
183
|
+
})));
|
|
184
|
+
var pgFn = this.createPgFn();
|
|
185
|
+
return [pgFnNamesType, pgFnParamsType, pgFnReturnTypes, pgFn];
|
|
186
|
+
};
|
|
187
|
+
KyselyTypeGenerator.prototype.getOverloads = function (functionDefinition) {
|
|
188
|
+
var _this = this;
|
|
189
|
+
return functionDefinition.overloadDefinitions
|
|
190
|
+
.flatMap(function (overload) {
|
|
191
|
+
return _this.getOptionalParameterPermutations(overload.parameterDefinitions).map(function (permutation) { return ({
|
|
192
|
+
parameterDefinitions: permutation,
|
|
193
|
+
returnTypeDefinition: overload.returnTypeDefinition,
|
|
194
|
+
}); });
|
|
195
|
+
})
|
|
196
|
+
.toSorted(function (a, b) {
|
|
197
|
+
var paramCountDiff = a.parameterDefinitions.length - b.parameterDefinitions.length;
|
|
198
|
+
if (paramCountDiff !== 0)
|
|
199
|
+
return paramCountDiff;
|
|
200
|
+
return (_this.countVariadicParams(a.parameterDefinitions) -
|
|
201
|
+
_this.countVariadicParams(b.parameterDefinitions));
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
KyselyTypeGenerator.prototype.getOptionalParameterPermutations = function (params) {
|
|
205
|
+
var permutation = params.map(function (p) { return ({
|
|
206
|
+
tsType: p.tsType,
|
|
207
|
+
isNullable: p.isNullable,
|
|
208
|
+
isArray: p.isArray,
|
|
209
|
+
isVariadic: p.isVariadic,
|
|
210
|
+
}); });
|
|
211
|
+
var lastParam = params.at(-1);
|
|
212
|
+
var rest = (lastParam === null || lastParam === void 0 ? void 0 : lastParam.isOptional)
|
|
213
|
+
? this.getOptionalParameterPermutations(params.slice(0, -1))
|
|
214
|
+
: [];
|
|
215
|
+
return __spreadArray([permutation], rest, true);
|
|
216
|
+
};
|
|
217
|
+
KyselyTypeGenerator.prototype.countVariadicParams = function (params) {
|
|
218
|
+
return params.filter(function (p) { return p.isVariadic; }).length;
|
|
219
|
+
};
|
|
220
|
+
KyselyTypeGenerator.prototype.buildConditionalChain = function (checkType, branches) {
|
|
221
|
+
return branches.reduceRight(function (falseType, _a) {
|
|
222
|
+
var extendsType = _a.extendsType, resultType = _a.resultType;
|
|
223
|
+
return typescript_1.default.factory.createConditionalTypeNode(checkType, extendsType, resultType, falseType);
|
|
224
|
+
}, typescript_1.default.factory.createKeywordTypeNode(typescript_1.default.SyntaxKind.NeverKeyword));
|
|
225
|
+
};
|
|
226
|
+
KyselyTypeGenerator.prototype.buildParamTypeNode = function (param) {
|
|
227
|
+
var type = this.tsTypeToTypeNode(param.tsType);
|
|
228
|
+
if (param.isArray && !param.isVariadic) {
|
|
229
|
+
type = typescript_1.default.factory.createArrayTypeNode(type);
|
|
230
|
+
}
|
|
231
|
+
type = this.applyNullable(type, param.isNullable);
|
|
232
|
+
type = typescript_1.default.factory.createTypeReferenceNode("Expression", [type]);
|
|
233
|
+
if (param.isVariadic) {
|
|
234
|
+
type = typescript_1.default.factory.createRestTypeNode(typescript_1.default.factory.createArrayTypeNode(type));
|
|
235
|
+
}
|
|
236
|
+
return type;
|
|
237
|
+
};
|
|
238
|
+
KyselyTypeGenerator.prototype.buildReturnTypeNode = function (returnType) {
|
|
239
|
+
var type = this.tsTypeToTypeNode(returnType.tsType);
|
|
240
|
+
if (returnType.isArray) {
|
|
241
|
+
type = typescript_1.default.factory.createArrayTypeNode(type);
|
|
242
|
+
}
|
|
243
|
+
type = this.applyNullable(type, returnType.isNullable);
|
|
244
|
+
return type;
|
|
245
|
+
};
|
|
246
|
+
KyselyTypeGenerator.prototype.createPgFn = function () {
|
|
247
|
+
return typescript_1.default.factory.createFunctionDeclaration([typescript_1.default.factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword)], undefined, "pgFn", [
|
|
248
|
+
typescript_1.default.factory.createTypeParameterDeclaration(undefined, typescript_1.default.factory.createIdentifier("T"), typescript_1.default.factory.createTypeReferenceNode("PgFnNames")),
|
|
249
|
+
typescript_1.default.factory.createTypeParameterDeclaration(undefined, typescript_1.default.factory.createIdentifier("V"), typescript_1.default.factory.createTypeReferenceNode("PgFnParams", [
|
|
250
|
+
typescript_1.default.factory.createTypeReferenceNode("T"),
|
|
251
|
+
])),
|
|
252
|
+
], [
|
|
253
|
+
typescript_1.default.factory.createParameterDeclaration(undefined, undefined, "fn", undefined, typescript_1.default.factory.createTypeReferenceNode("T")),
|
|
254
|
+
typescript_1.default.factory.createParameterDeclaration(undefined, undefined, "args", undefined, typescript_1.default.factory.createTypeReferenceNode("V")),
|
|
255
|
+
], typescript_1.default.factory.createTypeReferenceNode("RawBuilder", [
|
|
256
|
+
typescript_1.default.factory.createTypeReferenceNode("PgFnReturnTypes", [
|
|
257
|
+
typescript_1.default.factory.createTypeReferenceNode("T"),
|
|
258
|
+
typescript_1.default.factory.createTypeReferenceNode("V"),
|
|
259
|
+
]),
|
|
260
|
+
]), typescript_1.default.factory.createBlock([
|
|
261
|
+
typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createTaggedTemplateExpression(typescript_1.default.factory.createIdentifier("sql"), [
|
|
262
|
+
typescript_1.default.factory.createTypeReferenceNode("PgFnReturnTypes", [
|
|
263
|
+
typescript_1.default.factory.createTypeReferenceNode("T"),
|
|
264
|
+
typescript_1.default.factory.createTypeReferenceNode("V"),
|
|
265
|
+
]),
|
|
266
|
+
], typescript_1.default.factory.createTemplateExpression(typescript_1.default.factory.createTemplateHead(""), [
|
|
267
|
+
typescript_1.default.factory.createTemplateSpan(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier("sql"), "raw"), undefined, [typescript_1.default.factory.createIdentifier("fn")]), typescript_1.default.factory.createTemplateMiddle("(")),
|
|
268
|
+
typescript_1.default.factory.createTemplateSpan(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier("sql"), "join"), undefined, [typescript_1.default.factory.createIdentifier("args")]), typescript_1.default.factory.createTemplateTail(")")),
|
|
269
|
+
]))),
|
|
270
|
+
], true));
|
|
271
|
+
};
|
|
272
|
+
KyselyTypeGenerator.prototype.genImportStatements = function () {
|
|
273
|
+
return typescript_1.default.factory.createImportDeclaration(undefined, typescript_1.default.factory.createImportClause(false, undefined, typescript_1.default.factory.createNamedImports(this.kyselyImports.map(function (_a) {
|
|
274
|
+
var value = _a.value, isTypeImport = _a.isTypeImport;
|
|
275
|
+
return typescript_1.default.factory.createImportSpecifier(isTypeImport, undefined, typescript_1.default.factory.createIdentifier(value));
|
|
276
|
+
}))), typescript_1.default.factory.createStringLiteral("kysely", true));
|
|
277
|
+
};
|
|
278
|
+
KyselyTypeGenerator.prototype.tsTypeToTypeNode = function (tsType) {
|
|
279
|
+
return typeof tsType === "object"
|
|
280
|
+
? this.createEnumTypeNode(tsType)
|
|
281
|
+
: typescript_1.default.factory.createTypeReferenceNode(tsType, undefined);
|
|
282
|
+
};
|
|
283
|
+
KyselyTypeGenerator.prototype.createEnumTypeNode = function (_a) {
|
|
284
|
+
var _b, _c, _d;
|
|
285
|
+
var enumSchema = _a.enumSchema, enumName = _a.enumName;
|
|
286
|
+
var values = (_d = (_c = (_b = this.schemaDefinitions
|
|
287
|
+
.find(function (schemaDefinition) { return schemaDefinition.pgName === enumSchema; })) === null || _b === void 0 ? void 0 : _b.enumDefinitions.find(function (enumDefinition) { return enumDefinition.pgName === enumName; })) === null || _c === void 0 ? void 0 : _c.values) !== null && _d !== void 0 ? _d : [];
|
|
288
|
+
return values.length > 0
|
|
289
|
+
? typescript_1.default.factory.createUnionTypeNode(values.map(function (value) {
|
|
290
|
+
return typescript_1.default.factory.createLiteralTypeNode(typescript_1.default.factory.createStringLiteral(value));
|
|
291
|
+
}))
|
|
292
|
+
: typescript_1.default.factory.createKeywordTypeNode(typescript_1.default.SyntaxKind.NeverKeyword);
|
|
293
|
+
};
|
|
294
|
+
KyselyTypeGenerator.prototype.applyNullable = function (typeNode, isNullable) {
|
|
295
|
+
if (!isNullable)
|
|
296
|
+
return typeNode;
|
|
297
|
+
return typescript_1.default.factory.createUnionTypeNode([
|
|
298
|
+
typeNode,
|
|
299
|
+
typescript_1.default.factory.createLiteralTypeNode(typescript_1.default.factory.createNull()),
|
|
300
|
+
]);
|
|
301
|
+
};
|
|
302
|
+
KyselyTypeGenerator.prototype.applyTSDocComment = function (propertySignature, tsDocComment) {
|
|
303
|
+
if (tsDocComment) {
|
|
304
|
+
tsDocComment = tsDocComment.slice(tsDocComment.indexOf("/*") + "/*".length, tsDocComment.lastIndexOf("*/"));
|
|
305
|
+
propertySignature = typescript_1.default.addSyntheticLeadingComment(propertySignature, typescript_1.default.SyntaxKind.MultiLineCommentTrivia, tsDocComment, true);
|
|
306
|
+
}
|
|
307
|
+
return propertySignature;
|
|
308
|
+
};
|
|
309
|
+
KyselyTypeGenerator.prototype.formatOutput = function (output) {
|
|
310
|
+
return sync_1.default
|
|
311
|
+
.format(output, {
|
|
312
|
+
parser: "typescript",
|
|
313
|
+
plugins: [require.resolve("prettier-plugin-jsdoc")],
|
|
314
|
+
printWidth: 80,
|
|
315
|
+
tsDoc: true,
|
|
316
|
+
jsdocPreferCodeFences: true,
|
|
317
|
+
})
|
|
318
|
+
.trim();
|
|
319
|
+
};
|
|
320
|
+
KyselyTypeGenerator.KYSELY_GENERATED_TYPE = "Generated";
|
|
321
|
+
KyselyTypeGenerator.KYSELY_GENERATED_ALWAYS_TYPE = "GeneratedAlways";
|
|
322
|
+
return KyselyTypeGenerator;
|
|
323
|
+
}());
|
|
324
|
+
exports.KyselyTypeGenerator = KyselyTypeGenerator;
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "introspeql-kysely",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Powered by IntrospeQL, IntrospeQL-Kysely reads types from your Postgres database and generates a type definitions file that is compatible with Kysely",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md",
|
|
10
|
+
"LICENSE"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"Kysely",
|
|
17
|
+
"IntrospeQL",
|
|
18
|
+
"PostgreSQL",
|
|
19
|
+
"Postgres",
|
|
20
|
+
"TypeScript"
|
|
21
|
+
],
|
|
22
|
+
"author": "Joseph Dvorak",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/dvorakjt/introspeql-kysely.git"
|
|
26
|
+
},
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"type": "commonjs",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@prettier/sync": "^0.6.1",
|
|
31
|
+
"introspeql": "^2.0.1",
|
|
32
|
+
"prettier-plugin-jsdoc": "^1.8.0",
|
|
33
|
+
"typescript": "^5.9.3"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^25.3.5"
|
|
37
|
+
}
|
|
38
|
+
}
|