@toa.io/storages.mongodb 0.20.0-dev.31 → 0.20.0-dev.34

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/storages.mongodb",
3
- "version": "0.20.0-dev.31",
3
+ "version": "0.20.0-dev.34",
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,11 +19,11 @@
19
19
  "test": "echo \"Error: run tests from root\" && exit 1"
20
20
  },
21
21
  "dependencies": {
22
- "@toa.io/console": "0.20.0-dev.31",
23
- "@toa.io/core": "0.20.0-dev.31",
24
- "@toa.io/generic": "0.20.0-dev.31",
25
- "@toa.io/pointer": "0.20.0-dev.31",
22
+ "@toa.io/console": "0.20.0-dev.34",
23
+ "@toa.io/core": "0.20.0-dev.34",
24
+ "@toa.io/generic": "0.20.0-dev.34",
25
+ "@toa.io/pointer": "0.20.0-dev.34",
26
26
  "mongodb": "4.13.0"
27
27
  },
28
- "gitHead": "f9ad6bf2bab1298b96ca52557b7e36b6041cfc88"
28
+ "gitHead": "7035b1985fe9bb844069308a272d061bfbd38bf0"
29
29
  }
package/src/connection.js CHANGED
@@ -5,40 +5,40 @@
5
5
  const { MongoClient } = require('mongodb')
6
6
  const { Connector } = require('@toa.io/core')
7
7
  const { console } = require('@toa.io/console')
8
+ const { resolve } = require('@toa.io/pointer')
9
+ const { ID } = require('./deployment')
8
10
 
