Skip to content

Commit

Permalink
Added detach
Browse files Browse the repository at this point in the history
  • Loading branch information
drodrigues4 committed Jul 24, 2024
1 parent 4b186e6 commit b42d3cb
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
7 changes: 7 additions & 0 deletions drizzle-orm/src/singlestore-core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +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';

export class SingleStoreDatabase<
TQueryResult extends SingleStoreQueryResultHKT,
Expand Down Expand Up @@ -473,6 +474,12 @@ export class SingleStoreDatabase<
): Promise<T> {
return this.session.transaction(transaction, config);
}

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

export type SingleStoreWithReplicas<Q> = Q & { $primary: Q };
Expand Down
7 changes: 7 additions & 0 deletions drizzle-orm/src/singlestore-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import type { SingleStoreUpdateConfig } from './query-builders/update.ts';
import type { SingleStoreSession } from './session.ts';
import { SingleStoreTable } from './table.ts';
import { SingleStoreViewBase } from './view-base.ts';
import type { SingleStoreDetachConfig } from './query-builders/detach.ts';

export class SingleStoreDialect {
static readonly [entityKind]: string = 'SingleStoreDialect';
Expand Down Expand Up @@ -1078,4 +1079,10 @@ export class SingleStoreDialect {
selection,
};
}

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

return sql`detach database ${database}${milestoneSql}`;
}
}
145 changes: 145 additions & 0 deletions drizzle-orm/src/singlestore-core/query-builders/detach.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { entityKind } from '~/entity.ts';
import { QueryPromise } from '~/query-promise.ts';
import type { SingleStoreDialect } from '~/singlestore-core/dialect.ts';
import type {
AnySingleStoreQueryResultHKT,
PreparedQueryHKTBase,
PreparedQueryKind,
SingleStorePreparedQueryConfig,
SingleStoreQueryResultHKT,
SingleStoreQueryResultKind,
SingleStoreSession,
} from '~/singlestore-core/session.ts';
import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts';

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

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

export type SingleStoreDetachPrepare<T extends AnySingleStoreDetachBase> = PreparedQueryKind<
T['_']['preparedQueryHKT'],
SingleStorePreparedQueryConfig & {
execute: SingleStoreQueryResultKind<T['_']['queryResult'], never>;
iterator: never;
},
true
>;

type SingleStoreDetachDynamic<T extends AnySingleStoreDetachBase> = SingleStoreDetach<
T['_']['database'],
T['_']['queryResult'],
T['_']['preparedQueryHKT']
>;

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

export interface SingleStoreDetachBase<
TDatabase extends string,
TQueryResult extends SingleStoreQueryResultHKT,
TPreparedQueryHKT extends PreparedQueryHKTBase,
TExcludedMethods extends string = never,
> extends QueryPromise<SingleStoreQueryResultKind<TQueryResult, never>> {
readonly _: {
readonly database: TDatabase;
readonly queryResult: TQueryResult;
readonly preparedQueryHKT: TPreparedQueryHKT;
readonly excludedMethods: TExcludedMethods;
};
}

export class SingleStoreDetachBase<
TDatabase extends string,
TQueryResult extends SingleStoreQueryResultHKT,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
TPreparedQueryHKT extends PreparedQueryHKTBase,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
TExcludedMethods extends string = never,
> extends QueryPromise<SingleStoreQueryResultKind<TQueryResult, never>> implements SQLWrapper {
static readonly [entityKind]: string = 'SingleStoreDetach';

private config: SingleStoreDetachConfig;

constructor(
private database: TDatabase,
private session: SingleStoreSession,
private dialect: SingleStoreDialect,
) {
super();
this.config = { database };
}

/**
* Adds a `where` clause to the query.
*
* Calling this method will delete only those rows that fulfill a specified condition.
*
* See docs: {@link https://orm.drizzle.team/docs/delete}
*
* @param where the `where` clause.
*
* @example
* You can use conditional operators and `sql function` to filter the rows to be deleted.
*
* ```ts
* // Delete all cars with green color
* db.delete(cars).where(eq(cars.color, 'green'));
* // or
* db.delete(cars).where(sql`${cars.color} = 'green'`)
* ```
*
* You can logically combine conditional operators with `and()` and `or()` operators:
*
* ```ts
* // Delete 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
* 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;
// }

/** @internal */
getSQL(): SQL {
return this.dialect.buildDetachQuery(this.config);
}

toSQL(): Query {
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
return rest;
}

prepare(): SingleStoreDetachPrepare<this> {
return this.session.prepareQuery(
this.dialect.sqlToQuery(this.getSQL()),
undefined,
) as SingleStoreDetachPrepare<this>;
}

override execute: ReturnType<this['prepare']>['execute'] = (placeholderValues) => {
return this.prepare().execute(placeholderValues);
};

private createIterator = (): ReturnType<this['prepare']>['iterator'] => {
const self = this;
return async function*(placeholderValues) {
yield* self.prepare().iterator(placeholderValues);
};
};

iterator = this.createIterator();

$dynamic(): SingleStoreDetachDynamic<this> {
return this as any;
}
}

0 comments on commit b42d3cb

Please sign in to comment.