-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
190 lines (163 loc) · 5.69 KB
/
index.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { EventEmitter } from "events";
import * as BetterSqlite3 from "better-sqlite3";
const isV7 = parseInt(require("better-sqlite3/package.json").version) >= 7;
const releaseEvent = "release";
export interface PoolConnection extends BetterSqlite3.Database {
/** Whether the connection is available and can be acquired. */
readonly available: boolean;
/** Releases the connection. */
release(): void;
}
export interface PoolOptions extends BetterSqlite3.Options {
/**
* The number of milliseconds to wait when executing queries on a locked
* database, before throwing a SQLITE_BUSY error. Also, this option is used
* to determine how long it'd be waited before throwing timeout error when
* acquiring the connection. (default: 5000).
*/
timeout?: number;
/**
* A function that gets called with every SQL string executed by the
* database connection (default: `null`).
*/
verbose?: (...args: any[]) => any;
/** Max connections in the pool, default is `5`. */
max?: number;
onConnectionCreated?: (conn: PoolConnection) => void;
}
export class Pool extends EventEmitter implements PoolOptions {
readonly path: string;
readonly memory: boolean;
readonly readonly: boolean = false;
readonly fileMustExist: boolean = false;
readonly timeout: number = 5000;
readonly verbose: (...args: any[]) => any;
onConnectionCreated?: (conn: PoolConnection) => void;
readonly max: number = 5;
protected connections: PoolConnection[] = [];
private _closed = false;
/**
* Creates a new pool to store database connections.
*
* @param path A SQLite database file path, can be set to
* `:memory` to open a memory based database.
* @param options If this argument is set to a boolean, it's equivalent to
* `readonly`, if set to a number, it's equivalent to `max`.
*
* @see https://github.com/JoshuaWise/better-sqlite3/wiki/API#new-databasepath-options
*/
constructor(path: string, options?: number | boolean | PoolOptions) {
super();
if (options === undefined || options === null) {
options = {};
} else if (typeof options === "boolean") {
options = { readonly: options };
} else if (typeof options === "number") {
options = { max: options };
}
Object.assign(this, {
path,
memory: path === ":memory",
verbose: null,
}, options);
}
/**
* Acquires a connection from the pool.
* @see https://github.com/JoshuaWise/better-sqlite3/wiki/API#class-database
*/
acquire(): Promise<PoolConnection> {
if (this._closed) {
throw new Error("Database already closed");
}
const conn = this._getAvailableConnection()
|| this._createConnection();
if (conn) {
return Promise.resolve(conn);
} else {
return this._waitConnection();
}
}
private _getAvailableConnection() {
for (let conn of this.connections) {
if (conn.available && conn.open) {
Object.assign(conn, {
available: false,
} as Partial<PoolConnection>);
return conn;
}
}
return null;
}
private _createConnection() {
if (this.connections.length < this.max) {
let conn = this._rawCreateConnection() as PoolConnection;
Object.assign(conn, {
available: false,
} as Partial<PoolConnection>);
conn.release = () => {
if (conn.open && conn.inTransaction)
conn.exec("rollback");
if (this._closed) {
conn.close();
}
else {
Object.assign(conn, {
available: conn.open && true,
} as Partial<PoolConnection>);
this.emit(releaseEvent);
}
};
if (this.onConnectionCreated) {
this.onConnectionCreated(conn);
}
this.connections.push(conn);
return conn;
}
return null;
}
/**
* low level create connection
* TODO: this should be abstract method for universal Database Pool
*/
private _rawCreateConnection() {
const options = {
"readonly": this.readonly,
"fileMustExist": this.fileMustExist,
"timeout": this.timeout,
"verbose": this.verbose,
};
if (isV7) {
Object.assign(options, { [":memory"]: this.memory });
} else {
Object.assign(options, { memory: this.memory });
}
return new BetterSqlite3(this.path, options);
}
private _waitConnection() {
return new Promise<PoolConnection>((resolve, reject) => {
const handler = () => {
clearTimeout(timer);
resolve(this.acquire());
};
const timer = setTimeout(() => {
this.removeListener(releaseEvent, handler);
reject(new Error("Timeout to acquire the connection."));
}, this.timeout);
this.once(releaseEvent, handler);
});
}
/**
* Closes all connections in the pool.
* @see https://github.com/JoshuaWise/better-sqlite3/wiki/API#close---this
*/
close() {
this._closed = true;
for (let id in this.connections) {
const conn = this.connections[id];
if (conn.available && conn.open) {
conn.close();
}
}
}
}
export default Pool;