Skip to content

Commit cac8cdf

Browse files
MrZenWCopilot
andauthored
feat: add collectionPrefix option (#88)
* add collection prefix * change the opt name * fix collection name in the index creations * fix: prefix should be removed * Update asyncPersistence.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update asyncPersistence.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * simplify the logic by removing the intermediate object * add the usage of collectionPrefix into doc --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent d12ca1e commit cac8cdf

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ It accepts a connections string `url` or you can pass your existing `db` object.
3535
- `db`: Existing MongoDB instance (if no `url` option is specified)
3636
- `dropExistingIndexes`: Flag used to drop any existing index previously created on collections (except default index `_id`)
3737
- `ttlAfterDisconnected`: Flag used to enable alternative behavior of the subscription ttl, the subscription will expire based on time since client last disconnected from the broker instead of time since the subscription was created.
38+
- `collectionPrefix`: Prefix to use for collection names (default: empty string)
3839

3940
> When changing ttl durations or switching on/off **ttlAfterDisconnected** on an existing database, **dropExistingIndexes** needs to be set to true for ttl indexes to be updated.
4041

asyncPersistence.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,15 @@ class AsyncMongoPersistence {
8888
const databaseName = this.#opts.database || pathname
8989
this.#db = mongoDBclient.db(databaseName)
9090
}
91+
92+
const collectionPrefix = `${this.#opts.collectionPrefix || ''}`
93+
9194
const db = this.#db
92-
const subscriptions = db.collection('subscriptions')
93-
const retained = db.collection('retained')
94-
const will = db.collection('will')
95-
const outgoing = db.collection('outgoing')
96-
const incoming = db.collection('incoming')
95+
const subscriptions = db.collection(`${collectionPrefix}subscriptions`)
96+
const retained = db.collection(`${collectionPrefix}retained`)
97+
const will = db.collection(`${collectionPrefix}will`)
98+
const outgoing = db.collection(`${collectionPrefix}outgoing`)
99+
const incoming = db.collection(`${collectionPrefix}incoming`)
97100
this.#cl = {
98101
subscriptions,
99102
retained,

0 commit comments

Comments
 (0)