@unchainedshop/mongodb 5.0.0-alpha.5 → 5.0.0-alpha.8
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 +18 -16
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -14,21 +14,16 @@ npm install @unchainedshop/mongodb
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
|
16
16
|
```typescript
|
|
17
|
-
import { initDb,
|
|
17
|
+
import { initDb, stopDb, generateDbObjectId } from '@unchainedshop/mongodb';
|
|
18
18
|
|
|
19
|
-
//
|
|
20
|
-
const db = await initDb(
|
|
21
|
-
connectionString: 'mongodb://localhost:27017/unchained',
|
|
22
|
-
});
|
|
19
|
+
// Connect to MONGO_URL, or start a local MongoDB instance when it is unset
|
|
20
|
+
const db = await initDb();
|
|
23
21
|
|
|
24
|
-
//
|
|
25
|
-
await startDb(db);
|
|
26
|
-
|
|
27
|
-
// Generate a new ObjectId
|
|
22
|
+
// Generate a random hexadecimal string ID (24 characters by default)
|
|
28
23
|
const id = generateDbObjectId();
|
|
29
24
|
|
|
30
25
|
// Stop the database when shutting down
|
|
31
|
-
await stopDb(
|
|
26
|
+
await stopDb();
|
|
32
27
|
```
|
|
33
28
|
|
|
34
29
|
## API Overview
|
|
@@ -37,15 +32,16 @@ await stopDb(db);
|
|
|
37
32
|
|
|
38
33
|
| Export | Description |
|
|
39
34
|
|--------|-------------|
|
|
40
|
-
| `initDb` |
|
|
41
|
-
| `startDb` | Start
|
|
35
|
+
| `initDb` | Connect using `MONGO_URL`, or start and connect to local MongoDB |
|
|
36
|
+
| `startDb` | Start a local MongoDB server and return its connection URL |
|
|
42
37
|
| `stopDb` | Close the database connection |
|
|
38
|
+
| `createDatabaseResource` | Create an async-disposable database resource |
|
|
43
39
|
|
|
44
40
|
### Query Utilities
|
|
45
41
|
|
|
46
42
|
| Export | Description |
|
|
47
43
|
|--------|-------------|
|
|
48
|
-
| `generateDbObjectId` | Generate a
|
|
44
|
+
| `generateDbObjectId` | Generate a random hexadecimal string ID |
|
|
49
45
|
| `generateDbFilterById` | Create a filter object for querying by ID |
|
|
50
46
|
| `buildDbIndexes` | Create indexes for a collection |
|
|
51
47
|
| `findPreservingIds` | Find documents while preserving ID order |
|
|
@@ -73,7 +69,13 @@ await stopDb(db);
|
|
|
73
69
|
|
|
74
70
|
## Environment Variables
|
|
75
71
|
|
|
76
|
-
|
|
72
|
+
| Variable | Purpose |
|
|
73
|
+
|----------|---------|
|
|
74
|
+
| `MONGO_URL` | External MongoDB connection URL; when unset, start local MongoDB |
|
|
75
|
+
| `PORT` | Local MongoDB defaults to this port plus one (`4011` if unset) |
|
|
76
|
+
| `NODE_ENV` | `test` selects ephemeral storage for local MongoDB |
|
|
77
|
+
|
|
78
|
+
`initDb()`, `startDb()`, and `createDatabaseResource()` accept `{ forceInMemory, port }`. Local non-test data is stored in `.db` under the current working directory; `stopDb()` preserves those persistent files.
|
|
77
79
|
|
|
78
80
|
## Best Practices
|
|
79
81
|
|
|
@@ -117,7 +119,7 @@ Queries should filter by `deleted: null` to exclude soft-deleted documents.
|
|
|
117
119
|
|
|
118
120
|
#### Sparse Indexes
|
|
119
121
|
|
|
120
|
-
Use sparse indexes when the indexed field
|
|
122
|
+
Use sparse indexes when the indexed field is absent from most documents:
|
|
121
123
|
|
|
122
124
|
```typescript
|
|
123
125
|
{
|
|
@@ -150,7 +152,7 @@ For full-text search, create compound text indexes:
|
|
|
150
152
|
}
|
|
151
153
|
```
|
|
152
154
|
|
|
153
|
-
|
|
155
|
+
`buildDbIndexes()` attempts the indexes supplied by each collection, including any text indexes. It does not detect database vendors or versions. Check your database's support for the index definitions used by your modules.
|
|
154
156
|
|
|
155
157
|
## License
|
|
156
158
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/mongodb",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.8",
|
|
4
4
|
"description": "MongoDB database abstraction layer for the Unchained Engine",
|
|
5
5
|
"main": "lib/mongodb-index.js",
|
|
6
6
|
"types": "lib/mongodb-index.d.ts",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
},
|
|
34
34
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@unchainedshop/logger": "^5.0.0-alpha.
|
|
37
|
-
"@unchainedshop/utils": "^5.0.0-alpha.
|
|
36
|
+
"@unchainedshop/logger": "^5.0.0-alpha.8",
|
|
37
|
+
"@unchainedshop/utils": "^5.0.0-alpha.8"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"@mongodb-js/zstd": ">= 7 < 8",
|