Skip to content

Commit

Permalink
Reverth all columnstore and rowstore work
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagoacastro committed Jul 24, 2024
1 parent fbf33ec commit 35abff0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 117 deletions.
48 changes: 19 additions & 29 deletions drizzle-orm/src/singlestore-core/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand All @@ -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' }`.
*/
Expand All @@ -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 {
Expand All @@ -50,31 +41,38 @@ export class IndexBuilderOn {
}
}

export class IndexBuilder<TConfig extends IndexConfig = IndexConfig> {
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;
}
Expand All @@ -85,14 +83,6 @@ export class IndexBuilder<TConfig extends IndexConfig = IndexConfig> {
}
}

export class IndexColumnstoreBuilder extends IndexBuilder<IndexColumnstoreConfig> {
static readonly [entityKind]: string = 'SingleStoreColumnstoreIndexBuilder';
}

export class IndexRowstoreBuilder extends IndexBuilder<IndexRowstoreConfig> {
static readonly [entityKind]: string = 'SingleStoreRowstoreIndexBuilder';
}

export class Index {
static readonly [entityKind]: string = 'SingleStoreIndex';

Expand Down
4 changes: 2 additions & 2 deletions drizzle-orm/src/singlestore-core/schema.ts
Original file line number Diff line number Diff line change
@@ -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<TName extends string = string> {
Expand All @@ -10,7 +10,7 @@ export class SingleStoreSchema<TName extends string = string> {
) {}

table: SingleStoreTableFn<TName> = (name, columns, extraConfig) => {
return singlestoreTableWithSchema(SinglestoreTableType.columnstore, name, columns, extraConfig, this.schemaName);
return singlestoreTableWithSchema(name, columns, extraConfig, this.schemaName);
};

view = ((name, columns) => {
Expand Down
106 changes: 20 additions & 86 deletions drizzle-orm/src/singlestore-core/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SingleStoreColumn>;

export abstract class SingleStoreTable<
TColumn extends TableConfig = TableConfig,
TExtraConfig extends SingleStoreTableExtraConfig = SingleStoreTableExtraConfig,
> extends Table<TColumn> {
export class SingleStoreTable<T extends TableConfig = TableConfig> extends Table<T> {
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<TColumn['columns']>;
override [Table.Symbol.Columns]!: NonNullable<T['columns']>;

/** @internal */
override [Table.Symbol.ExtraConfigBuilder]:
| ((self: Record<string, SingleStoreColumn>) => TExtraConfig)
| ((self: Record<string, SingleStoreColumn>) => SingleStoreTableExtraConfig)
| undefined = undefined;
}

export class SingleStoreRowstoreTable<T extends TableConfig = TableConfig>
extends SingleStoreTable<T, SingleStoreRowstoreTableExtraConfig>
{
static readonly [entityKind]: string = 'SingleStoreRowstoreTable';
}

export class SingleStoreColumnstoreTable<T extends TableConfig = TableConfig>
extends SingleStoreTable<T, SingleStoreColumnstoreTableExtraConfig>
{
static readonly [entityKind]: string = 'SingleStoreColumnstoreTable';
}

export type AnySingleStoreTable<TPartial extends Partial<TableConfig> = {}> = SingleStoreTable<
UpdateTableConfig<TableConfig, TPartial>
>;
Expand All @@ -67,17 +45,11 @@ export type SingleStoreTableWithColumns<T extends TableConfig> =
[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<string, SingleStoreColumnBuilderBase>,
>(
type: SinglestoreTableType,
name: TTableName,
columns: TColumnsMap,
extraConfig:
Expand All @@ -91,27 +63,12 @@ export function singlestoreTableWithSchema<
columns: BuildColumns<TTableName, TColumnsMap, 'singlestore'>;
dialect: 'singlestore';
}> {
let rawTable;
switch (type) {
case SinglestoreTableType.columnstore: {
rawTable = new SingleStoreColumnstoreTable<{
name: TTableName;
schema: TSchemaName;
columns: BuildColumns<TTableName, TColumnsMap, 'singlestore'>;
dialect: 'singlestore';
}>(name, schema, baseName);
break;
}
case SinglestoreTableType.rowstore: {
rawTable = new SingleStoreRowstoreTable<{
name: TTableName;
schema: TSchemaName;
columns: BuildColumns<TTableName, TColumnsMap, 'singlestore'>;
dialect: 'singlestore';
}>(name, schema, baseName);
break;
}
}
const rawTable = new SingleStoreTable<{
name: TTableName;
schema: TSchemaName;
columns: BuildColumns<TTableName, TColumnsMap, 'singlestore'>;
dialect: 'singlestore';
}>(name, schema, baseName);

const builtColumns = Object.fromEntries(
Object.entries(columns).map(([name, colBuilderBase]) => {
Expand Down Expand Up @@ -139,17 +96,14 @@ export function singlestoreTableWithSchema<
return table;
}

export interface SingleStoreTableFn<
TSchemaName extends string | undefined = undefined,
TExtraConfig extends SingleStoreTableExtraConfig = SingleStoreTableExtraConfig,
> {
export interface SingleStoreTableFn<TSchemaName extends string | undefined = undefined> {
<
TTableName extends string,
TColumnsMap extends Record<string, SingleStoreColumnBuilderBase>,
>(
name: TTableName,
columns: TColumnsMap,
extraConfig?: (self: BuildColumns<TTableName, TColumnsMap, 'singlestore'>) => TExtraConfig,
extraConfig?: (self: BuildColumns<TTableName, TColumnsMap, 'singlestore'>) => SingleStoreTableExtraConfig,
): SingleStoreTableWithColumns<{
name: TTableName;
schema: TSchemaName;
Expand All @@ -158,32 +112,12 @@ export interface SingleStoreTableFn<
}>;
}

export const singlestoreTable: SingleStoreTableFn<undefined, SingleStoreColumnstoreTableExtraConfig> = (
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<undefined, SingleStoreRowstoreTableExtraConfig> = (
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);
};
}

0 comments on commit 35abff0

Please sign in to comment.