Skip to content

Commit 3b1e770

Browse files
committed
0.2.6
1 parent 6146f52 commit 3b1e770

19 files changed

+414
-117
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
database.json
22
database.db
3+
unit.js
34
db.db
45
test/benchmarks/

README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ DnDB is an incredibly fast and powerful data store. All methods are streamed and
4545
**deno.land**
4646

4747
```javascript
48-
import Datastore from 'https://deno.land/x/dndb@0.2.4/mod.ts'
48+
import Datastore from 'https://deno.land/x/dndb@0.2.6/mod.ts'
4949
```
5050

5151
**nest.land**
5252

5353
```javascript
54-
import Datastore from 'https://x.nest.land/dndb@0.2.4/mod.ts'
54+
import Datastore from 'https://x.nest.land/dndb@0.2.6/mod.ts'
5555
```
5656

5757

@@ -71,7 +71,7 @@ All the api methods are asynchronous by default, so they return promises, but it
7171
## ✔️ Instantiating the collection
7272

7373
```javascript
74-
import Datastore from 'https://deno.land/x/dndb@0.2.4/mod.ts'
74+
import Datastore from 'https://deno.land/x/dndb@0.2.6/mod.ts'
7575

7676
const db = new Datastore({ filename:"./database.db", autoload: true })
7777

@@ -83,6 +83,8 @@ When you instantiate a collection you can pass it a config object with a couple
8383

8484
- `autoload`: The autoload option runs the `loadDatabase` method which creates the persistent file the first time DnDB is running in your project, this is optional, but if the loadDatabase method is not executed, the instance will not work until the persistent file exists.
8585

86+
- `bufSize`: The bufSize parameter rewrites the default size of the buffer. It must be indicated in numbers and represents the amount of bytes to be allocated. By default 4096 bytes.
87+
8688
> *Notice*: The configuration content is currently in alpha, more options will be available soon.
8789
8890
## 🖋️ Inserting documents
@@ -156,11 +158,11 @@ db.find({name:"Denyn"}, {}, (docs) => {
156158

157159
// or
158160

159-
let docs = await db.find({name:"Denyn"})
161+
let docs = await db.find({ name: "Denyn" })
160162

161163
// Finding unique document
162164

163-
let docs = await db.findOne({username:"denyncrawford"})
165+
let docs = await db.findOne({ username: "denyncrawford" })
164166

165167
// Deep querying syntax:
166168

demo/README.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@ This demo shows you how to use DnDB in a backend environment.
77
- Run the demo with the permission flags.
88

99
```bash
10-
deno run -A --unstable https://x.nest.land/dndb@0.0.23/demo/mod.js
10+
deno run -A --unstable https://x.nest.land/dndb/0.2.5/demo/mod.js
1111
```
1212

13-
- Just make a post request at /:your_username to add a username in your database
14-
- Make a get request at /:your_username to get the user object.
13+
You can start playing with the new demo UI. Fist try to insert a document typing an username and clicking 'Make Query'.
14+
15+
It is also a REST API, so you can:
16+
17+
- Make a POST request at /:your_username to add a username in your database.
18+
- Make a GET request at /all to get all the users collection.
19+
- Make a GET request at /all/:your_username to get the matching users collection.
20+
- Make a GET request at /:your_username to get the matching user document.
21+
- Make a PUT request at /all/:your_username/:new_username to update all the matching users.
22+
- Make a PUT request at /:your_username/:new_username to update the mathcing user document.
23+
- Make a DELETE request at /all to remove all the users collection.
24+
- Make a DELETE request at /all/:your_username to remove all the matching user documents.
25+
- Make a DELETE request at /:your_username to remove the matching user document.

demo/mod.js

+61-45
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,89 @@
1-
import Datastore from '../mod.ts'
2-
import { Application } from "https://deno.land/x/abc@v1/mod.ts";
1+
import Datastore from "https://deno.land/x/dndb/mod.ts";
2+
import { opine, serveStatic } from "https://deno.land/x/opine@1.0.0/mod.ts";
3+
import { join, dirname } from "../deps.ts";
4+
const __dirname = dirname(import.meta.url);
5+
const app = opine();
6+
const db = new Datastore({ filename: "./database.db", autoload: true });
37

4-
const app = new Application();
8+
app.use(serveStatic(join(__dirname, "public")));
59

6-
const db = new Datastore({filename: "./database.db", autoload:true})
10+
app.get("/", async (req, res) => {
11+
await res.sendFile(join(__dirname, './public/index.html'));
12+
});
713

8-
// Get all the users
14+
// Get all the users from the collection
915

10-
app.get("/all", async ctx => {
11-
let dbResponse = await db.find({})
12-
return JSON.stringify(dbResponse, null, 2);
13-
})
16+
app.get("/all", async (req, res) => {
17+
let doc = await db.find({});
18+
res.send(JSON.stringify(doc, null, 2));
19+
});
1420

1521
// Get all users that match in username
1622

17-
app.get("/all/:match", async ctx => {
18-
const { match } = ctx.params;
19-
let dbResponse = await db.find({username: match})
20-
return JSON.stringify(dbResponse, null, 2);
21-
})
23+
app.get("/all/:match", async (req, res) => {
24+
const { match } = req.params;
25+
let doc = await db.find({ username: match });
26+
res.send(JSON.stringify(doc, null, 2));
27+
});
2228

2329
// Get exact first match user
2430

25-
app.get("/:username", async (ctx) => {
26-
const { username } = ctx.params;
27-
let dbResponse = await db.findOne({username}, {username:0})
28-
return JSON.stringify(dbResponse, null, 2);
31+
app.get("/:username", async (req, res) => {
32+
const { username } = req.params;
33+
let doc = await db.findOne({ username }, { username: 0 });
34+
res.send(JSON.stringify(doc, null, 2));
2935
});
3036

3137
// Post a new user
3238

33-
app.post("/:username", async (ctx) => {
34-
const { username } = ctx.params;
35-
let doc = await db.insert({username, name:username})
36-
return JSON.stringify(doc, null, 2)
37-
})
39+
app.post("/:username", async (req, res) => {
40+
const { username } = req.params;
41+
let doc = await db.insert({ username, name: username });
42+
res.send(JSON.stringify(doc, null, 2));
43+
});
3844

3945
// Delete one user
4046

41-
app.delete("/:username", async (ctx) => {
42-
const { username } = ctx.params;
43-
let doc = await db.removeOne({username})
44-
return JSON.stringify(doc, null, 2)
45-
})
47+
app.delete("/:username", async (req, res) => {
48+
const { username } = req.params;
49+
let doc = await db.removeOne({ username });
50+
res.send(JSON.stringify(doc, null, 2));
51+
});
52+
53+
// Delete all users that matches
54+
55+
app.delete("/all/:username", async (req, res) => {
56+
const { username } = req.params;
57+
let doc = await db.remove({ username });
58+
res.send(JSON.stringify(doc, null, 2));
59+
});
4660

4761
// Delete all users
4862

49-
app.delete("/all/:username", async (ctx) => {
50-
const { username } = ctx.params;
51-
let doc = await db.remove({username})
52-
return JSON.stringify(doc, null, 2)
53-
})
63+
app.delete("/all", async (req, res) => {
64+
let doc = await db.remove({});
65+
res.send(JSON.stringify(doc, null, 2));
66+
});
5467

5568
// Update first user that match username
5669

57-
app.put("/:username/:newUsername", async (ctx) => {
58-
const { username, newUsername } = ctx.params;
59-
let doc = await db.updateOne({username}, {$set: {username:newUsername}})
60-
return JSON.stringify(doc, null, 2)
61-
})
70+
app.put("/:username/:newUsername", async (req, res) => {
71+
const { username, newUsername } = req.params;
72+
let doc = await db.updateOne(
73+
{ username },
74+
{ $set: { username: newUsername } }
75+
);
76+
res.send(JSON.stringify(doc, null, 2));
77+
});
6278

6379
// Update all users that match username
6480

65-
app.put("/all/:username/:newUsername", async (ctx) => {
66-
const { username, newUsername } = ctx.params;
67-
let doc = await db.update({username}, {$set: {username:newUsername}})
68-
return JSON.stringify(doc, null, 2)
69-
})
81+
app.put("/all/:username/:newUsername", async (req, res) => {
82+
const { username, newUsername } = req.params;
83+
let doc = await db.update({ username }, { $set: { username: newUsername } });
84+
res.send(JSON.stringify(doc, null, 2));
85+
});
7086

71-
app.start({ port: 3000 });
87+
app.listen(3000);
7288

73-
console.log("http://localhost:3000");
89+
console.log("http://localhost:3000");

demo/public/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
7+
<title>DnDB - The NoSql database for Deno</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
12+
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
13+
<script src="main.js"></script>
14+
</body>
15+
</html>

demo/public/main.js

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
const app = new Vue({
2+
el: "#app",
3+
template: `
4+
<div class="p-10">
5+
<h1 class="text-3xl max-w-prose">Please select one of the methods bellow before making a query to use the datastore in this demo</h1>
6+
<p class="mt-2 max-w-prose">This is a simple server (REST API) implementation for DnDB. You can insert, find, update and remove the typed username.</p>
7+
<p class="mt-2 max-w-prose">- To make an update, type the old username and the new one, separated with a comma ','. <br>- If you want to fetch all users,
8+
just make an empty query with the find method</p>
9+
<select ref="method" class="mt-5 p-2 rounded border border-black">
10+
<option v-for="(option, i) in options" :key="i">
11+
{{option}}
12+
</option>
13+
</select>
14+
<input @keydown="error = ''; success = ''" ref="query" type="text" class="ml-5 w-60 mt-5 p-2 rounded border border-black" placeholder="Type an username">
15+
<button @click="makeQuery" class="bg-black mt-5 ml-2 p-2 rounded text-white">Make Query</button>
16+
<p v-show="error" class="mt-5 text-red-600">Error: {{error}}</p>
17+
<p v-show="success" class="mt-5 text-green-600">Success: {{success}}</p>
18+
<pre class="max-w-prose mt-5 p-5 rounded bg-gray-100">{{lastResponse}}</pre>
19+
</div>
20+
`,
21+
data() {
22+
return {
23+
options: [
24+
'find',
25+
'findOne',
26+
'insert',
27+
'update',
28+
'updateOne',
29+
'remove',
30+
'removeOne'
31+
],
32+
lastResponse: '',
33+
error: "",
34+
success: ""
35+
}
36+
},
37+
async mounted() {
38+
let {
39+
data
40+
} = await axios('/all')
41+
this.lastResponse = data;
42+
},
43+
methods: {
44+
updateResponse(response) {
45+
this.lastResponse = response
46+
},
47+
async makeQuery() {
48+
const method = this.$refs.method.value
49+
const query = this.$refs.query.value
50+
switch (method) {
51+
case 'find':
52+
let findData;
53+
if (!query.length)
54+
findData = await axios('/all')
55+
else
56+
findData = await axios(`/all/${query}`)
57+
this.updateResponse(findData.data)
58+
this.success = `Fetched multiple documents matching the query: ${query.length ? query : 'all'}.`
59+
break;
60+
case 'findOne':
61+
let {
62+
data: findOneData
63+
} = await axios(`/${query || null}`)
64+
this.updateResponse(findOneData)
65+
this.success = `Fetched one document matching the query: ${query.length ? query : null}.`
66+
break;
67+
case 'insert':
68+
if (!query.length) return this.error = 'You can not insert an empty username (for this demo), please type an username.'
69+
let {
70+
data: insertData
71+
} = await axios.post(`/${query}`)
72+
this.updateResponse(insertData)
73+
this.success = "New document inserted."
74+
break;
75+
case 'update':
76+
if (!query.length) return this.error = 'You can not update an empty username, please type an username and followed by a comma the new update.'
77+
let updateQuery = query.split(',');
78+
if (updateQuery.length !== 2) return this.error = 'You must specify an update, please type an username and followed by a comma the new update.'
79+
let {
80+
data: updateData
81+
} = await axios.put(`/all/${updateQuery[0].trim()}/${updateQuery[1].trim()}`)
82+
this.updateResponse(updateData)
83+
this.success = `All document matching ${updateQuery[0]} updated to ${updateQuery[1]}.`
84+
break;
85+
case 'updateOne':
86+
if (!query.length) return this.error = 'You can not update an empty username, please type an username and followed by a comma the new update.'
87+
let updateOneQuery = query.split(',');
88+
if (updateOneQuery.length !== 2) return this.error = 'You must specify an update, please type an username and followed by a comma the new update.'
89+
let {
90+
data: updateOneData
91+
} = await axios.put(`/${updateOneQuery[0].trim()}/${updateOneQuery[1].trim()}`)
92+
this.updateResponse(updateOneData)
93+
this.success = `The first document matching ${updateOneQuery[0]} updated to ${updateOneQuery[1]}.`
94+
break;
95+
case 'remove':
96+
let removedData;
97+
if (!query.length)
98+
removedData = await axios.delete('/all')
99+
else
100+
removedData = await axios.delete(`/all/${query}`)
101+
this.updateResponse(removedData.data)
102+
this.success = `Removed multiple documents matching the query: ${query.length ? query : 'all'}.`
103+
break;
104+
case 'removeOne':
105+
let { data: removeOneData } = await axios.delete(`/${query}`);
106+
this.updateResponse(removeOneData)
107+
this.success = `Removed first documents matching the query: ${query}.`
108+
break;
109+
default:
110+
break;
111+
}
112+
}
113+
},
114+
})

deps.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import mongobj from "https://cdn.skypack.dev/mongobj"
22
import { matches } from 'https://raw.githubusercontent.com/denyncrawford/safe-filter/master/dist/index.js'
33
import project from 'https://raw.githubusercontent.com/denyncrawford/mongo-project.node/master/dist/bundle.js'
4+
import { EventEmitter } from "https://deno.land/std/node/events.ts";
5+
import { BufReader } from "https://deno.land/std/io/bufio.ts";
6+
import { existsSync } from "https://deno.land/std/fs/mod.ts";
7+
import { resolve, dirname, join } from 'https://deno.land/std/path/mod.ts';
48

59
export {
610
mongobj,
711
matches,
8-
project
12+
project,
13+
EventEmitter,
14+
BufReader,
15+
existsSync,
16+
resolve,
17+
dirname,
18+
join
919
}

egg.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
"description": "A Deno 🦕 persistent database for JS & TS",
44
"stable": true,
55
"entry": "./mod.ts",
6-
"version": "0.2.4",
6+
"version": "0.2.6",
77
"repository": "https://github.com/denyncrawford/dndb",
88
"files": [
99
"./mod.ts",
1010
"./src/*",
1111
"./src/methods/*",
1212
"./src/types/*",
1313
"./demo/*",
14+
"./demo/public/*",
1415
"README.md",
1516
"deps.ts"
1617
],

src/methods/find.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { matches, project } from '../../deps.ts';
22
import { ReadFileStream } from '../storage.ts';
33

4-
export default async (filename, query, projection) => {
5-
let stream = new ReadFileStream(filename);
4+
export default async (filename, query, projection, bufSize) => {
5+
const stream = new ReadFileStream(filename, bufSize);
66
let found = [];
77
query = query || {};
88
stream.on('document', obj => {

0 commit comments

Comments
 (0)