| 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 |
8x
8x
8x
8x
8x
8x
8x
8x
8x
8x
5x
5x
5x
5x
5x
2x
2x
2x
2x
2x
1x
1x
1x
1x
1x
1x
8x
| "use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const request_1 = require("./request");
const util_1 = require("./util");
const command_1 = require("./command");
const actionName = require("../constants/actionName");
class DocumentReference {
constructor(db, coll, docID, projection = {}) {
this._db = db;
this._coll = coll;
this.id = docID;
this.request = new request_1.Request(this._db);
this.projection = projection;
}
create(data) {
let params = {
CollectionName: this._coll,
Data: JSON.stringify(util_1.Util.encodeDocumentDataForReq(data, false, false))
};
Iif (this.id) {
params.Id = this.id;
}
return new Promise(resolve => {
this.request.send(actionName.CreateDocument, params).then((res) => {
resolve({
id: res.Id,
requestId: res.RequestId
});
});
});
}
set(data) {
if (!data || typeof data !== "object") {
return Promise.resolve({
code: 'INVALID_PARAM',
message: '参数必需是非空对象'
});
}
if (data.hasOwnProperty('_id')) {
return Promise.resolve({
code: 'INVALID_PARAM',
message: '不能更新_id的值'
});
}
let hasOperator = false;
const checkMixed = (objs) => {
if (typeof objs === 'object') {
for (let key in objs) {
if (objs[key] instanceof command_1.Command) {
hasOperator = true;
}
else if (typeof objs[key] === 'object') {
checkMixed(objs[key]);
}
}
}
};
checkMixed(data);
if (hasOperator) {
return Promise.resolve({
code: 'DATABASE_REQUEST_FAILED',
message: 'update operator complicit'
});
}
const merge = false;
let param = {
CollectionName: this._coll,
Query: this.id ? JSON.stringify({ _id: this.id }) : undefined,
Data: JSON.stringify(util_1.Util.encodeDocumentDataForReq(data, merge, false)),
Multi: false,
Merge: merge,
Upsert: true
};
return new Promise(resolve => {
this.request.send(actionName.ModifyDocument, param).then((res) => {
resolve({
updated: res.Updated,
upsertedId: res.UpsertedId,
requestId: res.RequestId
});
});
});
}
update(data) {
if (!data || typeof data !== "object") {
return Promise.resolve({
code: 'INVALID_PARAM',
message: '参数必需是非空对象'
});
}
if (data.hasOwnProperty('_id')) {
return Promise.resolve({
code: 'INVALID_PARAM',
message: '不能更新_id的值'
});
}
const query = { _id: this.id };
const merge = true;
const param = {
CollectionName: this._coll,
Data: JSON.stringify(util_1.Util.encodeDocumentDataForReq(data, merge, true)),
Query: JSON.stringify(query),
Multi: false,
Merge: merge,
Upsert: false
};
return new Promise(resolve => {
this.request.send(actionName.ModifyDocument, param).then((res) => {
resolve({
updated: res.Updated,
upsertedId: res.UpsertedId,
requestId: res.RequestId
});
});
});
}
remove() {
const query = { _id: this.id };
const param = {
CollectionName: this._coll,
Query: JSON.stringify(query),
Multi: false
};
return new Promise(resolve => {
this.request.send(actionName.DeleteDocument, param).then((res) => {
resolve({
deleted: res.Deleted,
requestId: res.RequestId
});
});
});
}
get() {
const query = { _id: this.id };
const param = {
CollectionName: this._coll,
Query: JSON.stringify(query),
Multi: false,
Projection: this.projection
};
return new Promise(resolve => {
this.request.send(actionName.DescribeDocument, param).then((res) => {
const documents = util_1.Util.formatResDocumentData(res.DocumentList);
resolve({
data: documents,
requestId: res.RequestId,
total: res.TotalCount,
limit: res.Limit,
offset: res.Offset
});
});
});
}
field(projection) {
for (let k in projection) {
if (projection[k]) {
projection[k] = 1;
}
else {
projection[k] = 0;
}
}
return new DocumentReference(this._db, this._coll, this.id, projection);
}
}
exports.DocumentReference = DocumentReference;
|