Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Neon HTTP driver types, implement raw query support for batch, fix formatting #2033

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions dprint.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"typescript": {
"useTabs": true,
"quoteStyle": "preferSingle",
"quoteProps": "asNeeded",
"arrowFunction.useParentheses": "force",
"jsx.quoteStyle": "preferSingle"
},
"json": {},
"markdown": {},
"includes": ["**/*.{ts,tsx,js,jsx,cjs,mjs,json,md}"],
"excludes": ["**/node_modules", "**/*-lock.json"],
"plugins": [
"https://plugins.dprint.dev/typescript-0.83.0.wasm",
"https://plugins.dprint.dev/json-0.17.0.wasm",
"https://plugins.dprint.dev/markdown-0.15.2.wasm"
]
"typescript": {
"useTabs": true,
"quoteStyle": "preferSingle",
"quoteProps": "asNeeded",
"arrowFunction.useParentheses": "force",
"jsx.quoteStyle": "preferSingle"
},
"json": {},
"markdown": {},
"includes": ["**/*.{ts,tsx,js,jsx,cjs,mjs}"],
"excludes": ["**/node_modules", "**/*-lock.json", "dist", "dist-dts", "dist.new"],
"plugins": [
"https://plugins.dprint.dev/typescript-0.83.0.wasm",
"https://plugins.dprint.dev/json-0.17.0.wasm",
"https://plugins.dprint.dev/markdown-0.15.2.wasm"
]
}
2 changes: 1 addition & 1 deletion drizzle-orm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
"@aws-sdk/client-rds-data": "^3.344.0",
"@cloudflare/workers-types": "^4.20230904.0",
"@libsql/client": "^0.5.6",
"@neondatabase/serverless": "^0.4.24",
"@neondatabase/serverless": "^0.9.0",
"@op-engineering/op-sqlite": "^2.0.16",
"@opentelemetry/api": "^1.4.1",
"@originjs/vite-plugin-commonjs": "^1.0.3",
Expand Down
11 changes: 10 additions & 1 deletion drizzle-orm/src/aws-data-api/pg/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class AwsDataApiPreparedQuery<T extends PreparedQueryConfig> extends PgPr
private fields: SelectedFieldsOrdered | undefined,
/** @internal */
readonly transactionId: string | undefined,
private _isResponseInArrayMode: boolean,
private customResultMapper?: (rows: unknown[][]) => T['execute'],
) {
super({ sql: queryString, params });
Expand Down Expand Up @@ -105,6 +106,11 @@ export class AwsDataApiPreparedQuery<T extends PreparedQueryConfig> extends PgPr
return row;
});
}

/** @internal */
isResponseInArrayMode(): boolean {
return this._isResponseInArrayMode;
}
}

export interface AwsDataApiSessionOptions {
Expand Down Expand Up @@ -149,7 +155,8 @@ export class AwsDataApiSession<
prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(
query: QueryWithTypings,
fields: SelectedFieldsOrdered | undefined,
transactionId?: string,
transactionId: string | undefined,
isResponseInArrayMode: boolean,
customResultMapper?: (rows: unknown[][]) => T['execute'],
): PgPreparedQuery<T> {
return new AwsDataApiPreparedQuery(
Expand All @@ -160,6 +167,7 @@ export class AwsDataApiSession<
this.options,
fields,
transactionId,
isResponseInArrayMode,
customResultMapper,
);
}
Expand All @@ -169,6 +177,7 @@ export class AwsDataApiSession<
this.dialect.sqlToQuery(query),
undefined,
this.transactionId,
false,
).execute();
}

Expand Down
17 changes: 16 additions & 1 deletion drizzle-orm/src/better-sqlite3/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,19 @@ export class BetterSQLiteSession<
query: Query,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
isResponseInArrayMode: boolean,
customResultMapper?: (rows: unknown[][]) => unknown,
): PreparedQuery<T> {
const stmt = this.client.prepare(query.sql);
return new PreparedQuery(stmt, query, this.logger, fields, executeMethod, customResultMapper);
return new PreparedQuery(
stmt,
query,
this.logger,
fields,
executeMethod,
isResponseInArrayMode,
customResultMapper,
);
}

override transaction<T>(
Expand Down Expand Up @@ -92,6 +101,7 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
private logger: Logger,
private fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
private _isResponseInArrayMode: boolean,
private customResultMapper?: (rows: unknown[][]) => unknown,
) {
super('sync', executeMethod, query);
Expand Down Expand Up @@ -145,4 +155,9 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
this.logger.logQuery(this.query.sql, params);
return this.stmt.raw().all(...params) as T['values'];
}

