@platformatic/sql-mapper
Advanced tools
Comparing version 0.29.0 to 0.30.0
'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
5230
+ Added@databases/sqlite-sync@1.1.0(transitive)
+ Added@matteo.collina/sqlite-pool@0.3.0(transitive)
+ Added@platformatic/types@0.30.0(transitive)
+ Added@types/better-sqlite3@7.6.12(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbetter-sqlite3@8.7.0(transitive)
+ Addedbindings@1.5.0(transitive)
+ Addedbl@4.1.0(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addedchownr@1.1.4(transitive)
+ Addeddecompress-response@6.0.0(transitive)
+ Addeddeep-extend@0.6.0(transitive)
+ Addeddetect-libc@2.0.3(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedexpand-template@2.0.3(transitive)
+ Addedfile-uri-to-path@1.0.0(transitive)
+ Addedfs-constants@1.0.0(transitive)
+ Addedgithub-from-package@0.0.0(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedini@1.3.8(transitive)
+ Addedmimic-response@3.1.0(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp-classic@0.5.3(transitive)
+ Addednapi-build-utils@2.0.0(transitive)
+ Addednode-abi@3.74.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedprebuild-install@7.1.3(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedrc@1.2.8(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsemver@7.7.1(transitive)
+ Addedsimple-concat@1.0.1(transitive)
+ Addedsimple-get@4.0.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedstrip-json-comments@2.0.1(transitive)
+ Addedtar-fs@2.1.2(transitive)
+ Addedtar-stream@2.2.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removed@databases/sqlite@^4.0.2
- Removed@platformatic/types@0.29.0(transitive)
Updated@platformatic/types@0.30.0