assai 0.2.0 → 0.2.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 +11 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ This is a small package to improve some things that you, as a developer, have to
|
|
|
9
9
|
# Example
|
|
10
10
|
|
|
11
11
|
```js
|
|
12
|
-
import {createMongoCollection} from "assai"
|
|
12
|
+
import { createMongoCollection } from "assai"
|
|
13
13
|
|
|
14
14
|
const collection = await createMongoCollection()
|
|
15
15
|
const docs = await collection.find({}, {limit: 10})
|
|
@@ -44,7 +44,9 @@ await collection.insertOne({
|
|
|
44
44
|
Every time you need a new id, you can call the `id` method:
|
|
45
45
|
|
|
46
46
|
```js
|
|
47
|
-
|
|
47
|
+
import { mongo } from 'assai'
|
|
48
|
+
|
|
49
|
+
const myStringId = mongo.generateNewId()
|
|
48
50
|
```
|
|
49
51
|
|
|
50
52
|
One example this could be useful is if have an API endpoint that accepts structured data. And you use these values to query the database. Like so:
|
|
@@ -106,19 +108,21 @@ You could also do this for simplicity, so instead of:
|
|
|
106
108
|
let cachedClient = null
|
|
107
109
|
export async function getDb () {
|
|
108
110
|
if (cachedClient == null) {
|
|
109
|
-
const client = await MongoClient('...')
|
|
111
|
+
const client = await MongoClient.connect('...')
|
|
110
112
|
cachedClient = client
|
|
111
113
|
}
|
|
112
114
|
return cachedClient.db()
|
|
113
115
|
}
|
|
114
116
|
|
|
115
117
|
// router.js
|
|
116
|
-
import {getDb} from 'db.js'
|
|
118
|
+
import { getDb } from 'db.js'
|
|
117
119
|
|
|
118
120
|
router.post('/', (req, res) => {
|
|
119
121
|
const db = await getDb()
|
|
120
|
-
const col db.collection('myCollection')
|
|
121
|
-
|
|
122
|
+
const col = db.collection('myCollection')
|
|
123
|
+
await col.insertOne({
|
|
124
|
+
name: req.body.name,
|
|
125
|
+
})
|
|
122
126
|
})
|
|
123
127
|
```
|
|
124
128
|
|
|
@@ -128,7 +132,7 @@ router.post('/', (req, res) => {
|
|
|
128
132
|
const col = createMongoCollection('myCollection')
|
|
129
133
|
// Here the connection is opened only if it is not opened already.
|
|
130
134
|
// Further calls to this route won't open a connection.
|
|
131
|
-
await
|
|
135
|
+
await col.insertOne({
|
|
132
136
|
name: req.body.name,
|
|
133
137
|
})
|
|
134
138
|
// ...
|