velocious 1.0.11 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/database/drivers/sqlite/base.js +1 -1
- package/src/database/drivers/sqlite/index.js +52 -0
- package/src/database/drivers/sqlite/query.js +20 -0
- package/src/database/migrate-from-require-context.js +2 -2
- package/src/database/migrator.js +1 -1
- package/src/database/record/index.js +1 -1
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@ import escapeString from "sql-string-escape"
|
|
|
9
9
|
import Insert from "../sqlite/sql/insert.js"
|
|
10
10
|
import Options from "../sqlite/options.js"
|
|
11
11
|
import QueryParser from "../sqlite/query-parser.js"
|
|
12
|
-
import Table from "./table"
|
|
12
|
+
import Table from "./table.js"
|
|
13
13
|
import Update from "../sqlite/sql/update.js"
|
|
14
14
|
|
|
15
15
|
export default class VelociousDatabaseDriversSqliteBase extends Base {
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import debounce from "debounce"
|
|
2
|
+
import {digg} from "diggerize"
|
|
3
|
+
import fs from "fs/promises"
|
|
4
|
+
import query from "./query.js"
|
|
5
|
+
import sqlite3 from "sqlite3"
|
|
6
|
+
import {open} from "sqlite"
|
|
7
|
+
|
|
8
|
+
import Base from "./base.js"
|
|
9
|
+
|
|
10
|
+
export default class VelociousDatabaseDriversSqliteNode extends Base {
|
|
11
|
+
async connect() {
|
|
12
|
+
const args = this.getArgs()
|
|
13
|
+
const databasePath = `db/${this.localStorageName()}.sqlite`
|
|
14
|
+
|
|
15
|
+
if (args.reset) {
|
|
16
|
+
await fs.unlink(databasePath)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
this.connection = await open({
|
|
20
|
+
filename: databasePath,
|
|
21
|
+
driver: sqlite3.Database
|
|
22
|
+
})
|
|
23
|
+
await this.registerVersion()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
localStorageName = () => `VelociousDatabaseDriversSqlite---${digg(this.getArgs(), "name")}`
|
|
27
|
+
disconnect = () => this.saveDatabase()
|
|
28
|
+
saveDatabase = async () => {
|
|
29
|
+
const localStorageContent = this.connection.export()
|
|
30
|
+
await this.betterLocaleStorage.set(this.localStorageName(), localStorageContent)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
saveDatabaseDebounce = debounce(this.saveDatabase, 500)
|
|
34
|
+
|
|
35
|
+
async close() {
|
|
36
|
+
await this.saveDatabase()
|
|
37
|
+
await this.connection.end()
|
|
38
|
+
this.connection = undefined
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
query = async (sql) => {
|
|
42
|
+
const result = await query(this.connection, sql)
|
|
43
|
+
const downcasedSQL = sql.toLowerCase().trim()
|
|
44
|
+
|
|
45
|
+
// Auto-save database in local storage in case we can find manipulating instructions in the SQL
|
|
46
|
+
if (downcasedSQL.startsWith("delete ") || downcasedSQL.startsWith("insert into ") || downcasedSQL.startsWith("update ")) {
|
|
47
|
+
this.saveDatabaseDebounce()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return result
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export default async function query(connection, sql) {
|
|
2
|
+
const rows = []
|
|
3
|
+
let result
|
|
4
|
+
|
|
5
|
+
try {
|
|
6
|
+
result = await connection.all(sql)
|
|
7
|
+
} catch (error) {
|
|
8
|
+
let sqlInErrorMessage = `${sql}`
|
|
9
|
+
|
|
10
|
+
if (sqlInErrorMessage.length >= 4096) {
|
|
11
|
+
sqlInErrorMessage = `${sqlInErrorMessage.substring(0, 4096)}...`
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
error.message += `\n\n${sqlInErrorMessage}`
|
|
15
|
+
|
|
16
|
+
throw error
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return result
|
|
20
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Configuration from "../configuration.js"
|
|
2
2
|
import * as inflection from "inflection"
|
|
3
|
-
import Migrator from "./migrator"
|
|
3
|
+
import Migrator from "./migrator.js"
|
|
4
4
|
|
|
5
5
|
export default class VelociousDatabaseMigrateFromRequireContext {
|
|
6
6
|
constructor(configuration) {
|
|
@@ -13,7 +13,7 @@ export default class VelociousDatabaseMigrateFromRequireContext {
|
|
|
13
13
|
|
|
14
14
|
const files = requireContext.keys()
|
|
15
15
|
.map((file) => {
|
|
16
|
-
const match = file.match(
|
|
16
|
+
const match = file.match(/(\d{14})-(.+)\.js$/)
|
|
17
17
|
|
|
18
18
|
if (!match) return null
|
|
19
19
|
|
package/src/database/migrator.js
CHANGED
|
@@ -145,7 +145,7 @@ export default class VelociousDatabaseRecord {
|
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
static async initializeRecord({configuration}) {
|
|
148
|
-
if (!configuration) throw new Error(
|
|
148
|
+
if (!configuration) throw new Error(`No configuration given for ${this.name}`)
|
|
149
149
|
|
|
150
150
|
this._configuration = configuration
|
|
151
151
|
this._configuration.registerModelClass(this)
|