-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into configurable-vfs
- Loading branch information
Showing
27 changed files
with
319 additions
and
197 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
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
70 changes: 70 additions & 0 deletions
70
packages/drizzle-driver/src/sqlite/PowerSyncSQLiteSession.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { AbstractPowerSyncDatabase, DBAdapter } from '@powersync/common'; | ||
import { entityKind } from 'drizzle-orm/entity'; | ||
import type { RelationalSchemaConfig, TablesRelationalConfig } from 'drizzle-orm/relations'; | ||
import { type Query } from 'drizzle-orm/sql/sql'; | ||
import type { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core/dialect'; | ||
import type { SelectedFieldsOrdered } from 'drizzle-orm/sqlite-core/query-builders/select.types'; | ||
import { | ||
type PreparedQueryConfig as PreparedQueryConfigBase, | ||
type SQLiteExecuteMethod | ||
} from 'drizzle-orm/sqlite-core/session'; | ||
import { PowerSyncSQLitePreparedQuery } from './PowerSyncSQLitePreparedQuery'; | ||
import { | ||
PowerSyncSQLiteSessionOptions, | ||
PowerSyncSQLiteTransaction, | ||
PowerSyncSQLiteTransactionConfig, | ||
PowerSyncSQLiteBaseSession | ||
} from './PowerSyncSQLiteBaseSession'; | ||
|
||
export class PowerSyncSQLiteSession< | ||
TFullSchema extends Record<string, unknown>, | ||
TSchema extends TablesRelationalConfig | ||
> extends PowerSyncSQLiteBaseSession<TFullSchema, TSchema> { | ||
static readonly [entityKind]: string = 'PowerSyncSQLiteSession'; | ||
protected client: AbstractPowerSyncDatabase; | ||
constructor( | ||
db: AbstractPowerSyncDatabase, | ||
dialect: SQLiteAsyncDialect, | ||
schema: RelationalSchemaConfig<TSchema> | undefined, | ||
options: PowerSyncSQLiteSessionOptions = {} | ||
) { | ||
super(db, dialect, schema, options); | ||
this.client = db; | ||
} | ||
|
||
transaction<T>( | ||
transaction: (tx: PowerSyncSQLiteTransaction<TFullSchema, TSchema>) => T, | ||
config: PowerSyncSQLiteTransactionConfig = {} | ||
): T { | ||
const { accessMode = 'read write' } = config; | ||
|
||
if (accessMode === 'read only') { | ||
return this.client.readLock(async (ctx) => this.internalTransaction(ctx, transaction, config)) as T; | ||
} | ||
|
||
return this.client.writeLock(async (ctx) => this.internalTransaction(ctx, transaction, config)) as T; | ||
} | ||
|
||
protected async internalTransaction<T>( | ||
connection: DBAdapter, | ||
fn: (tx: PowerSyncSQLiteTransaction<TFullSchema, TSchema>) => T, | ||
config: PowerSyncSQLiteTransactionConfig = {} | ||
): Promise<T> { | ||
const tx = new PowerSyncSQLiteTransaction<TFullSchema, TSchema>( | ||
'async', | ||
(this as any).dialect, | ||
new PowerSyncSQLiteBaseSession(connection, this.dialect, this.schema, this.options), | ||
this.schema | ||
); | ||
|
||
await connection.execute(`begin${config?.behavior ? ' ' + config.behavior : ''}`); | ||
try { | ||
const result = await fn(tx); | ||
await connection.execute(`commit`); | ||
return result; | ||
} catch (err) { | ||
await connection.execute(`rollback`); | ||
throw err; | ||
} | ||
} | ||
} |
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
4 changes: 2 additions & 2 deletions
4
...-driver/tests/sqlite/sqlite-query.test.ts → ...drizzle-driver/tests/sqlite/query.test.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
Oops, something went wrong.