@toa.io/storages.mongodb 1.0.0-alpha.20 → 1.0.0-alpha.21

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.
Files changed (2) hide show
  1. package/package.json +7 -7
  2. package/src/client.js +16 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/storages.mongodb",
3
- "version": "1.0.0-alpha.20",
3
+ "version": "1.0.0-alpha.21",
4
4
  "description": "Toa MongoDB Storage Connector",
5
5
  "author": "temich <tema.gurtovoy@gmail.com>",
6
6
  "homepage": "https://github.com/toa-io/toa#readme",
@@ -19,13 +19,13 @@
19
19
  "test": "echo \"Error: run tests from root\" && exit 1"
20
20
  },
21
21
  "dependencies": {
22
- "@toa.io/console": "1.0.0-alpha.20",
23
- "@toa.io/conveyor": "1.0.0-alpha.20",
24
- "@toa.io/core": "1.0.0-alpha.20",
25
- "@toa.io/generic": "1.0.0-alpha.20",
26
- "@toa.io/pointer": "1.0.0-alpha.20",
22
+ "@toa.io/console": "1.0.0-alpha.21",
23
+ "@toa.io/conveyor": "1.0.0-alpha.21",
24
+ "@toa.io/core": "1.0.0-alpha.21",
25
+ "@toa.io/generic": "1.0.0-alpha.21",
26
+ "@toa.io/pointer": "1.0.0-alpha.21",
27
27
  "mongodb": "6.3.0",
28
28
  "saslprep": "1.0.3"
29
29
  },
30
- "gitHead": "dcde3389a86ab38e377ebc5ab18e5d5a512b85bc"
30
+ "gitHead": "da9f4c278f6ab02a28f65c6e25c713c013cbfce9"
31
31
  }
package/src/client.js CHANGED
@@ -57,17 +57,27 @@ class Client extends Connector {
57
57
  */
58
58
  async open () {
59
59
  const urls = await this.resolveURLs()
60
- const db = this.resolveDB()
61
- const collection = this.locator.lowercase
60
+ const dbname = this.resolveDB()
61
+ const collname = this.locator.lowercase
62
62
 
63
- this.key = getKey(db, urls)
63
+ this.key = getKey(dbname, urls)
64
64
 
65
65
  INSTANCES[this.key] ??= this.createInstance(urls)
66
66
 
67
67
  this.instance = await INSTANCES[this.key]
68
68
  this.instance.count++
69
69
 
70
- this.collection = this.instance.client.db(db).collection(collection)
70
+ const db = this.instance.client.db(dbname)
71
+
72
+ try {
73
+ this.collection = await db.createCollection(collname)
74
+ } catch (e) {
75
+ if (e.code !== ALREADY_EXISTS) {
76
+ throw e
77
+ }
78
+
79
+ this.collection = db.collection(collname)
80
+ }
71
81
  }
72
82
 
73
83
  /**
@@ -144,4 +154,6 @@ const OPTIONS = {
144
154
  serverSelectionTimeoutMS: 0
145
155
  }
146
156
 
157
+ const ALREADY_EXISTS = 48
158
+
147
159
  exports.Client = Client