presidium-db-client-server 0.1.0 → 0.1.2
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/DatabaseServer.js +12 -1
- package/README.md +52 -0
- package/handleServerResponse.js +0 -3
- package/package.json +1 -1
package/DatabaseServer.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Presidium DB Client and Server may be freely distributed under the CFOSS license.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
require("rubico/global")
|
|
5
6
|
const HTTP = require("presidium/HTTP")
|
|
6
7
|
const DiskHashTable = require("presidium/DiskHashTable")
|
|
7
8
|
const DatabaseHandler = require("./DatabaseHandler")
|
|
@@ -65,7 +66,17 @@ class DatabaseServer {
|
|
|
65
66
|
hts: this.hts,
|
|
66
67
|
})
|
|
67
68
|
|
|
68
|
-
|
|
69
|
+
const handler = tryCatch(
|
|
70
|
+
databaseHandler,
|
|
71
|
+
function errorHandler(error, request, response) {
|
|
72
|
+
response.writeHead(error.code ?? 500, {
|
|
73
|
+
"Content-Type": "text/plain",
|
|
74
|
+
})
|
|
75
|
+
response.end(error.message)
|
|
76
|
+
}
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
this.server = HTTP.Server(handler)
|
|
69
80
|
}
|
|
70
81
|
|
|
71
82
|
/**
|
package/README.md
CHANGED
|
@@ -38,6 +38,58 @@ const sessionJson = await sessionHt.get(accessToken)
|
|
|
38
38
|
await sessionHt.delete(accessToken)
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
+
Query with DiskSortedHashTableClient.
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
const DatabaseServer = require("presidium-db-client-server/DatabaseServer")
|
|
45
|
+
const DiskSortedHashTableClient = require("presidium-db-client-server/DiskSortedHashTableClient")
|
|
46
|
+
|
|
47
|
+
const server = new DatabaseServer({
|
|
48
|
+
databasePath: `${__dirname}/database`,
|
|
49
|
+
})
|
|
50
|
+
await server.init()
|
|
51
|
+
|
|
52
|
+
server.listen(5000, () => {
|
|
53
|
+
console.log("Database server listening on port 5000")
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const postHt = new DiskSortedHashTableClient({
|
|
57
|
+
serverUrl: "http://localhost:7357",
|
|
58
|
+
tableName: "Post",
|
|
59
|
+
sortValueType: "number",
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const post1 = {
|
|
63
|
+
id: "post-1",
|
|
64
|
+
images: ["/assets/post-1/image-1.jpg"],
|
|
65
|
+
content: "First post",
|
|
66
|
+
time: Date.now(),
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const post2 = {
|
|
70
|
+
id: "post-2",
|
|
71
|
+
images: ["/assets/post-2/image-2.jpg"],
|
|
72
|
+
content: "Second post",
|
|
73
|
+
time: Date.now(),
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const post3 = {
|
|
77
|
+
id: "post-3",
|
|
78
|
+
images: ["/assets/post-3/image-3.jpg"],
|
|
79
|
+
content: "Third post",
|
|
80
|
+
time: Date.now(),
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
await postHt.set(post1.id, JSON.stringify(post1), post1.time)
|
|
84
|
+
await postHt.set(post2.id, JSON.stringify(post2), post2.time)
|
|
85
|
+
await postHt.set(post3.id, JSON.stringify(post3), post3.time)
|
|
86
|
+
|
|
87
|
+
const myPosts = []
|
|
88
|
+
for await (const postJson of postHt.reverseIterator()) {
|
|
89
|
+
myPosts.push(JSON.parse(postJson))
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
41
93
|
## Docs
|
|
42
94
|
Coming soon.
|
|
43
95
|
|
package/handleServerResponse.js
CHANGED
|
@@ -56,9 +56,6 @@ async function handleServerResponse(response, options = {}) {
|
|
|
56
56
|
const stream = new BinaryEntriesStream(response, entryLength, delimiter, encoding)
|
|
57
57
|
return AsyncIterator.fromEvents(stream, "entry")
|
|
58
58
|
}
|
|
59
|
-
if (response.headers["content-type"] == "application/json") {
|
|
60
|
-
return Readable.JSON(response)
|
|
61
|
-
}
|
|
62
59
|
if (response.headers["content-type"] == "application/octet-stream") {
|
|
63
60
|
return Readable.Buffer(response)
|
|
64
61
|
}
|