@platformatic/sql-mapper 0.29.0 → 0.30.0
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/lib/queries/sqlite.js +27 -4
- package/mapper.js +14 -8
- package/package.json +3 -3
- package/test/where.test.js +8 -6
package/lib/queries/sqlite.js
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { randomUUID } = require('crypto')
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
function fixValue (value) {
|
|
6
|
+
if (value instanceof Date) {
|
|
7
|
+
return value.toISOString()
|
|
8
|
+
} else if (typeof value === 'boolean') {
|
|
9
|
+
return value ? 1 : 0
|
|
10
|
+
}
|
|
11
|
+
return value
|
|
12
|
+
}
|
|
5
13
|
|
|
6
14
|
async function listTables (db, sql) {
|
|
7
15
|
const res = await db.query(sql`
|
|
@@ -113,7 +121,7 @@ async function insertOne (db, sql, table, schema, input, primaryKeys, fieldsToRe
|
|
|
113
121
|
|
|
114
122
|
for (const [key, value] of Object.entries(input)) {
|
|
115
123
|
insertedKeys.push(sql.ident(key))
|
|
116
|
-
insertedValues.push(sql.value(value))
|
|
124
|
+
insertedValues.push(sql.value(fixValue(value)))
|
|
117
125
|
}
|
|
118
126
|
|
|
119
127
|
const insertRawQuery = sql`
|
|
@@ -166,7 +174,7 @@ module.exports.insertOne = insertOne
|
|
|
166
174
|
async function updateOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve) {
|
|
167
175
|
const pairs = Object.keys(input).map((key) => {
|
|
168
176
|
const value = input[key]
|
|
169
|
-
return sql`${sql.ident(key)} = ${value}`
|
|
177
|
+
return sql`${sql.ident(key)} = ${fixValue(value)}`
|
|
170
178
|
})
|
|
171
179
|
|
|
172
180
|
const where = []
|
|
@@ -221,4 +229,19 @@ async function deleteAll (db, sql, table, schema, criteria, fieldsToRetrieve) {
|
|
|
221
229
|
|
|
222
230
|
module.exports.deleteAll = deleteAll
|
|
223
231
|
|
|
224
|
-
|
|
232
|
+
async function updateMany (db, sql, table, schema, criteria, input, fieldsToRetrieve) {
|
|
233
|
+
const pairs = Object.keys(input).map((key) => {
|
|
234
|
+
const value = input[key]
|
|
235
|
+
return sql`${sql.ident(key)} = ${fixValue(value)}`
|
|
236
|
+
})
|
|
237
|
+
const update = sql`
|
|
238
|
+
UPDATE ${sql.ident(table)}
|
|
239
|
+
SET ${sql.join(pairs, sql`, `)}
|
|
240
|
+
WHERE ${sql.join(criteria, sql` AND `)}
|
|
241
|
+
RETURNING ${sql.join(fieldsToRetrieve, sql`, `)}
|
|
242
|
+
`
|
|
243
|
+
const res = await db.query(update)
|
|
244
|
+
return res
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
module.exports.updateMany = updateMany
|
package/mapper.js
CHANGED
|
@@ -80,15 +80,21 @@ async function connect ({ connectionString, log, onDatabaseLoad, poolSize = 10,
|
|
|
80
80
|
queries = queriesFactory.mysql
|
|
81
81
|
}
|
|
82
82
|
} else if (connectionString.indexOf('sqlite') === 0) {
|
|
83
|
-
const sqlite = require('@
|
|
83
|
+
const sqlite = require('@matteo.collina/sqlite-pool')
|
|
84
84
|
const path = connectionString.replace('sqlite://', '')
|
|
85
|
-
db = sqlite(connectionString === 'sqlite://:memory:' ? undefined : path
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
85
|
+
db = sqlite.default(connectionString === 'sqlite://:memory:' ? undefined : path, {}, {
|
|
86
|
+
// TODO make this configurable
|
|
87
|
+
maxSize: 1,
|
|
88
|
+
// TODO make this configurable
|
|
89
|
+
// 10s max time to wait for a connection
|
|
90
|
+
releaseTimeoutMilliseconds: 10000,
|
|
91
|
+
onQuery ({ text, values }) {
|
|
92
|
+
log.trace({
|
|
93
|
+
query: {
|
|
94
|
+
text
|
|
95
|
+
}
|
|
96
|
+
}, 'query')
|
|
97
|
+
}
|
|
92
98
|
})
|
|
93
99
|
sql = sqlite.sql
|
|
94
100
|
queries = queriesFactory.sqlite
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/sql-mapper",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.30.0",
|
|
4
4
|
"description": "A data mapper utility for SQL databases",
|
|
5
5
|
"main": "mapper.js",
|
|
6
6
|
"types": "mapper.d.ts",
|
|
@@ -25,11 +25,11 @@
|
|
|
25
25
|
"@databases/mysql": "^5.2.1",
|
|
26
26
|
"@databases/pg": "^5.4.1",
|
|
27
27
|
"@databases/sql": "^3.3.0",
|
|
28
|
-
"@
|
|
28
|
+
"@matteo.collina/sqlite-pool": "^0.3.0",
|
|
29
29
|
"camelcase": "^6.3.0",
|
|
30
30
|
"fastify-plugin": "^4.5.0",
|
|
31
31
|
"inflected": "^2.1.0",
|
|
32
|
-
"@platformatic/types": "0.
|
|
32
|
+
"@platformatic/types": "0.30.0"
|
|
33
33
|
},
|
|
34
34
|
"tsd": {
|
|
35
35
|
"directory": "test/types"
|
package/test/where.test.js
CHANGED
|
@@ -709,12 +709,14 @@ test('LIKE', async ({ pass, teardown, same, equal }) => {
|
|
|
709
709
|
|
|
710
710
|
same(await entity.find({ where: { longText: { like: null } } }), [], 'where: { longText: { like: null } }')
|
|
711
711
|
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
712
|
+
if (!isSQLite) {
|
|
713
|
+
same(await entity.find({ where: { counter: { like: 4 } } }), [{
|
|
714
|
+
id: '4',
|
|
715
|
+
title: 'atmosphere',
|
|
716
|
+
longText: 'The atmosphere is not a sphere',
|
|
717
|
+
counter: 4
|
|
718
|
+
}], 'where: { counter: { like: 4 } }')
|
|
719
|
+
}
|
|
718
720
|
|
|
719
721
|
same(await entity.find({ where: { counter: { like: '%4' } } }), [{
|
|
720
722
|
id: '4',
|