forge-sql-orm
Version:
Drizzle ORM integration for Atlassian @forge/sql. Provides a custom driver, schema migration, two levels of caching (local and global via @forge/kvs), optimistic locking, and query analysis.
114 lines • 6.92 kB
TypeScript
import { MySqlRemoteDatabase, MySqlRemotePreparedQueryHKT, MySqlRemoteQueryResultHKT } from "drizzle-orm/mysql-proxy";
import { SelectedFields } from "drizzle-orm/mysql-core/query-builders/select.types";
import { ForgeSqlOrmOptions } from "../../..";
import { MySqlSelectBase, MySqlSelectBuilder } from "drizzle-orm/mysql-core";
import { MySqlTable } from "drizzle-orm/mysql-core/table";
import { MySqlDeleteBase, MySqlInsertBuilder, MySqlUpdateBuilder } from "drizzle-orm/mysql-core/query-builders";
import { SQLWrapper } from "drizzle-orm/sql/sql";
import type { MySqlQueryResultKind } from "drizzle-orm/mysql-core/session";
import { GetSelectTableName, GetSelectTableSelection, SelectResultField } from "drizzle-orm/query-builders/select.types";
/**
* Type for select queries with field aliasing
*/
export type SelectAliasedType = <TSelection extends SelectedFields>(fields: TSelection) => MySqlSelectBuilder<TSelection, MySqlRemotePreparedQueryHKT>;
/**
* Type for select distinct queries with field aliasing
*/
export type SelectAliasedDistinctType = <TSelection extends SelectedFields>(fields: TSelection) => MySqlSelectBuilder<TSelection, MySqlRemotePreparedQueryHKT>;
/**
* Type for select queries with field aliasing and caching
*/
export type SelectAliasedCacheableType = <TSelection extends SelectedFields>(fields: TSelection, cacheTtl?: number) => MySqlSelectBuilder<TSelection, MySqlRemotePreparedQueryHKT>;
/**
* Type for select distinct queries with field aliasing and caching
*/
export type SelectAliasedDistinctCacheableType = <TSelection extends SelectedFields>(fields: TSelection, cacheTtl?: number) => MySqlSelectBuilder<TSelection, MySqlRemotePreparedQueryHKT>;
/**
* Type for select queries from table with field aliasing
*/
export type SelectAllFromAliasedType = <T extends MySqlTable>(table: T) => MySqlSelectBase<GetSelectTableName<T>, GetSelectTableSelection<T>, "single", MySqlRemotePreparedQueryHKT, GetSelectTableName<T> extends string ? Record<string & GetSelectTableName<T>, "not-null"> : {}, false, never, {
[K in keyof {
[Key in keyof GetSelectTableSelection<T>]: SelectResultField<GetSelectTableSelection<T>[Key]>;
}]: {
[Key in keyof GetSelectTableSelection<T>]: SelectResultField<GetSelectTableSelection<T>[Key]>;
}[K];
}[], any>;
/**
* Type for select distinct queries from table with field aliasing
*/
export type SelectAllDistinctFromAliasedType = <T extends MySqlTable>(table: T) => MySqlSelectBase<GetSelectTableName<T>, GetSelectTableSelection<T>, "single", MySqlRemotePreparedQueryHKT, GetSelectTableName<T> extends string ? Record<string & GetSelectTableName<T>, "not-null"> : {}, false, never, {
[K in keyof {
[Key in keyof GetSelectTableSelection<T>]: SelectResultField<GetSelectTableSelection<T>[Key]>;
}]: {
[Key in keyof GetSelectTableSelection<T>]: SelectResultField<GetSelectTableSelection<T>[Key]>;
}[K];
}[], any>;
/**
* Type for select queries from table with field aliasing and caching
*/
export type SelectAllFromCacheableAliasedType = <T extends MySqlTable>(table: T, cacheTtl?: number) => MySqlSelectBase<GetSelectTableName<T>, GetSelectTableSelection<T>, "single", MySqlRemotePreparedQueryHKT, GetSelectTableName<T> extends string ? Record<string & GetSelectTableName<T>, "not-null"> : {}, false, never, {
[K in keyof {
[Key in keyof GetSelectTableSelection<T>]: SelectResultField<GetSelectTableSelection<T>[Key]>;
}]: {
[Key in keyof GetSelectTableSelection<T>]: SelectResultField<GetSelectTableSelection<T>[Key]>;
}[K];
}[], any>;
/**
* Type for select distinct queries from table with field aliasing and caching
*/
export type SelectAllDistinctFromCacheableAliasedType = <T extends MySqlTable>(table: T, cacheTtl?: number) => MySqlSelectBase<GetSelectTableName<T>, GetSelectTableSelection<T>, "single", MySqlRemotePreparedQueryHKT, GetSelectTableName<T> extends string ? Record<string & GetSelectTableName<T>, "not-null"> : {}, false, never, {
[K in keyof {
[Key in keyof GetSelectTableSelection<T>]: SelectResultField<GetSelectTableSelection<T>[Key]>;
}]: {
[Key in keyof GetSelectTableSelection<T>]: SelectResultField<GetSelectTableSelection<T>[Key]>;
}[K];
}[], any>;
/**
* Type for executing raw SQL queries with local cache
*/
export type ExecuteQuery = <T>(query: SQLWrapper | string) => Promise<MySqlQueryResultKind<MySqlRemoteQueryResultHKT, T>>;
/**
* Type for executing raw SQL queries with local and global cache
*/
export type ExecuteQueryCacheable = <T>(query: SQLWrapper | string, cacheTtl?: number) => Promise<MySqlQueryResultKind<MySqlRemoteQueryResultHKT, T>>;
/**
* Type for insert operations with cache eviction
*/
export type InsertAndEvictCacheType = <TTable extends MySqlTable>(table: TTable) => MySqlInsertBuilder<TTable, MySqlRemoteQueryResultHKT, MySqlRemotePreparedQueryHKT>;
/**
* Type for update operations with cache eviction
*/
export type UpdateAndEvictCacheType = <TTable extends MySqlTable>(table: TTable) => MySqlUpdateBuilder<TTable, MySqlRemoteQueryResultHKT, MySqlRemotePreparedQueryHKT>;
/**
* Type for delete operations with cache eviction
*/
export type DeleteAndEvictCacheType = <TTable extends MySqlTable>(table: TTable) => MySqlDeleteBase<TTable, MySqlRemoteQueryResultHKT, MySqlRemotePreparedQueryHKT>;
/**
* Patches a Drizzle database instance with additional methods for aliased selects and cache management.
*
* This function extends the database instance with:
* - selectAliased: Select with field aliasing support
* - selectAliasedDistinct: Select distinct with field aliasing support
* - selectAliasedCacheable: Select with field aliasing and caching
* - selectAliasedDistinctCacheable: Select distinct with field aliasing and caching
* - insertAndEvictCache: Insert operations that automatically evict cache
* - updateAndEvictCache: Update operations that automatically evict cache
* - deleteAndEvictCache: Delete operations that automatically evict cache
*
* @param db - The Drizzle database instance to patch
* @param options - Optional ForgeSQL ORM configuration
* @returns The patched database instance with additional methods
*/
export declare function patchDbWithSelectAliased(db: MySqlRemoteDatabase<any>, options?: ForgeSqlOrmOptions): MySqlRemoteDatabase<any> & {
selectAliased: SelectAliasedType;
selectAliasedDistinct: SelectAliasedDistinctType;
selectAliasedCacheable: SelectAliasedCacheableType;
selectAliasedDistinctCacheable: SelectAliasedDistinctCacheableType;
insertWithCacheContext: InsertAndEvictCacheType;
insertAndEvictCache: InsertAndEvictCacheType;
updateAndEvictCache: UpdateAndEvictCacheType;
updateWithCacheContext: UpdateAndEvictCacheType;
deleteAndEvictCache: DeleteAndEvictCacheType;
deleteWithCacheContext: DeleteAndEvictCacheType;
};
//# sourceMappingURL=additionalActions.d.ts.map