@sqb/connect
Version:
Multi-dialect database connection framework written with TypeScript
134 lines (133 loc) • 4.19 kB
JavaScript
import { camelCase, pascalCase } from 'putil-varhelpers';
import { FieldInfoMap } from './field-info-map.js';
export function applyNamingStrategy(value, namingStrategy) {
if (typeof namingStrategy === 'string' && namingStrategy !== 'original') {
switch (namingStrategy.toLowerCase()) {
case 'lowercase':
return value.toLowerCase();
case 'uppercase':
return value.toUpperCase();
case 'camelcase':
if (!value.match(/[a-z]/))
return camelCase(value.toLowerCase());
value = camelCase(value);
return value[0].toLowerCase() + value.substring(1);
case 'pascalcase':
if (!value.match(/[a-z]/))
return pascalCase(value.toLowerCase());
return pascalCase(value);
default:
break;
}
}
else if (typeof namingStrategy === 'function')
return namingStrategy(value);
return value;
}
export function wrapAdapterFields(oldFields, fieldNaming) {
const mapFieldInfo = (f, index) => {
const name = applyNamingStrategy(f.fieldName, fieldNaming);
if (name)
return { ...f, name, index };
};
const result = new FieldInfoMap();
let i = 0;
oldFields.forEach(f => {
const x = mapFieldInfo(f, i);
if (x) {
i++;
result.add(x);
}
});
return result;
}
export function normalizeRowsToObjectRows(fields, rowType, oldRows, options) {
return normalizeRows(fields, rowType, oldRows, {
...options,
objectRows: true,
});
}
export function normalizeRowsToArrayRows(fields, rowType, oldRows, options) {
return normalizeRows(fields, rowType, oldRows, {
...options,
objectRows: false,
});
}
function normalizeRows(fields, rowType, oldRows, options) {
const ignoreNulls = options.ignoreNulls && options.objectRows;
const transform = options.transform;
const coerceValue = (v, f) => {
if (v === undefined)
return;
if (transform)
v = transform(v, f);
if (v === null && ignoreNulls)
return;
return v;
};
if (options.objectRows) {
if (rowType === 'array') {
return oldRows.map((row) => {
const r = {};
for (const f of fields.values()) {
const v = coerceValue(row[f.index], f);
if (v !== undefined)
r[f.name] = v;
}
return r;
});
}
let hasFieldNaming = false;
for (const f of fields.values()) {
if (f.name !== f.fieldName) {
hasFieldNaming = true;
break;
}
}
if (hasFieldNaming || transform) {
return oldRows.map((row) => {
const r = {};
for (const f of fields.values()) {
const v = coerceValue(row[f.fieldName], f);
if (v !== undefined)
r[f.name] = v;
}
return r;
});
}
if (ignoreNulls) {
oldRows.forEach(row => {
for (const [k, v] of Object.entries(row)) {
if (v === null)
delete row[k];
}
});
}
return oldRows;
}
if (rowType === 'array') {
oldRows.forEach((row) => {
for (const [i, v] of row.entries()) {
row[i] = coerceValue(v, fields.get(i));
}
});
return oldRows;
}
return oldRows.map((row) => {
const r = [];
for (const f of fields.values()) {
r[f.index] = coerceValue(row[f.fieldName], f);
}
return r;
});
}
export function callFetchHooks(rows, request) {
const fetchHooks = request.fetchHooks;
if (!fetchHooks)
return;
for (const row of rows) {
for (const fn of fetchHooks) {
fn(row, request);
}
}
}