Skip to content

Commit

Permalink
chore: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicGBauer committed Aug 28, 2024
1 parent cded655 commit e0e8dcb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
10 changes: 9 additions & 1 deletion packages/common/src/db/schema/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,21 @@ export class Schema<S extends SchemaType = SchemaType> {

toJSON() {
return {
// This is required because "name" field is not present in TableV2
tables: this.tables.map((t) => t.toJSON())
};
}

private convertToClassicTables(props: S) {
return Object.entries(props).map(([name, table]) => {
const convertedTable = new Table({ name, columns: table.columns });
const convertedTable = new Table({
name,
columns: table.columns,
indexes: table.indexes,
localOnly: table.localOnly,
insertOnly: table.insertOnly,
viewName: table.viewName
});
return convertedTable;
});
}
Expand Down
53 changes: 52 additions & 1 deletion packages/common/src/db/schema/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,66 @@ export class Table<Columns extends ColumnsType = ColumnsType> {
static createTable(name: string, table: Table) {
return new Table({
name,
columns: Object.entries(table.columns).map(([name, col]) => new Column({ name, type: col.type })),
columns: table.columns,
indexes: table.indexes,
localOnly: table.options.localOnly,
insertOnly: table.options.insertOnly,
viewName: table.options.viewName
});
}

/**
* Creates a new Table instance.
*
* This constructor supports two different versions:
* 1. New constructor: Using a Columns object and an optional TableV2Options object
* 2. Deprecated constructor: Using a TableOptions object (will be removed in the next major release)
*
* @constructor
* @param {Columns | TableOptions} optionsOrColumns - Either a Columns object (for V2 syntax) or a TableOptions object (for V1 syntax)
* @param {TableV2Options} [v2Options] - Optional configuration options for V2 syntax
*
* @example
* <caption>New constructor example</caption>
* ```javascript
* const table = new Table(
* {
* name: { type: ColumnType.TEXT },
* age: { type: ColumnType.INTEGER }
* },
* { indexes: { nameIndex: ['name'] } }
* );
*```
*
*
* @example
* <caption>Deprecated constructor example</caption>
* ```javascript
* const table = new Table({
* name: 'users',
* columns: [
* new Column({ name: 'name', type: ColumnType.TEXT }),
* new Column({ name: 'age', type: ColumnType.INTEGER })
* ]
* });
*```
*/
constructor(columns: Columns, options?: TableV2Options);
/**
* @deprecated This constructor will be removed in the next major release.
* Use the new constructor shown below instead.
* @example
* <caption>Use this instead</caption>
* ```javascript
* const table = new Table(
* {
* name: { type: ColumnType.TEXT },
* age: { type: ColumnType.INTEGER }
* },
* { indexes: { nameIndex: ['name'] } }
* );
*```
*/
constructor(options: TableOptions);
constructor(optionsOrColumns: Columns | TableOptions, v2Options?: TableV2Options) {
if (this.isTableV1(optionsOrColumns)) {
Expand Down
4 changes: 3 additions & 1 deletion packages/common/src/db/schema/TableV2.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ColumnsType } from './Column';
import { Table } from './Table';

/*
/**
Generate a new table from the columns and indexes
@deprecated You should use {@link Table} instead as it now allows TableV2 syntax.
This will be removed in the next major release.
*/
export class TableV2<Columns extends ColumnsType = ColumnsType> extends Table<Columns> {}

0 comments on commit e0e8dcb

Please sign in to comment.