@platformatic/sql-mapper
Advanced tools
Comparing version
'use strict' | ||
const { randomUUID } = require('crypto') | ||
const shared = require('./shared') | ||
function fixValue (value) { | ||
if (value instanceof Date) { | ||
return value.toISOString() | ||
} else if (typeof value === 'boolean') { | ||
return value ? 1 : 0 | ||
} | ||
return value | ||
} | ||
async function listTables (db, sql) { | ||
@@ -116,3 +124,3 @@ const res = await db.query(sql` | ||
insertedKeys.push(sql.ident(key)) | ||
insertedValues.push(sql.value(value)) | ||
insertedValues.push(sql.value(fixValue(value))) | ||
} | ||
@@ -170,3 +178,3 @@ | ||
const value = input[key] | ||
return sql`${sql.ident(key)} = ${value}` | ||
return sql`${sql.ident(key)} = ${fixValue(value)}` | ||
}) | ||
@@ -226,2 +234,17 @@ | ||
module.exports.updateMany = shared.updateMany | ||
async function updateMany (db, sql, table, schema, criteria, input, fieldsToRetrieve) { | ||
const pairs = Object.keys(input).map((key) => { | ||
const value = input[key] | ||
return sql`${sql.ident(key)} = ${fixValue(value)}` | ||
}) | ||
const update = sql` | ||
UPDATE ${sql.ident(table)} | ||
SET ${sql.join(pairs, sql`, `)} | ||
WHERE ${sql.join(criteria, sql` AND `)} | ||
RETURNING ${sql.join(fieldsToRetrieve, sql`, `)} | ||
` | ||
const res = await db.query(update) | ||
return res | ||
} | ||
module.exports.updateMany = updateMany |
@@ -83,11 +83,17 @@ 'use strict' | ||
} else if (connectionString.indexOf('sqlite') === 0) { | ||
const sqlite = require('@databases/sqlite') | ||
const sqlite = require('@matteo.collina/sqlite-pool') | ||
const path = connectionString.replace('sqlite://', '') | ||
db = sqlite(connectionString === 'sqlite://:memory:' ? undefined : path) | ||
db._database.on('trace', sql => { | ||
log.trace({ | ||
query: { | ||
text: sql | ||
} | ||
}, 'query') | ||
db = sqlite.default(connectionString === 'sqlite://:memory:' ? undefined : path, {}, { | ||
// TODO make this configurable | ||
maxSize: 1, | ||
// TODO make this configurable | ||
// 10s max time to wait for a connection | ||
releaseTimeoutMilliseconds: 10000, | ||
onQuery ({ text, values }) { | ||
log.trace({ | ||
query: { | ||
text | ||
} | ||
}, 'query') | ||
} | ||
}) | ||
@@ -94,0 +100,0 @@ sql = sqlite.sql |
{ | ||
"name": "@platformatic/sql-mapper", | ||
"version": "0.29.0", | ||
"version": "0.30.0", | ||
"description": "A data mapper utility for SQL databases", | ||
@@ -28,7 +28,7 @@ "main": "mapper.js", | ||
"@databases/sql": "^3.3.0", | ||
"@databases/sqlite": "^4.0.2", | ||
"@matteo.collina/sqlite-pool": "^0.3.0", | ||
"camelcase": "^6.3.0", | ||
"fastify-plugin": "^4.5.0", | ||
"inflected": "^2.1.0", | ||
"@platformatic/types": "0.29.0" | ||
"@platformatic/types": "0.30.0" | ||
}, | ||
@@ -35,0 +35,0 @@ "tsd": { |
@@ -712,8 +712,10 @@ 'use strict' | ||
same(await entity.find({ where: { counter: { like: 4 } } }), [{ | ||
id: '4', | ||
title: 'atmosphere', | ||
longText: 'The atmosphere is not a sphere', | ||
counter: 4 | ||
}], 'where: { counter: { like: 4 } }') | ||
if (!isSQLite) { | ||
same(await entity.find({ where: { counter: { like: 4 } } }), [{ | ||
id: '4', | ||
title: 'atmosphere', | ||
longText: 'The atmosphere is not a sphere', | ||
counter: 4 | ||
}], 'where: { counter: { like: 4 } }') | ||
} | ||
@@ -720,0 +722,0 @@ same(await entity.find({ where: { counter: { like: '%4' } } }), [{ |
165297
0.54%5230
0.56%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
Updated