Skip to content

Commit

Permalink
Merge pull request #2186 from drizzle-team/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
dankochetov committed Apr 21, 2024
2 parents 4706ad1 + d939ec2 commit e0aaeb2
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 30 deletions.
3 changes: 3 additions & 0 deletions changelogs/drizzle-orm/0.30.9.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- 🐛 Fixed migrator in AWS Data API
- Added `setWhere` and `targetWhere` fields to `.onConflictDoUpdate()` config in SQLite instead of single `where` field
- 🛠️ Added schema information to Drizzle instances via `db._.fullSchema`
2 changes: 1 addition & 1 deletion drizzle-orm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-orm",
"version": "0.30.8",
"version": "0.30.9",
"description": "Drizzle ORM package for SQL databases",
"type": "module",
"scripts": {
Expand Down
14 changes: 10 additions & 4 deletions drizzle-orm/src/aws-data-api/pg/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ export class AwsDataApiPreparedQuery<T extends PreparedQueryConfig> extends PgPr
: result.rows!.map((row) => mapResultRow<T['execute']>(fields!, row, joinsNotNullableMap));
}

all(placeholderValues?: Record<string, unknown> | undefined): Promise<T['all']> {
return this.execute(placeholderValues);
async all(placeholderValues?: Record<string, unknown> | undefined): Promise<T['all']> {
const result = await this.execute(placeholderValues) as AwsDataApiPgQueryResult<unknown>;
return result.rows;
}

async values(placeholderValues: Record<string, unknown> = {}): Promise<T['values']> {
Expand Down Expand Up @@ -198,7 +199,7 @@ export class AwsDataApiSession<
): Promise<T> {
const { transactionId } = await this.client.send(new BeginTransactionCommand(this.rawQuery));
const session = new AwsDataApiSession(this.client, this.dialect, this.schema, this.options, transactionId);
const tx = new AwsDataApiTransaction(this.dialect, session, this.schema);
const tx = new AwsDataApiTransaction<TFullSchema, TSchema>(this.dialect, session, this.schema);
if (config) {
await tx.setTransaction(config);
}
Expand All @@ -223,7 +224,12 @@ export class AwsDataApiTransaction<
transaction: (tx: AwsDataApiTransaction<TFullSchema, TSchema>) => Promise<T>,
): Promise<T> {
const savepointName = `sp${this.nestedIndex + 1}`;
const tx = new AwsDataApiTransaction(this.dialect, this.session, this.schema, this.nestedIndex + 1);
const tx = new AwsDataApiTransaction<TFullSchema, TSchema>(
this.dialect,
this.session,
this.schema,
this.nestedIndex + 1,
);
await this.session.execute(sql.raw(`savepoint ${savepointName}`));
try {
const result = await transaction(tx);
Expand Down
10 changes: 8 additions & 2 deletions drizzle-orm/src/libsql/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@ export class LibSQLSession<
): Promise<T> {
// TODO: support transaction behavior
const libsqlTx = await this.client.transaction();
const session = new LibSQLSession(this.client, this.dialect, this.schema, this.options, libsqlTx);
const tx = new LibSQLTransaction('async', this.dialect, session, this.schema);
const session = new LibSQLSession<TFullSchema, TSchema>(
this.client,
this.dialect,
this.schema,
this.options,
libsqlTx,
);
const tx = new LibSQLTransaction<TFullSchema, TSchema>('async', this.dialect, session, this.schema);
try {
const result = await transaction(tx);
await libsqlTx.commit();
Expand Down
13 changes: 11 additions & 2 deletions drizzle-orm/src/mysql-core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class MySqlDatabase<

declare readonly _: {
readonly schema: TSchema | undefined;
readonly fullSchema: TFullSchema;
readonly tableNamesMap: Record<string, string>;
};

Expand All @@ -56,8 +57,16 @@ export class MySqlDatabase<
protected readonly mode: Mode,
) {
this._ = schema
? { schema: schema.schema, tableNamesMap: schema.tableNamesMap }
: { schema: undefined, tableNamesMap: {} };
? {
schema: schema.schema,
fullSchema: schema.fullSchema as TFullSchema,
tableNamesMap: schema.tableNamesMap,
}
: {
schema: undefined,
fullSchema: {} as TFullSchema,
tableNamesMap: {},
};
this.query = {} as typeof this['query'];
if (this._.schema) {
for (const [tableName, columns] of Object.entries(this._.schema)) {
Expand Down
11 changes: 8 additions & 3 deletions drizzle-orm/src/mysql2/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,14 @@ export class MySql2Session<
config?: MySqlTransactionConfig,
): Promise<T> {
const session = isPool(this.client)
? new MySql2Session(await this.client.getConnection(), this.dialect, this.schema, this.options)
? new MySql2Session(
await this.client.getConnection(),
this.dialect,
this.schema,
this.options,
)
: this;
const tx = new MySql2Transaction(
const tx = new MySql2Transaction<TFullSchema, TSchema>(
this.dialect,
session as MySqlSession<any, any, any, any>,
this.schema,
Expand Down Expand Up @@ -261,7 +266,7 @@ export class MySql2Transaction<

override async transaction<T>(transaction: (tx: MySql2Transaction<TFullSchema, TSchema>) => Promise<T>): Promise<T> {
const savepointName = `sp${this.nestedIndex + 1}`;
const tx = new MySql2Transaction(
const tx = new MySql2Transaction<TFullSchema, TSchema>(
this.dialect,
this.session,
this.schema,
Expand Down
4 changes: 2 additions & 2 deletions drizzle-orm/src/neon-serverless/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class NeonSession<
const session = this.client instanceof Pool // eslint-disable-line no-instanceof/no-instanceof
? new NeonSession(await this.client.connect(), this.dialect, this.schema, this.options)
: this;
const tx = new NeonTransaction(this.dialect, session, this.schema);
const tx = new NeonTransaction<TFullSchema, TSchema>(this.dialect, session, this.schema);
await tx.execute(sql`begin ${tx.getTransactionConfigSQL(config)}`);
try {
const result = await transaction(tx);
Expand All @@ -175,7 +175,7 @@ export class NeonTransaction<

override async transaction<T>(transaction: (tx: NeonTransaction<TFullSchema, TSchema>) => Promise<T>): Promise<T> {
const savepointName = `sp${this.nestedIndex + 1}`;
const tx = new NeonTransaction(this.dialect, this.session, this.schema, this.nestedIndex + 1);
const tx = new NeonTransaction<TFullSchema, TSchema>(this.dialect, this.session, this.schema, this.nestedIndex + 1);
await tx.execute(sql.raw(`savepoint ${savepointName}`));
try {
const result = await transaction(tx);
Expand Down
9 changes: 7 additions & 2 deletions drizzle-orm/src/node-postgres/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class NodePgSession<
const session = this.client instanceof Pool // eslint-disable-line no-instanceof/no-instanceof
? new NodePgSession(await this.client.connect(), this.dialect, this.schema, this.options)
: this;
const tx = new NodePgTransaction(this.dialect, session, this.schema);
const tx = new NodePgTransaction<TFullSchema, TSchema>(this.dialect, session, this.schema);
await tx.execute(sql`begin${config ? sql` ${tx.getTransactionConfigSQL(config)}` : undefined}`);
try {
const result = await transaction(tx);
Expand All @@ -174,7 +174,12 @@ export class NodePgTransaction<

override async transaction<T>(transaction: (tx: NodePgTransaction<TFullSchema, TSchema>) => Promise<T>): Promise<T> {
const savepointName = `sp${this.nestedIndex + 1}`;
const tx = new NodePgTransaction(this.dialect, this.session, this.schema, this.nestedIndex + 1);
const tx = new NodePgTransaction<TFullSchema, TSchema>(
this.dialect,
this.session,
this.schema,
this.nestedIndex + 1,
);
await tx.execute(sql.raw(`savepoint ${savepointName}`));
try {
const result = await transaction(tx);
Expand Down
16 changes: 14 additions & 2 deletions drizzle-orm/src/pg-core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export class PgDatabase<

declare readonly _: {
readonly schema: TSchema | undefined;
readonly fullSchema: TFullSchema;
readonly tableNamesMap: Record<string, string>;
readonly session: PgSession<TQueryResult, TFullSchema, TSchema>;
};

query: TFullSchema extends Record<string, never>
Expand All @@ -56,8 +58,18 @@ export class PgDatabase<
schema: RelationalSchemaConfig<TSchema> | undefined,
) {
this._ = schema
? { schema: schema.schema, tableNamesMap: schema.tableNamesMap }
: { schema: undefined, tableNamesMap: {} };
? {
schema: schema.schema,
fullSchema: schema.fullSchema as TFullSchema,
tableNamesMap: schema.tableNamesMap,
session,
}
: {
schema: undefined,
fullSchema: {} as TFullSchema,
tableNamesMap: {},
session,
};
this.query = {} as typeof this['query'];
if (this._.schema) {
for (const [tableName, columns] of Object.entries(this._.schema)) {
Expand Down
9 changes: 7 additions & 2 deletions drizzle-orm/src/pglite/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class PgliteSession<
this.schema,
this.options,
);
const tx = new PgliteTransaction(this.dialect, session, this.schema);
const tx = new PgliteTransaction<TFullSchema, TSchema>(this.dialect, session, this.schema);
if (config) {
await tx.setTransaction(config);
}
Expand All @@ -150,7 +150,12 @@ export class PgliteTransaction<

override async transaction<T>(transaction: (tx: PgliteTransaction<TFullSchema, TSchema>) => Promise<T>): Promise<T> {
const savepointName = `sp${this.nestedIndex + 1}`;
const tx = new PgliteTransaction(this.dialect, this.session, this.schema, this.nestedIndex + 1);
const tx = new PgliteTransaction<TFullSchema, TSchema>(
this.dialect,
this.session,
this.schema,
this.nestedIndex + 1,
);
await tx.execute(sql.raw(`savepoint ${savepointName}`));
try {
const result = await transaction(tx);
Expand Down
13 changes: 11 additions & 2 deletions drizzle-orm/src/planetscale-serverless/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ export class PlanetscaleSession<
): Promise<T> {
return this.baseClient.transaction((pstx) => {
const session = new PlanetscaleSession(this.baseClient, this.dialect, pstx, this.schema, this.options);
const tx = new PlanetScaleTransaction(this.dialect, session as MySqlSession<any, any, any, any>, this.schema);
const tx = new PlanetScaleTransaction<TFullSchema, TSchema>(
this.dialect,
session as MySqlSession<any, any, any, any>,
this.schema,
);
return transaction(tx);
});
}
Expand All @@ -139,7 +143,12 @@ export class PlanetScaleTransaction<
transaction: (tx: PlanetScaleTransaction<TFullSchema, TSchema>) => Promise<T>,
): Promise<T> {
const savepointName = `sp${this.nestedIndex + 1}`;
const tx = new PlanetScaleTransaction(this.dialect, this.session, this.schema, this.nestedIndex + 1);
const tx = new PlanetScaleTransaction<TFullSchema, TSchema>(
this.dialect,
this.session,
this.schema,
this.nestedIndex + 1,
);
await tx.execute(sql.raw(`savepoint ${savepointName}`));
try {
const result = await transaction(tx);
Expand Down
9 changes: 7 additions & 2 deletions drizzle-orm/src/postgres-js/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,13 @@ export class PostgresJsTransaction<
transaction: (tx: PostgresJsTransaction<TFullSchema, TSchema>) => Promise<T>,
): Promise<T> {
return this.session.client.savepoint((client) => {
const session = new PostgresJsSession(client, this.dialect, this.schema, this.session.options);
const tx = new PostgresJsTransaction(this.dialect, session, this.schema);
const session = new PostgresJsSession<TransactionSql, TFullSchema, TSchema>(
client,
this.dialect,
this.schema,
this.session.options,
);
const tx = new PostgresJsTransaction<TFullSchema, TSchema>(this.dialect, session, this.schema);
return transaction(tx);
}) as Promise<T>;
}
Expand Down
13 changes: 11 additions & 2 deletions drizzle-orm/src/sqlite-core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class BaseSQLiteDatabase<

declare readonly _: {
readonly schema: TSchema | undefined;
readonly fullSchema: TFullSchema;
readonly tableNamesMap: Record<string, string>;
};

Expand All @@ -54,8 +55,16 @@ export class BaseSQLiteDatabase<
schema: RelationalSchemaConfig<TSchema> | undefined,
) {
this._ = schema
? { schema: schema.schema, tableNamesMap: schema.tableNamesMap }
: { schema: undefined, tableNamesMap: {} };
? {
schema: schema.schema,
fullSchema: schema.fullSchema as TFullSchema,
tableNamesMap: schema.tableNamesMap,
}
: {
schema: undefined,
fullSchema: {} as TFullSchema,
tableNamesMap: {},
};
this.query = {} as typeof this['query'];
const query = this.query as {
[K in keyof TSchema]: RelationalQueryBuilder<TResultKind, TFullSchema, TSchema, TSchema[K]>;
Expand Down
15 changes: 13 additions & 2 deletions drizzle-orm/src/sqlite-core/query-builders/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ export type SQLiteInsertReturningAll<

export type SQLiteInsertOnConflictDoUpdateConfig<T extends AnySQLiteInsert> = {
target: IndexColumn | IndexColumn[];
/** @deprecated - use either `targetWhere` or `setWhere` */
where?: SQL;
// TODO: add tests for targetWhere and setWhere
targetWhere?: SQL;
setWhere?: SQL;
set: SQLiteUpdateSetSource<T['_']['table']>;
};

Expand Down Expand Up @@ -305,10 +309,17 @@ export class SQLiteInsertBase<
* ```
*/
onConflictDoUpdate(config: SQLiteInsertOnConflictDoUpdateConfig<this>): this {
if (config.where && (config.targetWhere || config.setWhere)) {
throw new Error(
'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.',
);
}
const whereSql = config.where ? sql` where ${config.where}` : undefined;
const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : undefined;
const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : undefined;
const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
const whereSql = config.where ? sql` where ${config.where}` : sql``;
const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
this.config.onConflict = sql`${targetSql} do update set ${setSql}${whereSql}`;
this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
return this;
}

Expand Down
9 changes: 7 additions & 2 deletions drizzle-orm/src/vercel-postgres/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class VercelPgSession<
const session = this.client instanceof VercelPool // eslint-disable-line no-instanceof/no-instanceof
? new VercelPgSession(await this.client.connect(), this.dialect, this.schema, this.options)
: this;
const tx = new VercelPgTransaction(this.dialect, session, this.schema);
const tx = new VercelPgTransaction<TFullSchema, TSchema>(this.dialect, session, this.schema);
await tx.execute(sql`begin${config ? sql` ${tx.getTransactionConfigSQL(config)}` : undefined}`);
try {
const result = await transaction(tx);
Expand All @@ -176,7 +176,12 @@ export class VercelPgTransaction<
transaction: (tx: VercelPgTransaction<TFullSchema, TSchema>) => Promise<T>,
): Promise<T> {
const savepointName = `sp${this.nestedIndex + 1}`;
const tx = new VercelPgTransaction(this.dialect, this.session, this.schema, this.nestedIndex + 1);
const tx = new VercelPgTransaction<TFullSchema, TSchema>(
this.dialect,
this.session,
this.schema,
this.nestedIndex + 1,
);
await tx.execute(sql.raw(`savepoint ${savepointName}`));
try {
const result = await transaction(tx);
Expand Down

0 comments on commit e0aaeb2

Please sign in to comment.