-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdb.updating.js
144 lines (130 loc) · 3.81 KB
/
db.updating.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const chai = require('chai')
const expect = chai.expect
const request = require('supertest')
const testutils = require('./testutils')
const { app, api } = testutils
const collectionNames = ['memorytest', 'filetest']
if (process.env.TEST_MONGO) {
collectionNames.push('mongotest')
}
if (process.env.TEST_POSTGRES) {
collectionNames.push('postgrestest')
}
collectionNames.forEach(function (collection) {
describe(`${collection} updating`, function () {
let db
let id
const collectionName = collection + '3'
before(async function () {
const coll = {
_id: collectionName,
schema: {
type: 'object',
additionalProperties: false,
properties: {
_id: {
type: 'string'
},
title: {
type: 'string'
},
data: {
type: 'object',
additionalProperties: true,
properties: {
nestedArr: {
type: 'array',
items: {
type: 'string'
}
},
}
},
arr: {
type: 'array',
items: {
type: 'object',
properties: {
color: {
type: 'string'
},
num: {
type: 'number'
},
subarray: {
type: 'array',
items: {
type: 'object',
properties: {
v: {
type: 'string'
}
}
}
}
}
}
},
meta: {
type: 'object',
propertyOrder: 2000,
properties: {
created: {
type: 'string'
},
updated: {
type: 'string'
},
}
}
},
required: [
'title'
]
},
storage: collection.replace('test', ''),
documentsHaveOwners: false,
}
const token = await testutils.getUserWithPermissions(api, 'collection: create')
await request(app)
.post('/collection')
.set('x-access-token', token)
.send(coll)
.expect(200)
db = api.db[collection]
id = await db.create({
title: 'first',
data: { field: '123' },
arr: [{ color: 'red', subarray: [{ v: 'abc' }] }],
})
await db.create({
title: 'second',
data: { field: '123' },
arr: [{ color: 'red', subarray: [{ v: 'abc' }] }],
})
await db.create({
title: 'third',
data: { field: '12345' },
arr: [{ color: 'red', subarray: [{ v: 'abc' }] }],
})
})
it('update by query with id', async function () {
const res = await db.updateWithQuery({ _id: id }, { $set: { title: 'first22' } })
expect(res.matchedCount).to.equal(1)
const doc = await db.get(id)
expect(doc.title).to.equal('first22')
})
it('update by query with sub field', async function () {
let docs = await db.find({})
expect(docs).to.have.length(3)
const res1 = await db.updateWithQuery({ 'data.field': '123' }, { $set: { test: true } })
docs = await db.find({test: true })
expect(docs).to.have.length(2)
expect(res1.matchedCount).to.equal(2)
const res2 = await db.updateWithQuery({ 'data.field': { $in: ['123', '12345'] } }, { $set: { test: true } })
docs = await db.find({ test: true })
expect(docs).to.have.length(3)
expect(res2.matchedCount).to.equal(3)
})
})
})