import { StringOrNull } from 'src/Types' ; export interface ITableBuilder { WithDatabase ( databaseName ) : ITableBuilder ; WithVersion ( version ) : ITableBuilder ; WithTableName ( tableName ) : ITableBuilder ; WithPrimaryField ( primaryField ) : ITableBuilder ; WithIndexName ( indexName ) : ITableBuilder ; } export interface ITable { Database ( ) : string ; Version ( ) : number ; TableName ( ) : string ; IndexName ( ) : string ; Build ( database ) : void ; } export class TableBuilder implements ITableBuilder , ITable { private database : StringOrNull ; private tableName : StringOrNull ; private primaryField : StringOrNull ; private indexName : StringOrNull ; private version : number = 1 ; public WithDatabase ( databaseName ) { this . database = databaseName ; return this ; } public WithVersion ( versionNumber ) { this . version = versionNumber ; return this ; } public WithTableName ( tableName ) { this . tableName = tableName ; return this ; } public WithPrimaryField ( primaryField ) { this . primaryField = primaryField ; return this ; } public WithIndexName ( indexName ) { this . indexName = indexName ; return this ; } public Database ( ) { if ( ! this . database ) { throw new Error ( "You must give a database name" ) ; } return this . database ; } public Version ( ) { return this . version ; } public TableName ( ) { if ( ! this . tableName ) { throw new Error ( "You must give a table name" ) ; } return this . tableName ; } public IndexName ( ) { if ( ! this . indexName ) { throw new Error ( "You must specify an index name" ) ; } return this . indexName ; } public Build ( database ) { if ( ! this . tableName ) { throw new Error ( "You must specify the table name" ) ; } if ( ! this . primaryField ) { throw new Error ( "You must specify a primary field" ) ; } if ( ! this . indexName ) { throw new Error ( "You must specify the index name" ) ; } const parameters = { keyPath : this . primaryField } ; const objectStore = database . createObjectStore ( this . tableName , parameters ) ; objectStore ! . createIndex ( this . indexName , this . primaryField ) ; } }