-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentitybag.js
123 lines (103 loc) · 3.75 KB
/
entitybag.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
//-----------------------------------------------------------
// Class Definition
//-----------------------------------------------------------
/*
* Required:
*/
var azure = require('azure');
/*
* The Interface for interacting with an EntityBag
*/
function EntityBag(name, tableservice, callback) {
this.IsReady=false;
this.TableService = tableservice
this.BagName = name;
this.LastError = undefined;
tableservice.createTableIfNotExists(name, EntityBagTableCallback.bind(this,callback));
function EntityBagTableCallback(err)
{
if(err)
{
this.LastError=err;
if(callback) {callback(err);}
}
else
{
this.IsReady=true;
if(callback) {callback(err);}
}
}
}
var p = EntityBag.prototype
//-----------------------------------------------------------
// Private Methods
//-----------------------------------------------------------
function SortableNormalize(val, forcejson)
{
forcejson = forcejson || false;
if(typeof(val)!=='string' || forcejson ) {return JSON.stringify(val);}
return val;
}
//-----------------------------------------------------------
// Public Methods
//-----------------------------------------------------------
p.addValue = function AddValue(partition,key,value,callback)
{
var Entity={
PartitionKey : SortableNormalize(partition)
, RowKey : SortableNormalize(key)
, value : SortableNormalize(value,true)};
this.TableService.insertOrReplaceEntity(this.BagName, Entity, AddValueCallback.bind(callback));
function AddValueCallback(err,val)
{
if(callback) { callback(err,val);}
else{ console.warn("WARN: EntityBag.addValue called without callback");}
}
}
p.getValue = function GetValue(partition,key,callback)
{
this.TableService.queryEntity(this.BagName, SortableNormalize(partition), SortableNormalize(key), GetValueCallback.bind(callback));
function GetValueCallback(err,val)
{
if(callback) {
if(err) {callback(err);}
else {callback(err,JSON.parse(val.value));};
}
else{ console.warn("WARN: EntityBag.getValue called without callback, this default callback is a bridge to nowhere.");}
}
}
p.removeValue = function RemoveValue(partition,key,callback)
{
var test = SortableNormalize(key);
this.TableService.deleteEntity(this.BagName, {PartitionKey:SortableNormalize(partition), RowKey:SortableNormalize(key)}, RemoveValueCallback.bind(callback));
function RemoveValueCallback(err)
{
if(callback) {
callback(err);
}
else{ console.warn("WARN: EntityBag.removeValue called without callback.");}
}
}
p.getValues = function GetValues(partition, keystart, keyend, callback)
{
var query = azure.TableQuery
.select()
.from(this.BagName)
.where('PartitionKey eq ?', SortableNormalize(partition));
if (keystart) { query.and('RowKey ge ?', SortableNormalize(keystart)); }
if (keyend) { query.and('RowKey le ?', SortableNormalize(keyend)); }
this.TableService.queryEntities(query,GetValuesCallback.bind(callback));
function GetValuesCallback(err,val)
{
if (callback) {
var values = {};
if (!err && val) { val.forEach(function (entry) { values[entry.RowKey] = entry.value; }.bind(values)); }
callback(err,values);
}
else{ console.error("ERROR: EntityBag.getValues called without callback, this default callback is a bridge to nowhere.");}
}
}
//-----------------------------------------------------------
// Exports - Class Constructor
//-----------------------------------------------------------
module.exports = EntityBag;