Comparing version 0.1.3 to 0.1.4
@@ -5,2 +5,10 @@ # Changelog | ||
## [0.1.4](https://gitlab.com/monstrous/monstrous/compare/v0.1.3...v0.1.4) (2023-03-26) | ||
### Bug Fixes | ||
* correct error message typo ([edcd31b](https://gitlab.com/monstrous/monstrous/commit/edcd31b5c2dfccbfdd320e807905e859b116fc45)) | ||
* only set values for known columns in update ([62cb47f](https://gitlab.com/monstrous/monstrous/commit/62cb47fea8fead7d8711eaa68c4f21ce647698ce)) | ||
## [0.1.3](https://gitlab.com/monstrous/monstrous/compare/v0.1.2...v0.1.3) (2023-03-06) | ||
@@ -7,0 +15,0 @@ |
@@ -280,3 +280,3 @@ import pgp from 'pg-promise'; | ||
if (values.length === 1 && Array.isArray(values[0])) { | ||
throw new Error('values to insert must objects; use the ... spread operator to pass an array'); | ||
throw new Error('values to insert must be objects; use the ... spread operator to pass an array'); | ||
} | ||
@@ -342,17 +342,30 @@ | ||
Object.entries(changes) | ||
.map(([attr, change]) => { | ||
if (this.#join.some(j => j.relation.qualified.indexOf(change) > -1)) { | ||
return `${attr} = ${change}`; | ||
} | ||
.reduce((acc, [attr, change]) => { | ||
const field = unquote(attr.split('.').pop()); // update columns cannot be qualified | ||
if (change instanceof Tuple) { | ||
if (!this.#join[0].relation.has(field)) { | ||
// 0. not a column, skip | ||
return acc; | ||
} else if (this.#join.some(j => j.relation.qualified.indexOf(change) > -1)) { | ||
// 1. it's another column in this or another joined relation | ||
acc.push(`"${field}" = ${change}`); | ||
return acc; | ||
} else if (change instanceof Tuple) { | ||
// 2. it's a Tuple, we'll parametrize the compiled record literal | ||
params.push(change.compile(true)); | ||
} else if (change instanceof Expr) { | ||
return `${attr} = ${change.compile()}`; | ||
// 3. it's a regular Expr, interpolate the compiled SQL | ||
acc.push(`"${field}" = ${change.compile()}`); | ||
return acc; | ||
} else { | ||
// 4. it's a literal, parametrize | ||
params.push(change); | ||
} | ||
return `${attr} = $${param_idx++}`; | ||
}) | ||
acc.push(`"${field}" = $${param_idx++}`); | ||
return acc; | ||
}, []) | ||
.join(', ') | ||
@@ -359,0 +372,0 @@ ); |
@@ -72,6 +72,2 @@ import pgp from 'pg-promise'; | ||
for (const qualified of (Array.isArray(val) ? val : [val])) { | ||
if (qualified === undefined) { | ||
throw new Error('undefined $key in projection definition; check your column names'); | ||
} | ||
const name = unquote(qualified.split('.').pop()); | ||
@@ -78,0 +74,0 @@ created.#key.push(name); |
{ | ||
"name": "monstrous", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "a lightweight SQL composer for Node.js and PostgreSQL", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -133,3 +133,3 @@ # monstrous | ||
Exprs can be used in many places: as criteria values in `join` and `filter`, as output fields in `project`, or as change values in `insert` and `update`. | ||
Exprs can be used in many places: as criteria values in `join` and `filter` (or in place of criteria!), as output fields in `project`, or as change values in `insert` and `update`. | ||
@@ -136,0 +136,0 @@ Composite types can be represented with exprs in persistence methods with SQL formatted as a record `($1, $2)` and appropriate parameters. |
86508
1790