9
- /**
10
- * @implements {toa.mongodb.Connection}
11
- */
12
11
  class Connection extends Connector {
13
- /** @type {toa.mongodb.Pointer} */
14
- #pointer
12
+ #locator
15
13
  /** @type {import('mongodb').MongoClient} */
16
14
  #client
17
15
  /** @type {import('mongodb').Collection<toa.mongodb.Record>} */
18
16
  #collection
19
17
 
20
- /**
21
- * @param {toa.mongodb.Pointer} pointer
22
- */
23
- constructor (pointer) {
18
+ constructor (locator) {
24
19
  super()
25
20
 
26
- this.#pointer = pointer
27
- this.#client = new MongoClient(pointer.reference, OPTIONS)
21
+ this.#locator = locator
28
22
  }
29
23
 
30
24
  async open () {
25
+ const urls = await this.#resolveURLs()
26
+ const db = this.#locator.namespace
27
+ const collection = this.#locator.name
28
+
29
+ this.#client = new MongoClient(urls[0], OPTIONS)
30
+
31
31
  await this.#client.connect()
32
32
 
33
- this.#collection = this.#client.db(this.#pointer.db).collection(this.#pointer.collection)
33
+ this.#collection = this.#client.db(db).collection(collection)
34
34
 
35
- console.info(`Storage Mongo connected to ${this.#pointer.label}/${this.#pointer.db}/${this.#pointer.collection}`)
35
+ console.info('Storage Mongo connected')
36
36
  }
37
37
 
38
38
  async close () {
39
39
  await this.#client.close()
40
40
 
41
- console.info(`Storage Mongo disconnected from ${this.#pointer.label}/${this.#pointer.db}/${this.#pointer.collection}`)
41
+ console.info('Storage Mongo disconnected')
42
42
  }
43
43
 
44
44
  /** @hot */
@@ -79,6 +79,11 @@ class Connection extends Connector {
79
79
  async update (query, update, options) {
80
80
  return this.#collection.findOneAndUpdate(query, update, options)
81
81
  }
82
+
83
+ async #resolveURLs () {
84
+ if (process.env.TOA_DEV === '1') return ['mongodb://developer:secret@localhost']
85
+ else return await resolve(ID, this.#locator.id)
86
+ }
82
87
  }
83
88
 
84
89
  const OPTIONS = {
package/src/deployment.js CHANGED
@@ -1,15 +1,22 @@
1
1
  'use strict'
2
2
 
3
- const pointer = require('@toa.io/pointer')
3
+ const { createVariables } = require('@toa.io/pointer')
4
4
 
5
- /** @type {toa.deployment.dependency.Constructor} */
6
5
  const deployment = (instances, annotation) => {
7
- if (annotation === undefined) throw new Error('MongoDB pointer annotation is required')
6
+ const requests = instances.map((instance) => createRequest(instance))
7
+ const variables = createVariables(ID, annotation, requests)
8
8
 
9
- return pointer.deployment(instances, annotation, OPTIONS)
9
+ return { variables }
10
10
  }
11
11
 
12
- /** @type {toa.pointer.deployment.Options} */
13
- const OPTIONS = { prefix: 'storages-mongodb' }
12
+ function createRequest (instance) {
13
+ return {
14
+ group: instance.locator.label,
15
+ selectors: [instance.locator.id]
16
+ }
17
+ }
18
+
19
+ const ID = 'mongodb'
14
20
 
21
+ exports.ID = ID
15
22
  exports.deployment = deployment
package/src/factory.js CHANGED
@@ -1,16 +1,11 @@
1
1
  'use strict'
2
2
 
3
- const { Pointer } = require('./pointer')
4
3
  const { Connection } = require('./connection')
5
4
  const { Storage } = require('./storage')
6
5
 
7
- /**
8
- * @implements {toa.core.storages.Factory}
9
- */
10
6
  class Factory {
11
7
  storage (locator) {
12
- const pointer = new Pointer(locator)
13
- const connection = new Connection(pointer)
8
+ const connection = new Connection(locator)
14
9
 
15
10
  return new Storage(connection)
16
11
  }
package/src/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  'use strict'
2
2
 
3
- const { annotation } = require('./annotation')
4
3
  const { deployment } = require('./deployment')
5
4
  const { Factory } = require('./factory')
6
5
 
7
- exports.annotation = annotation
8
6
  exports.deployment = deployment
9
7
  exports.Factory = Factory
package/src/annotation.js DELETED
@@ -1,5 +0,0 @@
1
- 'use strict'
2
-
3
- const { uris } = require('@toa.io/pointer')
4
-
5
- exports.annotation = uris.construct
package/src/pointer.js DELETED
@@ -1,28 +0,0 @@
1
- 'use strict'
2
-
3
- const { Pointer: Base } = require('@toa.io/pointer')
4
-
5
- // noinspection JSClosureCompilerSyntax
6
- /**
7
- * @implements {toa.mongodb.Pointer}
8
- */
9
- class Pointer extends Base {
10
- db
11
- collection
12
-
13
- /**
14
- * @param {toa.core.Locator} locator
15
- */
16
- constructor (locator) {
17
- super(PREFIX, locator, OPTIONS)
18
-
19
- this.db = locator.namespace
20
- this.collection = locator.name
21
- }
22
- }
23
-
24
- /** @type {toa.pointer.Options} */
25
- const OPTIONS = { protocol: 'mongodb:' }
26
- const PREFIX = 'storages-mongodb'
27
-
28
- exports.Pointer = Pointer
@@ -1,16 +0,0 @@
1
- 'use strict'
2
-
3
- const { generate } = require('randomstring')
4
-
5
- const mock = { uris: { construct: () => generate() }, Pointer: class {} }
6
-
7
- jest.mock('@toa.io/pointer', () => mock)
8
- const { annotation } = require('../')
9
-
10
- it('should export annotations', () => {
11
- expect(annotation).toBeDefined()
12
- })
13
-
14
- it('should export connectors.uris.construct', () => {
15
- expect(annotation).toStrictEqual(mock.uris.construct)
16
- })
@@ -1,13 +0,0 @@
1
- 'use strict'
2
-
3
- const { deployment } = require('../')
4
-
5
- it('should be', () => {
6
- expect(deployment).toBeDefined()
7
- })
8
-
9
- it('should throw if annotation is not defined', () => {
10
- const instances = []
11
-
12
- expect(() => deployment(instances, undefined)).toThrow('is required')
13
- })
@@ -1,18 +0,0 @@
1
- 'use strict'
2
-
3
- const { generate } = require('randomstring')
4
-
5
- const Connection = jest.fn().mockImplementation(function () {})
6
- const Storage = jest.fn().mockImplementation(function () {})
7
-
8
- /** @type {toa.core.Locator} */ const locator = {
9
- name: generate(),
10
- namespace: generate(),
11
- id: generate(),
12
- label: generate(),
13
- uppercase: generate().toUpperCase(),
14
- hostname: jest.fn(() => generate())
15
- }
16
-
17
- exports.mock = { Connection, Storage }
18
- exports.locator = locator
@@ -1,37 +0,0 @@
1
- 'use strict'
2
-
3
- const { encode } = require('@toa.io/generic')
4
-
5
- const fixtures = require('./factory.fixtures')
6
- const mock = fixtures.mock
7
-
8
- jest.mock('../src/storage', () => ({ Storage: mock.Storage }))
9
- jest.mock('../src/connection', () => ({ Connection: mock.Connection }))
10
-
11
- const { Factory } = require('../src/')
12
-
13
- /** @type {toa.core.storages.Factory} */
14
- let factory
15
-
16
- const uris = { default: 'mongodb://whatever' }
17
- const value = encode(uris)
18
-
19
- process.env.TOA_STORAGES_MONGODB_POINTER = value
20
-
21
- beforeEach(() => {
22
- factory = new Factory()
23
- })
24
-
25
- it('should create url', () => {
26
- factory.storage(fixtures.locator)
27
-
28
- expect(mock.Connection).toHaveBeenCalled()
29
-
30
- const instance = mock.Connection.mock.instances[0]
31
- /** @type {toa.mongodb.Pointer} */
32
- const pointer = mock.Connection.mock.calls[0][0]
33
-
34
- expect(mock.Storage).toHaveBeenLastCalledWith(instance)
35
-
36
- expect(pointer).toBeDefined()
37
- })
@@ -1,50 +0,0 @@
1
- 'use strict'
2
-
3
- const { generate } = require('randomstring')
4
- const { Locator } = require('@toa.io/core')
5
- const { encode } = require('@toa.io/generic')
6
-
7
- const { Pointer } = require('../src/pointer')
8
-
9
- it('should be', () => undefined)
10
-
11
- /** @type {toa.core.Locator} */
12
- let locator
13
-
14
- /** @type {toa..Pointer} */
15
- let pointer
16
-
17
- const uris = { default: 'mongodb://whatever' }
18
- const value = encode(uris)
19
-
20
- process.env.TOA_STORAGES_MONGODB_POINTER = value
21
-
22
- beforeEach(() => {
23
- const name = generate()
24
- const namespace = generate()
25
-
26
- locator = new Locator(name, namespace)
27
- pointer = new Pointer(locator)
28
- })
29
-
30
- it('should define schema', () => {
31
- expect(pointer.protocol).toStrictEqual('mongodb:')
32
- })
33
-
34
- it('should expose db', () => {
35
- expect(pointer.db).toStrictEqual(locator.namespace)
36
- })
37
-
38
- it('should expose collection', () => {
39
- expect(pointer.collection).toStrictEqual(locator.name)
40
- })
41
-
42
- it('should define schema on local environment', () => {
43
- process.env.TOA_DEV = '1'
44
-
45
- expect(() => (pointer = new Pointer(locator))).not.toThrow()
46
-
47
- expect(pointer.protocol).toStrictEqual('mongodb:')
48
-
49
- delete process.env.TOA_DEV
50
- })