This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
DocumentDbClient.js
123 lines (123 loc) · 4.59 KB
/
DocumentDbClient.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Consts = require("./Consts");
var documentdb_1 = require("documentdb");
var DocumentDbClient = (function () {
function DocumentDbClient(options) {
this.options = options;
}
DocumentDbClient.prototype.initialize = function (callback) {
var _this = this;
var client = new documentdb_1.DocumentClient(this.options.host, { masterKey: this.options.masterKey });
this.client = client;
this.getOrCreateDatabase(function (error, database) {
if (error) {
callback(DocumentDbClient.getError(error));
}
else {
_this.database = database;
_this.getOrCreateCollection(function (error, collection) {
if (error) {
callback(DocumentDbClient.getError(error));
}
else {
_this.collection = collection;
callback(null);
}
});
}
});
};
DocumentDbClient.prototype.insertOrReplace = function (partitionKey, rowKey, entity, isCompressed, callback) {
var docDbEntity = { id: partitionKey + ',' + rowKey, data: entity, isCompressed: isCompressed };
this.client.upsertDocument(this.collection._self, docDbEntity, {}, function (error, collection, responseHeaders) {
callback(DocumentDbClient.getError(error), null, responseHeaders);
});
};
DocumentDbClient.prototype.retrieve = function (partitionKey, rowKey, callback) {
var id = partitionKey + ',' + rowKey;
var querySpec = {
query: Consts.DocDbRootQuery,
parameters: [{
name: Consts.DocDbIdParam,
value: id
}]
};
var iterator = this.client.queryDocuments(this.collection._self, querySpec, {});
iterator.toArray(function (error, result, responseHeaders) {
if (error) {
callback(DocumentDbClient.getError(error), null, null);
}
else if (result.length == 0) {
callback(null, null, null);
}
else {
var document_1 = result[0];
callback(null, document_1, null);
}
});
};
DocumentDbClient.getError = function (error) {
if (!error)
return null;
return new Error('Error Code: ' + error.code + ' Error Body: ' + error.body);
};
DocumentDbClient.prototype.getOrCreateDatabase = function (callback) {
var _this = this;
var querySpec = {
query: Consts.DocDbRootQuery,
parameters: [{
name: Consts.DocDbIdParam,
value: this.options.database
}]
};
this.client.queryDatabases(querySpec).toArray(function (error, result, responseHeaders) {
if (error) {
callback(error, null);
}
else if (result.length == 0) {
_this.client.createDatabase({ id: _this.options.database }, {}, function (error, database) {
if (error) {
callback(error, null);
}
else {
callback(null, database);
}
});
}
else {
callback(null, result[0]);
}
});
};
DocumentDbClient.prototype.getOrCreateCollection = function (callback) {
var _this = this;
var querySpec = {
query: Consts.DocDbRootQuery,
parameters: [{
name: Consts.DocDbIdParam,
value: this.options.collection
}]
};
this.client.queryCollections(this.database._self, querySpec).toArray(function (error, result, responseHeaders) {
if (error) {
callback(error, null);
}
else if (result.length == 0) {
_this.client.createCollection(_this.database._self, { id: _this.options.collection }, {}, function (error, collection) {
if (error) {
callback(error, null);
}
else {
callback(null, collection);
}
});
}
else {
callback(null, result[0]);
}
});
};
return DocumentDbClient;
}());
exports.DocumentDbClient = DocumentDbClient;