-
Notifications
You must be signed in to change notification settings - Fork 2
/
sample.js
113 lines (93 loc) · 3.41 KB
/
sample.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
var config = require('./config');
var DocumentDbUtility = require('./index');
var dbUtil = new DocumentDbUtility(config);
async function sample() {
try {
let database = await dbUtil.database('test');
let collections = await dbUtil.listCollections(database);
let collection = await dbUtil.collection(database, 'people');
let superTimeTrigger = {
id: "validateDocumentContents",
serverScript: function validate() {
var context = getContext();
var request = context.getRequest();
var documentToCreate = request.getBody();
var ts = new Date();
documentToCreate["supertime"] = ts.getTime();
request.setBody(documentToCreate);
},
triggerType: 'Pre',
triggerOperation: 'Create'
}
let triggerInstance = await dbUtil.trigger(collection, superTimeTrigger);
await dbUtil.insert(collection, {
name: 'penguin',
profession: 'better guy',
income: 200
}, { preTriggerInclude: [superTimeTrigger.id] });
let spec = {
query: 'Select * from c where c.name = @name',
parameters: [
{
name: '@name',
value: 'penguin'
}
]
}
let doc = (await dbUtil.query(collection, spec))[0];
console.log('doc', doc);
doc.profession = "bad guy";
let docLink = dbUtil.createDocumentLink(database.id, collection.id, doc.id);
await dbUtil.update(docLink, doc);
let proc = {
id: "summer",
serverScript: function (a, b) {
var context = getContext();
var response = context.getResponse();
let sum = a + b;
response.setBody(sum);
}
}
let procInstance = await dbUtil.storedProcedure(collection, proc);
let result = await dbUtil.executeStoredProcedure(procInstance, [1, 2]);
console.log(`Store procedure result -> ${result}`);
var taxUdf = {
id: "tax",
serverScript: function tax(income) {
if (income == undefined)
throw 'no input';
if (income < 1000)
return income * 0.1;
else if (income < 10000)
return income * 0.2;
else
return income * 0.4;
}
}
let udf = await dbUtil.userDefinedFunction(collection, taxUdf);
await dbUtil.insert(collection, {
name: 'boomer',
profession: 'rich guy',
income: 10000
});
let spec2 = {
query: 'Select * from c WHERE udf.tax(c.income) > @taxAmount',
parameters: [
{
name: '@taxAmount',
value: 3000
}
]
}
let doc2 = await dbUtil.query(collection, spec2);
console.log('people', doc2)
await dbUtil.delete(docLink);
await dbUtil.deleteStoredProcedure(procInstance._self);
await dbUtil.deleteTrigger(triggerInstance._self);
await dbUtil.deleteCollection(database.id, collection.id);
await dbUtil.deleteDatabase(database.id);
} catch (e) {
console.log('error', e);
}
}
sample();