-
-
Notifications
You must be signed in to change notification settings - Fork 788
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ecfe1f
commit 0b4d01d
Showing
39 changed files
with
5,143 additions
and
1,942 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ dist.new | |
.rollup.cache | ||
dist-dts | ||
rollup.config-*.mjs | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { PrismaClient } from '@prisma/client/extension'; | ||
|
||
import { Prisma } from '@prisma/client'; | ||
|
||
import { entityKind } from '~/entity'; | ||
import type { Logger } from '~/logger.ts'; | ||
import { DefaultLogger } from '~/logger.ts'; | ||
import type { QueryResultHKT } from '~/mysql-core'; | ||
import { MySqlDatabase, MySqlDialect } from '~/mysql-core'; | ||
import type { DrizzleConfig } from '~/utils.ts'; | ||
import type { PrismaMySqlPreparedQueryHKT } from './session'; | ||
import { PrismaMySqlSession } from './session'; | ||
|
||
export class PrismaMySqlDatabase | ||
extends MySqlDatabase<QueryResultHKT, PrismaMySqlPreparedQueryHKT, Record<string, never>> | ||
{ | ||
static readonly [entityKind]: string = 'PrismaMySqlDatabase'; | ||
|
||
constructor(client: PrismaClient, logger: Logger | undefined) { | ||
const dialect = new MySqlDialect(); | ||
super(dialect, new PrismaMySqlSession(dialect, client, { logger }), undefined, 'default'); | ||
} | ||
} | ||
|
||
export type PrismaMySqlConfig = Omit<DrizzleConfig, 'schema'>; | ||
|
||
export function drizzle(config: PrismaMySqlConfig = {}) { | ||
let logger: Logger | undefined; | ||
if (config.logger === true) { | ||
logger = new DefaultLogger(); | ||
} else if (config.logger !== false) { | ||
logger = config.logger; | ||
} | ||
|
||
return Prisma.defineExtension((client) => { | ||
return client.$extends({ | ||
name: 'drizzle', | ||
client: { | ||
$drizzle: new PrismaMySqlDatabase(client, logger), | ||
}, | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './driver'; | ||
export * from './session'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import type { PrismaClient } from '@prisma/client/extension'; | ||
|
||
import { entityKind } from '~/entity'; | ||
import { type Logger, NoopLogger } from '~/logger'; | ||
import type { | ||
MySqlDialect, | ||
MySqlPreparedQueryHKT, | ||
MySqlTransaction, | ||
MySqlTransactionConfig, | ||
PreparedQueryConfig, | ||
QueryResultHKT, | ||
} from '~/mysql-core'; | ||
import { MySqlPreparedQuery, MySqlSession } from '~/mysql-core'; | ||
import { fillPlaceholders } from '~/sql'; | ||
import type { Query, SQL } from '~/sql'; | ||
import type { Assume } from '~/utils'; | ||
|
||
export class PrismaMySqlPreparedQuery<T> extends MySqlPreparedQuery<PreparedQueryConfig & { execute: T }> { | ||
override iterator(_placeholderValues?: Record<string, unknown> | undefined): AsyncGenerator<unknown, any, unknown> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
static readonly [entityKind]: string = 'PrismaMySqlPreparedQuery'; | ||
|
||
constructor( | ||
private readonly prisma: PrismaClient, | ||
private readonly query: Query, | ||
private readonly logger: Logger, | ||
) { | ||
super(); | ||
} | ||
|
||
override execute(placeholderValues?: Record<string, unknown>): Promise<T> { | ||
const params = fillPlaceholders(this.query.params, placeholderValues ?? {}); | ||
this.logger.logQuery(this.query.sql, params); | ||
return this.prisma.$queryRawUnsafe(this.query.sql, ...params); | ||
} | ||
} | ||
|
||
export interface PrismaMySqlSessionOptions { | ||
logger?: Logger; | ||
} | ||
|
||
export class PrismaMySqlSession extends MySqlSession { | ||
static readonly [entityKind]: string = 'PrismaMySqlSession'; | ||
|
||
private readonly logger: Logger; | ||
|
||
constructor( | ||
dialect: MySqlDialect, | ||
private readonly prisma: PrismaClient, | ||
private readonly options: PrismaMySqlSessionOptions, | ||
) { | ||
super(dialect); | ||
this.logger = options.logger ?? new NoopLogger(); | ||
} | ||
|
||
override execute<T>(query: SQL): Promise<T> { | ||
return this.prepareQuery<PreparedQueryConfig & { execute: T }>(this.dialect.sqlToQuery(query)).execute(); | ||
} | ||
|
||
override all<T = unknown>(_query: SQL): Promise<T[]> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
override prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(query: Query): MySqlPreparedQuery<T> { | ||
return new PrismaMySqlPreparedQuery(this.prisma, query, this.logger); | ||
} | ||
|
||
override transaction<T>( | ||
_transaction: ( | ||
tx: MySqlTransaction<QueryResultHKT, PrismaMySqlPreparedQueryHKT, Record<string, never>, Record<string, never>>, | ||
) => Promise<T>, | ||
_config?: MySqlTransactionConfig, | ||
): Promise<T> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} | ||
|
||
export interface PrismaMySqlPreparedQueryHKT extends MySqlPreparedQueryHKT { | ||
type: PrismaMySqlPreparedQuery<Assume<this['config'], PreparedQueryConfig>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { PrismaClient } from '@prisma/client/extension'; | ||
|
||
import { Prisma } from '@prisma/client'; | ||
|
||
import { entityKind } from '~/entity'; | ||
import type { Logger } from '~/logger.ts'; | ||
import { DefaultLogger } from '~/logger.ts'; | ||
import type { QueryResultHKT } from '~/pg-core'; | ||
import { PgDatabase, PgDialect } from '~/pg-core'; | ||
import type { DrizzleConfig } from '~/utils.ts'; | ||
import { PrismaPgSession } from './session'; | ||
|
||
export class PrismaPgDatabase extends PgDatabase<QueryResultHKT, Record<string, never>> { | ||
static readonly [entityKind]: string = 'PrismaPgDatabase'; | ||
|
||
constructor(client: PrismaClient, logger: Logger | undefined) { | ||
const dialect = new PgDialect(); | ||
super(dialect, new PrismaPgSession(dialect, client, { logger }), undefined); | ||
} | ||
} | ||
|
||
export type PrismaPgConfig = Omit<DrizzleConfig, 'schema'>; | ||
|
||
export function drizzle(config: PrismaPgConfig = {}) { | ||
let logger: Logger | undefined; | ||
if (config.logger === true) { | ||
logger = new DefaultLogger(); | ||
} else if (config.logger !== false) { | ||
logger = config.logger; | ||
} | ||
|
||
return Prisma.defineExtension((client) => { | ||
return client.$extends({ | ||
name: 'drizzle', | ||
client: { | ||
$drizzle: new PrismaPgDatabase(client, logger), | ||
}, | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './driver'; | ||
export * from './session'; |
Oops, something went wrong.