/** @internal */
isResponseInArrayMode(): boolean {
return this._isResponseInArrayMode;
}
}
17 changes: 16 additions & 1 deletion drizzle-orm/src/bun-sqlite/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,19 @@ export class SQLiteBunSession<
query: Query,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
isResponseInArrayMode: boolean,
customResultMapper?: (rows: unknown[][]) => unknown,
): PreparedQuery<T> {
const stmt = this.client.prepare(query.sql);
return new PreparedQuery(stmt, query, this.logger, fields, executeMethod, customResultMapper);
return new PreparedQuery(
stmt,
query,
this.logger,
fields,
executeMethod,
isResponseInArrayMode,
customResultMapper,
);
}

override transaction<T>(
Expand Down Expand Up @@ -102,6 +111,7 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
private logger: Logger,
private fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
private _isResponseInArrayMode: boolean,
private customResultMapper?: (rows: unknown[][]) => unknown,
) {
super('sync', executeMethod, query);
Expand Down Expand Up @@ -156,4 +166,9 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
this.logger.logQuery(this.query.sql, params);
return this.stmt.values(...params);
}

/** @internal */
isResponseInArrayMode(): boolean {
return this._isResponseInArrayMode;
}
}
99 changes: 17 additions & 82 deletions drizzle-orm/src/d1/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,22 @@ export class SQLiteD1Session<
query: Query,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
isResponseInArrayMode: boolean,
customResultMapper?: (rows: unknown[][]) => unknown,
): D1PreparedQuery {
const stmt = this.client.prepare(query.sql);
return new D1PreparedQuery(stmt, query, this.logger, fields, executeMethod, customResultMapper);
return new D1PreparedQuery(
stmt,
query,
this.logger,
fields,
executeMethod,
isResponseInArrayMode,
customResultMapper,
);
}

/*override */ async batch<U extends BatchItem, T extends Readonly<[U, ...U[]]>>(queries: T) {
async batch<U extends BatchItem, T extends Readonly<[U, ...U[]]>>(queries: T) {
const preparedQueries: PreparedQuery[] = [];
const builtQueries: D1PreparedStatement[] = [];

Expand All @@ -70,88 +79,8 @@ export class SQLiteD1Session<
}
}

// const queryToType: (
// | { mode: 'all' }
// | {
// mode: 'all_mapped';
// config: { fields: SelectedFieldsOrdered; joinsNotNullableMap?: Record<string, boolean> };
// }
// | { mode: 'get' }
// | { mode: 'values' }
// | { mode: 'raw' }
// | { mode: 'rqb'; mapper: any }
// )[] = [];

// const builtQueries: D1PreparedStatement[] = queries.map((query) => {
// if (is(query, SQLiteSelectBase<any, 'async', any, any, any>)) {
// const prepared = query.prepare() as D1PreparedQuery;
// prepared.fields === undefined
// ? queryToType.push({ mode: 'all' })
// : queryToType.push({
// mode: 'all_mapped',
// config: { fields: prepared.fields, joinsNotNullableMap: prepared.joinsNotNullableMap },
// });
// return prepared.stmt.bind(...prepared.params);
// } else if (
// is(query, SQLiteInsertBase<any, 'async', any, any>) || is(query, SQLiteUpdateBase<any, 'async', any, any>)
// || is(query, SQLiteDeleteBase<any, 'async', any, any>)
// ) {
// const prepared = query.prepare() as D1PreparedQuery;
// queryToType.push(
// query.config.returning
// ? {
// mode: 'all_mapped',
// config: { fields: query.config.returning },
// }
// : { mode: 'raw' },
// );
// return prepared.stmt.bind(...prepared.params);
// } else if (is(query, SQLiteRaw)) {
// const builtQuery = this.dialect.sqlToQuery(query.getSQL());
// queryToType.push(
// query.config.action === 'run' ? { mode: 'raw' } : { mode: query.config.action },
// );
// return this.client.prepare(builtQuery.sql).bind(...builtQuery.params);
// } else if (is(query, SQLiteRelationalQuery)) {
// const preparedRqb = query.prepare() as D1PreparedQuery;
// queryToType.push({ mode: 'rqb', mapper: preparedRqb.customResultMapper });
// return preparedRqb.stmt.bind(...preparedRqb.params);
// }
// throw new DrizzleError({ message: 'You can use only drizzle queries in D1 batch API' });
// });

const batchResults = await this.client.batch<any>(builtQueries);
return batchResults.map((result, i) => preparedQueries[i]!.mapResult(result, true));

