linkam-db 1.1.3 → 1.1.4
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/index.js +55 -17
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -22,12 +22,33 @@ async function init(db, credentialsFile, sqlDirectory) {
|
|
|
22
22
|
const sqlDir = sqlDirectory || process.cwd()
|
|
23
23
|
|
|
24
24
|
let creds = authorize.getCredentials(db, credentialsFile)
|
|
25
|
+
|
|
26
|
+
// Validate required credentials
|
|
27
|
+
const client = creds[`${db}.client`]
|
|
28
|
+
if (!client) {
|
|
29
|
+
throw new Error(`Missing required credential: client for database '${db}'`)
|
|
30
|
+
}
|
|
31
|
+
|
|
25
32
|
let credentials = {
|
|
26
|
-
client:
|
|
33
|
+
client: client,
|
|
27
34
|
user: creds[`${db}.username`],
|
|
28
35
|
password: creds[`${db}.password`],
|
|
29
|
-
connectString: creds[`${db}.connectString`]
|
|
36
|
+
connectString: creds[`${db}.connectString`],
|
|
37
|
+
host: creds[`${db}.host`],
|
|
38
|
+
database: creds[`${db}.database`]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Validate client-specific required fields
|
|
42
|
+
if (credentials.client === 'oracledb') {
|
|
43
|
+
if (!credentials.user || !credentials.password || !credentials.connectString) {
|
|
44
|
+
throw new Error(`Missing required Oracle credentials for database '${db}': user, password, and connectString are required`)
|
|
45
|
+
}
|
|
46
|
+
} else if (credentials.client === 'pg') {
|
|
47
|
+
if (!credentials.user || !credentials.password || !credentials.host || !credentials.database) {
|
|
48
|
+
throw new Error(`Missing required PostgreSQL credentials for database '${db}': user, password, host, and database are required`)
|
|
49
|
+
}
|
|
30
50
|
}
|
|
51
|
+
|
|
31
52
|
let connection = setConnection(credentials)
|
|
32
53
|
const knex = require(`knex`)({
|
|
33
54
|
//debug: true,
|
|
@@ -46,26 +67,43 @@ async function init(db, credentialsFile, sqlDirectory) {
|
|
|
46
67
|
* @param {string} query.sql - SQL filename (without .sql extension) or relative path from sqlDirectory
|
|
47
68
|
* @param {Object} [query.params] - Query parameters
|
|
48
69
|
* @returns {Promise<Array>} Query results
|
|
70
|
+
* @throws {Error} If SQL file not found or query execution fails
|
|
49
71
|
*/
|
|
50
72
|
knex.run = async function (query) {
|
|
73
|
+
if (!query || !query.sql) {
|
|
74
|
+
throw new Error('Query object with sql property is required')
|
|
75
|
+
}
|
|
76
|
+
|
|
51
77
|
const sqlPath = query.sql.endsWith('.sql') ? query.sql : `${query.sql}.sql`
|
|
52
78
|
const fullPath = path.isAbsolute(sqlPath) ? sqlPath : path.join(sqlDir, sqlPath)
|
|
53
|
-
|
|
79
|
+
|
|
80
|
+
// Check if file exists before trying to read
|
|
81
|
+
if (!fs.existsSync(fullPath)) {
|
|
82
|
+
throw new Error(`SQL file not found: ${fullPath}`)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let sql
|
|
86
|
+
try {
|
|
87
|
+
sql = fs.readFileSync(fullPath, `utf8`)
|
|
88
|
+
} catch (err) {
|
|
89
|
+
log.error(`Failed to read SQL file: ${fullPath}`)
|
|
90
|
+
throw err
|
|
91
|
+
}
|
|
92
|
+
|
|
54
93
|
log.debug(formatSqlForLogs(sql, query.params))
|
|
55
94
|
|
|
56
|
-
|
|
57
|
-
.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return results
|
|
95
|
+
try {
|
|
96
|
+
const response = await knex.raw(sql, query.params)
|
|
97
|
+
if (this.client.config.client === `oracledb`) {
|
|
98
|
+
return response
|
|
99
|
+
} else if (this.client.config.client === `pg`) {
|
|
100
|
+
return response.rows
|
|
101
|
+
}
|
|
102
|
+
return response
|
|
103
|
+
} catch (err) {
|
|
104
|
+
log.error(`Query execution failed for file: ${query.sql}`, err)
|
|
105
|
+
throw err
|
|
106
|
+
}
|
|
69
107
|
}
|
|
70
108
|
|
|
71
109
|
/**
|
|
@@ -117,7 +155,7 @@ function setConnection(credentials) {
|
|
|
117
155
|
}
|
|
118
156
|
|
|
119
157
|
function formatSqlForLogs(sql, params) {
|
|
120
|
-
if (Object.keys(params).length > 0) {
|
|
158
|
+
if (params && Object.keys(params).length > 0) {
|
|
121
159
|
for (let [key, value] of Object.entries(params)) {
|
|
122
160
|
sql = sql.replace(`:${key}`, value)
|
|
123
161
|
}
|