Skip to content

Commit b4c3135

Browse files
added event type to tell if insert, modify or remove
1 parent 28e7fc6 commit b4c3135

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/Table.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type EntityGroup = {
1111

1212
export type StreamEntityGroup = {
1313
[key: string]: {
14+
type: 'INSERT' | 'MODIFY' | 'REMOVE',
1415
new?: AnyEntity,
1516
old?: AnyEntity
1617
}[]

src/Table.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,9 @@ export class Table {
10661066
continue
10671067
}
10681068

1069-
const model = {}
1069+
const model = {
1070+
type: record.eventName
1071+
}
10701072
let typeNew
10711073
let typeOld
10721074

test/stream.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,19 @@ const event = {
116116

117117
test('Stream has New Images', async () => {
118118
const streamModels = table.stream(event.Records as DynamoDBRecord[]);
119-
const newModels = streamModels.User.filter((streamModel) => !!streamModel.new)
120-
.map((streamModel) => streamModel.new)
119+
const models = streamModels.User.filter((streamModel) => !!streamModel.new)
121120

122-
expect(newModels).toHaveLength(2);
123-
expect(newModels[0]).toEqual(
121+
expect(models).toHaveLength(2)
122+
expect(models[0].type).toEqual('INSERT')
123+
expect(models[0].new).toEqual(
124124
expect.objectContaining({
125125
id: '1234',
126126
name: 'alice',
127127
registered: new Date('2022-01-01Z'),
128128
})
129129
)
130-
expect(newModels[1]).toEqual(
130+
expect(models[1].type).toEqual('MODIFY')
131+
expect(models[1].new).toEqual(
131132
expect.objectContaining({
132133
id: '1235',
133134
name: 'bob',
@@ -138,11 +139,11 @@ test('Stream has New Images', async () => {
138139

139140
test('Stream has Old Images', async () => {
140141
const streamModels = table.stream(event.Records as DynamoDBRecord[]);
141-
const oldModels = streamModels.User.filter((streamModel) => !!streamModel.old)
142-
.map((streamModel) => streamModel.old)
142+
const models = streamModels.User.filter((streamModel) => !!streamModel.old)
143143

144-
expect(oldModels).toHaveLength(1);
145-
expect(oldModels[0]).toEqual(
144+
expect(models).toHaveLength(1)
145+
expect(models[0].type).toEqual('MODIFY')
146+
expect(models[0].old).toEqual(
146147
expect.objectContaining({
147148
name: 'rob',
148149
registered: new Date('2022-01-02Z'),

0 commit comments

Comments
 (0)