-
-
Notifications
You must be signed in to change notification settings - Fork 690
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from drizzle-team:dri-111-implement-neon-serve…
…rless-driver-support Implement NeonDB serverless driver support
- Loading branch information
Showing
19 changed files
with
401 additions
and
157 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# drizzle-orm-pg 0.13.3-beta.1 | ||
|
||
- Implemented NeonDB serverless driver support. | ||
- (internal) Added `session.all()` and `session.values()` methods. |
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 |
---|---|---|
@@ -1,12 +1,11 @@ | ||
export * from './alias'; | ||
export * from './checks'; | ||
export * from './columns'; | ||
export * from './connector'; | ||
export * from './db'; | ||
export * from './dialect'; | ||
export * from './driver'; | ||
export * from './foreign-keys'; | ||
export * from './indexes'; | ||
export * from './node-pg'; | ||
export * from './operations'; | ||
export * from './session'; | ||
export * from './table'; |
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,37 @@ | ||
import { Logger, MigrationConfig, readMigrationFiles } from 'drizzle-orm'; | ||
import { PgDialect } from '~/dialect'; | ||
import { PgSession } from '~/session'; | ||
import { NeonDriver } from './driver'; | ||
import { NeonClient, NeonSession } from './session'; | ||
|
||
export interface PgConnectorOptions { | ||
logger?: Logger; | ||
dialect?: PgDialect; | ||
driver?: NeonDriver; | ||
} | ||
|
||
export class NeonConnector { | ||
dialect: PgDialect; | ||
driver: NeonDriver; | ||
private session: NeonSession | undefined; | ||
|
||
constructor(client: NeonClient, options: PgConnectorOptions = {}) { | ||
this.dialect = new PgDialect(); | ||
this.driver = new NeonDriver(client, this.dialect, { logger: options.logger }); | ||
} | ||
|
||
private async getSession() { | ||
return this.session ?? (this.session = await this.driver.connect()); | ||
} | ||
|
||
async connect() { | ||
const session = await this.getSession(); | ||
return this.dialect.createDB(session); | ||
} | ||
|
||
async migrate(config: string | MigrationConfig) { | ||
const migrations = readMigrationFiles(config); | ||
const session = await this.getSession(); | ||
await this.dialect.migrate(migrations, 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,32 @@ | ||
import { types } from '@neondatabase/serverless'; | ||
import { Logger } from 'drizzle-orm'; | ||
import { PgDialect } from '~/dialect'; | ||
import { NeonClient, NeonSession } from './session'; | ||
|
||
export interface NeonDriverOptions { | ||
logger?: Logger; | ||
} | ||
|
||
export class NeonDriver { | ||
constructor( | ||
private client: NeonClient, | ||
private dialect: PgDialect, | ||
private options: NeonDriverOptions = {}, | ||
) { | ||
this.initMappers(); | ||
} | ||
|
||
async connect(): Promise<NeonSession> { | ||
return new NeonSession(this.client, this.dialect, { logger: this.options.logger }); | ||
} | ||
|
||
initMappers() { | ||
types.setTypeParser(types.builtins.TIMESTAMPTZ, (val) => val); | ||
types.setTypeParser(types.builtins.TIMESTAMP, (val) => val); | ||
types.setTypeParser(types.builtins.DATE, (val) => val); | ||
} | ||
} | ||
|
||
export function pg(client: NeonClient, options: NeonDriverOptions = {}) { | ||
return new NeonDriver(client, new PgDialect(), options); | ||
} |
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,3 @@ | ||
export * from './connector'; | ||
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,111 @@ | ||
import { | ||
Client, | ||
Pool, | ||
PoolClient, | ||
QueryArrayConfig, | ||
QueryConfig, | ||
QueryResult, | ||
QueryResultRow, | ||
} from '@neondatabase/serverless'; | ||
import { Logger, NoopLogger } from 'drizzle-orm'; | ||
import { fillPlaceholders, Query } from 'drizzle-orm/sql'; | ||
import { mapResultRow } from 'drizzle-orm/utils'; | ||
import { PgDialect } from '~/dialect'; | ||
import { SelectFieldsOrdered } from '~/operations'; | ||
import { PgSession, PreparedQuery, PreparedQueryConfig } from '~/session'; | ||
|
||
export type NeonClient = Pool | PoolClient | Client; | ||
|
||
export class NeonPreparedQuery<T extends PreparedQueryConfig> extends PreparedQuery<T> { | ||
private rawQuery: QueryConfig; | ||
private query: QueryArrayConfig; | ||
|
||
constructor( | ||
private client: NeonClient, | ||
queryString: string, | ||
private params: unknown[], | ||
private logger: Logger, | ||
private fields: SelectFieldsOrdered | undefined, | ||
name: string | undefined, | ||
) { | ||
super(); | ||
this.rawQuery = { | ||
name, | ||
text: queryString, | ||
}; | ||
this.query = { | ||
name, | ||
text: queryString, | ||
rowMode: 'array', | ||
}; | ||
} | ||
|
||
execute(placeholderValues: Record<string, unknown> | undefined = {}): Promise<T['execute']> { | ||
const params = fillPlaceholders(this.params, placeholderValues); | ||
|
||
this.logger.logQuery(this.rawQuery.text, params); | ||
|
||
const { fields } = this; | ||
if (!fields) { | ||
return this.client.query(this.rawQuery, params); | ||
} | ||
|
||
const result = this.client.query(this.query, params); | ||
|
||
return result.then((result) => result.rows.map((row) => mapResultRow<T['execute']>(fields, row))); | ||
} | ||
|
||
all(placeholderValues: Record<string, unknown> | undefined = {}): Promise<T['all']> { | ||
const params = fillPlaceholders(this.params, placeholderValues); | ||
this.logger.logQuery(this.rawQuery.text, params); | ||
return this.client.query(this.rawQuery, params).then((result) => result.rows); | ||
} | ||
|
||
values(placeholderValues: Record<string, unknown> | undefined = {}): Promise<T['values']> { | ||
const params = fillPlaceholders(this.params, placeholderValues); | ||
this.logger.logQuery(this.rawQuery.text, params); | ||
return this.client.query(this.query, params).then((result) => result.rows); | ||
} | ||
} | ||
|
||
export interface NeonSessionOptions { | ||
logger?: Logger; | ||
} | ||
|
||
export class NeonSession extends PgSession { | ||
private logger: Logger; | ||
|
||
constructor( | ||
private client: NeonClient, | ||
dialect: PgDialect, | ||
options: NeonSessionOptions = {}, | ||
) { | ||
super(dialect); | ||
this.logger = options.logger ?? new NoopLogger(); | ||
} | ||
|
||
prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>( | ||
query: Query, | ||
fields: SelectFieldsOrdered | undefined, | ||
name: string | undefined, | ||
): PreparedQuery<T> { | ||
return new NeonPreparedQuery(this.client, query.sql, query.params, this.logger, fields, name); | ||
} | ||
|
||
async query(query: string, params: unknown[]): Promise<QueryResult> { | ||
this.logger.logQuery(query, params); | ||
const result = await this.client.query({ | ||
rowMode: 'array', | ||
text: query, | ||
values: params, | ||
}); | ||
return result; | ||
} | ||
|
||
async queryObjects<T extends QueryResultRow>( | ||
query: string, | ||
params: unknown[], | ||
): Promise<QueryResult<T>> { | ||
return this.client.query<T>(query, params); | ||
} | ||
} |
13 changes: 7 additions & 6 deletions
13
drizzle-orm-pg/src/connector.ts → drizzle-orm-pg/src/node-pg/connector.ts
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,3 @@ | ||
export * from './connector'; | ||
export * from './driver'; | ||
export * from './session'; |
Oops, something went wrong.