Skip to content

Commit

Permalink
Fixed detach
Browse files Browse the repository at this point in the history
  • Loading branch information
drodrigues4 committed Jul 24, 2024
1 parent da7e81f commit 8e61e25
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
12 changes: 6 additions & 6 deletions drizzle-orm/src/singlestore-core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type {
} from './session.ts';
import type { WithSubqueryWithSelection } from './subquery.ts';
import type { SingleStoreTable } from './table.ts';
// import { SingleStoreDetachBase } from './query-builders/detach.ts';
import { SingleStoreDetachBase } from './query-builders/detach.ts';

export class SingleStoreDatabase<
TQueryResult extends SingleStoreQueryResultHKT,
Expand Down Expand Up @@ -475,11 +475,11 @@ export class SingleStoreDatabase<
return this.session.transaction(transaction, config);
}

// detach<TDatabase extends string>(
// db: TDatabase,
// ): SingleStoreDetachBase<TDatabase, TQueryResult, TPreparedQueryHKT> {
// return new SingleStoreDetachBase(db, this.session, this.dialect);
// }
detach<TDatabase extends string>(
database: TDatabase,
): SingleStoreDetachBase<TDatabase, TQueryResult, TPreparedQueryHKT> {
return new SingleStoreDetachBase(database, this.session, this.dialect);
}
}

export type SingleStoreWithReplicas<Q> = Q & { $primary: Q };
Expand Down
14 changes: 8 additions & 6 deletions drizzle-orm/src/singlestore-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ export class SingleStoreDialect {
return sql`${withSql}delete from ${table}${whereSql}${returningSql}`;
}

buildDetachQuery({ database, milestone, workspace }: SingleStoreDetachConfig): SQL {
const milestoneSql = milestone ? sql` at milestone ${milestone}` : undefined;

const workspaceSql = workspace ? sql` from workspace ${workspace}` : undefined;

return sql`delete from ${database}${milestoneSql}${workspaceSql}`;
}

buildUpdateSet(table: SingleStoreTable, set: UpdateSet): SQL {
const tableColumns = table[Table.Symbol.Columns];

Expand Down Expand Up @@ -1079,10 +1087,4 @@ export class SingleStoreDialect {
selection,
};
}

buildDetachQuery({ database, milestone }: SingleStoreDetachConfig): SQL {
const milestoneSql = milestone ? sql` at milestone ${milestone}` : undefined;

return sql`detach database ${database}${milestoneSql}`;
}
}
46 changes: 36 additions & 10 deletions drizzle-orm/src/singlestore-core/query-builders/detach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,32 @@ import type {
} from '~/singlestore-core/session.ts';
import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts';

export type SingleStoreDetachWithout<
T extends AnySingleStoreDetachBase,
TDynamic extends boolean,
K extends keyof T & string,
> = TDynamic extends true ? T
: Omit<
SingleStoreDetachBase<
T['_']['database'],
T['_']['queryResult'],
T['_']['preparedQueryHKT'],
TDynamic,
T['_']['excludedMethods'] | K
>,
T['_']['excludedMethods'] | K
>;

export type SingleStoreDetach<
TDatabase extends string = string,
TQueryResult extends SingleStoreQueryResultHKT = AnySingleStoreQueryResultHKT,
TPreparedQueryHKT extends PreparedQueryHKTBase = PreparedQueryHKTBase,
> = SingleStoreDetachBase<TDatabase, TQueryResult, TPreparedQueryHKT, never>;
> = SingleStoreDetachBase<TDatabase, TQueryResult, TPreparedQueryHKT, true, never>;

export interface SingleStoreDetachConfig {
database: string;
milestone?: string | undefined;
database: string;
workspace?: string | undefined;
}

export type SingleStoreDetachPrepare<T extends AnySingleStoreDetachBase> = PreparedQueryKind<
Expand All @@ -38,18 +55,20 @@ type SingleStoreDetachDynamic<T extends AnySingleStoreDetachBase> = SingleStoreD
T['_']['preparedQueryHKT']
>;

type AnySingleStoreDetachBase = SingleStoreDetachBase<any, any, any, any>;
type AnySingleStoreDetachBase = SingleStoreDetachBase<any, any, any, any, any>;

export interface SingleStoreDetachBase<
TDatabase extends string,
TQueryResult extends SingleStoreQueryResultHKT,
TPreparedQueryHKT extends PreparedQueryHKTBase,
TDynamic extends boolean = false,
TExcludedMethods extends string = never,
> extends QueryPromise<SingleStoreQueryResultKind<TQueryResult, never>> {
readonly _: {
readonly database: TDatabase;
readonly queryResult: TQueryResult;
readonly preparedQueryHKT: TPreparedQueryHKT;
readonly dynamic: TDynamic;
readonly excludedMethods: TExcludedMethods;
};
}
Expand All @@ -59,6 +78,7 @@ export class SingleStoreDetachBase<
TQueryResult extends SingleStoreQueryResultHKT,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
TPreparedQueryHKT extends PreparedQueryHKTBase,
TDynamic extends boolean = false,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
TExcludedMethods extends string = never,
> extends QueryPromise<SingleStoreQueryResultKind<TQueryResult, never>> implements SQLWrapper {
Expand Down Expand Up @@ -88,7 +108,7 @@ export class SingleStoreDetachBase<
* You can use conditional operators and `sql function` to filter the rows to be deleted.
*
* ```ts
* // Delete all cars with green color
* // Detach all cars with green color
* db.delete(cars).where(eq(cars.color, 'green'));
* // or
* db.delete(cars).where(sql`${cars.color} = 'green'`)
Expand All @@ -97,17 +117,23 @@ export class SingleStoreDetachBase<
* You can logically combine conditional operators with `and()` and `or()` operators:
*
* ```ts
* // Delete all BMW cars with a green color
* // Detach all BMW cars with a green color
* db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
*
* // Delete all cars with the green or blue color
* // Detach all cars with the green or blue color
* db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
* ```
*/
// atMilestone(milestone: string | undefined): SingleStoreDetach<this, TDynamic, 'where'> {
// this.config.milestone = milestone;
// return this as any;
// }
atMilestone(milestone: string | undefined): SingleStoreDetachWithout<this, TDynamic, 'atMilestone'> {
this.config.milestone = milestone;
return this as any;
}

// TODO: docs
fromWorkspace(workspace: string | undefined): SingleStoreDetachWithout<this, TDynamic, 'fromWorkspace'> {
this.config.workspace = workspace;
return this as any;
}

/** @internal */
getSQL(): SQL {
Expand Down

0 comments on commit 8e61e25

Please sign in to comment.