/
SqliteBase.ts
126 lines (108 loc) · 3.48 KB
/
SqliteBase.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
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/// <reference path="../../adonis-typings/index.ts" />
import { DialectContract, QueryClientContract, SqliteConfig } from '@ioc:Adonis/Lucid/Database'
export abstract class BaseSqliteDialect implements DialectContract {
public abstract readonly name: 'sqlite3' | 'better-sqlite3'
public readonly supportsAdvisoryLocks = false
public readonly supportsViews = true
public readonly supportsTypes = false
public readonly supportsReturningStatement = false
/**
* Reference to the database version. Knex.js fetches the version after
* the first database query, so it will be set to undefined initially
*/
public readonly version = this.client.getReadClient()['context']['client'].version
/**
* The default format for datetime column. The date formats is
* valid for luxon date parsing library
*/
public readonly dateTimeFormat = 'yyyy-MM-dd HH:mm:ss'
constructor(private client: QueryClientContract, private config: SqliteConfig) {}
/**
* Returns an array of table names
*/
public async getAllTables() {
const tables = await this.client
.query()
.from('sqlite_master')
.select('name as table_name')
.where('type', 'table')
.whereNot('name', 'like', 'sqlite_%')
.orderBy('name', 'asc')
return tables.map(({ table_name }) => table_name)
}
/**
* Returns an array of all views names
*/
public async getAllViews(): Promise<string[]> {
const tables = await this.client
.query()
.from('sqlite_master')
.select('name as table_name')
.where('type', 'view')
.whereNot('name', 'like', 'sqlite_%')
.orderBy('name', 'asc')
return tables.map(({ table_name }) => table_name)
}
/**
* Returns an array of all types names
*/
public async getAllTypes(): Promise<string[]> {
throw new Error("Sqlite doesn't support types")
}
/**
* Truncate SQLITE tables
*/
public async truncate(table: string) {
return this.client.knexQuery().table(table).truncate()
}
/**
* Drop all tables inside the database
*/
public async dropAllTables() {
await this.client.rawQuery('PRAGMA writable_schema = 1;')
await this.client
.knexQuery()
.delete()
.from('sqlite_master')
.whereIn('type', ['table', 'index', 'trigger'])
.whereNotIn('name', this.config.wipe?.ignoreTables || [])
await this.client.rawQuery('PRAGMA writable_schema = 0;')
await this.client.rawQuery('VACUUM;')
}
/**
* Drop all views inside the database
*/
public async dropAllViews(): Promise<void> {
await this.client.rawQuery('PRAGMA writable_schema = 1;')
await this.client.rawQuery(`delete from sqlite_schema where type = 'view';`)
await this.client.rawQuery('PRAGMA writable_schema = 0;')
await this.client.rawQuery('VACUUM;')
}
/**
* Drop all custom types inside the database
*/
public async dropAllTypes(): Promise<void> {
throw new Error("Sqlite doesn't support types")
}
/**
* Attempts to add advisory lock to the database and
* returns it's status.
*/
public getAdvisoryLock(): Promise<boolean> {
throw new Error("Sqlite doesn't support advisory locks")
}
/**
* Releases the advisory lock
*/
public releaseAdvisoryLock(): Promise<boolean> {
throw new Error("Sqlite doesn't support advisory locks")
}
}