-
-
Notifications
You must be signed in to change notification settings - Fork 476
/
session.ts
174 lines (149 loc) 路 5.49 KB
/
session.ts
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/// <reference types="bun-types" />
import type { Database, Statement as BunStatement } from 'bun:sqlite';
import { entityKind } from '~/entity.ts';
import type { Logger } from '~/logger.ts';
import { NoopLogger } from '~/logger.ts';
import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts';
import { fillPlaceholders, type Query, sql } from '~/sql/sql.ts';
import type { SQLiteSyncDialect } from '~/sqlite-core/dialect.ts';
import { SQLiteTransaction } from '~/sqlite-core/index.ts';
import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types.ts';
import type {
PreparedQueryConfig as PreparedQueryConfigBase,
SQLiteExecuteMethod,
SQLiteTransactionConfig,
} from '~/sqlite-core/session.ts';
import { SQLitePreparedQuery as PreparedQueryBase, SQLiteSession } from '~/sqlite-core/session.ts';
import { mapResultRow } from '~/utils.ts';
export interface SQLiteBunSessionOptions {
logger?: Logger;
}
type PreparedQueryConfig = Omit<PreparedQueryConfigBase, 'statement' | 'run'>;
type Statement = BunStatement<any>;
export class SQLiteBunSession<
TFullSchema extends Record<string, unknown>,
TSchema extends TablesRelationalConfig,
> extends SQLiteSession<'sync', void, TFullSchema, TSchema> {
static readonly [entityKind]: string = 'SQLiteBunSession';
private logger: Logger;
constructor(
private client: Database,
dialect: SQLiteSyncDialect,
private schema: RelationalSchemaConfig<TSchema> | undefined,
options: SQLiteBunSessionOptions = {},
) {
super(dialect);
this.logger = options.logger ?? new NoopLogger();
}
exec(query: string): void {
this.client.exec(query);
}
prepareQuery<T extends Omit<PreparedQueryConfig, 'run'>>(
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,
isResponseInArrayMode,
customResultMapper,
);
}
override transaction<T>(
transaction: (tx: SQLiteBunTransaction<TFullSchema, TSchema>) => T,
config: SQLiteTransactionConfig = {},
): T {
const tx = new SQLiteBunTransaction('sync', this.dialect, this, this.schema);
let result: T | undefined;
const nativeTx = this.client.transaction(() => {
result = transaction(tx);
});
nativeTx[config.behavior ?? 'deferred']();
return result!;
}
}
export class SQLiteBunTransaction<
TFullSchema extends Record<string, unknown>,
TSchema extends TablesRelationalConfig,
> extends SQLiteTransaction<'sync', void, TFullSchema, TSchema> {
static readonly [entityKind]: string = 'SQLiteBunTransaction';
override transaction<T>(transaction: (tx: SQLiteBunTransaction<TFullSchema, TSchema>) => T): T {
const savepointName = `sp${this.nestedIndex}`;
const tx = new SQLiteBunTransaction('sync', this.dialect, this.session, this.schema, this.nestedIndex + 1);
this.session.run(sql.raw(`savepoint ${savepointName}`));
try {
const result = transaction(tx);
this.session.run(sql.raw(`release savepoint ${savepointName}`));
return result;
} catch (err) {
this.session.run(sql.raw(`rollback to savepoint ${savepointName}`));
throw err;
}
}
}
export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends PreparedQueryBase<
{ type: 'sync'; run: void; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }
> {
static readonly [entityKind]: string = 'SQLiteBunPreparedQuery';
constructor(
private stmt: Statement,
query: Query,
private logger: Logger,
private fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
private _isResponseInArrayMode: boolean,
private customResultMapper?: (rows: unknown[][]) => unknown,
) {
super('sync', executeMethod, query);
}
run(placeholderValues?: Record<string, unknown>): void {
const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
this.logger.logQuery(this.query.sql, params);
return this.stmt.run(...params);
}
all(placeholderValues?: Record<string, unknown>): T['all'] {
const { fields, query, logger, joinsNotNullableMap, stmt, customResultMapper } = this;
if (!fields && !customResultMapper) {
const params = fillPlaceholders(query.params, placeholderValues ?? {});
logger.logQuery(query.sql, params);
return stmt.all(...params);
}
const rows = this.values(placeholderValues) as unknown[][];
if (customResultMapper) {
return customResultMapper(rows) as T['all'];
}
return rows.map((row) => mapResultRow(fields!, row, joinsNotNullableMap));
}
get(placeholderValues?: Record<string, unknown>): T['get'] {
const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
this.logger.logQuery(this.query.sql, params);
const row = this.stmt.values(...params)[0];
if (!row) {
return undefined;
}
const { fields, joinsNotNullableMap, customResultMapper } = this;
if (!fields && !customResultMapper) {
return row;
}
if (customResultMapper) {
return customResultMapper([row]) as T['get'];
}
return mapResultRow(fields!, row, joinsNotNullableMap);
}
values(placeholderValues?: Record<string, unknown>): T['values'] {
const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
this.logger.logQuery(this.query.sql, params);
return this.stmt.values(...params);
}
/** @internal */
isResponseInArrayMode(): boolean {
return this._isResponseInArrayMode;
}
}