-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathPowerSyncSQLiteBaseSession.ts
More file actions
116 lines (106 loc) · 3.57 KB
/
Copy pathPowerSyncSQLiteBaseSession.ts
File metadata and controls
116 lines (106 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import type { AbstractPowerSyncDatabase, QueryResult } from '@powersync/common';
import type { WithCacheConfig } from 'drizzle-orm/cache/core/types';
import { entityKind } from 'drizzle-orm/entity';
import type { Logger } from 'drizzle-orm/logger';
import { NoopLogger } from 'drizzle-orm/logger';
import type { AnyRelations, EmptyRelations, RelationalQueryMapperConfig } 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 {
SQLiteSession,
SQLiteTransaction,
type PreparedQueryConfig as PreparedQueryConfigBase,
type SQLiteExecuteMethod,
type SQLiteTransactionConfig
} from 'drizzle-orm/sqlite-core/session';
import { PowerSyncSQLitePreparedQuery, type ContextProvider } from './PowerSyncSQLitePreparedQuery.js';
type ResultMapper = (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => unknown;
type RelationalResultMapper = (
rows: Record<string, unknown>[],
mapColumnValue?: (value: unknown) => unknown
) => unknown;
export interface PowerSyncSQLiteSessionOptions {
logger?: Logger;
db: AbstractPowerSyncDatabase;
}
export type PowerSyncSQLiteTransactionConfig = SQLiteTransactionConfig & {
accessMode?: 'read only' | 'read write';
};
export class PowerSyncSQLiteTransaction<TRelations extends AnyRelations = EmptyRelations> extends SQLiteTransaction<
'async',
QueryResult,
Record<string, never>,
TRelations
> {
static readonly [entityKind]: string = 'PowerSyncSQLiteTransaction';
}
export class PowerSyncSQLiteBaseSession<TRelations extends AnyRelations = EmptyRelations> extends SQLiteSession<
'async',
QueryResult,
Record<string, never>,
TRelations
> {
static readonly [entityKind]: string = 'PowerSyncSQLiteBaseSession';
protected logger: Logger;
constructor(
protected contextProvider: ContextProvider,
protected dialect: SQLiteAsyncDialect,
protected relations: TRelations,
protected options: PowerSyncSQLiteSessionOptions
) {
super(dialect);
this.logger = options.logger ?? new NoopLogger();
}
prepareQuery<T extends PreparedQueryConfigBase & { type: 'async' }>(
query: Query,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
customResultMapper?: ResultMapper,
queryMetadata?: {
type: 'select' | 'update' | 'delete' | 'insert';
tables: string[];
},
cacheConfig?: WithCacheConfig
): PowerSyncSQLitePreparedQuery<T> {
return new PowerSyncSQLitePreparedQuery(
this.contextProvider,
query,
this.logger,
fields,
executeMethod,
false,
customResultMapper,
undefined,
queryMetadata,
cacheConfig
);
}
prepareRelationalQuery<T extends PreparedQueryConfigBase & { type: 'async' }>(
query: Query,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
customResultMapper: RelationalResultMapper,
_config: RelationalQueryMapperConfig
): PowerSyncSQLitePreparedQuery<T> {
return new PowerSyncSQLitePreparedQuery(
this.contextProvider,
query,
this.logger,
fields,
executeMethod,
false,
customResultMapper,
undefined,
{ type: 'select', tables: [] },
undefined,
true
);
}
transaction<T>(
_transaction: (tx: PowerSyncSQLiteTransaction<TRelations>) => T,
_config: PowerSyncSQLiteTransactionConfig = {}
): T {
throw new Error('Nested transactions are not supported');
}
}