@unchainedshop/mongodb 5.0.0-alpha.1 → 5.0.0-alpha.11

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 CHANGED
@@ -14,21 +14,16 @@ npm install @unchainedshop/mongodb
14
14
  ## Usage
15
15
 
16
16
  ```typescript
17
- import { initDb, startDb, stopDb, generateDbObjectId } from '@unchainedshop/mongodb';
17
+ import { initDb, stopDb, generateDbObjectId } from '@unchainedshop/mongodb';
18
18
 
19
- // Initialize the database connection
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
- // Start the database
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(db);
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` | Initialize MongoDB connection with connection string |
41
- | `startDb` | Start the database connection |
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 new MongoDB ObjectId |
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 |
@@ -53,13 +49,6 @@ await stopDb(db);
53
49
  | `findLocalizedText` | Find localized text by locale with fallback |
54
50
  | `insensitiveTrimmedRegexOperator` | Create case-insensitive trimmed regex for text search |
55
51
 
56
- ### DocumentDB Compatibility
57
-
58
- | Export | Description |
59
- |--------|-------------|
60
- | `isDocumentDBCompatModeEnabled` | Check if AWS DocumentDB compatibility mode is enabled |
61
- | `assertDocumentDBCompatMode` | Throw error if DocumentDB compat mode is enabled (for unsupported operations) |
62
-
63
52
  ### Re-exports
64
53
 
65
54
  | Export | Description |
@@ -80,9 +69,13 @@ await stopDb(db);
80
69
 
81
70
  ## Environment Variables
82
71
 
83
- | Variable | Description |
84
- |----------|-------------|
85
- | `UNCHAINED_DOCUMENTDB_COMPAT_MODE` | Enable AWS DocumentDB compatibility mode |
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.
86
79
 
87
80
  ## Best Practices
88
81
 
@@ -126,7 +119,7 @@ Queries should filter by `deleted: null` to exclude soft-deleted documents.
126
119
 
127
120
  #### Sparse Indexes
128
121
 
129
- Use sparse indexes when the indexed field may be null/undefined for most documents:
122
+ Use sparse indexes when the indexed field is absent from most documents:
130
123
 
131
124
  ```typescript
132
125
  {
@@ -159,31 +152,7 @@ For full-text search, create compound text indexes:
159
152
  }
160
153
  ```
161
154
 
162
- **Important:** Text indexes are not supported in DocumentDB compatibility mode. Use `isDocumentDBCompatModeEnabled()` to conditionally create them.
163
-
164
- ### DocumentDB Compatibility
165
-
166
- When running on AWS DocumentDB, some MongoDB features are not supported:
167
-
168
- | Feature | Alternative |
169
- |---------|-------------|
170
- | Text indexes | Use application-level search or external search service |
171
- | `$text` queries | Use regex queries (less performant) |
172
- | Some aggregation operators | Check DocumentDB documentation |
173
-
174
- Use the compatibility helpers:
175
-
176
- ```typescript
177
- import { isDocumentDBCompatModeEnabled, assertDocumentDBCompatMode } from '@unchainedshop/mongodb';
178
-
179
- // Check before using unsupported features
180
- if (!isDocumentDBCompatModeEnabled()) {
181
- // Create text index
182
- }
183
-
184
- // Throw error if DocumentDB mode is enabled
185
- assertDocumentDBCompatMode(); // Throws if in compat mode
186
- ```
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.
187
156
 
188
157
  ## License
189
158
 
@@ -7,21 +7,21 @@ const extendSelectorWithLocale = (selector, locale) => {
7
7
  };
