Skip to content

Commit 83d1f32

Browse files
authored
Revert "Revert "try to support Database as either interface or type alias""
1 parent 60f2aab commit 83d1f32

8 files changed

+33
-11
lines changed

example/beforeSchema.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type Json =
66
| { [key: string]: Json | undefined }
77
| Json[]
88

9-
export interface Database {
9+
export type Database = {
1010
public: {
1111
Tables: {
1212
todos: {

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"module": "dist/index.mjs",
99
"types": "dist/index.d.ts",
1010
"private": false,
11-
"version": "2.7.1",
11+
"version": "2.7.2",
1212
"license": "MIT",
1313
"scripts": {
1414
"build": "tsup ./src/index.ts --format cjs,esm --dts",
@@ -41,4 +41,4 @@
4141
"developer-tools",
4242
"automatic"
4343
]
44-
}
44+
}

src/utils/getDatabaseType.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { SourceFile, Node } from 'ts-morph';
2+
3+
export function getDatabaseType(sourceFile: SourceFile) {
4+
const databaseInterface = sourceFile.getInterface('Database');
5+
if (databaseInterface) {
6+
return databaseInterface;
7+
} else {
8+
const node = sourceFile
9+
.getTypeAliasOrThrow('Database')
10+
.getTypeNodeOrThrow();
11+
if (Node.isTypeLiteral(node)) {
12+
return node;
13+
} else {
14+
throw Error('Expected database type to be an object literal.');
15+
}
16+
}
17+
}

src/utils/getEnumsProperties.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { LiteralTypeNode, Project, SourceFile, ts } from 'ts-morph';
22
import { toCamelCase } from './toCamelCase';
33
import chalk from 'chalk';
44
import { toPascalCase } from './toPascalCase';
5+
import { getDatabaseType } from './getDatabaseType';
56

67
export function getEnumsProperties(
78
project: Project,
89
sourceFile: SourceFile,
910
schema: string
1011
) {
11-
const databaseInterface = sourceFile.getInterfaceOrThrow('Database');
12-
const publicProperty = databaseInterface.getPropertyOrThrow(schema);
12+
const databaseType = getDatabaseType(sourceFile);
13+
const publicProperty = databaseType.getPropertyOrThrow(schema);
1314
const publicType = publicProperty.getType();
1415

1516
const enumsProperty = publicType

src/utils/getFunctionProperties.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import chalk from 'chalk';
22
import { Project, SourceFile } from 'ts-morph';
3+
import { getDatabaseType } from './getDatabaseType';
34

45
export function getFunctionReturnTypes(
56
project: Project,
67
sourceFile: SourceFile,
78
schema: string
89
) {
9-
const databaseInterface = sourceFile.getInterfaceOrThrow('Database');
10-
const publicProperty = databaseInterface.getPropertyOrThrow(schema);
10+
const databaseType = getDatabaseType(sourceFile);
11+
const publicProperty = databaseType.getPropertyOrThrow(schema);
1112
const publicType = publicProperty.getType();
1213

1314
const functionProperty = publicType

src/utils/getSchemasProperties.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Project, SourceFile } from 'ts-morph';
2+
import { getDatabaseType } from './getDatabaseType';
23

34
export function getSchemasProperties(project: Project, sourceFile: SourceFile) {
4-
const databaseInterface = sourceFile.getInterfaceOrThrow('Database');
5+
const databaseType = getDatabaseType(sourceFile);
56

67
const schemasType = project
78
.getProgram()
89
.getTypeChecker()
9-
.getTypeAtLocation(databaseInterface);
10+
.getTypeAtLocation(databaseType);
1011
const schemasProperties = schemasType.getProperties();
1112

1213
if (schemasProperties.length < 1)

src/utils/getTablesProperties.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import chalk from 'chalk';
22
import { Project, SourceFile } from 'ts-morph';
3+
import { getDatabaseType } from './getDatabaseType';
34

45
export function getTablesProperties(
56
project: Project,
67
sourceFile: SourceFile,
78
schema: string
89
) {
9-
const databaseInterface = sourceFile.getInterfaceOrThrow('Database');
10+
const databaseInterface = getDatabaseType(sourceFile);
1011
const publicProperty = databaseInterface.getPropertyOrThrow(schema);
1112
const publicType = publicProperty.getType();
1213

src/utils/getViewsProperties.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import chalk from 'chalk';
22
import { Project, SourceFile } from 'ts-morph';
3+
import { getDatabaseType } from './getDatabaseType';
34

45
export function getViewsProperties(
56
project: Project,
67
sourceFile: SourceFile,
78
schema: string
89
) {
9-
const databaseInterface = sourceFile.getInterfaceOrThrow('Database');
10+
const databaseInterface = getDatabaseType(sourceFile);
1011
const publicProperty = databaseInterface.getPropertyOrThrow(schema);
1112
const publicType = publicProperty.getType();
1213

0 commit comments

Comments
 (0)