Skip to content

Commit 8149c36

Browse files
committed
Adds a function to utilize the aws-sdk marshall method for parsing JSON
Let, Optional Chaining, Default to not mocked
1 parent 40c519e commit 8149c36

File tree

6 files changed

+58
-6
lines changed

6 files changed

+58
-6
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
scratch/
2+
node_modules
3+
.vscode

changelog.md

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Architect Lambda invoker plugin
22

3+
---
4+
## [1.2.1] 2024-04-23
5+
6+
### Added
7+
- Added functionality for passing unmarshalled json as the payload for tables-streams invocations
8+
39
---
410

511
## [1.2.0] 2023-08-30
@@ -27,6 +33,29 @@
2733

2834
---
2935

36+
## [1.1.0] 2023-08-07
37+
38+
### Added
39+
40+
- Added support from mocks in `sandbox-invoke-mocks.cjs`, `sandbox-invoke-mocks.mjs` filenames, and importing as ES modules from `sandbox-invoke-mocks.js` (and `.mjs`, of course); fixes #6, thanks @danactive!
41+
- Note: per [Node.js #2806](https://github.com/nodejs/help/issues/2806), ES module data may be cached in Sandbox, which is intended to be a semi-long-lived process. If using ES modules, you may need to restart Sandbox when updating your mocks.
42+
43+
---
44+
45+
## [1.0.1] 2023-07-31
46+
47+
### Fixed
48+
49+
- Fixed missing `ansi-colors` dependency; thanks @lpsinger!
50+
51+
---
52+
53+
## [1.0.1] 2023-03-22
54+
55+
### Added
56+
- Added functionality for passing unmarshalled json as the payload for tables-streams invocations
57+
58+
3059
## [1.0.0] 2023-02-10
3160

3261
### Added

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"homepage": "https://github.com/architect/plugin-lambda-invoker#readme",
2626
"dependencies": {
2727
"@architect/utils": "^3.1.2",
28+
"@aws-sdk/util-dynamodb": "^3.296.0",
2829
"ansi-colors": "^4.1.1",
2930
"enquirer": "^2.3.6"
3031
},

readme.md

+10
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ analytics
7474
7575
@scheduled
7676
backup-database
77+
78+
@tables-streams
79+
tableName
7780
```
7881

7982
If you wanted to add one or more mocks to each of the three Lambdas above, create the following `sandbox-invoke-mocks.js` (or equivalent JSON) file with the format of `[pragmaName][lambdaName][mockName]`:
@@ -98,6 +101,13 @@ module.exports = {
98101
'the-last-mock': { /* payload */ },
99102
}
100103
},
104+
'tables-streams':{
105+
tableName:{
106+
INSERT:{ /* payload */ },
107+
MODIFY:{ /* payload */ },
108+
REMOVE:{ /* payload */ },
109+
}
110+
}
101111
}
102112
```
103113

src/event-mocks.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function scheduled () {
3939
}
4040
}
4141

42-
function tablesStreams (eventName) {
42+
function tablesStreams (eventName, dynamoData) {
4343
return {
4444
Records: [
4545
{
@@ -50,9 +50,8 @@ function tablesStreams (eventName) {
5050
awsRegion: 'Sandbox',
5151
dynamodb: {
5252
ApproximateCreationDateTime: new Date() / 1000,
53-
// Keys + NewImage are in DynamoDB JSON, perhaps we can mock transform that later
54-
Keys: { NOT_MOCKED: true },
55-
NewImage: { NOT_MOCKED: true },
53+
Keys: dynamoData?.Keys ?? { NOT_MOCKED: true },
54+
NewImage: dynamoData?.NewImage ?? { NOT_MOCKED: true },
5655
SequenceNumber: 0,
5756
SizeBytes: 0,
5857
StreamViewType: 'NEW_AND_OLD_IMAGES'

src/index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ let { prompt } = require('enquirer')
55
let colors = require('ansi-colors')
66
let update = updater('Invoker')
77
let mock = require('./event-mocks')
8+
let { marshall } = require('@aws-sdk/util-dynamodb')
89
let deactivatedInvoke = async () => console.log('Sandbox not yet started!')
10+
911
let lastInvoke
1012

1113
let sandbox = {
@@ -140,7 +142,7 @@ let sandbox = {
140142
message: 'Which kind of Dynamo Stream event do you want to invoke?',
141143
choices: [ 'INSERT', 'MODIFY', 'REMOVE' ]
142144
})
143-
payload = mock.tablesStreams(eventName)
145+
payload = mock.tablesStreams(eventName, marshallJson(mocks?.[pragma]?.[name]?.[eventName]))
144146
}
145147
else {
146148
if (!Object.keys(userPayload).length) {
@@ -212,7 +214,7 @@ async function getMod (filepath) {
212214
}
213215
catch (err) {
214216
if (hasEsmError(err)) {
215-
let path = process.platform.startsWith('win')
217+
let path = process.platform.startsWith('win')
216218
? 'file://' + filepath
217219
: filepath
218220
let imported = await import(path)
@@ -225,3 +227,12 @@ async function getMod (filepath) {
225227

226228
return mod
227229
}
230+
// Marshalls Json from the mock into keys and newimage for tables-streams
231+
function marshallJson (json) {
232+
const marshalled = marshall(json)
233+
const Keys = Object.keys(marshalled).reduce((keys, key) => {
234+
keys[key] = { [Object.keys(marshalled[key])[0]]: true }
235+
return keys
236+
}, {})
237+
return { Keys, NewImage: marshalled }
238+
}

0 commit comments

Comments
 (0)