From 35abff089be38a869076de37bcb5dbb30b05bc7e Mon Sep 17 00:00:00 2001 From: Tiago Castro Date: Wed, 24 Jul 2024 18:38:27 +0100 Subject: [PATCH] Reverth all columnstore and rowstore work --- drizzle-orm/src/singlestore-core/indexes.ts | 48 ++++----- drizzle-orm/src/singlestore-core/schema.ts | 4 +- drizzle-orm/src/singlestore-core/table.ts | 106 ++++---------------- 3 files changed, 41 insertions(+), 117 deletions(-) diff --git a/drizzle-orm/src/singlestore-core/indexes.ts b/drizzle-orm/src/singlestore-core/indexes.ts index 4fc25f840..59d2bfb11 100644 --- a/drizzle-orm/src/singlestore-core/indexes.ts +++ b/drizzle-orm/src/singlestore-core/indexes.ts @@ -3,7 +3,7 @@ import type { SQL } from '~/sql/sql.ts'; import type { AnySingleStoreColumn, SingleStoreColumn } from './columns/index.ts'; import type { SingleStoreTable } from './table.ts'; -interface IndexCommonConfig { +interface IndexConfig { name: string; columns: IndexColumn[]; @@ -13,6 +13,11 @@ interface IndexCommonConfig { */ unique?: boolean; + /** + * If set, the index will be created as `create index ... using { 'btree' | 'hash' }`. + */ + using?: 'btree' | 'hash'; + /** * If set, the index will be created as `create index ... algorythm { 'default' | 'inplace' | 'copy' }`. */ @@ -24,20 +29,6 @@ interface IndexCommonConfig { lock?: 'default' | 'none' | 'shared' | 'exclusive'; } -type IndexColumnstoreConfig = IndexCommonConfig & { - /** - * If set, the index will be created as `create index ... using { 'hash' }`. - */ - using?: 'hash'; -}; -type IndexRowstoreConfig = IndexCommonConfig & { - /** - * If set, the index will be created as `create index ... using { 'btree' | 'hash' }`. - */ - using?: 'btree' | 'hash'; -}; -type IndexConfig = IndexColumnstoreConfig | IndexRowstoreConfig; - export type IndexColumn = SingleStoreColumn | SQL; export class IndexBuilderOn { @@ -50,31 +41,38 @@ export class IndexBuilderOn { } } -export class IndexBuilder { +export interface AnyIndexBuilder { + build(table: SingleStoreTable): Index; +} + +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface IndexBuilder extends AnyIndexBuilder {} + +export class IndexBuilder implements AnyIndexBuilder { static readonly [entityKind]: string = 'SingleStoreIndexBuilder'; /** @internal */ - config: TConfig; + config: IndexConfig; constructor(name: string, columns: IndexColumn[], unique: boolean) { this.config = { name, columns, unique, - } as TConfig; + }; } - using(using: TConfig['using']): this { + using(using: IndexConfig['using']): this { this.config.using = using; return this; } - algorythm(algorythm: TConfig['algorythm']): this { + algorythm(algorythm: IndexConfig['algorythm']): this { this.config.algorythm = algorythm; return this; } - lock(lock: TConfig['lock']): this { + lock(lock: IndexConfig['lock']): this { this.config.lock = lock; return this; } @@ -85,14 +83,6 @@ export class IndexBuilder { } } -export class IndexColumnstoreBuilder extends IndexBuilder { - static readonly [entityKind]: string = 'SingleStoreColumnstoreIndexBuilder'; -} - -export class IndexRowstoreBuilder extends IndexBuilder { - static readonly [entityKind]: string = 'SingleStoreRowstoreIndexBuilder'; -} - export class Index { static readonly [entityKind]: string = 'SingleStoreIndex'; diff --git a/drizzle-orm/src/singlestore-core/schema.ts b/drizzle-orm/src/singlestore-core/schema.ts index 2a278e4e0..82da44a49 100644 --- a/drizzle-orm/src/singlestore-core/schema.ts +++ b/drizzle-orm/src/singlestore-core/schema.ts @@ -1,5 +1,5 @@ import { entityKind, is } from '~/entity.ts'; -import { type SingleStoreTableFn, SinglestoreTableType, singlestoreTableWithSchema } from './table.ts'; +import { type SingleStoreTableFn, singlestoreTableWithSchema } from './table.ts'; import { type singlestoreView, singlestoreViewWithSchema } from './view.ts'; export class SingleStoreSchema { @@ -10,7 +10,7 @@ export class SingleStoreSchema { ) {} table: SingleStoreTableFn = (name, columns, extraConfig) => { - return singlestoreTableWithSchema(SinglestoreTableType.columnstore, name, columns, extraConfig, this.schemaName); + return singlestoreTableWithSchema(name, columns, extraConfig, this.schemaName); }; view = ((name, columns) => { diff --git a/drizzle-orm/src/singlestore-core/table.ts b/drizzle-orm/src/singlestore-core/table.ts index cca6fc631..16db288e9 100644 --- a/drizzle-orm/src/singlestore-core/table.ts +++ b/drizzle-orm/src/singlestore-core/table.ts @@ -3,60 +3,38 @@ import { entityKind } from '~/entity.ts'; import { Table, type TableConfig as TableConfigBase, type UpdateTableConfig } from '~/table.ts'; import type { CheckBuilder } from './checks.ts'; import type { SingleStoreColumn, SingleStoreColumnBuilder, SingleStoreColumnBuilderBase } from './columns/common.ts'; +import type { AnyIndexBuilder } from './indexes.ts'; import type { PrimaryKeyBuilder } from './primary-keys.ts'; import type { UniqueConstraintBuilder } from './unique-constraint.ts'; -import type { IndexColumnstoreBuilder, IndexRowstoreBuilder } from './indexes.ts'; -export type SingleStoreColumnstoreTableExtraConfig = Record< +export type SingleStoreTableExtraConfig = Record< string, - | IndexColumnstoreBuilder + | AnyIndexBuilder | CheckBuilder | PrimaryKeyBuilder | UniqueConstraintBuilder >; -export type SingleStoreRowstoreTableExtraConfig = Record< - string, - | IndexRowstoreBuilder - | CheckBuilder - | PrimaryKeyBuilder - | UniqueConstraintBuilder ->; -export type SingleStoreTableExtraConfig = SingleStoreColumnstoreTableExtraConfig | SingleStoreRowstoreTableExtraConfig; export type TableConfig = TableConfigBase; -export abstract class SingleStoreTable< - TColumn extends TableConfig = TableConfig, - TExtraConfig extends SingleStoreTableExtraConfig = SingleStoreTableExtraConfig, -> extends Table { +export class SingleStoreTable extends Table { static readonly [entityKind]: string = 'SingleStoreTable'; - declare protected $columns: TColumn['columns']; + declare protected $columns: T['columns']; /** @internal */ - static override readonly Symbol = Object.assign({}, Table.Symbol, {}); + static override readonly Symbol = Object.assign({}, Table.Symbol, { + }); /** @internal */ - override [Table.Symbol.Columns]!: NonNullable; + override [Table.Symbol.Columns]!: NonNullable; /** @internal */ override [Table.Symbol.ExtraConfigBuilder]: - | ((self: Record) => TExtraConfig) + | ((self: Record) => SingleStoreTableExtraConfig) | undefined = undefined; } -export class SingleStoreRowstoreTable - extends SingleStoreTable -{ - static readonly [entityKind]: string = 'SingleStoreRowstoreTable'; -} - -export class SingleStoreColumnstoreTable - extends SingleStoreTable -{ - static readonly [entityKind]: string = 'SingleStoreColumnstoreTable'; -} - export type AnySingleStoreTable = {}> = SingleStoreTable< UpdateTableConfig >; @@ -67,17 +45,11 @@ export type SingleStoreTableWithColumns = [Key in keyof T['columns']]: T['columns'][Key]; }; -export enum SinglestoreTableType { - rowstore, - columnstore, -} - export function singlestoreTableWithSchema< TTableName extends string, TSchemaName extends string | undefined, TColumnsMap extends Record, >( - type: SinglestoreTableType, name: TTableName, columns: TColumnsMap, extraConfig: @@ -91,27 +63,12 @@ export function singlestoreTableWithSchema< columns: BuildColumns; dialect: 'singlestore'; }> { - let rawTable; - switch (type) { - case SinglestoreTableType.columnstore: { - rawTable = new SingleStoreColumnstoreTable<{ - name: TTableName; - schema: TSchemaName; - columns: BuildColumns; - dialect: 'singlestore'; - }>(name, schema, baseName); - break; - } - case SinglestoreTableType.rowstore: { - rawTable = new SingleStoreRowstoreTable<{ - name: TTableName; - schema: TSchemaName; - columns: BuildColumns; - dialect: 'singlestore'; - }>(name, schema, baseName); - break; - } - } + const rawTable = new SingleStoreTable<{ + name: TTableName; + schema: TSchemaName; + columns: BuildColumns; + dialect: 'singlestore'; + }>(name, schema, baseName); const builtColumns = Object.fromEntries( Object.entries(columns).map(([name, colBuilderBase]) => { @@ -139,17 +96,14 @@ export function singlestoreTableWithSchema< return table; } -export interface SingleStoreTableFn< - TSchemaName extends string | undefined = undefined, - TExtraConfig extends SingleStoreTableExtraConfig = SingleStoreTableExtraConfig, -> { +export interface SingleStoreTableFn { < TTableName extends string, TColumnsMap extends Record, >( name: TTableName, columns: TColumnsMap, - extraConfig?: (self: BuildColumns) => TExtraConfig, + extraConfig?: (self: BuildColumns) => SingleStoreTableExtraConfig, ): SingleStoreTableWithColumns<{ name: TTableName; schema: TSchemaName; @@ -158,32 +112,12 @@ export interface SingleStoreTableFn< }>; } -export const singlestoreTable: SingleStoreTableFn = ( - name, - columns, - extraConfig, -) => { - return singlestoreTableWithSchema(SinglestoreTableType.columnstore, name, columns, extraConfig, undefined, name); -}; - -// TODO: Need to access drizzle kit to add rowstore support on migrations -export const singlestoreRowstoreTable: SingleStoreTableFn = ( - name, - columns, - extraConfig, -) => { - return singlestoreTableWithSchema(SinglestoreTableType.rowstore, name, columns, extraConfig, undefined, name); +export const singlestoreTable: SingleStoreTableFn = (name, columns, extraConfig) => { + return singlestoreTableWithSchema(name, columns, extraConfig, undefined, name); }; export function singlestoreTableCreator(customizeTableName: (name: string) => string): SingleStoreTableFn { return (name, columns, extraConfig) => { - return singlestoreTableWithSchema( - SinglestoreTableType.columnstore, - customizeTableName(name) as typeof name, - columns, - extraConfig, - undefined, - name, - ); + return singlestoreTableWithSchema(customizeTableName(name) as typeof name, columns, extraConfig, undefined, name); }; }