Skip to content

Commit 153bc50

Browse files
authored
feat: RDS variableMap support (#121)
* adds support for variableMap in VTL templates * Adds simple documentation on variableMap usage for RDS DL * Cleanup * just to avoid confusion * cleanup * cleanup * Adds back console on error * linting... * Feedback from code review * oops * Comments from Jariz * Replace all boolean and null variables
1 parent f5eaac0 commit 153bc50

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,25 @@ This plugin supports resolvers implemented by `amplify-appsync-simulator`, as we
366366
$utils.toJson($response)
367367
```
368368
369+
### Using Variable Map
370+
Variable map support is limited and does not differentiate numbers and strings data types, please inject them directly if needed.
371+
372+
Will be escaped properly: `null`, `true`, and `false` values.
373+
374+
```
375+
{
376+
"version": "2018-05-29",
377+
"statements": [
378+
"UPDATE <name-of-table> set deleted_at=NOW() WHERE id=:ID",
379+
"SELECT * FROM <name-of-table> WHERE id=:ID and unix_timestamp > $ctx.args.newerThan"
380+
],
381+
variableMap: {
382+
":ID": $ctx.args.id,
383+
## ":TIMESTAMP": $ctx.args.newerThan -- This will be handled as a string!!!
384+
}
385+
}
386+
```
387+
369388
## Contributors ✨
370389
371390
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

src/data-loaders/RelationalDataLoader.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ const convertSQLResponseToRDSRecords = (rows) => {
7474

7575
const convertPostgresSQLResponseToColumnMetaData = (rows) => {
7676
return rows.map((row) => {
77-
const typeName = Object.keys(pgTypes.builtins)
78-
.find((d) => pgTypes.builtins[d] === row.dataTypeID)
79-
.toUpperCase();
77+
const typeName =
78+
Object.keys(pgTypes.builtins).find(
79+
(d) => pgTypes.builtins[d] === row.dataTypeID,
80+
) ?? 'UNKNOWN';
8081
// @TODO: Add support for the following fields
8182
// isAutoIncrement,
8283
// nullable,
@@ -98,8 +99,34 @@ const convertPostgresSQLResponseToColumnMetaData = (rows) => {
9899
};
99100
});
100101
};
102+
103+
const injectVariables = (statement, req) => {
104+
const { variableMap } = req;
105+
if (!variableMap) {
106+
return statement;
107+
}
108+
const result = Object.keys(variableMap).reduce((statmnt, key) => {
109+
if (variableMap[key] === null || typeof variableMap[key] == 'boolean') {
110+
return statmnt.replaceAll(key, `${variableMap[key]}`);
111+
}
112+
// @TODO: Differentiate number from string inputs...
113+
return statmnt.replaceAll(key, `'${variableMap[key]}'`);
114+
}, statement);
115+
return result;
116+
};
117+
101118
const executeSqlStatements = async (client, req) =>
102-
Promise.mapSeries(req.statements, (statement) => client.query(statement));
119+
Promise.mapSeries(req.statements, async (statement) => {
120+
statement = injectVariables(statement, req);
121+
try {
122+
const result = await client.query(statement);
123+
return result;
124+
} catch (error) {
125+
console.log(`RDS_DATALOADER: Failed to execute: `, statement, error);
126+
throw error;
127+
}
128+
});
129+
103130
export default class RelationalDataLoader {
104131
constructor(config) {
105132
this.config = config;
@@ -132,7 +159,6 @@ export default class RelationalDataLoader {
132159
database: this.config.rds.dbName,
133160
port: this.config.rds.dbPort,
134161
};
135-
136162
const res = {};
137163
if (this.config.rds.dbDialect === 'mysql') {
138164
const client = await mysql.createConnection(dbConfig);

0 commit comments

Comments
 (0)