-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common-sdk): add schema and table enhancements
- Loading branch information
DominicGBauer
committed
Feb 29, 2024
1 parent
2780270
commit 5bca7a0
Showing
7 changed files
with
51 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,45 @@ | ||
import type { Table as OldTable } from './Table'; | ||
import { RowType, Table } from './TableV2'; | ||
import { Table as ClassicTable } from './Table'; | ||
import { RowType, TableV2 } from './TableV2'; | ||
|
||
export type SchemaType = Record<string, Table<any>>; | ||
type SchemaType = Record<string, TableV2<any>>; | ||
|
||
export type SchemaTableType<S extends SchemaType> = { | ||
type SchemaTableType<S extends SchemaType> = { | ||
[K in keyof S]: RowType<S[K]>; | ||
}; | ||
|
||
export class Schema<S extends SchemaType> { | ||
/** | ||
* A schema is a collection of tables. It is used to define the structure of a database. | ||
*/ | ||
export class Schema<S extends SchemaType = SchemaType> { | ||
/* | ||
Only available when constructing with mapped typed definition columns | ||
*/ | ||
readonly types: SchemaTableType<S>; | ||
readonly props: S; | ||
constructor(public tables: OldTable[] | S) { | ||
constructor(public tables: ClassicTable[] | S) { | ||
if (Array.isArray(tables)) { | ||
this.tables = tables; | ||
} else { | ||
this.props = tables as S; | ||
this.tables = this.convertToTables(this.props); | ||
this.tables = this.convertToClassicTables(this.props); | ||
} | ||
} | ||
|
||
build() { | ||
return new Schema(this.tables); | ||
} | ||
|
||
validate() { | ||
for (const table of this.tables as OldTable[]) { | ||
for (const table of this.tables as ClassicTable[]) { | ||
table.validate(); | ||
} | ||
} | ||
|
||
toJSON() { | ||
return { | ||
tables: (this.tables as OldTable[]).map((t) => t.toJSON()) | ||
tables: (this.tables as ClassicTable[]).map((t) => t.toJSON()) | ||
}; | ||
} | ||
|
||
private convertToTables(props: S) { | ||
return Object.entries(props).map(([name, type]) => { | ||
return type.table(name); | ||
private convertToClassicTables(props: S) { | ||
return Object.entries(props).map(([name, table]) => { | ||
return ClassicTable.createTable(name, table); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters