@vanit-co/sql-ts 0.8.0 → 0.9.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.
Files changed (2) hide show
  1. package/dist/index.js +23 -5
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -114,8 +114,26 @@ const alias = transformer(tableWithAlias, (s, c) => [
114
114
  ...prefixedColumn(s, c),
115
115
  [' as ', identifier(`${c.prefix}_${c.name}`)]
116
116
  ]);
117
+ const dropUndefined = (strings, binds) => {
118
+ const outStrings = [];
119
+ const outBinds = [];
120
+ let carry = '';
121
+ strings.forEach((s, i) => {
122
+ if (i === strings.length - 1)
123
+ outStrings.push(carry + s);
124
+ else if (binds[i] === undefined)
125
+ carry = carry + s;
126
+ else {
127
+ outStrings.push(carry + s);
128
+ outBinds.push(binds[i]);
129
+ carry = '';
130
+ }
131
+ });
132
+ return [outStrings, outBinds];
133
+ };
117
134
  const buildTag = (fn) => (strings, ...binds) => {
118
- const [s, v] = ramda.transpose(zipLongest(strings, binds.map(b => b === undefined ? null : b)).flatMap(fn));
135
+ const [ss, bs] = dropUndefined(strings, binds);
136
+ const [s, v] = ramda.transpose(zipLongest(ss, bs).flatMap(fn));
119
137
  return result(fragment(s, v ?? []));
120
138
  };
121
139
  const sql = buildTag(pure);
@@ -124,19 +142,19 @@ const selectAs = buildTag(alias);
124
142
  const empty = sql ``;
125
143
  const insert = (table, ...colsVals) => {
126
144
  const tableName = table[SYM_TABLE].name;
127
- const columns = Object.keys(colsVals[0]);
145
+ const columns = Object.keys(colsVals[0]).filter(c => colsVals[0][c] !== undefined);
128
146
  const colStrings = columns.map((_, i) => i === 0 ? ' (' : ' ,');
129
147
  const colBinds = columns.map(c => identifier(c));
130
148
  const [valStrings, valBinds] = colsVals.reduce(([ss, bs], row, rowIdx) => {
131
149
  const rowStrings = columns.map((_, colIdx) => colIdx === 0 ? (rowIdx === 0 ? ') values (' : ') ,(') : ' ,');
132
- return [[...ss, ...rowStrings], [...bs, ...columns.map(c => row[c] ?? null)]];
150
+ return [[...ss, ...rowStrings], [...bs, ...columns.map(c => row[c])]];
133
151
  }, [[], []]);
134
152
  return result(fragment(['insert into ', ...colStrings, ...valStrings, ')'], [identifier(tableName), ...colBinds, ...valBinds]));
135
153
  };
136
154
  const update = (table, colsVals) => {
137
155
  const tableName = table[SYM_TABLE].name;
138
- const entries = Object.entries(colsVals);
139
- const [strings, binds] = entries.reduce(([ss, bs], [colName, value], i) => [[...ss, i === 0 ? ' set ' : ' ,', ' = '], [...bs, identifier(colName), value ?? null]], [['update '], [identifier(tableName)]]);
156
+ const entries = Object.entries(colsVals).filter(([, value]) => value !== undefined);
157
+ const [strings, binds] = entries.reduce(([ss, bs], [colName, value], i) => [[...ss, i === 0 ? ' set ' : ' ,', ' = '], [...bs, identifier(colName), value]], [['update '], [identifier(tableName)]]);
140
158
  return result(fragment([...strings, ''], binds));
141
159
  };
142
160
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanit-co/sql-ts",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "A TypeScript SQL query builder using tagged template literals. Write plain SQL with safe, automatic parameter binding and properly quoted identifiers. No DSL to learn, no magic, no ORM.",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",