Skip to content

Commit

Permalink
Merge pull request Yedidyar#6 from Yedidyar/4-implement-foreign-keys-…
Browse files Browse the repository at this point in the history
…such-as-it-will-be-robust

feat: add getForeignKeys & extract commands & add types
  • Loading branch information
Yedidyar authored Aug 29, 2023
2 parents 73fac13 + 5430da1 commit 21f6c35
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 184 deletions.
33 changes: 33 additions & 0 deletions source/DbTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ForeignKey } from "./postgres/commands.js";
import { toCamelCase, toPascalCase } from "./utils/case.js";

export class DbTable {
schema: string;
dbName: string;
key: string;
className: string;
foreignKeys: {
columnName: string;
foreignClassName: string;
}[];

constructor(dbName: string, schema: string, foreignKeys: ForeignKey[]) {
this.schema = schema;
this.dbName = dbName;

this.foreignKeys = foreignKeys.map(
({ foreign_table_name, column_name: columnName }) => ({
columnName,
foreignClassName: toPascalCase(foreign_table_name),
})
);
this.className = toPascalCase(dbName);

this.key = toCamelCase(this.className) + "s";

// TODO: kinda hacky provide a custom mapping?
if (this.key.endsWith("ys")) {
this.key = this.key.slice(0, -2) + "ies";
}
}
}
Loading

0 comments on commit 21f6c35

Please sign in to comment.