Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

crudely-typed

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

crudely-typed - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2-beta

69

dist/src/index.js

@@ -71,2 +71,31 @@ "use strict";

}
// In the case that the user is trying to match against a NULL value, we
// need to replace "col = $1" with "col IS NULL". We keep the "col = $1"
// clause, even though it can never match, to avoid having to renumber.
function updateQueryWithIsNull(query, whereValues, whereNames) {
let thisQuery = query;
const whereIdx = query.indexOf('WHERE'); // only tweak WHERE clause, not SET clause for UPDATE.
whereValues.forEach((value, i) => {
if (value === null || (Array.isArray(value) && value.includes(null))) {
const name = whereNames[i];
let pat = `${name} = $`;
let idx = thisQuery.indexOf(pat, whereIdx);
if (idx === -1) {
pat = `${name} = ANY($`;
idx = thisQuery.indexOf(pat, whereIdx);
}
if (idx >= 0) {
const pre = thisQuery.slice(0, idx);
const post = thisQuery.slice(idx + pat.length);
const m = /^(\d+)(.*)/.exec(post);
if (!m) {
throw new Error('Unable to match null in ' + query);
}
const [, dig, rest] = m;
thisQuery = `${pre}(${name} IS NULL OR ${pat}${dig})${rest}`;
}
}
});
return thisQuery;
}
class Select {

@@ -104,2 +133,3 @@ constructor(tableSchema, table, cols, whereCols, whereAnyCols, order, joins, isSingular) {

}
const whereNames = [];
const whereKeys = [];

@@ -114,3 +144,5 @@ const whereClauses = [];

// while node-postgres does not require it.
whereClauses.push(`${tab}${col} = $${n}`);
const name = `${tab}${col}`;
whereClauses.push(`${name} = $${n}`);
whereNames.push(name);
}

@@ -123,3 +155,5 @@ }

const n = whereKeys.length;
const name = `${tab}${col}`;
whereClauses.push(`${tab}${col} = ANY($${n})`);
whereNames.push(name);
}

@@ -138,3 +172,4 @@ }

: whereObj[col]);
const result = await db.query(query, where);
const thisQuery = updateQueryWithIsNull(query, where, whereNames);
const result = await db.query(thisQuery, where);
if (this.isSingular) {

@@ -252,2 +287,3 @@ if (result.rowCount === 0) {

const whereClauses = [];
const whereNames = [];
if (this.whereCols) {

@@ -258,2 +294,3 @@ for (const col of this.whereCols) {

whereClauses.push(`${col} = $${n}`);
whereNames.push(col);
}

@@ -267,2 +304,3 @@ }

whereClauses.push(`${col} = ANY($${n})`);
whereNames.push(col);
}

@@ -279,8 +317,8 @@ }

return async (db, whereObj, updateObj) => {
const vals = setCols
.map(col => updateObj[col])
.concat(whereKeys.map(col => whereObj[col] instanceof Set
const whereVals = whereKeys.map(col => whereObj[col] instanceof Set
? Array.from(whereObj[col])
: whereObj[col]));
const result = await db.query(query, vals);
: whereObj[col]);
const vals = setCols.map(col => updateObj[col]).concat(whereVals);
const thisQuery = updateQueryWithIsNull(query, whereVals, whereNames);
const result = await db.query(thisQuery, vals);
if (this.isSingular) {

@@ -298,7 +336,6 @@ return result.rowCount === 0 ? null : result.rows[0];

const dynamicSetCols = Object.keys(updateObj);
const vals = whereKeys
.map(col => whereObj[col] instanceof Set
const whereVals = whereKeys.map(col => whereObj[col] instanceof Set
? Array.from(whereObj[col])
: whereObj[col])
.concat(dynamicSetCols.map(col => updateObj[col]));
: whereObj[col]);
const vals = whereVals.concat(dynamicSetCols.map(col => updateObj[col]));
let dynamicPlaceholder = placeholder;

@@ -313,3 +350,5 @@ const dynamicSetKeys = [];

const setSql = dynamicSetClauses.join(', ');
const query = `UPDATE ${this.table} SET ${setSql}${whereClause}${limitClause} RETURNING *`;
let query = `UPDATE ${this.table} SET ${setSql}${whereClause}${limitClause} RETURNING *`;
query = updateQueryWithIsNull(query, whereVals, whereNames);
console.log(query);
const result = await db.query(query, vals);

@@ -334,2 +373,3 @@ if (this.isSingular) {

const whereClauses = [];
const whereNames = [];
if (this.whereCols) {

@@ -340,2 +380,3 @@ for (const col of this.whereCols) {

whereClauses.push(`${col} = $${n}`);
whereNames.push(col);
}

@@ -349,2 +390,3 @@ }

whereClauses.push(`${col} = ANY($${n})`);
whereNames.push(col);
}

@@ -361,3 +403,4 @@ }

: whereObj[col]);
const result = await db.query(query, vals);
const thisQuery = updateQueryWithIsNull(query, vals, whereNames);
const result = await db.query(thisQuery, vals);
if (this.isSingular) {

@@ -364,0 +407,0 @@ return result.rowCount === 0 ? null : result.rows[0];

2

package.json
{
"name": "crudely-typed",
"version": "0.1.1",
"version": "0.1.2-beta",
"description": "Simple \"everyday CRUD\" Postgres queries with perfect TypeScript types",

@@ -5,0 +5,0 @@ "main": "dist/src/index.js",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc