| 10 | }; |
| 11 | |
| 12 | export default class SqliteDatabase implements DatabaseInterface { |
| 13 | public readonly connection: Database; |
| 14 | |
| 15 | constructor(connection: SqliteConfig|Database) { |
| 16 | if (connection instanceof Database) { |
| 17 | this.connection = connection; |
| 18 | } else { |
| 19 | this.connection = new Database( |
| 20 | connection.filename, |
| 21 | connection.mode, |
| 22 | ); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | async disconnect(): Promise<void> { |
| 27 | if (this.connection instanceof Database) { |
| 28 | return new Promise((resolve, reject) => { |
| 29 | this.connection.close(err => { |
| 30 | if (err) { |
| 31 | reject(err); |
| 32 | } else { |
| 33 | resolve(); |
| 34 | } |
| 35 | }); |
| 36 | }); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | indexToPlaceholder (i: number): string { |
| 41 | return '?'; |
| 42 | } |
| 43 | |
| 44 | async query(query: SqlQuery): Promise<any[]> { |
| 45 | return new Promise((resolve, reject) => { |
| 46 | const compiledQuery = query.compile(this.indexToPlaceholder, formatIdentifier); |
| 47 | |
| 48 | this.connection.all( |
| 49 | compiledQuery.sql, |
| 50 | <any[]><any>compiledQuery.params, |
| 51 | (error, rows) => { |
| 52 | if (error) { |
| 53 | reject(error); |
| 54 | } else { |
| 55 | resolve(rows); |
| 56 | } |
| 57 | }, |
| 58 | ); |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | async sequence<T>( |
| 63 | sequence: (sequenceDb: SqliteDatabase) => Promise<T>, |
| 64 | ): Promise<T> { |
| 65 | return new Promise((resolve, reject) => { |
| 66 | this.connection.serialize(async () => { |
| 67 | try { |
| 68 | const result = await sequence(this); |
| 69 | resolve(result); |
nothing calls this directly
no outgoing calls
no test coverage detected