Skip to content

Commit

Permalink
fix: supabase new type gen broke this (#32)
Browse files Browse the repository at this point in the history
reapply "try to support Database as either interface or type alias" #32
  • Loading branch information
FroggyPanda authored Mar 3, 2024
2 parents 60f2aab + 83d1f32 commit ce651f1
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion example/beforeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type Json =
| { [key: string]: Json | undefined }
| Json[]

export interface Database {
export type Database = {
public: {
Tables: {
todos: {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"private": false,
"version": "2.7.1",
"version": "2.7.2",
"license": "MIT",
"scripts": {
"build": "tsup ./src/index.ts --format cjs,esm --dts",
Expand Down Expand Up @@ -41,4 +41,4 @@
"developer-tools",
"automatic"
]
}
}
17 changes: 17 additions & 0 deletions src/utils/getDatabaseType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SourceFile, Node } from 'ts-morph';

export function getDatabaseType(sourceFile: SourceFile) {
const databaseInterface = sourceFile.getInterface('Database');
if (databaseInterface) {
return databaseInterface;
} else {
const node = sourceFile
.getTypeAliasOrThrow('Database')
.getTypeNodeOrThrow();
if (Node.isTypeLiteral(node)) {
return node;
} else {
throw Error('Expected database type to be an object literal.');
}
}
}
5 changes: 3 additions & 2 deletions src/utils/getEnumsProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { LiteralTypeNode, Project, SourceFile, ts } from 'ts-morph';
import { toCamelCase } from './toCamelCase';
import chalk from 'chalk';
import { toPascalCase } from './toPascalCase';
import { getDatabaseType } from './getDatabaseType';

export function getEnumsProperties(
project: Project,
sourceFile: SourceFile,
schema: string
) {
const databaseInterface = sourceFile.getInterfaceOrThrow('Database');
const publicProperty = databaseInterface.getPropertyOrThrow(schema);
const databaseType = getDatabaseType(sourceFile);
const publicProperty = databaseType.getPropertyOrThrow(schema);
const publicType = publicProperty.getType();

const enumsProperty = publicType
Expand Down
5 changes: 3 additions & 2 deletions src/utils/getFunctionProperties.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import chalk from 'chalk';
import { Project, SourceFile } from 'ts-morph';
import { getDatabaseType } from './getDatabaseType';

export function getFunctionReturnTypes(
project: Project,
sourceFile: SourceFile,
schema: string
) {
const databaseInterface = sourceFile.getInterfaceOrThrow('Database');
const publicProperty = databaseInterface.getPropertyOrThrow(schema);
const databaseType = getDatabaseType(sourceFile);
const publicProperty = databaseType.getPropertyOrThrow(schema);
const publicType = publicProperty.getType();

const functionProperty = publicType
Expand Down
5 changes: 3 additions & 2 deletions src/utils/getSchemasProperties.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Project, SourceFile } from 'ts-morph';
import { getDatabaseType } from './getDatabaseType';

export function getSchemasProperties(project: Project, sourceFile: SourceFile) {
const databaseInterface = sourceFile.getInterfaceOrThrow('Database');
const databaseType = getDatabaseType(sourceFile);

const schemasType = project
.getProgram()
.getTypeChecker()
.getTypeAtLocation(databaseInterface);
.getTypeAtLocation(databaseType);
const schemasProperties = schemasType.getProperties();

if (schemasProperties.length < 1)
Expand Down
3 changes: 2 additions & 1 deletion src/utils/getTablesProperties.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import chalk from 'chalk';
import { Project, SourceFile } from 'ts-morph';
import { getDatabaseType } from './getDatabaseType';

export function getTablesProperties(
project: Project,
sourceFile: SourceFile,
schema: string
) {
const databaseInterface = sourceFile.getInterfaceOrThrow('Database');
const databaseInterface = getDatabaseType(sourceFile);
const publicProperty = databaseInterface.getPropertyOrThrow(schema);
const publicType = publicProperty.getType();

Expand Down
3 changes: 2 additions & 1 deletion src/utils/getViewsProperties.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import chalk from 'chalk';
import { Project, SourceFile } from 'ts-morph';
import { getDatabaseType } from './getDatabaseType';

export function getViewsProperties(
project: Project,
sourceFile: SourceFile,
schema: string
) {
const databaseInterface = sourceFile.getInterfaceOrThrow('Database');
const databaseInterface = getDatabaseType(sourceFile);
const publicProperty = databaseInterface.getPropertyOrThrow(schema);
const publicType = publicProperty.getType();

Expand Down

0 comments on commit ce651f1

Please sign in to comment.