This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
CRUDService.ts
145 lines (111 loc) · 5.72 KB
/
CRUDService.ts
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
import { GraphbackOperationType } from "@graphback/core"
import * as DataLoader from "dataloader";
import { PubSubEngine } from 'graphql-subscriptions';
import { GraphbackRuntimeContext } from '../api/GraphbackRuntimeContext';
import { GraphbackRuntimeOptions } from '../api/GraphbackRuntimeOptions';
import { GraphbackDataProvider } from "../data/GraphbackDataProvider";
import { defaultLogger, GraphbackMessageLogger } from '../utils/Logger';
import { GraphbackCRUDService } from "./GraphbackCRUDService";
import { subscriptionTopicMapping } from './subscriptionTopicMapping';
/**
* Default implementation of the CRUD service offering following capabilities:
*
* - Subscriptions: using default publish subscribe method
* - Logging: using logging abstraction
*/
// tslint:disable-next-line: no-any
export class CRUDService<T = any> implements GraphbackCRUDService<T, GraphbackRuntimeContext | any> {
private db: GraphbackDataProvider;
private logger: GraphbackMessageLogger;
private pubSub: PubSubEngine;
constructor(db: GraphbackDataProvider, pubSub?: PubSubEngine,
logger?: GraphbackMessageLogger) {
this.db = db
this.pubSub = pubSub;
this.logger = logger || defaultLogger;
}
public async create(name: string, data: T, options?: GraphbackRuntimeOptions, context?: GraphbackRuntimeContext): Promise<T> {
this.logger.log(`Creating object ${name}`)
const result = await this.db.create(name, data, context);
if (this.pubSub && options && options && options.publishEvent) {
const topic = subscriptionTopicMapping(GraphbackOperationType.CREATE, name);
const payload = this.buildEventPayload('new', name, result);
await this.pubSub.publish(topic, payload);
}
return result;
}
public async update(name: string, id: string, data: T, options?: GraphbackRuntimeOptions, context?: GraphbackRuntimeContext): Promise<T> {
this.logger.log(`Updating object ${name}`)
const result = await this.db.update(name, id, data, context);
if (this.pubSub && options && options.publishEvent) {
const topic = subscriptionTopicMapping(GraphbackOperationType.UPDATE, name);
const payload = this.buildEventPayload('updated', name, result);
await this.pubSub.publish(topic, payload);
}
return result;
}
// tslint:disable-next-line: no-reserved-keywords
public async delete(name: string, id: string, data?: T, options?: GraphbackRuntimeOptions, context?: GraphbackRuntimeContext): Promise<string> {
this.logger.log(`deleting object ${name}`)
const result = await this.db.delete(name, id, data, context);
if (this.pubSub && options && options.publishEvent) {
const topic = subscriptionTopicMapping(GraphbackOperationType.DELETE, name);
const payload = this.buildEventPayload('deleted', name, result);
await this.pubSub.publish(topic, payload);
}
return result;
}
public read(name: string, id: string, options?: GraphbackRuntimeOptions, context?: GraphbackRuntimeContext): Promise<T> {
this.logger.log(`reading object ${name}`)
return this.db.read(name, id, context);
}
public findAll(name: string, options?: GraphbackRuntimeOptions, context?: GraphbackRuntimeContext): Promise<T[]> {
this.logger.log(`querying object ${name}`)
return this.db.findAll(name, context);
}
// tslint:disable-next-line: no-any
public findBy(name: string, filter: any, options?: GraphbackRuntimeOptions, context?: GraphbackRuntimeContext): Promise<T[]> {
this.logger.log(`querying object ${name} with filter ${JSON.stringify(filter)}`)
return this.db.findBy(name, filter, context);
}
public subscribeToCreate(name: string, context?: GraphbackRuntimeContext): AsyncIterator<T> | undefined {
if (!this.pubSub) {
this.logger.log(`Cannot subscribe to events for ${name}`)
throw Error(`Missing PubSub implementation in CRUDService`);
}
const createSubKey = subscriptionTopicMapping(GraphbackOperationType.CREATE, name);
return this.pubSub.asyncIterator(createSubKey)
}
public subscribeToUpdate(name: string, context?: GraphbackRuntimeContext): AsyncIterator<T> | undefined {
if (!this.pubSub) {
this.logger.log(`Cannot subscribe to events for ${name}`)
throw Error(`Missing PubSub implementation in CRUDService`);
}
const updateSubKey = subscriptionTopicMapping(GraphbackOperationType.UPDATE, name);
return this.pubSub.asyncIterator(updateSubKey)
}
public subscribeToDelete(name: string, context?: GraphbackRuntimeContext): AsyncIterator<T> | undefined {
if (!this.pubSub) {
this.logger.log(`Cannot subscribe to events for ${name}`)
throw Error(`Missing PubSub implementation in CRUDService`);
}
const deleteSubKey = subscriptionTopicMapping(GraphbackOperationType.DELETE, name);
return this.pubSub.asyncIterator(deleteSubKey)
}
public batchLoadData(name: string, relationField: string, id: string | number, context: any) {
const keyName = `${name}DataLoader`;
if (!context[keyName]) {
context[keyName] = new DataLoader<string, any>((keys: string[]) => {
return this.db.batchRead(name, relationField, keys);
});
}
return context[keyName].load(id);
}
private buildEventPayload(action: string, name: string, result: string) {
const payload = {};
const capitalName = name[0].toUpperCase() +
name.slice(1);
payload[`${action}${capitalName}`] = result;
return payload;
}
}