/
op-result.js
93 lines (69 loc) · 1.72 KB
/
op-result.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
import { OperationType } from './constants'
import { OpResultValueReference } from './op-result-value-reference'
export class OpResult {
constructor(uow, { operationType, table, payload }) {
this.uow = uow
this.operationType = operationType
this.table = table
this.payload = payload
const stackName = this.uow.getOpStackName(operationType, table)
const resultIndex = this.uow.getNextOpResultIndex(stackName)
this.opResultId = `${stackName}${resultIndex}`
this.result = null
}
setResult(result) {
this.result = result
}
getResult() {
return this.result
}
setError(error) {
this.error = error
}
getError() {
return this.error
}
getType() {
return this.operationType
}
isFindRef() {
return this.operationType === OperationType.FIND
}
isCollectionRef() {
return this.operationType === OperationType.FIND || this.operationType === OperationType.CREATE_BULK
}
isObjectRef() {
return (
this.operationType === OperationType.CREATE ||
this.operationType === OperationType.UPDATE ||
this.operationType === OperationType.UPSERT
)
}
setOpResultId(opResultId) {
this.opResultId = opResultId
return this
}
getOpResultId() {
return this.opResultId
}
getTableName() {
return this.table
}
resolveTo(resultIndex, propName) {
if (typeof resultIndex === 'string') {
propName = resultIndex
resultIndex = undefined
}
return new OpResultValueReference(this, { resultIndex, propName })
}
toJSON() {
const result = {
___ref : true,
opResultId: this.opResultId,
}
if (this.isObjectRef()) {
result.propName = 'objectId'
}
return result
}
}