presidium 4.5.0 → 4.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +181 -111
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,113 +37,113 @@ npm i presidium
|
|
|
37
37
|
require Presidium in [CommonJS](https://nodejs.org/docs/latest/api/modules.html#modules-commonjs-modules):
|
|
38
38
|
```javascript
|
|
39
39
|
// import Presidium globally
|
|
40
|
-
require(
|
|
40
|
+
require("presidium/global")
|
|
41
41
|
|
|
42
42
|
// import Presidium
|
|
43
|
-
const presidium = require(
|
|
43
|
+
const presidium = require("presidium")
|
|
44
44
|
|
|
45
45
|
// import Presidium clients
|
|
46
|
-
const DynamoDBTable = require(
|
|
47
|
-
const S3Bucket = require(
|
|
48
|
-
const WebSocket = require(
|
|
49
|
-
const Readable = require(
|
|
46
|
+
const DynamoDBTable = require("presidium/DynamoDBTable")
|
|
47
|
+
const S3Bucket = require("presidium/S3Bucket")
|
|
48
|
+
const WebSocket = require("presidium/WebSocket")
|
|
49
|
+
const Readable = require("presidium/Readable")
|
|
50
50
|
```
|
|
51
51
|
|
|
52
52
|
## [Handle HTTP](https://presidium.services/docs/HTTP)
|
|
53
53
|
```javascript
|
|
54
|
-
const HTTP = require(
|
|
54
|
+
const HTTP = require("presidium/HTTP")
|
|
55
55
|
|
|
56
56
|
const server = HTTP.Server((request, response) => {
|
|
57
|
-
response.writeHead(200, {
|
|
58
|
-
response.write(JSON.stringify({ greeting:
|
|
57
|
+
response.writeHead(200, { "Content-Type": "application/json" })
|
|
58
|
+
response.write(JSON.stringify({ greeting: "Hello World" }))
|
|
59
59
|
response.end()
|
|
60
60
|
})
|
|
61
61
|
|
|
62
62
|
server.listen(3000)
|
|
63
63
|
|
|
64
|
-
const http = new HTTP(
|
|
64
|
+
const http = new HTTP("http://localhost:3000/")
|
|
65
65
|
|
|
66
|
-
http.get(
|
|
66
|
+
http.get("/")
|
|
67
67
|
.then(response => response.json())
|
|
68
|
-
.then(console.log) // { greeting:
|
|
68
|
+
.then(console.log) // { greeting: "Hello World" }
|
|
69
69
|
```
|
|
70
70
|
|
|
71
71
|
## [Send messages with WebSocket](https://presidium.services/docs/WebSocket)
|
|
72
72
|
```javascript
|
|
73
|
-
const WebSocket = require(
|
|
73
|
+
const WebSocket = require("presidium/WebSocket")
|
|
74
74
|
|
|
75
75
|
const server = new WebSocket.Server(websocket => {
|
|
76
|
-
websocket.on(
|
|
77
|
-
console.log(
|
|
78
|
-
websocket.send(
|
|
76
|
+
websocket.on("message", message => {
|
|
77
|
+
console.log("Message from client:", message)
|
|
78
|
+
websocket.send("Hello Client!")
|
|
79
79
|
})
|
|
80
|
-
websocket.on(
|
|
81
|
-
console.log(
|
|
80
|
+
websocket.on("close", () => {
|
|
81
|
+
console.log("websocket closed")
|
|
82
82
|
})
|
|
83
83
|
})
|
|
84
84
|
server.listen(1337)
|
|
85
85
|
|
|
86
|
-
const websocket = new WebSocket(
|
|
87
|
-
websocket.on(
|
|
88
|
-
websocket.send(
|
|
86
|
+
const websocket = new WebSocket("ws://localhost:1337/")
|
|
87
|
+
websocket.on("open", () => {
|
|
88
|
+
websocket.send("Hello Server!")
|
|
89
89
|
})
|
|
90
|
-
websocket.on(
|
|
91
|
-
console.log(
|
|
90
|
+
websocket.on("message", message => {
|
|
91
|
+
console.log("Message from server:", message)
|
|
92
92
|
})
|
|
93
93
|
```
|
|
94
94
|
|
|
95
95
|
## [Create, read, update, delete, and query with AWS DynamoDB](https://presidium.services/docs/DynamoDBTable)
|
|
96
96
|
```javascript
|
|
97
|
-
const DynamoDBTable = require(
|
|
98
|
-
const DynamoDBGlobalSecondaryIndex = require(
|
|
99
|
-
const AwsCredentials = require(
|
|
97
|
+
const DynamoDBTable = require("presidium/DynamoDBTable")
|
|
98
|
+
const DynamoDBGlobalSecondaryIndex = require("presidium/DynamoDBGlobalSecondaryIndex")
|
|
99
|
+
const AwsCredentials = require("presidium/AwsCredentials")
|
|
100
100
|
|
|
101
|
-
const awsCreds = await AwsCredentials(
|
|
102
|
-
awsCreds.region =
|
|
101
|
+
const awsCreds = await AwsCredentials("default")
|
|
102
|
+
awsCreds.region = "us-east-1"
|
|
103
103
|
|
|
104
104
|
const myTable = new DynamoDBTable({
|
|
105
|
-
name:
|
|
106
|
-
key: [{ id:
|
|
105
|
+
name: "my-table",
|
|
106
|
+
key: [{ id: "string" }],
|
|
107
107
|
...awsCreds,
|
|
108
108
|
})
|
|
109
109
|
await myTable.ready
|
|
110
110
|
|
|
111
111
|
const myTypeAgeIndex = new DynamoDBGlobalSecondaryIndex({
|
|
112
|
-
table:
|
|
113
|
-
key: [{ type:
|
|
112
|
+
table: "my-table",
|
|
113
|
+
key: [{ type: "string" }, { age: "number" }],
|
|
114
114
|
...awsCreds,
|
|
115
115
|
})
|
|
116
116
|
await myTypeAgeIndex.ready
|
|
117
117
|
|
|
118
|
-
await myTable.putItem({ id: { S:
|
|
119
|
-
await myTable.updateItem({ id: { S:
|
|
118
|
+
await myTable.putItem({ id: { S: "1" }, name: { S: "John" }, type: { S: "person" } })
|
|
119
|
+
await myTable.updateItem({ id: { S: "1" } }, { age: { N: "32" } })
|
|
120
120
|
|
|
121
|
-
await myTable.putItemJSON({ id:
|
|
122
|
-
await myTable.putItemJSON({ id:
|
|
121
|
+
await myTable.putItemJSON({ id: "2", name: "Joe", age: 19, type: "person" })
|
|
122
|
+
await myTable.putItemJSON({ id: "3", name: "Jane", age: 33, type: "person" })
|
|
123
123
|
|
|
124
124
|
{
|
|
125
|
-
const data = await myTable.getItem({ id: { S:
|
|
125
|
+
const data = await myTable.getItem({ id: { S: "1" } }),
|
|
126
126
|
console.log(data)
|
|
127
|
-
// { Item: { id: { S:
|
|
127
|
+
// { Item: { id: { S: "1" }, name: { S: "John" }, age: { N: "32" } } }
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
{
|
|
131
|
-
const data = await myTable.getItemJSON({ id:
|
|
131
|
+
const data = await myTable.getItemJSON({ id: "1" })
|
|
132
132
|
console.log(data)
|
|
133
|
-
// { item: { id:
|
|
133
|
+
// { item: { id: "1", name: "John", age: 32 } }
|
|
134
134
|
}
|
|
135
135
|
|
|
136
136
|
{
|
|
137
137
|
const data = await myTypeAgeIndex.query(
|
|
138
|
-
|
|
139
|
-
{ type: { S:
|
|
138
|
+
"type = :type AND age < :age",
|
|
139
|
+
{ type: { S: "person" }, age: { N: "100" } },
|
|
140
140
|
{ Limit: 2, ScanIndexForward: true },
|
|
141
141
|
)
|
|
142
142
|
console.log(data)
|
|
143
143
|
// {
|
|
144
144
|
// Items: [
|
|
145
|
-
// { id: { S:
|
|
146
|
-
// { id: { S:
|
|
145
|
+
// { id: { S: "1" }, name: { S: "John" }, age: { N: "32" }, type: { S: "person" } },
|
|
146
|
+
// { id: { S: "2" }, name: { S: "Joe" }, age: { N: "19" }, type: { S: "person" } },
|
|
147
147
|
// ],
|
|
148
148
|
// Count: 2,
|
|
149
149
|
// ScannedCount: 2,
|
|
@@ -152,15 +152,15 @@ await myTable.putItemJSON({ id: '3', name: 'Jane', age: 33, type: 'person' })
|
|
|
152
152
|
|
|
153
153
|
{
|
|
154
154
|
const data = await myTypeAgeIndex.queryJSON(
|
|
155
|
-
|
|
156
|
-
{ type:
|
|
155
|
+
"type = :type AND age < :age",
|
|
156
|
+
{ type: "person", age: 100 },
|
|
157
157
|
{ Limit: 2, ScanIndexForward: true },
|
|
158
158
|
)
|
|
159
159
|
console.log(data)
|
|
160
160
|
// {
|
|
161
161
|
// ItemsJSON: [
|
|
162
|
-
// { id:
|
|
163
|
-
// { id:
|
|
162
|
+
// { id: "1", name: "John", age: 32, type: "person" },
|
|
163
|
+
// { id: "2", name: "Joe", age: 19, type: "person" },
|
|
164
164
|
// ],
|
|
165
165
|
// Count: 2,
|
|
166
166
|
// ScannedCount: 2,
|
|
@@ -169,51 +169,51 @@ await myTable.putItemJSON({ id: '3', name: 'Jane', age: 33, type: 'person' })
|
|
|
169
169
|
|
|
170
170
|
{
|
|
171
171
|
const ItemIterator = myTypeAgeIndex.queryItemsIterator(
|
|
172
|
-
|
|
173
|
-
{ type: { S:
|
|
172
|
+
"type = :type AND age < :age",
|
|
173
|
+
{ type: { S: "person" }, age: { N: "100" } },
|
|
174
174
|
{ ScanIndexForward: true },
|
|
175
175
|
)
|
|
176
176
|
for await (const Item of ItemIterator) {
|
|
177
177
|
console.log(Item)
|
|
178
|
-
// { id: { S:
|
|
179
|
-
// { id: { S:
|
|
180
|
-
// { id: { S:
|
|
178
|
+
// { id: { S: "2" }, name: { S: "Joe" }, age: { N: "19" }, type: { S: "person" } },
|
|
179
|
+
// { id: { S: "1" }, name: { S: "John" }, age: { N: "32" }, type: { S: "person" } },
|
|
180
|
+
// { id: { S: "3" }, name: { S: "Jane" }, age: { N: "33" }, type: { S: "person" } },
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
183
|
|
|
184
184
|
{
|
|
185
185
|
const itemsIterator = myTypeAgeIndex.queryItemsIteratorJSON(
|
|
186
|
-
|
|
187
|
-
{ type:
|
|
186
|
+
"type = :type AND age < :age",
|
|
187
|
+
{ type: "person", age: 100 },
|
|
188
188
|
{ ScanIndexForward: true },
|
|
189
189
|
)
|
|
190
190
|
for await (const item of itemsIterator) {
|
|
191
191
|
console.log(item)
|
|
192
|
-
// { id:
|
|
193
|
-
// { id:
|
|
194
|
-
// { id:
|
|
192
|
+
// { id: "2", name: "Joe", age: 19, type: "person" }
|
|
193
|
+
// { id: "1", name: "John", age: 32, type: "person" }
|
|
194
|
+
// { id: "3", name: "Jane", age: 33, type: "person" }
|
|
195
195
|
}
|
|
196
196
|
}
|
|
197
197
|
```
|
|
198
198
|
|
|
199
199
|
## [Consume AWS DynamoDB Streams](https://presidium.services/docs/DynamoDBStream)
|
|
200
200
|
```javascript
|
|
201
|
-
const DynamoDBTable = require(
|
|
202
|
-
const DynamoDBStream = require(
|
|
203
|
-
const AwsCredentials = require(
|
|
201
|
+
const DynamoDBTable = require("presidium/DynamoDBTable")
|
|
202
|
+
const DynamoDBStream = require("presidium/DynamoDBStream")
|
|
203
|
+
const AwsCredentials = require("presidium/AwsCredentials")
|
|
204
204
|
|
|
205
|
-
const awsCreds = await AwsCredentials(
|
|
206
|
-
awsCreds.region =
|
|
205
|
+
const awsCreds = await AwsCredentials("default")
|
|
206
|
+
awsCreds.region = "us-east-1"
|
|
207
207
|
|
|
208
208
|
const myTable = new DynamoDBTable({
|
|
209
|
-
name:
|
|
210
|
-
key: [{ id:
|
|
209
|
+
name: "my-table",
|
|
210
|
+
key: [{ id: "string" }],
|
|
211
211
|
...awsCreds,
|
|
212
212
|
})
|
|
213
213
|
await myTable.ready
|
|
214
214
|
|
|
215
215
|
const myStream = new DynamoDBStream({
|
|
216
|
-
table:
|
|
216
|
+
table: "my-table",
|
|
217
217
|
...awsCreds,
|
|
218
218
|
})
|
|
219
219
|
await myStream.ready
|
|
@@ -226,7 +226,7 @@ for await (const record of myStream) {
|
|
|
226
226
|
}
|
|
227
227
|
|
|
228
228
|
const myStreamJSON = new DynamoDBStream({
|
|
229
|
-
table:
|
|
229
|
+
table: "my-table",
|
|
230
230
|
...awsCreds,
|
|
231
231
|
JSON: true,
|
|
232
232
|
})
|
|
@@ -242,25 +242,25 @@ for await (const record of myStreamJSON) {
|
|
|
242
242
|
|
|
243
243
|
## [Download and upload with AWS S3](https://presidium.services/docs/S3Bucket)
|
|
244
244
|
```javascript
|
|
245
|
-
const S3Bucket = require(
|
|
246
|
-
const AwsCredentials = require(
|
|
245
|
+
const S3Bucket = require("presidium/S3Bucket")
|
|
246
|
+
const AwsCredentials = require("presidium/AwsCredentials")
|
|
247
247
|
|
|
248
|
-
const awsCreds = await AwsCredentials(
|
|
249
|
-
awsCreds.region =
|
|
248
|
+
const awsCreds = await AwsCredentials("default")
|
|
249
|
+
awsCreds.region = "us-east-1"
|
|
250
250
|
|
|
251
251
|
const myBucket = new S3Bucket({
|
|
252
|
-
name:
|
|
252
|
+
name: "my-bucket",
|
|
253
253
|
...awsCreds,
|
|
254
254
|
})
|
|
255
255
|
await myBucket.ready
|
|
256
256
|
|
|
257
|
-
await myBucket.putObject(
|
|
258
|
-
ContentType:
|
|
257
|
+
await myBucket.putObject("some-key", "{"hello":"world"}", {
|
|
258
|
+
ContentType: "application/json",
|
|
259
259
|
})
|
|
260
260
|
|
|
261
|
-
const data = await myBucket.getObject(
|
|
261
|
+
const data = await myBucket.getObject("some-key")
|
|
262
262
|
console.log(data)
|
|
263
|
-
// { Etag:
|
|
263
|
+
// { Etag: "...", Body: <Buffer 7b 22 68 ...>, ContentType: "application/json" }
|
|
264
264
|
|
|
265
265
|
await myBucket.deleteAllObjects()
|
|
266
266
|
await myBucket.delete()
|
|
@@ -268,19 +268,19 @@ await myBucket.delete()
|
|
|
268
268
|
|
|
269
269
|
## [Build and push Docker images](https://presidium.services/docs/Docker)
|
|
270
270
|
```javascript
|
|
271
|
-
const Docker = require(
|
|
272
|
-
const NpmToken = require(
|
|
273
|
-
const fs = require(
|
|
271
|
+
const Docker = require("presidium/Docker")
|
|
272
|
+
const NpmToken = require("presidium/NpmToken")
|
|
273
|
+
const fs = require("fs")
|
|
274
274
|
|
|
275
|
-
const myImage =
|
|
275
|
+
const myImage = "my-app:1.0.0"
|
|
276
276
|
|
|
277
|
-
const npmrc = fs.createWriteStream(
|
|
277
|
+
const npmrc = fs.createWriteStream(".npmrc")
|
|
278
278
|
npmrc.write(`//registry.npmjs.org/:_authToken=${await NpmToken()}`)
|
|
279
279
|
npmrc.end()
|
|
280
280
|
|
|
281
281
|
const buildStream = await docker.buildImage(__dirname, {
|
|
282
282
|
image: myImage,
|
|
283
|
-
ignore: [
|
|
283
|
+
ignore: [".github", "node_modules"],
|
|
284
284
|
archive: {
|
|
285
285
|
Dockerfile: `
|
|
286
286
|
FROM node:15-alpine
|
|
@@ -297,10 +297,10 @@ CMD ["npm", "start"]
|
|
|
297
297
|
|
|
298
298
|
buildStream.pipe(process.stdout)
|
|
299
299
|
|
|
300
|
-
buildStream.on(
|
|
300
|
+
buildStream.on("end", () => {
|
|
301
301
|
const pushStream = await docker.pushImage({
|
|
302
302
|
image: myImage,
|
|
303
|
-
repository:
|
|
303
|
+
repository: "my-registry.io",
|
|
304
304
|
})
|
|
305
305
|
pushStream.pipe(process.stdout)
|
|
306
306
|
})
|
|
@@ -309,14 +309,14 @@ buildStream.on('end', () => {
|
|
|
309
309
|
|
|
310
310
|
## [Run Docker containers](https://presidium.services/docs/Docker)
|
|
311
311
|
```javascript
|
|
312
|
-
const Docker = require(
|
|
312
|
+
const Docker = require("presidium/Docker")
|
|
313
313
|
|
|
314
314
|
const docker = new Docker()
|
|
315
315
|
|
|
316
316
|
const runStream = await docker.runContainer({
|
|
317
|
-
image:
|
|
318
|
-
env: { FOO:
|
|
319
|
-
cmd: [
|
|
317
|
+
image: "node:15-alpine",
|
|
318
|
+
env: { FOO: "Example" },
|
|
319
|
+
cmd: ["node", "-e", "console.log(process.env.FOO)"],
|
|
320
320
|
rm: true,
|
|
321
321
|
})
|
|
322
322
|
|
|
@@ -325,18 +325,18 @@ runStream.pipe(process.stdout) // Example
|
|
|
325
325
|
|
|
326
326
|
## [Deploy Docker Swarm services](https://presidium.services/docs/Docker)
|
|
327
327
|
```javascript
|
|
328
|
-
const Docker = require(
|
|
328
|
+
const Docker = require("presidium/Docker")
|
|
329
329
|
|
|
330
330
|
const docker = new Docker()
|
|
331
331
|
|
|
332
332
|
// initialize docker swarm
|
|
333
|
-
await docker.initSwarm(
|
|
333
|
+
await docker.initSwarm("eth0:2377")
|
|
334
334
|
|
|
335
335
|
await docker.createService({
|
|
336
|
-
name:
|
|
337
|
-
image:
|
|
336
|
+
name: "my-service",
|
|
337
|
+
image: "nginx:1.19",
|
|
338
338
|
publish: { 80: 80 },
|
|
339
|
-
healthCheck: [
|
|
339
|
+
healthCheck: ["curl", "[::1]"],
|
|
340
340
|
replicas: 5,
|
|
341
341
|
})
|
|
342
342
|
// new nginx service is deploying to the docker swarm
|
|
@@ -344,8 +344,8 @@ await docker.createService({
|
|
|
344
344
|
|
|
345
345
|
## [Automate tests with Google Chrome for Testing](https://presidium.services/docs/GoogleChromeDevTools)
|
|
346
346
|
```javascript
|
|
347
|
-
const GoogleChromeForTesting = require(
|
|
348
|
-
const GoogleChromeDevTools = require(
|
|
347
|
+
const GoogleChromeForTesting = require("presidium/GoogleChromeForTesting")
|
|
348
|
+
const GoogleChromeDevTools = require("presidium/GoogleChromeDevTools")
|
|
349
349
|
|
|
350
350
|
const googleChromeForTesting = new GoogleChromeForTesting()
|
|
351
351
|
await googleChromeForTesting.init() // downloads Google Chrome for Testing
|
|
@@ -355,7 +355,7 @@ await googleChromeDevTools.init() // connects to the DevTools server
|
|
|
355
355
|
|
|
356
356
|
// get targets
|
|
357
357
|
const targetsData = await googleChromeDevTools.Target.getTargets()
|
|
358
|
-
const pageTarget = targetsData.result.targetInfos.find(info => info.type ==
|
|
358
|
+
const pageTarget = targetsData.result.targetInfos.find(info => info.type == "page")
|
|
359
359
|
|
|
360
360
|
// attach to target
|
|
361
361
|
const attachToTargetData = await googleChromeDevTools.Target.attachToTarget({
|
|
@@ -371,39 +371,39 @@ const data = await googleChromeDevTools.Page.navigate({
|
|
|
371
371
|
})
|
|
372
372
|
```
|
|
373
373
|
|
|
374
|
-
## [Store data on disk
|
|
374
|
+
## [Store data on disk in a hash table](https://presidium.services/docs/DiskHashTable)
|
|
375
375
|
```javascript
|
|
376
|
-
const DiskHashTable = require(
|
|
376
|
+
const DiskHashTable = require("presidium/DiskHashTable")
|
|
377
377
|
|
|
378
378
|
const ht = new DiskHashTable({
|
|
379
|
-
storageFilepath:
|
|
380
|
-
headerFilepath:
|
|
379
|
+
storageFilepath: "/path/to/ht-storage-file",
|
|
380
|
+
headerFilepath: "/path/to/ht-header-file",
|
|
381
381
|
initialLength: 1024,
|
|
382
382
|
})
|
|
383
383
|
await ht.init()
|
|
384
384
|
|
|
385
|
-
await ht.set(
|
|
385
|
+
await ht.set("my-key", "my-value")
|
|
386
386
|
|
|
387
|
-
const myValue = await ht.get(
|
|
388
|
-
console.log(myValue) //
|
|
387
|
+
const myValue = await ht.get("my-key")
|
|
388
|
+
console.log(myValue) // my-value
|
|
389
389
|
|
|
390
|
-
await ht.delete(
|
|
390
|
+
await ht.delete("my-key")
|
|
391
391
|
```
|
|
392
392
|
|
|
393
|
-
## [Store data on disk
|
|
393
|
+
## [Store data on disk in a sorted hash table](https://presidium.services/docs/DiskSortedHashTable)
|
|
394
394
|
```javascript
|
|
395
|
-
const DiskSortedHashTable = require(
|
|
395
|
+
const DiskSortedHashTable = require("presidium/DiskSortedHashTable")
|
|
396
396
|
|
|
397
397
|
const sortedHt = new DiskSortedHashTable({
|
|
398
|
-
storageFilepath:
|
|
399
|
-
headerFilepath:
|
|
398
|
+
storageFilepath: "/path/to/sortedHt-storage-file",
|
|
399
|
+
headerFilepath: "/path/to/sortedHt-header-file",
|
|
400
400
|
initialLength: 1024,
|
|
401
401
|
})
|
|
402
402
|
await sortedHt.init()
|
|
403
403
|
|
|
404
|
-
await sortedHt.set(
|
|
405
|
-
await sortedHt.set(
|
|
406
|
-
await sortedHt.set(
|
|
404
|
+
await sortedHt.set("first-key", "first-value", 1)
|
|
405
|
+
await sortedHt.set("second-key", "second-value", 2)
|
|
406
|
+
await sortedHt.set("third-key", "third-value", 3)
|
|
407
407
|
|
|
408
408
|
for await (const value of sortedHt.forwardIterator()) {
|
|
409
409
|
console.log(value) // first-value
|
|
@@ -428,6 +428,76 @@ for await (const value of sortedHt.reverseIterator({ startingSortValue: 2, endin
|
|
|
428
428
|
}
|
|
429
429
|
```
|
|
430
430
|
|
|
431
|
+
## [Host your own distributed database](https://presidium.services/docs/DatabaseServer)
|
|
432
|
+
```javascript
|
|
433
|
+
const DatabaseServer = require("presidium/DatabaseServer")
|
|
434
|
+
const DiskHashTableClient = require("presidium/DiskHashTableClient")
|
|
435
|
+
|
|
436
|
+
const server = new DatabaseServer({
|
|
437
|
+
databasePath: `${__dirname}/database`,
|
|
438
|
+
})
|
|
439
|
+
await server.init()
|
|
440
|
+
|
|
441
|
+
server.listen(5000, () => {
|
|
442
|
+
console.log("Database server listening on port 5000")
|
|
443
|
+
})
|
|
444
|
+
|
|
445
|
+
const sessionHt = new DiskHashTableClient({
|
|
446
|
+
serverUrl: "http://localhost:5000",
|
|
447
|
+
tableName: "Session",
|
|
448
|
+
})
|
|
449
|
+
|
|
450
|
+
const accessToken = "token"
|
|
451
|
+
const session = { accessToken }
|
|
452
|
+
|
|
453
|
+
await sessionHt.set(accessToken, JSON.stringify(session))
|
|
454
|
+
|
|
455
|
+
const sessionJson = await sessionHt.get(accessToken)
|
|
456
|
+
|
|
457
|
+
await sessionHt.delete(accessToken)
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
## [Query your distributed database](https://presidium.services/docs/DiskSortedHashTableClient)
|
|
461
|
+
```javascript
|
|
462
|
+
const DiskSortedHashTableClient = require("presidium/DiskSortedHashTableClient")
|
|
463
|
+
|
|
464
|
+
const postHt = new DiskSortedHashTableClient({
|
|
465
|
+
serverUrl: "http://localhost:5000",
|
|
466
|
+
tableName: "Post",
|
|
467
|
+
sortValueType: "number",
|
|
468
|
+
})
|
|
469
|
+
|
|
470
|
+
const post1 = {
|
|
471
|
+
id: "post-1",
|
|
472
|
+
images: ["/assets/post-1/image-1.jpg"],
|
|
473
|
+
content: "First post",
|
|
474
|
+
time: Date.now(),
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
const post2 = {
|
|
478
|
+
id: "post-2",
|
|
479
|
+
images: ["/assets/post-2/image-2.jpg"],
|
|
480
|
+
content: "Second post",
|
|
481
|
+
time: Date.now(),
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const post3 = {
|
|
485
|
+
id: "post-3",
|
|
486
|
+
images: ["/assets/post-3/image-3.jpg"],
|
|
487
|
+
content: "Third post",
|
|
488
|
+
time: Date.now(),
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
await postHt.set(post1.id, JSON.stringify(post1), post1.time)
|
|
492
|
+
await postHt.set(post2.id, JSON.stringify(post2), post2.time)
|
|
493
|
+
await postHt.set(post3.id, JSON.stringify(post3), post3.time)
|
|
494
|
+
|
|
495
|
+
const myPosts = []
|
|
496
|
+
for await (const postJson of postHt.reverseIterator()) {
|
|
497
|
+
myPosts.push(JSON.parse(postJson))
|
|
498
|
+
}
|
|
499
|
+
```
|
|
500
|
+
|
|
431
501
|
## License
|
|
432
502
|
Presidium is distributed under the [CFOSS License](https://cloutsworld.com/en-us/legal/license/cfoss).
|
|
433
503
|
|