-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
187 lines (168 loc) · 5.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Copyright (c) 2017 Chris Baker <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
'use strict';
var AWS = require('aws-sdk');
var EventStore = module.exports = function (options) {
this.tableName = options.tableName;
this.indexName = options.indexName;
this.database = new AWS.DynamoDB(options);
this.now = options.now || Date.now;
};
EventStore.AWS = AWS;
EventStore.VersionConflictError = function () {
Error.call(this);
this.name = 'EventStoreVersionConflictError';
this.message = 'A commit already exists with the specified version';
Error.captureStackTrace(this, this.constructor);
};
EventStore.prototype.createTable = function () {
var params = {
TableName: this.tableName,
AttributeDefinitions: [
{
AttributeName: 'aggregateId',
AttributeType: 'S'
},
{
AttributeName: 'commitId',
AttributeType: 'S'
},
{
AttributeName: 'active',
AttributeType: 'S'
},
{
AttributeName: 'version',
AttributeType: 'N'
}
],
KeySchema: [
{
AttributeName: 'aggregateId',
KeyType: 'HASH'
},
{
AttributeName: 'version',
KeyType: 'RANGE'
}
],
ProvisionedThroughput: {
ReadCapacityUnits: 15,
WriteCapacityUnits: 15
},
GlobalSecondaryIndexes: [
{
IndexName: this.indexName,
KeySchema: [
{
AttributeName: 'active',
KeyType: 'HASH'
},
{
AttributeName: 'commitId',
KeyType: 'RANGE'
}
],
Projection: {
ProjectionType: 'ALL'
},
ProvisionedThroughput: {
ReadCapacityUnits: 15,
WriteCapacityUnits: 15
}
}
]
};
return this.database.createTable(params).promise();
};
EventStore.prototype.deleteTable = function () {
var params = {
TableName: this.tableName
};
return this.database.deleteTable(params).promise();
};
EventStore.prototype.append = function (commit) {
var now = this.now();
var date = new Date(now).toISOString().replace(/[^0-9]/g, '');
var commitId = date + ':' + commit.aggregateId;
var params = {
TableName: this.tableName,
Item: {
commitId: { S: commitId },
committedAt: { N: now.toString() },
aggregateId: { S: commit.aggregateId },
version: { N: commit.version.toString() },
events: { S: JSON.stringify(commit.events) },
active: { S: 't' }
},
ConditionExpression: 'attribute_not_exists(version)',
ReturnValues: 'NONE'
};
return this.database.putItem(params).promise()
.catch(function (err) {
if (err.name === 'ConditionalCheckFailedException') {
throw new EventStore.VersionConflictError();
}
throw err;
});
};
EventStore.prototype._query = function (params) {
return this.database.query(params).promise()
.then(function (response) {
return response.Items
.map(function (item) {
return {
commitId: item.commitId.S,
committedAt: parseInt(item.committedAt.N, 10),
aggregateId: item.aggregateId.S,
version: parseInt(item.version.N, 10),
events: JSON.parse(item.events.S)
};
});
});
};
EventStore.prototype.query = function (options) {
var params = {
TableName: this.tableName,
ConsistentRead: true,
KeyConditionExpression: 'aggregateId = :a AND version >= :v',
ExpressionAttributeValues: {
':a': { S: options.aggregateId },
':v': { N: options.version.toString() }
}
};
return this._query(params);
};
EventStore.prototype.scan = function (options) {
var commitId = (options && options.commitId) || '0';
var params = {
TableName: this.tableName,
IndexName: this.indexName,
KeyConditionExpression: 'active = :a AND commitId >= :c',
ExpressionAttributeValues: {
':a': { S: 't' },
':c': { S: commitId }
}
};
return this._query(params);
};