@unchainedshop/mongodb 4.8.29 → 5.0.0-alpha.10
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 +21 -15
- package/lib/initDb.js +2 -2
- package/package.json +7 -4
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 |
|
|
@@ -71,6 +67,16 @@ await stopDb(db);
|
|
|
71
67
|
| `MigrationRepository` | Interface for managing migrations |
|
|
72
68
|
| `ModuleInput` | Interface for core module initialization input |
|
|
73
69
|
|
|
70
|
+
## Environment Variables
|
|
71
|
+
|
|
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.
|
|
79
|
+
|
|
74
80
|
## Best Practices
|
|
75
81
|
|
|
76
82
|
### Collection Naming Conventions
|
|
@@ -113,7 +119,7 @@ Queries should filter by `deleted: null` to exclude soft-deleted documents.
|
|
|
113
119
|
|
|
114
120
|
#### Sparse Indexes
|
|
115
121
|
|
|
116
|
-
Use sparse indexes when the indexed field
|
|
122
|
+
Use sparse indexes when the indexed field is absent from most documents:
|
|
117
123
|
|
|
118
124
|
```typescript
|
|
119
125
|
{
|
|
@@ -146,7 +152,7 @@ For full-text search, create compound text indexes:
|
|
|
146
152
|
}
|
|
147
153
|
```
|
|
148
154
|
|
|
149
|
-
|
|
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.
|
|
150
156
|
|
|
151
157
|
## License
|
|
152
158
|
|
package/lib/initDb.js
CHANGED
|
@@ -26,7 +26,7 @@ export const startDb = async (options) => {
|
|
|
26
26
|
try {
|
|
27
27
|
mongod = MongoMemoryServer.create({
|
|
28
28
|
instance: useInMemory
|
|
29
|
-
? { dbName: 'test', port: mongoPort, storageEngine: '
|
|
29
|
+
? { dbName: 'test', port: mongoPort, storageEngine: 'wiredTiger' }
|
|
30
30
|
: {
|
|
31
31
|
dbPath: `${process.cwd()}/.db`,
|
|
32
32
|
storageEngine: 'wiredTiger',
|
|
@@ -45,7 +45,7 @@ export const startDb = async (options) => {
|
|
|
45
45
|
`This usually happens after a MongoDB upgrade. ` +
|
|
46
46
|
`To fix this, remove the .db directory: rm -rf ${process.cwd()}/.db`, { cause: e });
|
|
47
47
|
}
|
|
48
|
-
throw
|
|
48
|
+
throw e;
|
|
49
49
|
}
|
|
50
50
|
throw new Error("Can't connect to MongoDB: Could not start mongodb-memory-server and MONGO_URL env is not set");
|
|
51
51
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/mongodb",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-alpha.10",
|
|
4
|
+
"files": [
|
|
5
|
+
"lib"
|
|
6
|
+
],
|
|
4
7
|
"description": "MongoDB database abstraction layer for the Unchained Engine",
|
|
5
8
|
"main": "lib/mongodb-index.js",
|
|
6
9
|
"types": "lib/mongodb-index.d.ts",
|
|
@@ -33,8 +36,8 @@
|
|
|
33
36
|
},
|
|
34
37
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
35
38
|
"dependencies": {
|
|
36
|
-
"@unchainedshop/logger": "^
|
|
37
|
-
"@unchainedshop/utils": "^
|
|
39
|
+
"@unchainedshop/logger": "^5.0.0-alpha.10",
|
|
40
|
+
"@unchainedshop/utils": "^5.0.0-alpha.10"
|
|
38
41
|
},
|
|
39
42
|
"peerDependencies": {
|
|
40
43
|
"@mongodb-js/zstd": ">= 7 < 8",
|
|
@@ -54,6 +57,6 @@
|
|
|
54
57
|
},
|
|
55
58
|
"devDependencies": {
|
|
56
59
|
"@types/node": "^26.2.0",
|
|
57
|
-
"typescript": "^
|
|
60
|
+
"typescript": "^6.0.3"
|
|
58
61
|
}
|
|
59
62
|
}
|