forked from typeorm/typeorm
/
SqliteDriver.ts
145 lines (118 loc) · 4.78 KB
/
SqliteDriver.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
import mkdirp from 'mkdirp';
import path from 'path';
import { DriverPackageNotInstalledError } from "../../error/DriverPackageNotInstalledError";
import { SqliteQueryRunner } from "./SqliteQueryRunner";
import { DriverOptionNotSetError } from "../../error/DriverOptionNotSetError";
import { PlatformTools } from "../../platform/PlatformTools";
import { Connection } from "../../connection/Connection";
import { SqliteConnectionOptions } from "./SqliteConnectionOptions";
import { ColumnType } from "../types/ColumnTypes";
import { QueryRunner } from "../../query-runner/QueryRunner";
import { AbstractSqliteDriver } from "../sqlite-abstract/AbstractSqliteDriver";
/**
* Organizes communication with sqlite DBMS.
*/
export class SqliteDriver extends AbstractSqliteDriver {
// -------------------------------------------------------------------------
// Public Properties
// -------------------------------------------------------------------------
/**
* Connection options.
*/
options: SqliteConnectionOptions;
/**
* SQLite underlying library.
*/
sqlite: any;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(connection: Connection) {
super(connection);
this.connection = connection;
this.options = connection.options as SqliteConnectionOptions;
this.database = this.options.database;
// validate options to make sure everything is set
if (!this.options.database)
throw new DriverOptionNotSetError("database");
// load sqlite package
this.loadDependencies();
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Closes connection with database.
*/
async disconnect(): Promise<void> {
return new Promise<void>((ok, fail) => {
this.queryRunner = undefined;
this.databaseConnection.close((err: any) => err ? fail(err) : ok());
});
}
/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master" | "slave" = "master"): QueryRunner {
if (!this.queryRunner)
this.queryRunner = new SqliteQueryRunner(this);
return this.queryRunner;
}
normalizeType(column: { type?: ColumnType, length?: number | string, precision?: number | null, scale?: number }): string {
if ((column.type as any) === Buffer) {
return "blob";
}
return super.normalizeType(column);
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
/**
* Creates connection with the database.
*/
protected async createDatabaseConnection() {
await this.createDatabaseDirectory(this.options.database);
const databaseConnection: any = await new Promise((ok, fail) => {
const connection = new this.sqlite.Database(this.options.database, (err: any) => {
if (err) return fail(err);
ok(connection);
});
});
// Internal function to run a command on the connection and fail if an error occured.
function run(line: string): Promise<void> {
return new Promise((ok, fail) => {
databaseConnection.run(line, (err: any) => {
if (err) return fail(err);
ok();
});
});
}
if (this.options.enableWAL) {
await run(`PRAGMA journal_mode = WAL;`);
}
// we need to enable foreign keys in sqlite to make sure all foreign key related features
// working properly. this also makes onDelete to work with sqlite.
await run(`PRAGMA foreign_keys = ON;`);
// in the options, if encryption key for SQLCipher is setted.
if (this.options.key) {
await run(`PRAGMA key = ${JSON.stringify(this.options.key)};`);
}
return databaseConnection;
}
/**
* If driver dependency is not given explicitly, then try to load it via "require".
*/
protected loadDependencies(): void {
try {
this.sqlite = PlatformTools.load("sqlite3").verbose();
} catch (e) {
throw new DriverPackageNotInstalledError("SQLite", "sqlite3");
}
}
/**
* Auto creates database directory if it does not exist.
*/
protected async createDatabaseDirectory(fullPath: string): Promise<void> {
await mkdirp(path.dirname(fullPath));
}
}