8
8
  export const findLocalizedText = async (collection, selector, locale) => {
9
9
  const exactTranslation = await collection.findOne(extendSelectorWithLocale(selector, locale), {
10
- sort: { updated: -1 },
10
+ sort: { updated: -1, _id: -1 },
11
11
  });
12
12
  if (exactTranslation) {
13
13
  return exactTranslation;
14
14
  }
15
15
  if (systemLocale.baseName !== locale.baseName) {
16
16
  const fallbackTranslation = await collection.findOne(extendSelectorWithLocale(selector, systemLocale), {
17
- sort: { updated: -1 },
17
+ sort: { updated: -1, _id: -1 },
18
18
  });
19
19
  if (fallbackTranslation) {
20
20
  return fallbackTranslation;
21
21
  }
22
22
  }
23
23
  const foundText = await collection.findOne(selector, {
24
- sort: { updated: -1 },
24
+ sort: { updated: -1, _id: -1 },
25
25
  });
26
26
  return foundText;
27
27
  };
package/lib/initDb.js CHANGED
@@ -43,9 +43,9 @@ export const startDb = async (options) => {
43
43
  if (error.message?.includes('code "62"')) {
44
44
  throw new Error(`MongoDB database files in .db are incompatible with the current MongoDB version. ` +
45
45
  `This usually happens after a MongoDB upgrade. ` +
46
- `To fix this, remove the .db directory: rm -rf ${process.cwd()}/.db`, { cause: error });
46
+ `To fix this, remove the .db directory: rm -rf ${process.cwd()}/.db`, { cause: e });
47
47
  }
48
- throw error;
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
  };
@@ -8,7 +8,6 @@ export { findPreservingIds } from './find-preserving-ids.ts';
8
8
  export { buildSortOptions } from './build-sort-option.ts';
9
9
  export { findLocalizedText } from './find-localized-text.ts';
10
10
  export { insensitiveTrimmedRegexOperator, escapeRegexString, } from './insensitive-trimmed-regex-operator.ts';
11
- export * from './documentdb-compat-mode.ts';
12
11
  export { mongodb };
13
12
  export interface LogFields {
14
13
  log: ({
@@ -7,5 +7,4 @@ export { findPreservingIds } from "./find-preserving-ids.js";
7
7
  export { buildSortOptions } from "./build-sort-option.js";
8
8
  export { findLocalizedText } from "./find-localized-text.js";
9
9
  export { insensitiveTrimmedRegexOperator, escapeRegexString, } from "./insensitive-trimmed-regex-operator.js";
10
- export * from "./documentdb-compat-mode.js";
11
10
  export { mongodb };
package/package.json CHANGED
@@ -1,6 +1,9 @@
1
1
  {
2
2
  "name": "@unchainedshop/mongodb",
3
- "version": "5.0.0-alpha.1",
3
+ "version": "5.0.0-alpha.11",
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,7 +36,8 @@
33
36
  },
34
37
  "homepage": "https://github.com/unchainedshop/unchained#readme",
35
38
  "dependencies": {
36
- "@unchainedshop/utils": "^5.0.0-alpha.1"
39
+ "@unchainedshop/logger": "^5.0.0-alpha.11",
40
+ "@unchainedshop/utils": "^5.0.0-alpha.11"
37
41
  },
38
42
  "peerDependencies": {
39
43
  "@mongodb-js/zstd": ">= 7 < 8",
@@ -52,7 +56,7 @@
52
56
  }
53
57
  },
54
58
  "devDependencies": {
55
- "@types/node": "^25.0.0",
56
- "typescript": "^5.8.3"
59
+ "@types/node": "^26.2.0",
60
+ "typescript": "^6.0.3"
57
61
  }
58
62
  }
@@ -1,2 +0,0 @@
1
- export declare function assertDocumentDBCompatMode(): void;
2
- export declare const isDocumentDBCompatModeEnabled: () => boolean;
@@ -1,5 +0,0 @@
1
- export function assertDocumentDBCompatMode() {
2
- if (isDocumentDBCompatModeEnabled())
3
- throw new Error('Text search is not supported in DocumentDB Compatibility Mode');
4
- }
5
- export const isDocumentDBCompatModeEnabled = () => Boolean(process.env.UNCHAINED_DOCUMENTDB_COMPAT_MODE);