-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcavemen.js
232 lines (191 loc) · 5.78 KB
/
cavemen.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const docBasePath = "/api/rest/v2/namespaces/workshop/collections/cavemen";
const restBasePath = "/api/rest/v2/keyspaces/workshop/";
const restSchemaPath = "/api/rest/v2/schemas/keyspaces/workshop/tables/";
let data;
let response;
let status;
function wait(timeout) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, timeout);
});
}
async function requestWithRetry(astraClient, url) {
const MAX_RETRIES = 20;
for (let i = 1; i <= MAX_RETRIES; i++) {
try {
let response = await astraClient.get(url);
if (response.status == "200") {
console.log("It's good?");
return response;
} else {
const timeout = 5000 * i * 10;
console.log("Waiting", timeout, "ms");
await wait(timeout);
}
} catch {
const timeout = 5000 * i * 10;
console.log("Waiting", timeout, "ms");
await wait(timeout);
}
}
}
async function postWithRetry(astraClient, url) {
const MAX_RETRIES = 20;
for (let i = 1; i <= MAX_RETRIES; i++) {
try {
let response = await astraClient.post(url);
if (response.status == "200") {
console.log("It's good?");
return response;
} else {
const timeout = 5000 * i * 10;
console.log("Waiting", timeout, "ms");
await wait(timeout);
}
} catch {
const timeout = 5000 * i * 10;
console.log("Waiting", timeout, "ms");
await wait(timeout);
}
}
}
async function runCalls() {
const astraRest = require("@astrajs/rest");
const astraDoc = require("@astrajs/collections");
const astraClient = await astraRest.createClient({
astraDatabaseId: process.env.ASTRA_DB_ID,
astraDatabaseRegion: process.env.ASTRA_DB_REGION,
authToken: process.env.ASTRA_DB_APPLICATION_TOKEN,
});
const docClient = await astraDoc.createClient({
astraDatabaseId: process.env.ASTRA_DB_ID,
astraDatabaseRegion: process.env.ASTRA_DB_REGION,
authToken: process.env.ASTRA_DB_APPLICATION_TOKEN,
});
testCollection = docClient.namespace("workshop").collection("cavemen");
// REST API
// List my tables
response = await astraClient.get(restSchemaPath);
console.log(restSchemaPath);
console.log(status);
// Delete cavemen if it exists
try {
response = await astraClient.delete(restSchemaPath + "cavemen");
console.log(restSchemaPath);
console.log(status);
} catch {
console.log("No cavemen found");
}
// Create the cavemen table
response = await astraClient.post(restSchemaPath, {
name: "cavemen",
columnDefinitions: [
{
name: "firstname",
typeDefinition: "text",
static: false,
},
{
name: "lastname",
typeDefinition: "text",
static: false,
},
{
name: "occupation",
typeDefinition: "text",
},
],
primaryKey: {
partitionKey: ["lastname"],
clusteringKey: ["firstname"],
},
});
console.log(response);
let query = '{"lastname":{"$in":["Rubble","Flintstone"]}}';
response = postWithRetry(astraClient, restBasePath + "cavemen", {
firstname: "Fred",
lastname: "Flintstone",
});
if (response.status == 201) {
console.log("Created Fred in the cavemen collection");
}
// create a new user
response = postWithRetry(astraClient, restBasePath + "cavemen/BarneyRubble", {
firstname: "Barney",
lastname: "Rubble",
});
if (response.status == 201) {
console.log("Created Barney in the cavemen collection");
}
response = await astraClient.get(restSchemaPath);
console.log(restSchemaPath);
console.log(response);
// check our tables again
query = '{"lastname":{"$in":["Rubble","Flintstone"]}}';
response = await astraClient.get(restBasePath + "cavemen?where=" + query);
console.log(response);
// Delete cavemen if it exists
try {
response = await astraClient.delete(restSchemaPath + "cavemen");
console.log(restSchemaPath);
console.log(status);
} catch {
console.log("No cavemen found");
}
response = await astraClient.get(restSchemaPath);
console.log(response);
// Document API
// create a new user without a document id
response = await testCollection.create({
firstname: "Fred",
lastname: "Flintstone",
});
console.log(response);
if (response.status == 201) {
console.log("Created Fred in the cavemen collection");
let fredID = data.documentId;
}
response = await testCollection.create("BarneyRubble", {
firstname: "Barney",
lastname: "Rubble",
});
if (response.status == 201) {
console.log("Created Barney in the cavemen collection");
}
// search a collection of documents
response = await testCollection.find( {
firstname: { $eq: "Fred" }
});
console.log(response)
// search a collection of documents
response = await testCollection.find( {
firstname: { $eq: "Barney" }
});
console.log(response)
// get a single user by document id
response = await testCollection.get("BarneyRubble");
console.log(response);
// get a subdocument by path
response = await testCollection.get("BarneyRubble/firstname");
// create a user subdocument
await testCollection.update("BarneyRubble/addresses/home", {
city: "Stone City",
});
// partially update user
response = await testCollection.update("BarneyRubble", {
occupation: "Fred's Friend",
});
response = await testCollection.get("BarneyRubble");
console.log(response);
// delete a user subdocument
response = await testCollection.delete("BarneyRubble/addresses");
response = await testCollection.get("BarneyRubble");
console.log(response);
// delete a user
response = await testCollection.delete("BarneyRubble/addresses");
response = await testCollection.get("BarneyRubble");
console.log(response);
}
runCalls();