velocious 1.0.101 → 1.0.103
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/spec/database/drivers/create-sql/create-index-sql-spec.js +24 -0
- package/src/configuration-types.js +7 -1
- package/src/configuration.js +2 -2
- package/src/database/drivers/base-column.js +2 -2
- package/src/database/drivers/base-columns-index.js +6 -0
- package/src/database/drivers/base.js +60 -22
- package/src/database/drivers/mssql/index.js +70 -22
- package/src/database/drivers/mssql/sql/create-database.js +9 -2
- package/src/database/drivers/mssql/table.js +14 -4
- package/src/database/drivers/mysql/column.js +6 -0
- package/src/database/drivers/mysql/columns-index.js +3 -6
- package/src/database/drivers/mysql/foreign-key.js +2 -0
- package/src/database/drivers/mysql/index.js +43 -16
- package/src/database/drivers/mysql/query-parser.js +2 -0
- package/src/database/drivers/mysql/query.js +7 -2
- package/src/database/drivers/mysql/table.js +6 -0
- package/src/database/drivers/pgsql/index.js +78 -11
- package/src/database/drivers/sqlite/base.js +77 -25
- package/src/database/drivers/sqlite/column.js +8 -0
- package/src/database/drivers/sqlite/sql/alter-table.js +25 -20
- package/src/database/drivers/sqlite/sql/create-index.js +2 -0
- package/src/database/drivers/sqlite/sql/create-table.js +2 -0
- package/src/database/drivers/sqlite/sql/delete.js +4 -2
- package/src/database/drivers/sqlite/sql/drop-table.js +2 -0
- package/src/database/drivers/sqlite/sql/insert.js +2 -0
- package/src/database/drivers/sqlite/sql/update.js +2 -0
- package/src/database/drivers/sqlite/table.js +14 -0
- package/src/database/migration/index.js +6 -4
- package/src/database/pool/base-methods-forward.js +2 -2
- package/src/database/query/alter-table-base.js +1 -1
- package/src/database/query/base.js +2 -2
- package/src/database/query/create-database-base.js +8 -4
- package/src/database/query/create-index-base.js +12 -7
- package/src/database/query/create-table-base.js +4 -4
- package/src/database/query/drop-table-base.js +8 -8
- package/src/database/query/index.js +31 -18
- package/src/database/query/insert-base.js +18 -3
- package/src/database/query-parser/base-query-parser.js +2 -2
- package/src/database/record/index.js +444 -172
- package/src/database/record/instance-relationships/base.js +41 -44
- package/src/database/record/instance-relationships/belongs-to.js +15 -3
- package/src/database/record/instance-relationships/has-many.js +49 -28
- package/src/database/record/instance-relationships/has-one.js +22 -7
- package/src/database/record/relationships/base.js +33 -43
- package/src/database/record/relationships/belongs-to.js +13 -3
- package/src/database/record/relationships/has-many.js +8 -2
- package/src/database/record/relationships/has-one.js +8 -2
- package/src/database/record/validators/base.js +14 -2
- package/src/database/record/validators/presence.js +7 -0
- package/src/database/table-data/table-column.js +3 -3
- package/src/environment-handlers/node.js +1 -2
package/package.json
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import dummyConfiguration from "../../../dummy/src/config/configuration.js"
|
|
4
|
+
|
|
5
|
+
describe("database - create sql - create index sql", () => {
|
|
6
|
+
it("runs migrations", {databaseCleaning: {transaction: false}}, async () => {
|
|
7
|
+
await dummyConfiguration.ensureConnections(async (dbs) => {
|
|
8
|
+
const createIndexSQLs = await dbs.default.createIndexSQLs({
|
|
9
|
+
columns: ["id", "created_at"],
|
|
10
|
+
tableName: "projects"
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
if (dbs.default.getType() == "sqlite") {
|
|
14
|
+
expect(createIndexSQLs).toEqual("CREATE INDEX `index_on_projects_id_and_created_at` ON `projects` (`id`, `created_at`)")
|
|
15
|
+
} else if (dbs.default.getType() == "mssql") {
|
|
16
|
+
expect(createIndexSQLs).toEqual("CREATE INDEX [index_on_id_and_created_at] ON [projects] ([id], [created_at])")
|
|
17
|
+
} else if (dbs.default.getType() == "pgsql") {
|
|
18
|
+
expect(createIndexSQLs).toEqual(`CREATE INDEX "index_on_id_and_created_at" ON "projects" ("id", "created_at")`)
|
|
19
|
+
} else {
|
|
20
|
+
expect(createIndexSQLs).toEqual("CREATE INDEX `index_on_id_and_created_at` ON `projects` (`id`, `created_at`)")
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
})
|
|
24
|
+
})
|
|
@@ -21,9 +21,15 @@
|
|
|
21
21
|
* @property {string} [host]
|
|
22
22
|
* @property {boolean} [migrations]
|
|
23
23
|
* @property {string} [password]
|
|
24
|
+
* @property {object} [record]
|
|
25
|
+
* @property {boolean} [record.transactions]
|
|
24
26
|
* @property {string} [username]
|
|
25
27
|
*/
|
|
26
28
|
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {Record<string, string[]>} LocaleFallbacksType
|
|
31
|
+
*/
|
|
32
|
+
|
|
27
33
|
/**
|
|
28
34
|
* @typedef {object} ConfigurationArgsType
|
|
29
35
|
* @property {object} args
|
|
@@ -37,7 +43,7 @@
|
|
|
37
43
|
* @property {InitializersType} initializers
|
|
38
44
|
* @property {string | function() : string} locale
|
|
39
45
|
* @property {string[]} locales
|
|
40
|
-
* @property {
|
|
46
|
+
* @property {LocaleFallbacksType} localeFallbacks
|
|
41
47
|
* @property {string} testing
|
|
42
48
|
*/
|
|
43
49
|
|
package/src/configuration.js
CHANGED
|
@@ -162,12 +162,12 @@ export default class VelociousConfiguration {
|
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
/**
|
|
165
|
-
* @returns {
|
|
165
|
+
* @returns {import("./configuration-types.js").LocaleFallbacksType | undefined}
|
|
166
166
|
*/
|
|
167
167
|
getLocaleFallbacks() { return this.localeFallbacks }
|
|
168
168
|
|
|
169
169
|
/**
|
|
170
|
-
* @param {
|
|
170
|
+
* @param {import("./configuration-types.js").LocaleFallbacksType} newLocaleFallbacks
|
|
171
171
|
* @returns {void}
|
|
172
172
|
*/
|
|
173
173
|
setLocaleFallbacks(newLocaleFallbacks) { this.localeFallbacks = newLocaleFallbacks }
|
|
@@ -46,7 +46,7 @@ export default class VelociousDatabaseDriversBaseColumn {
|
|
|
46
46
|
|
|
47
47
|
tableData.addColumn(column)
|
|
48
48
|
|
|
49
|
-
const sqls = await this.getDriver().
|
|
49
|
+
const sqls = await this.getDriver().alterTableSQLs(tableData)
|
|
50
50
|
|
|
51
51
|
for (const sql of sqls) {
|
|
52
52
|
await this.getDriver().query(sql)
|
|
@@ -70,7 +70,7 @@ export default class VelociousDatabaseDriversBaseColumn {
|
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
72
|
* @abstract
|
|
73
|
-
* @returns {number}
|
|
73
|
+
* @returns {number | undefined}
|
|
74
74
|
*/
|
|
75
75
|
getMaxLength() {
|
|
76
76
|
throw new Error("getMaxLength not implemented")
|
|
@@ -12,6 +12,12 @@ export default class VelociousDatabaseDriversBaseColumnsIndex {
|
|
|
12
12
|
this.table = table
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* @abstract
|
|
17
|
+
* @returns {string[]}
|
|
18
|
+
*/
|
|
19
|
+
getColumnNames() { throw new Error("'getColumnNames' not implemented") }
|
|
20
|
+
|
|
15
21
|
/**
|
|
16
22
|
* @returns {import("./base.js").default}
|
|
17
23
|
*/
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
* @property {string[]} [columns]
|
|
24
24
|
* @property {{[key: string]: any}} [data]
|
|
25
25
|
* @property {boolean} [multiple]
|
|
26
|
-
* @property {
|
|
26
|
+
* @property {string[]} [returnLastInsertedColumnNames]
|
|
27
27
|
* @property {Array<Array<any>>} [rows]
|
|
28
28
|
* @property {string} tableName
|
|
29
29
|
*/
|
|
@@ -31,6 +31,12 @@
|
|
|
31
31
|
* @typedef {Record<string, any>} QueryRowType
|
|
32
32
|
* @typedef {Array<QueryRowType>} QueryResultType
|
|
33
33
|
*/
|
|
34
|
+
/**
|
|
35
|
+
* @typedef {object}UpdateSqlArgsType
|
|
36
|
+
* @property {object} conditions
|
|
37
|
+
* @property {object} data
|
|
38
|
+
* @property {string} tableName
|
|
39
|
+
*/
|
|
34
40
|
|
|
35
41
|
import {Logger} from "../../logger.js"
|
|
36
42
|
import Query from "../query/index.js"
|
|
@@ -83,7 +89,7 @@ export default class VelociousDatabaseDriversBase {
|
|
|
83
89
|
|
|
84
90
|
tableData.addForeignKey(tableForeignKey)
|
|
85
91
|
|
|
86
|
-
const alterTableSQLs = await this.
|
|
92
|
+
const alterTableSQLs = await this.alterTableSQLs(tableData)
|
|
87
93
|
|
|
88
94
|
for (const alterTableSQL of alterTableSQLs) {
|
|
89
95
|
await this.query(alterTableSQL)
|
|
@@ -95,8 +101,8 @@ export default class VelociousDatabaseDriversBase {
|
|
|
95
101
|
* @param {import("../table-data/index.js").default} _tableData
|
|
96
102
|
* @returns {Promise<string[]>}
|
|
97
103
|
*/
|
|
98
|
-
|
|
99
|
-
throw new Error("
|
|
104
|
+
alterTableSQLs(_tableData) { // eslint-disable-line no-unused-vars
|
|
105
|
+
throw new Error("alterTableSQLs not implemented")
|
|
100
106
|
}
|
|
101
107
|
|
|
102
108
|
/**
|
|
@@ -107,13 +113,22 @@ export default class VelociousDatabaseDriversBase {
|
|
|
107
113
|
throw new Error("'connect' not implemented")
|
|
108
114
|
}
|
|
109
115
|
|
|
116
|
+
/**
|
|
117
|
+
* @abstract
|
|
118
|
+
* @param {string} databaseName
|
|
119
|
+
* @param {object} [args]
|
|
120
|
+
* @param {boolean} [args.ifNotExists]
|
|
121
|
+
* @returns {string[]}
|
|
122
|
+
*/
|
|
123
|
+
createDatabaseSql(databaseName, args) { throw new Error("'createDatabaseSql' not implemented") } // eslint-disable-line no-unused-vars
|
|
124
|
+
|
|
110
125
|
/**
|
|
111
126
|
* @abstract
|
|
112
127
|
* @param {CreateIndexSqlArgs} indexData
|
|
113
|
-
* @returns {string}
|
|
128
|
+
* @returns {string[]}
|
|
114
129
|
*/
|
|
115
|
-
|
|
116
|
-
throw new Error("'
|
|
130
|
+
createIndexSQLs(indexData) { // eslint-disable-line no-unused-vars
|
|
131
|
+
throw new Error("'createIndexSQLs' not implemented")
|
|
117
132
|
}
|
|
118
133
|
|
|
119
134
|
/**
|
|
@@ -162,7 +177,7 @@ export default class VelociousDatabaseDriversBase {
|
|
|
162
177
|
* @returns {Promise<void>}
|
|
163
178
|
*/
|
|
164
179
|
async dropTable(tableName, args) {
|
|
165
|
-
const sqls = this.
|
|
180
|
+
const sqls = this.dropTableSQLs(tableName, args)
|
|
166
181
|
|
|
167
182
|
for (const sql of sqls) {
|
|
168
183
|
await this.query(sql)
|
|
@@ -173,10 +188,10 @@ export default class VelociousDatabaseDriversBase {
|
|
|
173
188
|
* @abstract
|
|
174
189
|
* @param {string} tableName
|
|
175
190
|
* @param {DropTableSqlArgsType} [args]
|
|
176
|
-
* @returns {string}
|
|
191
|
+
* @returns {string[]}
|
|
177
192
|
*/
|
|
178
|
-
|
|
179
|
-
throw new Error("
|
|
193
|
+
dropTableSQLs(tableName, args) { // eslint-disable-line no-unused-vars
|
|
194
|
+
throw new Error("dropTableSQLs not implemented")
|
|
180
195
|
}
|
|
181
196
|
|
|
182
197
|
/**
|
|
@@ -189,7 +204,7 @@ export default class VelociousDatabaseDriversBase {
|
|
|
189
204
|
}
|
|
190
205
|
|
|
191
206
|
/**
|
|
192
|
-
* @returns {
|
|
207
|
+
* @returns {import("../../configuration-types.js").DatabaseConfigurationType}
|
|
193
208
|
*/
|
|
194
209
|
getArgs() {
|
|
195
210
|
return this._args
|
|
@@ -213,7 +228,7 @@ export default class VelociousDatabaseDriversBase {
|
|
|
213
228
|
|
|
214
229
|
/**
|
|
215
230
|
* @abstract
|
|
216
|
-
* @returns {Array<import("./base-table.js").default
|
|
231
|
+
* @returns {Promise<Array<import("./base-table.js").default>>}
|
|
217
232
|
*/
|
|
218
233
|
getTables() {
|
|
219
234
|
throw new Error(`${this.constructor.name}#getTables not implemented`)
|
|
@@ -227,9 +242,21 @@ export default class VelociousDatabaseDriversBase {
|
|
|
227
242
|
*/
|
|
228
243
|
async getTableByName(name, args) {
|
|
229
244
|
const tables = await this.getTables()
|
|
230
|
-
const
|
|
245
|
+
const tableNames = []
|
|
246
|
+
let table
|
|
247
|
+
|
|
248
|
+
for (const candidate of tables) {
|
|
249
|
+
const candidateName = candidate.getName()
|
|
250
|
+
|
|
251
|
+
if (candidateName == name) {
|
|
252
|
+
table = candidate
|
|
253
|
+
break
|
|
254
|
+
}
|
|
231
255
|
|
|
232
|
-
|
|
256
|
+
tableNames.push(candidateName)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (!table && args?.throwError !== false) throw new Error(`Couldn't find a table by that name "${name}" in: ${tableNames.join(", ")}`)
|
|
233
260
|
|
|
234
261
|
return table
|
|
235
262
|
}
|
|
@@ -252,6 +279,17 @@ export default class VelociousDatabaseDriversBase {
|
|
|
252
279
|
await this.query(sql)
|
|
253
280
|
}
|
|
254
281
|
|
|
282
|
+
/**
|
|
283
|
+
* @abstract
|
|
284
|
+
* @param {string} tableName
|
|
285
|
+
* @param {Array<string>} columns
|
|
286
|
+
* @param {Array<Array<string>>} rows
|
|
287
|
+
* @returns {Promise<void>}
|
|
288
|
+
*/
|
|
289
|
+
async insertMultiple(tableName, columns, rows) { // eslint-disable-line no-unused-vars
|
|
290
|
+
throw new Error("'insertMultiple' not implemented")
|
|
291
|
+
}
|
|
292
|
+
|
|
255
293
|
/**
|
|
256
294
|
* @abstract
|
|
257
295
|
* @param {InsertSqlArgsType} args
|
|
@@ -368,6 +406,12 @@ export default class VelociousDatabaseDriversBase {
|
|
|
368
406
|
throw new Error(`'shouldSetAutoIncrementWhenPrimaryKey' not implemented`)
|
|
369
407
|
}
|
|
370
408
|
|
|
409
|
+
/**
|
|
410
|
+
* @abstract
|
|
411
|
+
* @returns {boolean}
|
|
412
|
+
*/
|
|
413
|
+
supportsInsertIntoReturning() { return false }
|
|
414
|
+
|
|
371
415
|
/**
|
|
372
416
|
* @param {string} tableName
|
|
373
417
|
* @returns {Promise<boolean>}
|
|
@@ -578,7 +622,7 @@ export default class VelociousDatabaseDriversBase {
|
|
|
578
622
|
|
|
579
623
|
tableData.addColumn(tableColumn)
|
|
580
624
|
|
|
581
|
-
const alterTableSQLs = await this.
|
|
625
|
+
const alterTableSQLs = await this.alterTableSQLs(tableData)
|
|
582
626
|
|
|
583
627
|
for (const alterTableSQL of alterTableSQLs) {
|
|
584
628
|
await this.query(alterTableSQL)
|
|
@@ -656,12 +700,6 @@ export default class VelociousDatabaseDriversBase {
|
|
|
656
700
|
})
|
|
657
701
|
}
|
|
658
702
|
|
|
659
|
-
/**
|
|
660
|
-
* @typedef {object}UpdateSqlArgsType
|
|
661
|
-
* @property {object} conditions
|
|
662
|
-
* @property {object} data
|
|
663
|
-
* @property {string} tableName
|
|
664
|
-
*/
|
|
665
703
|
/**
|
|
666
704
|
* @param {UpdateSqlArgsType} args
|
|
667
705
|
* @returns {Promise<void>}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import AlterTable from "./sql/alter-table.js"
|
|
2
4
|
import Base from "../base.js"
|
|
3
5
|
import CreateDatabase from "./sql/create-database.js"
|
|
@@ -24,27 +26,36 @@ export default class VelociousDatabaseDriversMssql extends Base{
|
|
|
24
26
|
this.connection = new mssql.ConnectionPool(sqlConfig)
|
|
25
27
|
await this.connection.connect()
|
|
26
28
|
} catch (error) {
|
|
27
|
-
|
|
29
|
+
// Re-throw to fix unuseable stack trace.
|
|
30
|
+
if (error instanceof Error) {
|
|
31
|
+
throw new Error(`Couldn't connect to database: ${error.message}`)
|
|
32
|
+
} else {
|
|
33
|
+
throw new Error(`Couldn't connect to database: ${error}`)
|
|
34
|
+
}
|
|
28
35
|
}
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
async close() {
|
|
32
|
-
await this.connection
|
|
39
|
+
await this.connection?.close()
|
|
33
40
|
this.connection = undefined
|
|
34
41
|
}
|
|
35
42
|
|
|
36
43
|
/**
|
|
37
|
-
* @
|
|
44
|
+
* @param {import("../../table-data/index.js").default} tableData
|
|
45
|
+
* @returns {Promise<string[]>}
|
|
38
46
|
*/
|
|
39
|
-
async
|
|
47
|
+
async alterTableSQLs(tableData) {
|
|
40
48
|
const alterArgs = {tableData, driver: this}
|
|
41
49
|
const alterTable = new AlterTable(alterArgs)
|
|
42
50
|
|
|
43
|
-
return await alterTable.
|
|
51
|
+
return await alterTable.toSQLs()
|
|
44
52
|
}
|
|
45
53
|
|
|
46
54
|
/**
|
|
47
|
-
* @
|
|
55
|
+
* @param {string} databaseName
|
|
56
|
+
* @param {object} [args]
|
|
57
|
+
* @param {boolean} [args.ifNotExists]
|
|
58
|
+
* @returns {string[]}
|
|
48
59
|
*/
|
|
49
60
|
createDatabaseSql(databaseName, args) {
|
|
50
61
|
const createArgs = Object.assign({databaseName, driver: this}, args)
|
|
@@ -54,17 +65,19 @@ export default class VelociousDatabaseDriversMssql extends Base{
|
|
|
54
65
|
}
|
|
55
66
|
|
|
56
67
|
/**
|
|
57
|
-
* @
|
|
68
|
+
* @param {import("../base.js").CreateIndexSqlArgs} indexData
|
|
69
|
+
* @returns {string[]}
|
|
58
70
|
*/
|
|
59
|
-
|
|
71
|
+
createIndexSQLs(indexData) {
|
|
60
72
|
const createArgs = Object.assign({driver: this}, indexData)
|
|
61
73
|
const createIndex = new CreateIndex(createArgs)
|
|
62
74
|
|
|
63
|
-
return createIndex.
|
|
75
|
+
return createIndex.toSQLs()
|
|
64
76
|
}
|
|
65
77
|
|
|
66
78
|
/**
|
|
67
|
-
* @
|
|
79
|
+
* @param {import("../../table-data/index.js").default} tableData
|
|
80
|
+
* @returns {string[]}
|
|
68
81
|
*/
|
|
69
82
|
createTableSql(tableData) {
|
|
70
83
|
const createArgs = {tableData, driver: this, indexInCreateTable: false}
|
|
@@ -91,13 +104,15 @@ export default class VelociousDatabaseDriversMssql extends Base{
|
|
|
91
104
|
}
|
|
92
105
|
|
|
93
106
|
/**
|
|
94
|
-
* @
|
|
107
|
+
* @param {string} tableName
|
|
108
|
+
* @param {import("../base.js").DropTableSqlArgsType} [args]
|
|
109
|
+
* @returns {string[]}
|
|
95
110
|
*/
|
|
96
|
-
|
|
111
|
+
dropTableSQLs(tableName, args = {}) {
|
|
97
112
|
const dropArgs = Object.assign({tableName, driver: this}, args)
|
|
98
113
|
const dropTable = new DropTable(dropArgs)
|
|
99
114
|
|
|
100
|
-
return dropTable.
|
|
115
|
+
return dropTable.toSQLs()
|
|
101
116
|
}
|
|
102
117
|
|
|
103
118
|
/**
|
|
@@ -110,8 +125,13 @@ export default class VelociousDatabaseDriversMssql extends Base{
|
|
|
110
125
|
*/
|
|
111
126
|
primaryKeyType() { return "bigint" }
|
|
112
127
|
|
|
128
|
+
/**
|
|
129
|
+
* @param {string} sql
|
|
130
|
+
* @returns {Promise<import("../base.js").QueryResultType>}
|
|
131
|
+
*/
|
|
113
132
|
async _queryActual(sql) {
|
|
114
|
-
let result
|
|
133
|
+
let result
|
|
134
|
+
let request, tries = 0
|
|
115
135
|
|
|
116
136
|
if (this._currentTransaction) {
|
|
117
137
|
request = new mssql.Request(this._currentTransaction)
|
|
@@ -126,21 +146,25 @@ export default class VelociousDatabaseDriversMssql extends Base{
|
|
|
126
146
|
result = await request.query(sql)
|
|
127
147
|
break
|
|
128
148
|
} catch (error) {
|
|
129
|
-
if (error.message == "No connection is specified for that request." && tries <= 3) {
|
|
149
|
+
if (error instanceof Error && error.message == "No connection is specified for that request." && tries <= 3) {
|
|
130
150
|
this.logger.log("Reconnecting to database")
|
|
131
151
|
await this.connect()
|
|
132
152
|
// Retry
|
|
133
|
-
} else {
|
|
153
|
+
} else if (error instanceof Error) {
|
|
134
154
|
// Re-throw error because the stack-trace is broken and can't be used for app-development.
|
|
135
155
|
throw new Error(`Query failed '${error.message}': ${sql}`)
|
|
156
|
+
} else {
|
|
157
|
+
throw new Error(`Query failed '${error}': ${sql}`)
|
|
136
158
|
}
|
|
137
159
|
}
|
|
138
160
|
}
|
|
139
161
|
|
|
162
|
+
// @ts-expect-error
|
|
140
163
|
return result.recordsets[0]
|
|
141
164
|
}
|
|
142
165
|
|
|
143
166
|
/**
|
|
167
|
+
* @param {import("../../query/index.js").default} query
|
|
144
168
|
* @returns {string}
|
|
145
169
|
*/
|
|
146
170
|
queryToSql(query) { return new QueryParser({query}).toSql() }
|
|
@@ -158,7 +182,7 @@ export default class VelociousDatabaseDriversMssql extends Base{
|
|
|
158
182
|
|
|
159
183
|
if (type != "string") value = `${value}`
|
|
160
184
|
|
|
161
|
-
const resultWithQuotes = escapeString(value)
|
|
185
|
+
const resultWithQuotes = escapeString(value, null)
|
|
162
186
|
const result = resultWithQuotes.substring(1, resultWithQuotes.length - 1)
|
|
163
187
|
|
|
164
188
|
return result
|
|
@@ -176,7 +200,7 @@ export default class VelociousDatabaseDriversMssql extends Base{
|
|
|
176
200
|
if (type == "number") return value
|
|
177
201
|
if (type != "string") value = `${value}`
|
|
178
202
|
|
|
179
|
-
return escapeString(value)
|
|
203
|
+
return escapeString(value, null)
|
|
180
204
|
}
|
|
181
205
|
|
|
182
206
|
/**
|
|
@@ -211,7 +235,9 @@ export default class VelociousDatabaseDriversMssql extends Base{
|
|
|
211
235
|
}
|
|
212
236
|
|
|
213
237
|
/**
|
|
214
|
-
* @
|
|
238
|
+
* @abstract
|
|
239
|
+
* @param {import("../base.js").InsertSqlArgsType} args
|
|
240
|
+
* @returns {string}
|
|
215
241
|
*/
|
|
216
242
|
insertSql(args) {
|
|
217
243
|
const insertArgs = Object.assign({driver: this}, args)
|
|
@@ -220,6 +246,9 @@ export default class VelociousDatabaseDriversMssql extends Base{
|
|
|
220
246
|
return insert.toSql()
|
|
221
247
|
}
|
|
222
248
|
|
|
249
|
+
/**
|
|
250
|
+
* @returns {Promise<Array<import("../base-table.js").default>>}
|
|
251
|
+
*/
|
|
223
252
|
async getTables() {
|
|
224
253
|
const result = await this.query(`SELECT [TABLE_NAME] FROM [INFORMATION_SCHEMA].[TABLES] WHERE [TABLE_CATALOG] = DB_NAME() AND [TABLE_SCHEMA] = 'dbo'`)
|
|
225
254
|
const tables = []
|
|
@@ -233,14 +262,20 @@ export default class VelociousDatabaseDriversMssql extends Base{
|
|
|
233
262
|
return tables
|
|
234
263
|
}
|
|
235
264
|
|
|
236
|
-
|
|
237
|
-
|
|
265
|
+
/**
|
|
266
|
+
* @param {string} name
|
|
267
|
+
* @param {object} [args]
|
|
268
|
+
* @param {boolean} args.throwError
|
|
269
|
+
* @returns {Promise<import("../base-table.js").default | undefined>}
|
|
270
|
+
*/
|
|
271
|
+
async getTableByName(name, args) {
|
|
272
|
+
const result = await this.query(`SELECT [TABLE_NAME] FROM [INFORMATION_SCHEMA].[TABLES] WHERE [TABLE_CATALOG] = DB_NAME() AND [TABLE_SCHEMA] = 'dbo' AND [TABLE_NAME] = ${this.quote(name)}`)
|
|
238
273
|
|
|
239
274
|
if (result[0]) {
|
|
240
275
|
return new Table(this, result[0])
|
|
241
276
|
}
|
|
242
277
|
|
|
243
|
-
if (args?.throwError !== false) throw new Error(`Couldn't find a table by that name: ${
|
|
278
|
+
if (args?.throwError !== false) throw new Error(`Couldn't find a table by that name: ${name}`)
|
|
244
279
|
}
|
|
245
280
|
|
|
246
281
|
async lastInsertID() {
|
|
@@ -282,14 +317,26 @@ export default class VelociousDatabaseDriversMssql extends Base{
|
|
|
282
317
|
this._currentTransaction = null
|
|
283
318
|
}
|
|
284
319
|
|
|
320
|
+
/**
|
|
321
|
+
* @param {string} savePointName
|
|
322
|
+
* @returns {Promise<void>}
|
|
323
|
+
*/
|
|
285
324
|
async _startSavePointAction(savePointName) {
|
|
286
325
|
await this.query(`SAVE TRANSACTION [${savePointName}]`)
|
|
287
326
|
}
|
|
288
327
|
|
|
328
|
+
/**
|
|
329
|
+
* @param {string} savePointName
|
|
330
|
+
* @returns {Promise<void>}
|
|
331
|
+
*/
|
|
289
332
|
async _releaseSavePointAction(savePointName) { // eslint-disable-line no-unused-vars
|
|
290
333
|
// Do nothing in MS-SQL.
|
|
291
334
|
}
|
|
292
335
|
|
|
336
|
+
/**
|
|
337
|
+
* @param {string} savePointName
|
|
338
|
+
* @returns {Promise<void>}
|
|
339
|
+
*/
|
|
293
340
|
async _rollbackSavePointAction(savePointName) {
|
|
294
341
|
await this.query(`ROLLBACK TRANSACTION [${savePointName}]`)
|
|
295
342
|
}
|
|
@@ -299,6 +346,7 @@ export default class VelociousDatabaseDriversMssql extends Base{
|
|
|
299
346
|
}
|
|
300
347
|
|
|
301
348
|
/**
|
|
349
|
+
* @param {import("../base.js").UpdateSqlArgsType} args
|
|
302
350
|
* @returns {string}
|
|
303
351
|
*/
|
|
304
352
|
updateSql({conditions, data, tableName}) {
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import CreateDatabaseBase from "../../../query/create-database-base.js"
|
|
2
4
|
|
|
3
5
|
export default class VelociousDatabaseConnectionDriversMssqlSqlCreateDatabase extends CreateDatabaseBase {
|
|
6
|
+
/**
|
|
7
|
+
* @param {object} args
|
|
8
|
+
* @param {import("../../base.js").default} args.driver
|
|
9
|
+
* @param {string} args.databaseName
|
|
10
|
+
* @param {boolean} [args.ifNotExists]
|
|
11
|
+
*/
|
|
4
12
|
constructor({driver, databaseName, ifNotExists}) {
|
|
5
|
-
super({driver})
|
|
6
|
-
this.databaseName = databaseName
|
|
13
|
+
super({databaseName, driver})
|
|
7
14
|
this.ifNotExists = ifNotExists
|
|
8
15
|
}
|
|
9
16
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import BaseTable from "../base-table.js"
|
|
2
4
|
import Column from "./column.js"
|
|
3
5
|
import ColumnsIndex from "./columns-index.js"
|
|
@@ -5,6 +7,10 @@ import {digg} from "diggerize"
|
|
|
5
7
|
import ForeignKey from "./foreign-key.js"
|
|
6
8
|
|
|
7
9
|
export default class VelociousDatabaseDriversMssqlTable extends BaseTable {
|
|
10
|
+
/**
|
|
11
|
+
* @param {import("../base.js").default} driver
|
|
12
|
+
* @param {Record<string, any>} data
|
|
13
|
+
*/
|
|
8
14
|
constructor(driver, data) {
|
|
9
15
|
super()
|
|
10
16
|
this.data = data
|
|
@@ -107,13 +113,17 @@ export default class VelociousDatabaseDriversMssqlTable extends BaseTable {
|
|
|
107
113
|
return digg(this.data, "TABLE_NAME")
|
|
108
114
|
}
|
|
109
115
|
|
|
110
|
-
|
|
116
|
+
/**
|
|
117
|
+
* @param {{cascade: boolean}} [args]
|
|
118
|
+
* @returns {Promise<Array<Record<string, any>>>}
|
|
119
|
+
*/
|
|
120
|
+
async truncate(args) { // eslint-disable-line no-unused-vars
|
|
111
121
|
try {
|
|
112
|
-
await this.getDriver().query(`TRUNCATE TABLE ${this.getOptions().quoteTableName(this.getName())}`)
|
|
122
|
+
return await this.getDriver().query(`TRUNCATE TABLE ${this.getOptions().quoteTableName(this.getName())}`)
|
|
113
123
|
} catch (error) {
|
|
114
|
-
if (error.message.startsWith("Query failed 'Cannot truncate table")) {
|
|
124
|
+
if (error instanceof Error && error.message.startsWith("Query failed 'Cannot truncate table")) {
|
|
115
125
|
// Truncate table is really buggy for some reason - fall back to delete all rows instead
|
|
116
|
-
await this.getDriver().query(`DELETE FROM ${this.getOptions().quoteTableName(this.getName())}`)
|
|
126
|
+
return await this.getDriver().query(`DELETE FROM ${this.getOptions().quoteTableName(this.getName())}`)
|
|
117
127
|
} else {
|
|
118
128
|
throw error
|
|
119
129
|
}
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import BaseColumn from "../base-column.js"
|
|
2
4
|
import ColumnsIndex from "./columns-index.js"
|
|
3
5
|
import {digg} from "diggerize"
|
|
4
6
|
|
|
5
7
|
export default class VelociousDatabaseDriversMysqlColumn extends BaseColumn {
|
|
8
|
+
/**
|
|
9
|
+
* @param {import("../base-table.js").default} table
|
|
10
|
+
* @param {Record<string, any>} data
|
|
11
|
+
*/
|
|
6
12
|
constructor(table, data) {
|
|
7
13
|
super()
|
|
8
14
|
this.data = data
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import BaseColumnsIndex from "../base-columns-index.js"
|
|
2
4
|
|
|
3
|
-
export default class
|
|
4
|
-
constructor(table, data) {
|
|
5
|
-
super()
|
|
6
|
-
this.data = data
|
|
7
|
-
this.table = table
|
|
8
|
-
}
|
|
5
|
+
export default class VelociousDatabaseDriversMysqlColumnsIndex extends BaseColumnsIndex {
|
|
9
6
|
}
|