// const res = this.client.batch<any>(builtQueries).then((stmt) =>
// stmt.map(({ results }, index) => {
// const action = queryToType[index]!;
// if (action.mode === 'all') {
// return results;
// }
// if (action.mode === 'all_mapped') {
// const mappedRows = this.d1ToRawMapping(results);
// return mappedRows!.map((row) => {
// return mapResultRow(
// action.config.fields,
// row,
// action.config.joinsNotNullableMap,
// );
// });
// }
// if (action.mode === 'get') {
// return results![0] as unknown[];
// }
// if (action.mode === 'values') {
// return this.d1ToRawMapping(results);
// }
// if (action.mode === 'raw') {
// return stmt[index];
// }
// return action.mapper(this.d1ToRawMapping(results));
// })
// );
// return res;
}

override extractRawAllValueFromBatchResult(result: unknown): unknown {
Expand Down Expand Up @@ -239,6 +168,7 @@ export class D1PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig
private logger: Logger,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
private _isResponseInArrayMode: boolean,
customResultMapper?: (rows: unknown[][]) => unknown,
) {
super('async', executeMethod, query);
Expand Down Expand Up @@ -324,4 +254,9 @@ export class D1PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig
this.logger.logQuery(this.query.sql, params);
return this.stmt.bind(...params).raw();
}

/** @internal */
isResponseInArrayMode(): boolean {
return this._isResponseInArrayMode;
}
}
58 changes: 29 additions & 29 deletions drizzle-orm/src/expo-sqlite/driver.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
import type { SQLiteDatabase, SQLiteRunResult } from 'expo-sqlite/next';
import { DefaultLogger } from '~/logger.ts';
import {
createTableRelationsHelpers,
extractTablesRelationalConfig,
type RelationalSchemaConfig,
type TablesRelationalConfig,
createTableRelationsHelpers,
extractTablesRelationalConfig,
type RelationalSchemaConfig,
type TablesRelationalConfig,
} from '~/relations.ts';
import { BaseSQLiteDatabase } from '~/sqlite-core/db.ts';
import { SQLiteSyncDialect } from '~/sqlite-core/dialect.ts';
import type { DrizzleConfig } from '~/utils.ts';
import { ExpoSQLiteSession } from './session.ts';

export type ExpoSQLiteDatabase<
TSchema extends Record<string, unknown> = Record<string, never>,
TSchema extends Record<string, unknown> = Record<string, never>,
> = BaseSQLiteDatabase<'sync', SQLiteRunResult, TSchema>;

export function drizzle<TSchema extends Record<string, unknown> = Record<string, never>>(
client: SQLiteDatabase,
config: DrizzleConfig<TSchema> = {},
client: SQLiteDatabase,
config: DrizzleConfig<TSchema> = {},
): ExpoSQLiteDatabase<TSchema> {
const dialect = new SQLiteSyncDialect();
let logger;
if (config.logger === true) {
logger = new DefaultLogger();
} else if (config.logger !== false) {
logger = config.logger;
}
const dialect = new SQLiteSyncDialect();
let logger;
if (config.logger === true) {
logger = new DefaultLogger();
} else if (config.logger !== false) {
logger = config.logger;
}

let schema: RelationalSchemaConfig<TablesRelationalConfig> | undefined;
if (config.schema) {
const tablesConfig = extractTablesRelationalConfig(
config.schema,
createTableRelationsHelpers,
);
schema = {
fullSchema: config.schema,
schema: tablesConfig.tables,
tableNamesMap: tablesConfig.tableNamesMap,
};
}
let schema: RelationalSchemaConfig<TablesRelationalConfig> | undefined;
if (config.schema) {
const tablesConfig = extractTablesRelationalConfig(
config.schema,
createTableRelationsHelpers,
);
schema = {
fullSchema: config.schema,
schema: tablesConfig.tables,
tableNamesMap: tablesConfig.tableNamesMap,
};
}

const session = new ExpoSQLiteSession(client, dialect, schema, { logger });
return new BaseSQLiteDatabase('sync', dialect, session, schema) as ExpoSQLiteDatabase<TSchema>;
}
const session = new ExpoSQLiteSession(client, dialect, schema, { logger });
return new BaseSQLiteDatabase('sync', dialect, session, schema) as ExpoSQLiteDatabase<TSchema>;
}
2 changes: 1 addition & 1 deletion drizzle-orm/src/expo-sqlite/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './driver.ts';
export * from './session.ts';
export * from './session.ts';
6 changes: 3 additions & 3 deletions drizzle-orm/src/expo-sqlite/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ export const useMigrations = (db: ExpoSQLiteDatabase<any>, migrations: {
const [state, dispatch] = useReducer(fetchReducer, initialState);

useEffect(() => {
dispatch({ type: 'migrating' })
dispatch({ type: 'migrating' });
migrate(db, migrations as any).then(() => {
dispatch({ type: 'migrated', payload: true })
dispatch({ type: 'migrated', payload: true });
}).catch((error) => {
dispatch({ type: 'error', payload: error as Error })
dispatch({ type: 'error', payload: error as Error });
});
}, []);

Expand Down
Loading
Loading