@toa.io/storages.mongodb 0.1.0-dev.0
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/LICENSE +22 -0
- package/deploy/service.yaml +21 -0
- package/package.json +29 -0
- package/src/client.js +76 -0
- package/src/deployments.js +36 -0
- package/src/index.js +5 -0
- package/src/record.js +18 -0
- package/src/storage.js +84 -0
- package/src/translate/criteria.js +48 -0
- package/src/translate/options.js +33 -0
- package/src/translate/rename.js +9 -0
- package/src/translate.js +16 -0
- package/test/client.fixtures.js +46 -0
- package/test/client.test.js +81 -0
- package/test/entry.test.js +45 -0
- package/test/query/criteria.fixtures.js +21 -0
- package/test/query/criteria.test.js +10 -0
- package/test/storage.fixtures.js +25 -0
- package/test/storage.test.js +73 -0
- package/test/translate.test.js +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2020-present Artem Gurtovoi
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
apiVersion: v1
|
|
2
|
+
kind: Service
|
|
3
|
+
metadata:
|
|
4
|
+
name: messages-mongodb
|
|
5
|
+
spec:
|
|
6
|
+
selector:
|
|
7
|
+
app.kubernetes.io/name: rabbitmq
|
|
8
|
+
ports:
|
|
9
|
+
- port: 27017
|
|
10
|
+
targetPort: 27017
|
|
11
|
+
---
|
|
12
|
+
apiVersion: v1
|
|
13
|
+
kind: Service
|
|
14
|
+
metadata:
|
|
15
|
+
name: credits-mongodb
|
|
16
|
+
spec:
|
|
17
|
+
selector:
|
|
18
|
+
app.kubernetes.io/name: rabbitmq
|
|
19
|
+
ports:
|
|
20
|
+
- port: 27017
|
|
21
|
+
targetPort: 27017
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@toa.io/storages.mongodb",
|
|
3
|
+
"version": "0.1.0-dev.0",
|
|
4
|
+
"description": "Toa MongoDB Storage Connector",
|
|
5
|
+
"author": "temich <tema.gurtovoy@gmail.com>",
|
|
6
|
+
"homepage": "https://github.com/toa-io/toa#readme",
|
|
7
|
+
"main": "src/index.js",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/toa-io/toa.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/toa-io/toa/issues"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"mongodb": "4.1.4"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@toa.io/core": "^0.0.0",
|
|
26
|
+
"@toa.io/gears": "^0.0.0"
|
|
27
|
+
},
|
|
28
|
+
"gitHead": "632df6cead03909ad2cfb8852e178914ac4ab5d2"
|
|
29
|
+
}
|
package/src/client.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { MongoClient } = require('mongodb')
|
|
4
|
+
const { Connector } = require('@toa.io/core')
|
|
5
|
+
const { console } = require('@toa.io/gears')
|
|
6
|
+
|
|
7
|
+
class Client extends Connector {
|
|
8
|
+
#connection
|
|
9
|
+
|
|
10
|
+
#client
|
|
11
|
+
#collection
|
|
12
|
+
|
|
13
|
+
constructor (host, db, collection) {
|
|
14
|
+
super()
|
|
15
|
+
|
|
16
|
+
this.#connection = { host, db, collection }
|
|
17
|
+
|
|
18
|
+
const url = this.#url()
|
|
19
|
+
this.#client = new MongoClient(url, OPTIONS)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async connection () {
|
|
23
|
+
await this.#client.connect()
|
|
24
|
+
|
|
25
|
+
this.#collection = this.#client.db(this.#connection.db).collection(this.#connection.collection)
|
|
26
|
+
|
|
27
|
+
console.info(`Storage MongoDB connected to ${this.#url()}`)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async disconnection () {
|
|
31
|
+
await this.#client.close()
|
|
32
|
+
|
|
33
|
+
console.info('Storage MongoDB disconnected from ' +
|
|
34
|
+
`${this.#url}/${this.#connection.db}/${this.#connection.collection}`)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async add (record) {
|
|
38
|
+
const { acknowledged } = await this.#collection.insertOne(record)
|
|
39
|
+
|
|
40
|
+
return acknowledged
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async get (query, options) {
|
|
44
|
+
return await this.#collection.findOne(query, options)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async replace (query, record, options) {
|
|
48
|
+
return this.#collection.findOneAndReplace(query, record, options)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async update (query, update, options) {
|
|
52
|
+
return this.#collection.findOneAndUpdate(query, update, options)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async find (query, options) {
|
|
56
|
+
const cursor = await this.#collection.find(query, options)
|
|
57
|
+
|
|
58
|
+
return cursor.toArray()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#url () {
|
|
62
|
+
// TODO: see ./deployment.js
|
|
63
|
+
const user = 'user'
|
|
64
|
+
const password = 'password'
|
|
65
|
+
|
|
66
|
+
return `mongodb://${user}:${password}@${this.#connection.host}:27017/?authSource=${this.#connection.db}&readPreference=primary&appname=svc&ssl=false`
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const OPTIONS = {
|
|
71
|
+
useNewUrlParser: true,
|
|
72
|
+
useUnifiedTopology: true,
|
|
73
|
+
ignoreUndefined: true
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
exports.Client = Client
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const deployments = (values) => {
|
|
4
|
+
const domains = new Set(values.map((value) => value.domain))
|
|
5
|
+
|
|
6
|
+
return [...domains].map(deployment)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const deployment = (domain) => {
|
|
10
|
+
const alias = domain + '-mongodb'
|
|
11
|
+
|
|
12
|
+
// TODO: credentials management
|
|
13
|
+
const usernames = ['user']
|
|
14
|
+
const passwords = ['password']
|
|
15
|
+
const databases = [domain]
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
chart: {
|
|
19
|
+
name: 'mongodb',
|
|
20
|
+
version: '10.29.2',
|
|
21
|
+
repository: 'https://charts.bitnami.com/bitnami',
|
|
22
|
+
alias
|
|
23
|
+
},
|
|
24
|
+
values: {
|
|
25
|
+
architecture: 'standalone',
|
|
26
|
+
fullnameOverride: alias,
|
|
27
|
+
auth: {
|
|
28
|
+
usernames,
|
|
29
|
+
passwords,
|
|
30
|
+
databases
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
exports.deployments = deployments
|
package/src/index.js
ADDED
package/src/record.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const to = (entity) => {
|
|
4
|
+
const { id, _version, ...rest } = entity
|
|
5
|
+
|
|
6
|
+
return { _id: id, _version: _version === undefined ? 1 : _version + 1, ...rest }
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const from = (record) => {
|
|
10
|
+
if (record === undefined || record === null) return null
|
|
11
|
+
|
|
12
|
+
const { _id, ...rest } = record
|
|
13
|
+
|
|
14
|
+
return { id: _id, ...rest }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
exports.to = to
|
|
18
|
+
exports.from = from
|
package/src/storage.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { Connector } = require('@toa.io/core')
|
|
4
|
+
|
|
5
|
+
const { Client } = require('./client')
|
|
6
|
+
const { translate } = require('./translate')
|
|
7
|
+
const { to, from } = require('./record')
|
|
8
|
+
|
|
9
|
+
class Storage extends Connector {
|
|
10
|
+
#client
|
|
11
|
+
|
|
12
|
+
constructor (locator) {
|
|
13
|
+
super()
|
|
14
|
+
|
|
15
|
+
const host = locator.host('mongodb')
|
|
16
|
+
|
|
17
|
+
// TODO: create Factory, add test for host
|
|
18
|
+
this.#client = new Client(host, locator.domain, locator.name)
|
|
19
|
+
this.depends(this.#client)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async get (query) {
|
|
23
|
+
const { criteria, options } = translate(query)
|
|
24
|
+
|
|
25
|
+
const record = await this.#client.get(criteria, options)
|
|
26
|
+
|
|
27
|
+
return from(record)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async add (entity) {
|
|
31
|
+
let result
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
result = await this.#client.add(to(entity))
|
|
35
|
+
} catch (e) {
|
|
36
|
+
if (e.code === 11000) result = false // duplicate id
|
|
37
|
+
else throw e
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return result
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async set (entity) {
|
|
44
|
+
const criteria = { _id: entity.id, _version: entity._version }
|
|
45
|
+
const result = await this.#client.replace(criteria, to(entity))
|
|
46
|
+
|
|
47
|
+
return result.value !== null
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async store (entity) {
|
|
51
|
+
return entity._version === 0 ? this.add(entity) : this.set(entity)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async upsert (query, changeset, insert) {
|
|
55
|
+
const { criteria, options } = translate(query)
|
|
56
|
+
const update = { $set: { ...changeset }, $inc: { _version: 1 } }
|
|
57
|
+
|
|
58
|
+
if (insert !== undefined) {
|
|
59
|
+
delete insert._version
|
|
60
|
+
|
|
61
|
+
options.upsert = true
|
|
62
|
+
|
|
63
|
+
if (criteria._id !== undefined) insert._id = criteria._id
|
|
64
|
+
else return null // this shouldn't ever happen
|
|
65
|
+
|
|
66
|
+
if (Object.keys(insert) > 0) update.$setOnInsert = insert
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
options.returnDocument = 'after'
|
|
70
|
+
|
|
71
|
+
const result = await this.#client.update(criteria, update, options)
|
|
72
|
+
|
|
73
|
+
return from(result.value)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async find (query) {
|
|
77
|
+
const { criteria, options } = translate(query)
|
|
78
|
+
const recordset = await this.#client.find(criteria, options)
|
|
79
|
+
|
|
80
|
+
return recordset.map(from)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
exports.Storage = Storage
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { rename } = require('./rename')
|
|
4
|
+
const criteria = (node) => {
|
|
5
|
+
if (!TYPES[node.type]) { throw new Error('Query criteria AST parse error') }
|
|
6
|
+
|
|
7
|
+
return TYPES[node.type](node)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const OPERATORS = {
|
|
11
|
+
LOGIC: {
|
|
12
|
+
and: '$and',
|
|
13
|
+
';': '$and',
|
|
14
|
+
or: '$or',
|
|
15
|
+
',': '$or'
|
|
16
|
+
},
|
|
17
|
+
COMPARISON: {
|
|
18
|
+
'==': '$eq',
|
|
19
|
+
'>': '$gt',
|
|
20
|
+
'>=': '$gte',
|
|
21
|
+
'=in=': '$in',
|
|
22
|
+
'<': '$lt',
|
|
23
|
+
'<=': '$lte',
|
|
24
|
+
'!=': '$ne',
|
|
25
|
+
'=out=': '$nin'
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const TYPES = {}
|
|
30
|
+
|
|
31
|
+
TYPES.LOGIC = (expression) => {
|
|
32
|
+
const left = criteria(expression.left)
|
|
33
|
+
const right = criteria(expression.right)
|
|
34
|
+
|
|
35
|
+
return { [OPERATORS.LOGIC[expression.operator]]: [left, right] }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
TYPES.COMPARISON = (expression) => {
|
|
39
|
+
const left = criteria(expression.left)
|
|
40
|
+
const right = criteria(expression.right)
|
|
41
|
+
|
|
42
|
+
return { [left]: { [OPERATORS.COMPARISON[expression.operator]]: right } }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
TYPES.SELECTOR = (expression) => rename(expression.selector)
|
|
46
|
+
TYPES.VALUE = (expression) => expression.value
|
|
47
|
+
|
|
48
|
+
exports.criteria = criteria
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { rename } = require('./rename')
|
|
4
|
+
|
|
5
|
+
const options = (options) => {
|
|
6
|
+
const result = {}
|
|
7
|
+
|
|
8
|
+
if (options.omit) { result.skip = options.omit }
|
|
9
|
+
if (options.limit) { result.limit = options.limit }
|
|
10
|
+
if (options.sort) { result.sort = sort(options.sort) }
|
|
11
|
+
if (options.projection) { result.projection = projection(options.projection) }
|
|
12
|
+
|
|
13
|
+
return result
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const sort = (sort) => {
|
|
17
|
+
return sort.map(([property, direction]) => [rename(property), DIRECTIONS[direction]])
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const projection = (projection) => {
|
|
21
|
+
const result = {}
|
|
22
|
+
|
|
23
|
+
for (const property of projection) { result[rename(property)] = 1 }
|
|
24
|
+
|
|
25
|
+
return result
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const DIRECTIONS = {
|
|
29
|
+
asc: 1,
|
|
30
|
+
desc: -1
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
exports.options = options
|
package/src/translate.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const parse = { ...require('./translate/criteria'), ...require('./translate/options') }
|
|
4
|
+
|
|
5
|
+
const translate = (query) => {
|
|
6
|
+
const result = { criteria: {}, options: {} }
|
|
7
|
+
|
|
8
|
+
if (query.criteria) result.criteria = parse.criteria(query.criteria)
|
|
9
|
+
if (query.options) result.options = parse.options(query.options)
|
|
10
|
+
if (query.id) result.criteria._id = query.id
|
|
11
|
+
if (query.version) result.criteria._version = query.version
|
|
12
|
+
|
|
13
|
+
return result
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exports.translate = translate
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const MongoClient = jest.fn().mockImplementation(function () {
|
|
4
|
+
this.db = jest.fn(() => new DB())
|
|
5
|
+
|
|
6
|
+
this.connect = jest.fn()
|
|
7
|
+
this.close = jest.fn()
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
const DB = jest.fn().mockImplementation(function () {
|
|
11
|
+
this.collection = jest.fn(() => new Collection())
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
const results = {
|
|
15
|
+
insertOne: { acknowledged: 1 }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const Collection = jest.fn().mockImplementation(function () {
|
|
19
|
+
this.find = jest.fn(() => ({
|
|
20
|
+
toArray: jest.fn(() => ({ id: 1, foo: 'bar' }))
|
|
21
|
+
}))
|
|
22
|
+
this.findOne = jest.fn(() => ({ id: 1, foo: 'bar' }))
|
|
23
|
+
this.insertOne = jest.fn(() => results.insertOne)
|
|
24
|
+
this.findOneAndReplace = jest.fn(() => ({ ok: 1 }))
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const OPTIONS = {
|
|
28
|
+
useNewUrlParser: true,
|
|
29
|
+
useUnifiedTopology: true,
|
|
30
|
+
ignoreUndefined: true
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const query = {
|
|
34
|
+
criteria: {
|
|
35
|
+
id: 1
|
|
36
|
+
},
|
|
37
|
+
options: {
|
|
38
|
+
limit: 1
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
exports.mock = { MongoClient }
|
|
43
|
+
exports.OPTIONS = OPTIONS
|
|
44
|
+
exports.locator = { host: 'bar.foo', db: 'foo', collection: 'bar' }
|
|
45
|
+
exports.object = { id: '1', foo: 'bar' }
|
|
46
|
+
exports.query = query
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const fixtures = require('./client.fixtures')
|
|
4
|
+
const mock = fixtures.mock
|
|
5
|
+
|
|
6
|
+
jest.mock('mongodb', () => ({ MongoClient: mock.MongoClient }))
|
|
7
|
+
|
|
8
|
+
const { Client } = require('../src/client')
|
|
9
|
+
|
|
10
|
+
let instance, client, collection
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
jest.clearAllMocks()
|
|
14
|
+
|
|
15
|
+
const env = process.env.TOA_ENV
|
|
16
|
+
|
|
17
|
+
delete process.env.TOA_ENV
|
|
18
|
+
|
|
19
|
+
instance = new Client(fixtures.locator.host, fixtures.locator.db, fixtures.locator.collection)
|
|
20
|
+
await instance.connect()
|
|
21
|
+
|
|
22
|
+
process.env.TOA_ENV = env
|
|
23
|
+
|
|
24
|
+
client = fixtures.mock.MongoClient.mock.instances[0]
|
|
25
|
+
|
|
26
|
+
collection = fixtures.mock.MongoClient.mock.instances[0]
|
|
27
|
+
.db.mock.results[0].value
|
|
28
|
+
.collection.mock.results[0].value
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('should create client', () => {
|
|
32
|
+
expect(client).toBeDefined()
|
|
33
|
+
expect(fixtures.mock.MongoClient).toHaveBeenCalledWith(
|
|
34
|
+
expect.any(String),
|
|
35
|
+
fixtures.OPTIONS
|
|
36
|
+
)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('should connect', async () => {
|
|
40
|
+
expect(client.connect).toHaveBeenCalled()
|
|
41
|
+
expect(client.db).toHaveBeenCalledWith(fixtures.locator.db)
|
|
42
|
+
|
|
43
|
+
const db = client.db.mock.results[0].value
|
|
44
|
+
|
|
45
|
+
expect(db.collection).toHaveBeenCalledWith(fixtures.locator.collection)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('should disconnect', async () => {
|
|
49
|
+
await instance.disconnect()
|
|
50
|
+
|
|
51
|
+
expect(client.connect).toHaveBeenCalled()
|
|
52
|
+
expect(client.close).toHaveBeenCalled()
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('should add', async () => {
|
|
56
|
+
const ok = await instance.add(fixtures.object)
|
|
57
|
+
|
|
58
|
+
expect(collection.insertOne).toHaveBeenCalledWith(fixtures.object)
|
|
59
|
+
expect(ok).toBe(collection.insertOne.mock.results[0].value.acknowledged)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('should get', async () => {
|
|
63
|
+
const entity = await instance.get(fixtures.query.criteria, fixtures.query.options)
|
|
64
|
+
|
|
65
|
+
expect(entity).toBe(collection.findOne.mock.results[0].value)
|
|
66
|
+
expect(collection.findOne).toHaveBeenCalledWith(fixtures.query.criteria, fixtures.query.options)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('should replace', async () => {
|
|
70
|
+
const update = { ...fixtures.object, foo: 'foo' }
|
|
71
|
+
await instance.replace(fixtures.query.criteria, update, fixtures.query.options)
|
|
72
|
+
|
|
73
|
+
expect(collection.findOneAndReplace).toHaveBeenCalledWith(fixtures.query.criteria, update, fixtures.query.options)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('should find', async () => {
|
|
77
|
+
const set = await instance.find(fixtures.query.criteria, fixtures.query.options)
|
|
78
|
+
|
|
79
|
+
expect(set).toStrictEqual(collection.find.mock.results[0].value.toArray.mock.results[0].value)
|
|
80
|
+
expect(collection.find).toHaveBeenCalledWith(fixtures.query.criteria, fixtures.query.options)
|
|
81
|
+
})
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { to, from } = require('../src/record')
|
|
4
|
+
const { random } = require('@toa.io/gears')
|
|
5
|
+
|
|
6
|
+
describe('to', () => {
|
|
7
|
+
it('should rename id to _id', () => {
|
|
8
|
+
const entity = { id: 1 }
|
|
9
|
+
const record = to(entity)
|
|
10
|
+
|
|
11
|
+
expect(record).toMatchObject({ _id: 1 })
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should not modify argument', () => {
|
|
15
|
+
const entity = { id: 1 }
|
|
16
|
+
|
|
17
|
+
to(entity)
|
|
18
|
+
|
|
19
|
+
expect(entity).toStrictEqual({ id: 1 })
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('should increment _version', () => {
|
|
23
|
+
const entity = { _version: random() }
|
|
24
|
+
const record = to(entity)
|
|
25
|
+
|
|
26
|
+
expect(record).toMatchObject({ _version: entity._version + 1 })
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
describe('from', () => {
|
|
31
|
+
it('should rename _id to id', () => {
|
|
32
|
+
const record = { _id: 1 }
|
|
33
|
+
const entity = from(record)
|
|
34
|
+
|
|
35
|
+
expect(entity).toStrictEqual({ id: 1 })
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('should not modify argument', () => {
|
|
39
|
+
const record = { _id: 1 }
|
|
40
|
+
|
|
41
|
+
from(record)
|
|
42
|
+
|
|
43
|
+
expect(record).toStrictEqual({ _id: 1 })
|
|
44
|
+
})
|
|
45
|
+
})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const ast = {
|
|
4
|
+
left: {
|
|
5
|
+
type: 'SELECTOR',
|
|
6
|
+
selector: 'id'
|
|
7
|
+
},
|
|
8
|
+
type: 'COMPARISON',
|
|
9
|
+
operator: '==',
|
|
10
|
+
right: {
|
|
11
|
+
type: 'VALUE',
|
|
12
|
+
value: 100500
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const criteria = {
|
|
17
|
+
_id: { $eq: 100500 }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
exports.ast = ast
|
|
21
|
+
exports.criteria = criteria
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Client = jest.fn().mockImplementation(function () {
|
|
4
|
+
this.connect = jest.fn()
|
|
5
|
+
this.disconnect = jest.fn()
|
|
6
|
+
this.add = jest.fn(() => true)
|
|
7
|
+
this.get = jest.fn(() => ({ foo: 'bar' }))
|
|
8
|
+
this.replace = jest.fn(() => ({ ok: 1 }))
|
|
9
|
+
this.find = jest.fn(() => [{ foo: 'bar' }, { bar: 'foo' }])
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
const query = {
|
|
13
|
+
criteria: {},
|
|
14
|
+
options: {}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const translate = jest.fn(() => ({ criteria: 'foo', options: 'bar' }))
|
|
18
|
+
|
|
19
|
+
const to = jest.fn((entity) => entity)
|
|
20
|
+
const from = jest.fn((record) => record)
|
|
21
|
+
|
|
22
|
+
exports.mock = { Client, translate, to, from }
|
|
23
|
+
exports.locator = { host: jest.fn(() => 'bar.foo.local'), domain: 'foo', name: 'bar' }
|
|
24
|
+
exports.entity = { id: '1', foo: 'bar' }
|
|
25
|
+
exports.query = query
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const fixtures = require('./storage.fixtures')
|
|
4
|
+
const mock = fixtures.mock
|
|
5
|
+
|
|
6
|
+
jest.mock('../src/client', () => ({ Client: mock.Client }))
|
|
7
|
+
jest.mock('../src/translate', () => ({ translate: mock.translate }))
|
|
8
|
+
jest.mock('../src/record', () => ({ to: mock.to, from: mock.from }))
|
|
9
|
+
|
|
10
|
+
const { Storage } = require('../src/storage')
|
|
11
|
+
|
|
12
|
+
let connector, client
|
|
13
|
+
|
|
14
|
+
beforeAll(() => {
|
|
15
|
+
connector = new Storage(fixtures.locator)
|
|
16
|
+
|
|
17
|
+
client = fixtures.mock.Client.mock.instances[0]
|
|
18
|
+
|
|
19
|
+
expect(client).toBeDefined()
|
|
20
|
+
|
|
21
|
+
expect(fixtures.mock.Client).toHaveBeenCalledWith(
|
|
22
|
+
fixtures.locator.host.mock.results[0].value,
|
|
23
|
+
fixtures.locator.domain,
|
|
24
|
+
fixtures.locator.name
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
expect(fixtures.locator.host.mock.calls[0][0].toLowerCase()).toBe('mongodb')
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
jest.clearAllMocks()
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('should add', async () => {
|
|
35
|
+
const result = await connector.add(fixtures.entity)
|
|
36
|
+
|
|
37
|
+
expect(client.add).toHaveBeenCalledWith(mock.to.mock.results[0].value)
|
|
38
|
+
expect(mock.to).toHaveBeenCalledWith(fixtures.entity)
|
|
39
|
+
expect(result).toBe(client.add.mock.results[0].value)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('should get', async () => {
|
|
43
|
+
const entity = await connector.get(fixtures.query)
|
|
44
|
+
|
|
45
|
+
expect(entity).toBe(mock.from.mock.results[0].value)
|
|
46
|
+
expect(mock.from).toHaveBeenCalledWith(client.get.mock.results[0].value)
|
|
47
|
+
|
|
48
|
+
const { criteria, options } = mock.translate.mock.results[0].value
|
|
49
|
+
|
|
50
|
+
expect(client.get).toHaveBeenCalledWith(criteria, options)
|
|
51
|
+
expect(mock.translate).toHaveBeenCalledWith(fixtures.query)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('should set', async () => {
|
|
55
|
+
await connector.set(fixtures.entity)
|
|
56
|
+
const criteria = { _id: fixtures.entity.id }
|
|
57
|
+
|
|
58
|
+
expect(client.replace).toHaveBeenCalledWith(criteria, mock.to.mock.results[0].value)
|
|
59
|
+
expect(mock.to).toHaveBeenCalledWith(fixtures.entity)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('should find', async () => {
|
|
63
|
+
const set = await connector.find(fixtures.query)
|
|
64
|
+
const found = client.find.mock.results[0].value
|
|
65
|
+
|
|
66
|
+
const expected = found.map((result, index) => {
|
|
67
|
+
expect(mock.from).toHaveBeenNthCalledWith(index + 1, result, index, found)
|
|
68
|
+
|
|
69
|
+
return mock.from.mock.results[index].value
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
expect(set).toStrictEqual(expected)
|
|
73
|
+
})
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { translate } = require('../src/translate')
|
|
4
|
+
|
|
5
|
+
describe('options', () => {
|
|
6
|
+
it('should translate omit', () => {
|
|
7
|
+
const options = { omit: 10 }
|
|
8
|
+
const query = translate({ options })
|
|
9
|
+
|
|
10
|
+
expect(query.options.skip).toStrictEqual(options.omit)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('should translate limit', () => {
|
|
14
|
+
const options = { limit: 10 }
|
|
15
|
+
const query = translate({ options })
|
|
16
|
+
|
|
17
|
+
expect(query.options.limit).toStrictEqual(options.limit)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('should translate sort', () => {
|
|
21
|
+
const options = { sort: [['a', 'asc'], ['b', 'desc'], ['id', 'asc']] }
|
|
22
|
+
const query = translate({ options })
|
|
23
|
+
|
|
24
|
+
expect(query.options.sort).toStrictEqual([['a', 1], ['b', -1], ['_id', 1]])
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('should translate projection', () => {
|
|
28
|
+
const options = { projection: ['a', 'b', 'c', 'id'] }
|
|
29
|
+
const query = translate({ options })
|
|
30
|
+
|
|
31
|
+
expect(query.options.projection).toStrictEqual({ a: 1, b: 1, c: 1, _id: 1 })
|
|
32
|
+
})
|
|
33
|
+
})
|