This repository has been archived by the owner on Nov 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
123 lines (106 loc) · 3.49 KB
/
client.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
var {Serialized} = require("@serialized/serialized-client")
var {TodoList} = require('./todolist');
class TodoListClient {
constructor(serializedClient) {
this.serializedClient = serializedClient
}
static defaultClient() {
// Setup client to use access key headers from environment variables
const serializedInstance = Serialized.create({
accessKey: process.env.SERIALIZED_ACCESS_KEY,
secretAccessKey: process.env.SERIALIZED_SECRET_ACCESS_KEY
});
serializedInstance.axiosClient.interceptors.request.use(r => {
console.log(r.url);
return r;
})
return new TodoListClient(serializedInstance)
}
async updateProjections() {
await this.createStatsProjection();
await this.createListsProjection();
}
async createStatsProjection() {
try {
const request = {
projectionName: 'list-stats',
feedName: 'list',
aggregated: true,
handlers: [
{
eventType: "TodoListCreated",
functions: [{function: "inc", targetSelector: "$.projection.listCount"}]
},
{
eventType: "TodoAdded",
functions: [{function: "inc", targetSelector: "$.projection.todoCount",}]
},
]
};
await this.serializedClient.projections.createOrUpdateDefinition(request)
} catch (e) {
console.log('Error response: ', e.response.data);
}
}
async createListsProjection() {
try {
await this.serializedClient.projections.createOrUpdateDefinition({
projectionName: 'lists',
feedName: 'list',
handlers: [
{
eventType: "TodoListCreated",
functions: [
{function: "set", eventSelector: "$.event.name", targetSelector: "$.projection.name"},
{function: "set", eventSelector: "$.metadata.aggregateId", targetSelector: "$.projection.listId"}
]
},
{
eventType: "TodoAdded",
functions: [{function: "push", targetSelector: "$.projection.todos"}]
},
{
eventType: "TodoCompleted",
functions: [{
function: "set",
targetSelector: "$.projection.todos[?].status",
targetFilter: "[?(@.todoId == $.event.todoId)]",
rawData: "COMPLETED"
}]
},
{
eventType: "TodoListCompleted",
functions: [{
function: "set",
targetSelector: "$.projection.status",
rawData: "COMPLETED"
}]
},
]
})
} catch (e) {
console.log('Error response: ', e.response.data);
}
}
async createTodoList(todoList) {
await this.serializedClient.aggregates.create(todoList);
}
async saveTodoList(todoList) {
await this.serializedClient.aggregates.save(todoList, true);
}
async loadTodoList(listId) {
let todoList = new TodoList(listId);
await this.serializedClient.aggregates.load(todoList)
return todoList;
}
async findListProjections() {
return (await this.serializedClient.projections.listSingleProjections({projectionName: 'lists'})).projections;
}
async findListStats() {
return await this.serializedClient.projections.getAggregatedProjection({projectionName: 'list-stats'})
}
async findListProjection(listId) {
return await this.serializedClient.projections.getSingleProjection({projectionName: 'lists', projectionId: listId})
}
}
module.exports = TodoListClient;