@e22m4u/js-repository-mongodb-adapter 0.0.23 → 0.0.24
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 +8 -8
- package/src/mongodb-adapter.js +18 -0
- package/src/mongodb-adapter.spec.js +990 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e22m4u/js-repository-mongodb-adapter",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.24",
|
|
4
4
|
"description": "MongoDB адаптер для @e22m4u/js-repository",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -38,22 +38,22 @@
|
|
|
38
38
|
"peerDependencies": {
|
|
39
39
|
"@e22m4u/js-format": "*",
|
|
40
40
|
"@e22m4u/js-service": "*",
|
|
41
|
-
"@e22m4u/js-repository": "~0.0.
|
|
41
|
+
"@e22m4u/js-repository": "~0.0.41"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@commitlint/cli": "^17.
|
|
45
|
-
"@commitlint/config-conventional": "^17.
|
|
44
|
+
"@commitlint/cli": "^17.8.0",
|
|
45
|
+
"@commitlint/config-conventional": "^17.8.0",
|
|
46
46
|
"c8": "^8.0.1",
|
|
47
|
-
"chai": "^4.3.
|
|
47
|
+
"chai": "^4.3.10",
|
|
48
48
|
"chai-as-promised": "^7.1.1",
|
|
49
49
|
"chai-spies": "^1.0.0",
|
|
50
50
|
"dotenv": "^16.3.1",
|
|
51
|
-
"eslint": "^8.
|
|
51
|
+
"eslint": "^8.51.0",
|
|
52
52
|
"eslint-config-prettier": "^9.0.0",
|
|
53
53
|
"eslint-plugin-chai-expect": "^3.0.0",
|
|
54
|
-
"eslint-plugin-mocha": "^10.
|
|
54
|
+
"eslint-plugin-mocha": "^10.2.0",
|
|
55
55
|
"husky": "^8.0.3",
|
|
56
56
|
"mocha": "^10.2.0",
|
|
57
|
-
"prettier": "^3.0.
|
|
57
|
+
"prettier": "^3.0.3"
|
|
58
58
|
}
|
|
59
59
|
}
|
package/src/mongodb-adapter.js
CHANGED
|
@@ -650,6 +650,24 @@ export class MongodbAdapter extends Adapter {
|
|
|
650
650
|
return this._fromDatabase(modelName, replacedData);
|
|
651
651
|
}
|
|
652
652
|
|
|
653
|
+
/**
|
|
654
|
+
* Patch.
|
|
655
|
+
*
|
|
656
|
+
* @param {string} modelName
|
|
657
|
+
* @param {object} modelData
|
|
658
|
+
* @param {object|undefined} where
|
|
659
|
+
* @return {Promise<number>}
|
|
660
|
+
*/
|
|
661
|
+
async patch(modelName, modelData, where = undefined) {
|
|
662
|
+
const idPropName = this._getIdPropName(modelName);
|
|
663
|
+
delete modelData[idPropName];
|
|
664
|
+
const query = this._buildQuery(modelName, where) || {};
|
|
665
|
+
const tableData = this._toDatabase(modelName, modelData);
|
|
666
|
+
const table = this._getCollection(modelName);
|
|
667
|
+
const {matchedCount} = await table.updateMany(query, {$set: tableData});
|
|
668
|
+
return matchedCount;
|
|
669
|
+
}
|
|
670
|
+
|
|
653
671
|
/**
|
|
654
672
|
* Patch by id.
|
|
655
673
|
*
|
|
@@ -1977,6 +1977,940 @@ describe('MongodbAdapter', function () {
|
|
|
1977
1977
|
});
|
|
1978
1978
|
});
|
|
1979
1979
|
|
|
1980
|
+
describe('patch', function () {
|
|
1981
|
+
it('updates only provided properties for all items and returns their number', async function () {
|
|
1982
|
+
const schema = createSchema();
|
|
1983
|
+
schema.defineModel({
|
|
1984
|
+
name: 'model',
|
|
1985
|
+
datasource: 'mongodb',
|
|
1986
|
+
properties: {
|
|
1987
|
+
foo: DataType.STRING,
|
|
1988
|
+
bar: DataType.STRING,
|
|
1989
|
+
},
|
|
1990
|
+
});
|
|
1991
|
+
const rep = schema.getRepository('model');
|
|
1992
|
+
const input1 = {foo: 'a1', bar: 'a2'};
|
|
1993
|
+
const input2 = {foo: 'b1', bar: 'b2'};
|
|
1994
|
+
const input3 = {foo: 'c1', bar: 'c2'};
|
|
1995
|
+
const created1 = await rep.create(input1);
|
|
1996
|
+
const created2 = await rep.create(input2);
|
|
1997
|
+
const created3 = await rep.create(input3);
|
|
1998
|
+
const id1 = created1[DEF_PK];
|
|
1999
|
+
const id2 = created2[DEF_PK];
|
|
2000
|
+
const id3 = created3[DEF_PK];
|
|
2001
|
+
const result = await rep.patch({foo: 'd1'});
|
|
2002
|
+
expect(result).to.be.eq(3);
|
|
2003
|
+
const rawData = await MDB_CLIENT.db()
|
|
2004
|
+
.collection('model')
|
|
2005
|
+
.find()
|
|
2006
|
+
.toArray();
|
|
2007
|
+
expect(rawData).to.be.eql([
|
|
2008
|
+
{_id: new ObjectId(id1), foo: 'd1', bar: 'a2'},
|
|
2009
|
+
{_id: new ObjectId(id2), foo: 'd1', bar: 'b2'},
|
|
2010
|
+
{_id: new ObjectId(id3), foo: 'd1', bar: 'c2'},
|
|
2011
|
+
]);
|
|
2012
|
+
});
|
|
2013
|
+
|
|
2014
|
+
it('does not throw an error if a partial data does not have required property', async function () {
|
|
2015
|
+
const schema = createSchema();
|
|
2016
|
+
schema.defineModel({
|
|
2017
|
+
name: 'model',
|
|
2018
|
+
datasource: 'mongodb',
|
|
2019
|
+
properties: {
|
|
2020
|
+
foo: DataType.STRING,
|
|
2021
|
+
bar: {
|
|
2022
|
+
type: DataType.STRING,
|
|
2023
|
+
required: true,
|
|
2024
|
+
},
|
|
2025
|
+
},
|
|
2026
|
+
});
|
|
2027
|
+
const rep = schema.getRepository('model');
|
|
2028
|
+
const input1 = {foo: 'a1', bar: 'a2'};
|
|
2029
|
+
const input2 = {foo: 'b1', bar: 'b2'};
|
|
2030
|
+
const input3 = {foo: 'c1', bar: 'c2'};
|
|
2031
|
+
const created1 = await rep.create(input1);
|
|
2032
|
+
const created2 = await rep.create(input2);
|
|
2033
|
+
const created3 = await rep.create(input3);
|
|
2034
|
+
const id1 = created1[DEF_PK];
|
|
2035
|
+
const id2 = created2[DEF_PK];
|
|
2036
|
+
const id3 = created3[DEF_PK];
|
|
2037
|
+
const result = await rep.patch({foo: 'd1'});
|
|
2038
|
+
expect(result).to.be.eq(3);
|
|
2039
|
+
const rawData = await MDB_CLIENT.db()
|
|
2040
|
+
.collection('model')
|
|
2041
|
+
.find()
|
|
2042
|
+
.toArray();
|
|
2043
|
+
expect(rawData).to.be.eql([
|
|
2044
|
+
{_id: new ObjectId(id1), foo: 'd1', bar: 'a2'},
|
|
2045
|
+
{_id: new ObjectId(id2), foo: 'd1', bar: 'b2'},
|
|
2046
|
+
{_id: new ObjectId(id3), foo: 'd1', bar: 'c2'},
|
|
2047
|
+
]);
|
|
2048
|
+
});
|
|
2049
|
+
|
|
2050
|
+
it('ignores identifier value in a given data in case of a default primary key', async function () {
|
|
2051
|
+
const schema = createSchema();
|
|
2052
|
+
schema.defineModel({
|
|
2053
|
+
name: 'model',
|
|
2054
|
+
datasource: 'mongodb',
|
|
2055
|
+
properties: {
|
|
2056
|
+
foo: DataType.STRING,
|
|
2057
|
+
bar: DataType.STRING,
|
|
2058
|
+
},
|
|
2059
|
+
});
|
|
2060
|
+
const rep = schema.getRepository('model');
|
|
2061
|
+
const input1 = {foo: 'a1', bar: 'a2'};
|
|
2062
|
+
const input2 = {foo: 'b1', bar: 'b2'};
|
|
2063
|
+
const input3 = {foo: 'c1', bar: 'c2'};
|
|
2064
|
+
const created1 = await rep.create(input1);
|
|
2065
|
+
const created2 = await rep.create(input2);
|
|
2066
|
+
const created3 = await rep.create(input3);
|
|
2067
|
+
const id1 = created1[DEF_PK];
|
|
2068
|
+
const id2 = created2[DEF_PK];
|
|
2069
|
+
const id3 = created3[DEF_PK];
|
|
2070
|
+
const result = await rep.patch({[DEF_PK]: '100', foo: 'd1'});
|
|
2071
|
+
expect(result).to.be.eq(3);
|
|
2072
|
+
const rawData = await MDB_CLIENT.db()
|
|
2073
|
+
.collection('model')
|
|
2074
|
+
.find()
|
|
2075
|
+
.toArray();
|
|
2076
|
+
expect(rawData).to.be.eql([
|
|
2077
|
+
{_id: new ObjectId(id1), foo: 'd1', bar: 'a2'},
|
|
2078
|
+
{_id: new ObjectId(id2), foo: 'd1', bar: 'b2'},
|
|
2079
|
+
{_id: new ObjectId(id3), foo: 'd1', bar: 'c2'},
|
|
2080
|
+
]);
|
|
2081
|
+
});
|
|
2082
|
+
|
|
2083
|
+
it('ignores identifier value in a given data in case of a specified primary key', async function () {
|
|
2084
|
+
const schema = createSchema();
|
|
2085
|
+
schema.defineModel({
|
|
2086
|
+
name: 'model',
|
|
2087
|
+
datasource: 'mongodb',
|
|
2088
|
+
properties: {
|
|
2089
|
+
id: {
|
|
2090
|
+
type: DataType.STRING,
|
|
2091
|
+
primaryKey: true,
|
|
2092
|
+
},
|
|
2093
|
+
foo: DataType.STRING,
|
|
2094
|
+
bar: DataType.STRING,
|
|
2095
|
+
},
|
|
2096
|
+
});
|
|
2097
|
+
const rep = schema.getRepository('model');
|
|
2098
|
+
const input1 = {foo: 'a1', bar: 'a2'};
|
|
2099
|
+
const input2 = {foo: 'b1', bar: 'b2'};
|
|
2100
|
+
const input3 = {foo: 'c1', bar: 'c2'};
|
|
2101
|
+
const created1 = await rep.create(input1);
|
|
2102
|
+
const created2 = await rep.create(input2);
|
|
2103
|
+
const created3 = await rep.create(input3);
|
|
2104
|
+
const id1 = created1[DEF_PK];
|
|
2105
|
+
const id2 = created2[DEF_PK];
|
|
2106
|
+
const id3 = created3[DEF_PK];
|
|
2107
|
+
const result = await rep.patch({id: '100', foo: 'd1'});
|
|
2108
|
+
expect(result).to.be.eq(3);
|
|
2109
|
+
const rawData = await MDB_CLIENT.db()
|
|
2110
|
+
.collection('model')
|
|
2111
|
+
.find()
|
|
2112
|
+
.toArray();
|
|
2113
|
+
expect(rawData).to.be.eql([
|
|
2114
|
+
{_id: new ObjectId(id1), foo: 'd1', bar: 'a2'},
|
|
2115
|
+
{_id: new ObjectId(id2), foo: 'd1', bar: 'b2'},
|
|
2116
|
+
{_id: new ObjectId(id3), foo: 'd1', bar: 'c2'},
|
|
2117
|
+
]);
|
|
2118
|
+
});
|
|
2119
|
+
|
|
2120
|
+
it('sets a default values for patched properties with an undefined value', async function () {
|
|
2121
|
+
const schema = createSchema();
|
|
2122
|
+
schema.defineModel({
|
|
2123
|
+
name: 'model',
|
|
2124
|
+
datasource: 'mongodb',
|
|
2125
|
+
properties: {
|
|
2126
|
+
foo: {
|
|
2127
|
+
type: DataType.STRING,
|
|
2128
|
+
default: 'fooVal',
|
|
2129
|
+
},
|
|
2130
|
+
bar: {
|
|
2131
|
+
type: DataType.STRING,
|
|
2132
|
+
default: 'barVal',
|
|
2133
|
+
},
|
|
2134
|
+
},
|
|
2135
|
+
});
|
|
2136
|
+
const rep = schema.getRepository('model');
|
|
2137
|
+
const input1 = {foo: 'a1', bar: 'a2'};
|
|
2138
|
+
const input2 = {foo: 'b1', bar: 'b2'};
|
|
2139
|
+
const input3 = {foo: 'c1', bar: 'c2'};
|
|
2140
|
+
const created1 = await rep.create(input1);
|
|
2141
|
+
const created2 = await rep.create(input2);
|
|
2142
|
+
const created3 = await rep.create(input3);
|
|
2143
|
+
const id1 = created1[DEF_PK];
|
|
2144
|
+
const id2 = created2[DEF_PK];
|
|
2145
|
+
const id3 = created3[DEF_PK];
|
|
2146
|
+
const result = await rep.patch({foo: undefined});
|
|
2147
|
+
expect(result).to.be.eq(3);
|
|
2148
|
+
const rawData = await MDB_CLIENT.db()
|
|
2149
|
+
.collection('model')
|
|
2150
|
+
.find()
|
|
2151
|
+
.toArray();
|
|
2152
|
+
expect(rawData).to.be.eql([
|
|
2153
|
+
{_id: new ObjectId(id1), foo: 'fooVal', bar: 'a2'},
|
|
2154
|
+
{_id: new ObjectId(id2), foo: 'fooVal', bar: 'b2'},
|
|
2155
|
+
{_id: new ObjectId(id3), foo: 'fooVal', bar: 'c2'},
|
|
2156
|
+
]);
|
|
2157
|
+
});
|
|
2158
|
+
|
|
2159
|
+
it('sets a default values for patched properties with a null value', async function () {
|
|
2160
|
+
const schema = createSchema();
|
|
2161
|
+
schema.defineModel({
|
|
2162
|
+
name: 'model',
|
|
2163
|
+
datasource: 'mongodb',
|
|
2164
|
+
properties: {
|
|
2165
|
+
foo: {
|
|
2166
|
+
type: DataType.STRING,
|
|
2167
|
+
default: 'fooVal',
|
|
2168
|
+
},
|
|
2169
|
+
bar: {
|
|
2170
|
+
type: DataType.STRING,
|
|
2171
|
+
default: 'barVal',
|
|
2172
|
+
},
|
|
2173
|
+
},
|
|
2174
|
+
});
|
|
2175
|
+
const rep = schema.getRepository('model');
|
|
2176
|
+
const input1 = {foo: 'a1', bar: 'a2'};
|
|
2177
|
+
const input2 = {foo: 'b1', bar: 'b2'};
|
|
2178
|
+
const input3 = {foo: 'c1', bar: 'c2'};
|
|
2179
|
+
const created1 = await rep.create(input1);
|
|
2180
|
+
const created2 = await rep.create(input2);
|
|
2181
|
+
const created3 = await rep.create(input3);
|
|
2182
|
+
const id1 = created1[DEF_PK];
|
|
2183
|
+
const id2 = created2[DEF_PK];
|
|
2184
|
+
const id3 = created3[DEF_PK];
|
|
2185
|
+
const result = await rep.patch({foo: null});
|
|
2186
|
+
expect(result).to.be.eq(3);
|
|
2187
|
+
const rawData = await MDB_CLIENT.db()
|
|
2188
|
+
.collection('model')
|
|
2189
|
+
.find()
|
|
2190
|
+
.toArray();
|
|
2191
|
+
expect(rawData).to.be.eql([
|
|
2192
|
+
{_id: new ObjectId(id1), foo: 'fooVal', bar: 'a2'},
|
|
2193
|
+
{_id: new ObjectId(id2), foo: 'fooVal', bar: 'b2'},
|
|
2194
|
+
{_id: new ObjectId(id3), foo: 'fooVal', bar: 'c2'},
|
|
2195
|
+
]);
|
|
2196
|
+
});
|
|
2197
|
+
|
|
2198
|
+
it('uses a specified column name for a regular property', async function () {
|
|
2199
|
+
const schema = createSchema();
|
|
2200
|
+
schema.defineModel({
|
|
2201
|
+
name: 'model',
|
|
2202
|
+
datasource: 'mongodb',
|
|
2203
|
+
properties: {
|
|
2204
|
+
foo: {
|
|
2205
|
+
type: DataType.STRING,
|
|
2206
|
+
columnName: 'fooCol',
|
|
2207
|
+
},
|
|
2208
|
+
bar: {
|
|
2209
|
+
type: DataType.STRING,
|
|
2210
|
+
columnName: 'barCol',
|
|
2211
|
+
},
|
|
2212
|
+
},
|
|
2213
|
+
});
|
|
2214
|
+
const rep = schema.getRepository('model');
|
|
2215
|
+
const input1 = {foo: 'a1', bar: 'a2'};
|
|
2216
|
+
const input2 = {foo: 'b1', bar: 'b2'};
|
|
2217
|
+
const input3 = {foo: 'c1', bar: 'c2'};
|
|
2218
|
+
const created1 = await rep.create(input1);
|
|
2219
|
+
const created2 = await rep.create(input2);
|
|
2220
|
+
const created3 = await rep.create(input3);
|
|
2221
|
+
const id1 = created1[DEF_PK];
|
|
2222
|
+
const id2 = created2[DEF_PK];
|
|
2223
|
+
const id3 = created3[DEF_PK];
|
|
2224
|
+
const result = await rep.patch({foo: 'd1'});
|
|
2225
|
+
expect(result).to.be.eq(3);
|
|
2226
|
+
const rawData = await MDB_CLIENT.db()
|
|
2227
|
+
.collection('model')
|
|
2228
|
+
.find()
|
|
2229
|
+
.toArray();
|
|
2230
|
+
expect(rawData).to.be.eql([
|
|
2231
|
+
{_id: new ObjectId(id1), fooCol: 'd1', barCol: 'a2'},
|
|
2232
|
+
{_id: new ObjectId(id2), fooCol: 'd1', barCol: 'b2'},
|
|
2233
|
+
{_id: new ObjectId(id3), fooCol: 'd1', barCol: 'c2'},
|
|
2234
|
+
]);
|
|
2235
|
+
});
|
|
2236
|
+
|
|
2237
|
+
it('uses a specified column name for a regular property with a default value', async function () {
|
|
2238
|
+
const schema = createSchema();
|
|
2239
|
+
schema.defineModel({
|
|
2240
|
+
name: 'model',
|
|
2241
|
+
datasource: 'mongodb',
|
|
2242
|
+
properties: {
|
|
2243
|
+
foo: {
|
|
2244
|
+
type: DataType.STRING,
|
|
2245
|
+
columnName: 'fooCol',
|
|
2246
|
+
default: 'fooVal',
|
|
2247
|
+
},
|
|
2248
|
+
bar: {
|
|
2249
|
+
type: DataType.STRING,
|
|
2250
|
+
columnName: 'barCol',
|
|
2251
|
+
default: 'barVal',
|
|
2252
|
+
},
|
|
2253
|
+
},
|
|
2254
|
+
});
|
|
2255
|
+
const rep = schema.getRepository('model');
|
|
2256
|
+
const input1 = {foo: 'a1', bar: 'a2'};
|
|
2257
|
+
const input2 = {foo: 'b1', bar: 'b2'};
|
|
2258
|
+
const input3 = {foo: 'c1', bar: 'c2'};
|
|
2259
|
+
const created1 = await rep.create(input1);
|
|
2260
|
+
const created2 = await rep.create(input2);
|
|
2261
|
+
const created3 = await rep.create(input3);
|
|
2262
|
+
const id1 = created1[DEF_PK];
|
|
2263
|
+
const id2 = created2[DEF_PK];
|
|
2264
|
+
const id3 = created3[DEF_PK];
|
|
2265
|
+
const result = await rep.patch({foo: undefined});
|
|
2266
|
+
expect(result).to.be.eq(3);
|
|
2267
|
+
const rawData = await MDB_CLIENT.db()
|
|
2268
|
+
.collection('model')
|
|
2269
|
+
.find()
|
|
2270
|
+
.toArray();
|
|
2271
|
+
expect(rawData).to.be.eql([
|
|
2272
|
+
{_id: new ObjectId(id1), fooCol: 'fooVal', barCol: 'a2'},
|
|
2273
|
+
{_id: new ObjectId(id2), fooCol: 'fooVal', barCol: 'b2'},
|
|
2274
|
+
{_id: new ObjectId(id3), fooCol: 'fooVal', barCol: 'c2'},
|
|
2275
|
+
]);
|
|
2276
|
+
});
|
|
2277
|
+
|
|
2278
|
+
it('returns zero if nothing matched by the "where" clause', async function () {
|
|
2279
|
+
const schema = createSchema();
|
|
2280
|
+
schema.defineModel({
|
|
2281
|
+
name: 'model',
|
|
2282
|
+
datasource: 'mongodb',
|
|
2283
|
+
properties: {
|
|
2284
|
+
foo: DataType.STRING,
|
|
2285
|
+
bar: DataType.STRING,
|
|
2286
|
+
},
|
|
2287
|
+
});
|
|
2288
|
+
const rep = schema.getRepository('model');
|
|
2289
|
+
const input1 = {foo: 'a1', bar: 'a2'};
|
|
2290
|
+
const input2 = {foo: 'b1', bar: 'b2'};
|
|
2291
|
+
const input3 = {foo: 'c1', bar: 'c2'};
|
|
2292
|
+
const created1 = await rep.create(input1);
|
|
2293
|
+
const created2 = await rep.create(input2);
|
|
2294
|
+
const created3 = await rep.create(input3);
|
|
2295
|
+
const id1 = created1[DEF_PK];
|
|
2296
|
+
const id2 = created2[DEF_PK];
|
|
2297
|
+
const id3 = created3[DEF_PK];
|
|
2298
|
+
const result = await rep.patch({foo: 'test'}, {baz: 'd3'});
|
|
2299
|
+
expect(result).to.be.eq(0);
|
|
2300
|
+
const rawData = await MDB_CLIENT.db()
|
|
2301
|
+
.collection('model')
|
|
2302
|
+
.find()
|
|
2303
|
+
.toArray();
|
|
2304
|
+
expect(rawData).to.be.eql([
|
|
2305
|
+
{_id: new ObjectId(id1), foo: 'a1', bar: 'a2'},
|
|
2306
|
+
{_id: new ObjectId(id2), foo: 'b1', bar: 'b2'},
|
|
2307
|
+
{_id: new ObjectId(id3), foo: 'c1', bar: 'c2'},
|
|
2308
|
+
]);
|
|
2309
|
+
});
|
|
2310
|
+
|
|
2311
|
+
it('uses the "where" clause to patch specific items', async function () {
|
|
2312
|
+
const schema = createSchema();
|
|
2313
|
+
schema.defineModel({
|
|
2314
|
+
name: 'model',
|
|
2315
|
+
datasource: 'mongodb',
|
|
2316
|
+
properties: {
|
|
2317
|
+
foo: DataType.STRING,
|
|
2318
|
+
bar: DataType.STRING,
|
|
2319
|
+
},
|
|
2320
|
+
});
|
|
2321
|
+
const rep = schema.getRepository('model');
|
|
2322
|
+
const input1 = {foo: 'a', bar: '1'};
|
|
2323
|
+
const input2 = {foo: 'b', bar: '2'};
|
|
2324
|
+
const input3 = {foo: 'c', bar: '2'};
|
|
2325
|
+
const created1 = await rep.create(input1);
|
|
2326
|
+
const created2 = await rep.create(input2);
|
|
2327
|
+
const created3 = await rep.create(input3);
|
|
2328
|
+
const id1 = created1[DEF_PK];
|
|
2329
|
+
const id2 = created2[DEF_PK];
|
|
2330
|
+
const id3 = created3[DEF_PK];
|
|
2331
|
+
const result = await rep.patch({foo: 'd'}, {bar: '2'});
|
|
2332
|
+
expect(result).to.be.eq(2);
|
|
2333
|
+
const rawData = await MDB_CLIENT.db()
|
|
2334
|
+
.collection('model')
|
|
2335
|
+
.find()
|
|
2336
|
+
.toArray();
|
|
2337
|
+
expect(rawData).to.be.eql([
|
|
2338
|
+
{_id: new ObjectId(id1), foo: 'a', bar: '1'},
|
|
2339
|
+
{_id: new ObjectId(id2), foo: 'd', bar: '2'},
|
|
2340
|
+
{_id: new ObjectId(id3), foo: 'd', bar: '2'},
|
|
2341
|
+
]);
|
|
2342
|
+
});
|
|
2343
|
+
|
|
2344
|
+
it('the "where" clause uses property names instead of column names', async function () {
|
|
2345
|
+
const schema = createSchema();
|
|
2346
|
+
schema.defineModel({
|
|
2347
|
+
name: 'model',
|
|
2348
|
+
datasource: 'mongodb',
|
|
2349
|
+
properties: {
|
|
2350
|
+
foo: {
|
|
2351
|
+
type: DataType.STRING,
|
|
2352
|
+
columnName: 'fooCol',
|
|
2353
|
+
},
|
|
2354
|
+
bar: {
|
|
2355
|
+
type: DataType.STRING,
|
|
2356
|
+
columnName: 'barCol',
|
|
2357
|
+
},
|
|
2358
|
+
},
|
|
2359
|
+
});
|
|
2360
|
+
const rep = schema.getRepository('model');
|
|
2361
|
+
const input1 = {foo: 'a', bar: '1'};
|
|
2362
|
+
const input2 = {foo: 'b', bar: '2'};
|
|
2363
|
+
const input3 = {foo: 'c', bar: '2'};
|
|
2364
|
+
const created1 = await rep.create(input1);
|
|
2365
|
+
const created2 = await rep.create(input2);
|
|
2366
|
+
const created3 = await rep.create(input3);
|
|
2367
|
+
const id1 = created1[DEF_PK];
|
|
2368
|
+
const id2 = created2[DEF_PK];
|
|
2369
|
+
const id3 = created3[DEF_PK];
|
|
2370
|
+
const result = await rep.patch({foo: 'd'}, {bar: '2'});
|
|
2371
|
+
expect(result).to.be.eq(2);
|
|
2372
|
+
const rawData = await MDB_CLIENT.db()
|
|
2373
|
+
.collection('model')
|
|
2374
|
+
.find()
|
|
2375
|
+
.toArray();
|
|
2376
|
+
expect(rawData).to.be.eql([
|
|
2377
|
+
{_id: new ObjectId(id1), fooCol: 'a', barCol: '1'},
|
|
2378
|
+
{_id: new ObjectId(id2), fooCol: 'd', barCol: '2'},
|
|
2379
|
+
{_id: new ObjectId(id3), fooCol: 'd', barCol: '2'},
|
|
2380
|
+
]);
|
|
2381
|
+
});
|
|
2382
|
+
|
|
2383
|
+
it('the "where" clause uses a persisted data instead of default values in case of undefined', async function () {
|
|
2384
|
+
const schema = createSchema();
|
|
2385
|
+
schema.defineModel({
|
|
2386
|
+
name: 'model',
|
|
2387
|
+
datasource: 'mongodb',
|
|
2388
|
+
properties: {
|
|
2389
|
+
foo: DataType.STRING,
|
|
2390
|
+
bar: {
|
|
2391
|
+
type: DataType.STRING,
|
|
2392
|
+
default: 'barVal',
|
|
2393
|
+
},
|
|
2394
|
+
},
|
|
2395
|
+
});
|
|
2396
|
+
const rep = schema.getRepository('model');
|
|
2397
|
+
const input1 = {foo: 'a', bar: undefined};
|
|
2398
|
+
const input2 = {foo: 'b', bar: undefined};
|
|
2399
|
+
const input3 = {foo: 'c', bar: 10};
|
|
2400
|
+
const input4 = {foo: 'd', bar: null};
|
|
2401
|
+
const table = await MDB_CLIENT.db().collection('model');
|
|
2402
|
+
const {insertedIds} = await table.insertMany([
|
|
2403
|
+
input1,
|
|
2404
|
+
input2,
|
|
2405
|
+
input3,
|
|
2406
|
+
input4,
|
|
2407
|
+
]);
|
|
2408
|
+
const result = await rep.patch({foo: 'd'}, {bar: undefined});
|
|
2409
|
+
expect(result).to.be.eq(3);
|
|
2410
|
+
const rawData = await table.find().toArray();
|
|
2411
|
+
expect(rawData).to.be.eql([
|
|
2412
|
+
{_id: insertedIds[0], foo: 'd', bar: null},
|
|
2413
|
+
{_id: insertedIds[1], foo: 'd', bar: null},
|
|
2414
|
+
{_id: insertedIds[2], foo: 'c', bar: 10},
|
|
2415
|
+
{_id: insertedIds[3], foo: 'd', bar: null},
|
|
2416
|
+
]);
|
|
2417
|
+
});
|
|
2418
|
+
|
|
2419
|
+
it('the "where" clause uses a persisted data instead of default values in case of null', async function () {
|
|
2420
|
+
const schema = createSchema();
|
|
2421
|
+
schema.defineModel({
|
|
2422
|
+
name: 'model',
|
|
2423
|
+
datasource: 'mongodb',
|
|
2424
|
+
properties: {
|
|
2425
|
+
foo: DataType.STRING,
|
|
2426
|
+
bar: {
|
|
2427
|
+
type: DataType.STRING,
|
|
2428
|
+
default: 'barVal',
|
|
2429
|
+
},
|
|
2430
|
+
},
|
|
2431
|
+
});
|
|
2432
|
+
const rep = schema.getRepository('model');
|
|
2433
|
+
const input1 = {foo: 'a', bar: undefined};
|
|
2434
|
+
const input2 = {foo: 'b', bar: undefined};
|
|
2435
|
+
const input3 = {foo: 'c', bar: 10};
|
|
2436
|
+
const input4 = {foo: 'd', bar: null};
|
|
2437
|
+
const table = await MDB_CLIENT.db().collection('model');
|
|
2438
|
+
const {insertedIds} = await table.insertMany([
|
|
2439
|
+
input1,
|
|
2440
|
+
input2,
|
|
2441
|
+
input3,
|
|
2442
|
+
input4,
|
|
2443
|
+
]);
|
|
2444
|
+
const result = await rep.patch({foo: 'd'}, {bar: null});
|
|
2445
|
+
expect(result).to.be.eq(3);
|
|
2446
|
+
const rawData = await table.find().toArray();
|
|
2447
|
+
expect(rawData).to.be.eql([
|
|
2448
|
+
{_id: insertedIds[0], foo: 'd', bar: null},
|
|
2449
|
+
{_id: insertedIds[1], foo: 'd', bar: null},
|
|
2450
|
+
{_id: insertedIds[2], foo: 'c', bar: 10},
|
|
2451
|
+
{_id: insertedIds[3], foo: 'd', bar: null},
|
|
2452
|
+
]);
|
|
2453
|
+
});
|
|
2454
|
+
|
|
2455
|
+
describe('patches by a where clause', function () {
|
|
2456
|
+
it('matches by a document subset', async function () {
|
|
2457
|
+
const schema = createSchema();
|
|
2458
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2459
|
+
const rep = schema.getRepository('model');
|
|
2460
|
+
const input1 = await rep.create({foo: 5});
|
|
2461
|
+
const input2 = await rep.create({foo: 10});
|
|
2462
|
+
const input3 = await rep.create({foo: 20});
|
|
2463
|
+
const id1 = input1[DEF_PK];
|
|
2464
|
+
const id2 = input2[DEF_PK];
|
|
2465
|
+
const id3 = input3[DEF_PK];
|
|
2466
|
+
const result = await rep.patch({foo: 7}, {foo: 10});
|
|
2467
|
+
expect(result).to.be.eq(1);
|
|
2468
|
+
const rawData = await MDB_CLIENT.db()
|
|
2469
|
+
.collection('model')
|
|
2470
|
+
.find()
|
|
2471
|
+
.toArray();
|
|
2472
|
+
expect(rawData).to.be.eql([
|
|
2473
|
+
{_id: new ObjectId(id1), foo: 5},
|
|
2474
|
+
{_id: new ObjectId(id2), foo: 7},
|
|
2475
|
+
{_id: new ObjectId(id3), foo: 20},
|
|
2476
|
+
]);
|
|
2477
|
+
});
|
|
2478
|
+
|
|
2479
|
+
it('matches by the "eq" operator', async function () {
|
|
2480
|
+
const schema = createSchema();
|
|
2481
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2482
|
+
const rep = schema.getRepository('model');
|
|
2483
|
+
const input1 = await rep.create({foo: 5});
|
|
2484
|
+
const input2 = await rep.create({foo: 10});
|
|
2485
|
+
const input3 = await rep.create({foo: 20});
|
|
2486
|
+
const id1 = input1[DEF_PK];
|
|
2487
|
+
const id2 = input2[DEF_PK];
|
|
2488
|
+
const id3 = input3[DEF_PK];
|
|
2489
|
+
const result = await rep.patch({foo: 7}, {foo: {eq: 10}});
|
|
2490
|
+
expect(result).to.be.eq(1);
|
|
2491
|
+
const rawData = await MDB_CLIENT.db()
|
|
2492
|
+
.collection('model')
|
|
2493
|
+
.find()
|
|
2494
|
+
.toArray();
|
|
2495
|
+
expect(rawData).to.be.eql([
|
|
2496
|
+
{_id: new ObjectId(id1), foo: 5},
|
|
2497
|
+
{_id: new ObjectId(id2), foo: 7},
|
|
2498
|
+
{_id: new ObjectId(id3), foo: 20},
|
|
2499
|
+
]);
|
|
2500
|
+
});
|
|
2501
|
+
|
|
2502
|
+
it('matches by the "neq" operator', async function () {
|
|
2503
|
+
const schema = createSchema();
|
|
2504
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2505
|
+
const rep = schema.getRepository('model');
|
|
2506
|
+
const input1 = await rep.create({foo: 5});
|
|
2507
|
+
const input2 = await rep.create({foo: 10});
|
|
2508
|
+
const input3 = await rep.create({foo: 20});
|
|
2509
|
+
const id1 = input1[DEF_PK];
|
|
2510
|
+
const id2 = input2[DEF_PK];
|
|
2511
|
+
const id3 = input3[DEF_PK];
|
|
2512
|
+
const result = await rep.patch({foo: 7}, {foo: {neq: 10}});
|
|
2513
|
+
expect(result).to.be.eq(2);
|
|
2514
|
+
const rawData = await MDB_CLIENT.db()
|
|
2515
|
+
.collection('model')
|
|
2516
|
+
.find()
|
|
2517
|
+
.toArray();
|
|
2518
|
+
expect(rawData).to.be.eql([
|
|
2519
|
+
{_id: new ObjectId(id1), foo: 7},
|
|
2520
|
+
{_id: new ObjectId(id2), foo: 10},
|
|
2521
|
+
{_id: new ObjectId(id3), foo: 7},
|
|
2522
|
+
]);
|
|
2523
|
+
});
|
|
2524
|
+
|
|
2525
|
+
it('matches by the "gt" operator', async function () {
|
|
2526
|
+
const schema = createSchema();
|
|
2527
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2528
|
+
const rep = schema.getRepository('model');
|
|
2529
|
+
const input1 = await rep.create({foo: 5});
|
|
2530
|
+
const input2 = await rep.create({foo: 10});
|
|
2531
|
+
const input3 = await rep.create({foo: 20});
|
|
2532
|
+
const id1 = input1[DEF_PK];
|
|
2533
|
+
const id2 = input2[DEF_PK];
|
|
2534
|
+
const id3 = input3[DEF_PK];
|
|
2535
|
+
const result = await rep.patch({foo: 7}, {foo: {gt: 10}});
|
|
2536
|
+
expect(result).to.be.eq(1);
|
|
2537
|
+
const rawData = await MDB_CLIENT.db()
|
|
2538
|
+
.collection('model')
|
|
2539
|
+
.find()
|
|
2540
|
+
.toArray();
|
|
2541
|
+
expect(rawData).to.be.eql([
|
|
2542
|
+
{_id: new ObjectId(id1), foo: 5},
|
|
2543
|
+
{_id: new ObjectId(id2), foo: 10},
|
|
2544
|
+
{_id: new ObjectId(id3), foo: 7},
|
|
2545
|
+
]);
|
|
2546
|
+
});
|
|
2547
|
+
|
|
2548
|
+
it('matches by the "lt" operator', async function () {
|
|
2549
|
+
const schema = createSchema();
|
|
2550
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2551
|
+
const rep = schema.getRepository('model');
|
|
2552
|
+
const input1 = await rep.create({foo: 5});
|
|
2553
|
+
const input2 = await rep.create({foo: 10});
|
|
2554
|
+
const input3 = await rep.create({foo: 20});
|
|
2555
|
+
const id1 = input1[DEF_PK];
|
|
2556
|
+
const id2 = input2[DEF_PK];
|
|
2557
|
+
const id3 = input3[DEF_PK];
|
|
2558
|
+
const result = await rep.patch({foo: 7}, {foo: {lt: 10}});
|
|
2559
|
+
expect(result).to.be.eq(1);
|
|
2560
|
+
const rawData = await MDB_CLIENT.db()
|
|
2561
|
+
.collection('model')
|
|
2562
|
+
.find()
|
|
2563
|
+
.toArray();
|
|
2564
|
+
expect(rawData).to.be.eql([
|
|
2565
|
+
{_id: new ObjectId(id1), foo: 7},
|
|
2566
|
+
{_id: new ObjectId(id2), foo: 10},
|
|
2567
|
+
{_id: new ObjectId(id3), foo: 20},
|
|
2568
|
+
]);
|
|
2569
|
+
});
|
|
2570
|
+
|
|
2571
|
+
it('matches by the "gte" operator', async function () {
|
|
2572
|
+
const schema = createSchema();
|
|
2573
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2574
|
+
const rep = schema.getRepository('model');
|
|
2575
|
+
const input1 = await rep.create({foo: 5});
|
|
2576
|
+
const input2 = await rep.create({foo: 10});
|
|
2577
|
+
const input3 = await rep.create({foo: 20});
|
|
2578
|
+
const id1 = input1[DEF_PK];
|
|
2579
|
+
const id2 = input2[DEF_PK];
|
|
2580
|
+
const id3 = input3[DEF_PK];
|
|
2581
|
+
const result = await rep.patch({foo: 7}, {foo: {gte: 10}});
|
|
2582
|
+
expect(result).to.be.eq(2);
|
|
2583
|
+
const rawData = await MDB_CLIENT.db()
|
|
2584
|
+
.collection('model')
|
|
2585
|
+
.find()
|
|
2586
|
+
.toArray();
|
|
2587
|
+
expect(rawData).to.be.eql([
|
|
2588
|
+
{_id: new ObjectId(id1), foo: 5},
|
|
2589
|
+
{_id: new ObjectId(id2), foo: 7},
|
|
2590
|
+
{_id: new ObjectId(id3), foo: 7},
|
|
2591
|
+
]);
|
|
2592
|
+
});
|
|
2593
|
+
|
|
2594
|
+
it('matches by the "lte" operator', async function () {
|
|
2595
|
+
const schema = createSchema();
|
|
2596
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2597
|
+
const rep = schema.getRepository('model');
|
|
2598
|
+
const input1 = await rep.create({foo: 5});
|
|
2599
|
+
const input2 = await rep.create({foo: 10});
|
|
2600
|
+
const input3 = await rep.create({foo: 20});
|
|
2601
|
+
const id1 = input1[DEF_PK];
|
|
2602
|
+
const id2 = input2[DEF_PK];
|
|
2603
|
+
const id3 = input3[DEF_PK];
|
|
2604
|
+
const result = await rep.patch({foo: 7}, {foo: {lte: 10}});
|
|
2605
|
+
expect(result).to.be.eq(2);
|
|
2606
|
+
const rawData = await MDB_CLIENT.db()
|
|
2607
|
+
.collection('model')
|
|
2608
|
+
.find()
|
|
2609
|
+
.toArray();
|
|
2610
|
+
expect(rawData).to.be.eql([
|
|
2611
|
+
{_id: new ObjectId(id1), foo: 7},
|
|
2612
|
+
{_id: new ObjectId(id2), foo: 7},
|
|
2613
|
+
{_id: new ObjectId(id3), foo: 20},
|
|
2614
|
+
]);
|
|
2615
|
+
});
|
|
2616
|
+
|
|
2617
|
+
it('matches by the "inq" operator', async function () {
|
|
2618
|
+
const schema = createSchema();
|
|
2619
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2620
|
+
const rep = schema.getRepository('model');
|
|
2621
|
+
const input1 = await rep.create({foo: 5});
|
|
2622
|
+
const input2 = await rep.create({foo: 10});
|
|
2623
|
+
const input3 = await rep.create({foo: 20});
|
|
2624
|
+
const id1 = input1[DEF_PK];
|
|
2625
|
+
const id2 = input2[DEF_PK];
|
|
2626
|
+
const id3 = input3[DEF_PK];
|
|
2627
|
+
const result = await rep.patch({foo: 7}, {foo: {inq: [5, 10]}});
|
|
2628
|
+
expect(result).to.be.eq(2);
|
|
2629
|
+
const rawData = await MDB_CLIENT.db()
|
|
2630
|
+
.collection('model')
|
|
2631
|
+
.find()
|
|
2632
|
+
.toArray();
|
|
2633
|
+
expect(rawData).to.be.eql([
|
|
2634
|
+
{_id: new ObjectId(id1), foo: 7},
|
|
2635
|
+
{_id: new ObjectId(id2), foo: 7},
|
|
2636
|
+
{_id: new ObjectId(id3), foo: 20},
|
|
2637
|
+
]);
|
|
2638
|
+
});
|
|
2639
|
+
|
|
2640
|
+
it('matches by the "nin" operator', async function () {
|
|
2641
|
+
const schema = createSchema();
|
|
2642
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2643
|
+
const rep = schema.getRepository('model');
|
|
2644
|
+
const input1 = await rep.create({foo: 5});
|
|
2645
|
+
const input2 = await rep.create({foo: 10});
|
|
2646
|
+
const input3 = await rep.create({foo: 20});
|
|
2647
|
+
const id1 = input1[DEF_PK];
|
|
2648
|
+
const id2 = input2[DEF_PK];
|
|
2649
|
+
const id3 = input3[DEF_PK];
|
|
2650
|
+
const result = await rep.patch({foo: 7}, {foo: {nin: [5, 10]}});
|
|
2651
|
+
expect(result).to.be.eq(1);
|
|
2652
|
+
const rawData = await MDB_CLIENT.db()
|
|
2653
|
+
.collection('model')
|
|
2654
|
+
.find()
|
|
2655
|
+
.toArray();
|
|
2656
|
+
expect(rawData).to.be.eql([
|
|
2657
|
+
{_id: new ObjectId(id1), foo: 5},
|
|
2658
|
+
{_id: new ObjectId(id2), foo: 10},
|
|
2659
|
+
{_id: new ObjectId(id3), foo: 7},
|
|
2660
|
+
]);
|
|
2661
|
+
});
|
|
2662
|
+
|
|
2663
|
+
it('matches by the "between" operator', async function () {
|
|
2664
|
+
const schema = createSchema();
|
|
2665
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2666
|
+
const rep = schema.getRepository('model');
|
|
2667
|
+
const input1 = await rep.create({foo: 5});
|
|
2668
|
+
const input2 = await rep.create({foo: 10});
|
|
2669
|
+
const input3 = await rep.create({foo: 20});
|
|
2670
|
+
const id1 = input1[DEF_PK];
|
|
2671
|
+
const id2 = input2[DEF_PK];
|
|
2672
|
+
const id3 = input3[DEF_PK];
|
|
2673
|
+
const result = await rep.patch({foo: 7}, {foo: {between: [5, 10]}});
|
|
2674
|
+
expect(result).to.be.eq(2);
|
|
2675
|
+
const rawData = await MDB_CLIENT.db()
|
|
2676
|
+
.collection('model')
|
|
2677
|
+
.find()
|
|
2678
|
+
.toArray();
|
|
2679
|
+
expect(rawData).to.be.eql([
|
|
2680
|
+
{_id: new ObjectId(id1), foo: 7},
|
|
2681
|
+
{_id: new ObjectId(id2), foo: 7},
|
|
2682
|
+
{_id: new ObjectId(id3), foo: 20},
|
|
2683
|
+
]);
|
|
2684
|
+
});
|
|
2685
|
+
|
|
2686
|
+
it('matches by the "exists" operator in case of true', async function () {
|
|
2687
|
+
const schema = createSchema();
|
|
2688
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2689
|
+
const rep = schema.getRepository('model');
|
|
2690
|
+
const input1 = await rep.create({foo: undefined});
|
|
2691
|
+
const input2 = await rep.create({foo: null});
|
|
2692
|
+
const input3 = await rep.create({foo: 10});
|
|
2693
|
+
const input4 = await rep.create({bar: 20});
|
|
2694
|
+
const id1 = input1[DEF_PK];
|
|
2695
|
+
const id2 = input2[DEF_PK];
|
|
2696
|
+
const id3 = input3[DEF_PK];
|
|
2697
|
+
const id4 = input4[DEF_PK];
|
|
2698
|
+
const result = await rep.patch({foo: 7}, {foo: {exists: true}});
|
|
2699
|
+
expect(result).to.be.eq(3);
|
|
2700
|
+
const rawData = await MDB_CLIENT.db()
|
|
2701
|
+
.collection('model')
|
|
2702
|
+
.find()
|
|
2703
|
+
.toArray();
|
|
2704
|
+
expect(rawData).to.be.eql([
|
|
2705
|
+
{_id: new ObjectId(id1), foo: 7},
|
|
2706
|
+
{_id: new ObjectId(id2), foo: 7},
|
|
2707
|
+
{_id: new ObjectId(id3), foo: 7},
|
|
2708
|
+
{_id: new ObjectId(id4), bar: 20},
|
|
2709
|
+
]);
|
|
2710
|
+
});
|
|
2711
|
+
|
|
2712
|
+
it('matches by the "exists" operator in case of false', async function () {
|
|
2713
|
+
const schema = createSchema();
|
|
2714
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2715
|
+
const rep = schema.getRepository('model');
|
|
2716
|
+
const input1 = await rep.create({foo: undefined});
|
|
2717
|
+
const input2 = await rep.create({foo: null});
|
|
2718
|
+
const input3 = await rep.create({foo: 10});
|
|
2719
|
+
const input4 = await rep.create({bar: 20});
|
|
2720
|
+
const id1 = input1[DEF_PK];
|
|
2721
|
+
const id2 = input2[DEF_PK];
|
|
2722
|
+
const id3 = input3[DEF_PK];
|
|
2723
|
+
const id4 = input4[DEF_PK];
|
|
2724
|
+
const result = await rep.patch({foo: 7}, {foo: {exists: false}});
|
|
2725
|
+
expect(result).to.be.eq(1);
|
|
2726
|
+
const rawData = await MDB_CLIENT.db()
|
|
2727
|
+
.collection('model')
|
|
2728
|
+
.find()
|
|
2729
|
+
.toArray();
|
|
2730
|
+
expect(rawData).to.be.eql([
|
|
2731
|
+
{_id: new ObjectId(id1), foo: null},
|
|
2732
|
+
{_id: new ObjectId(id2), foo: null},
|
|
2733
|
+
{_id: new ObjectId(id3), foo: 10},
|
|
2734
|
+
{_id: new ObjectId(id4), foo: 7, bar: 20},
|
|
2735
|
+
]);
|
|
2736
|
+
});
|
|
2737
|
+
|
|
2738
|
+
it('matches by the "like" operator', async function () {
|
|
2739
|
+
const schema = createSchema();
|
|
2740
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2741
|
+
const rep = schema.getRepository('model');
|
|
2742
|
+
const input1 = await rep.create({foo: 'lorem ipsum'});
|
|
2743
|
+
const input2 = await rep.create({foo: 'dolor sit amet'});
|
|
2744
|
+
const input3 = await rep.create({foo: 'DOLOR SIT AMET'});
|
|
2745
|
+
const input4 = await rep.create({foo: 'sit amet'});
|
|
2746
|
+
const id1 = input1[DEF_PK];
|
|
2747
|
+
const id2 = input2[DEF_PK];
|
|
2748
|
+
const id3 = input3[DEF_PK];
|
|
2749
|
+
const id4 = input4[DEF_PK];
|
|
2750
|
+
const result = await rep.patch(
|
|
2751
|
+
{foo: 'test'},
|
|
2752
|
+
{foo: {like: 'sit amet'}},
|
|
2753
|
+
);
|
|
2754
|
+
expect(result).to.be.eq(2);
|
|
2755
|
+
const rawData = await MDB_CLIENT.db()
|
|
2756
|
+
.collection('model')
|
|
2757
|
+
.find()
|
|
2758
|
+
.toArray();
|
|
2759
|
+
expect(rawData).to.be.eql([
|
|
2760
|
+
{_id: new ObjectId(id1), foo: 'lorem ipsum'},
|
|
2761
|
+
{_id: new ObjectId(id2), foo: 'test'},
|
|
2762
|
+
{_id: new ObjectId(id3), foo: 'DOLOR SIT AMET'},
|
|
2763
|
+
{_id: new ObjectId(id4), foo: 'test'},
|
|
2764
|
+
]);
|
|
2765
|
+
});
|
|
2766
|
+
|
|
2767
|
+
it('matches by the "nlike" operator', async function () {
|
|
2768
|
+
const schema = createSchema();
|
|
2769
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2770
|
+
const rep = schema.getRepository('model');
|
|
2771
|
+
const input1 = await rep.create({foo: 'lorem ipsum'});
|
|
2772
|
+
const input2 = await rep.create({foo: 'dolor sit amet'});
|
|
2773
|
+
const input3 = await rep.create({foo: 'DOLOR SIT AMET'});
|
|
2774
|
+
const input4 = await rep.create({foo: 'sit amet'});
|
|
2775
|
+
const id1 = input1[DEF_PK];
|
|
2776
|
+
const id2 = input2[DEF_PK];
|
|
2777
|
+
const id3 = input3[DEF_PK];
|
|
2778
|
+
const id4 = input4[DEF_PK];
|
|
2779
|
+
const result = await rep.patch(
|
|
2780
|
+
{foo: 'test'},
|
|
2781
|
+
{foo: {nlike: 'sit amet'}},
|
|
2782
|
+
);
|
|
2783
|
+
expect(result).to.be.eq(2);
|
|
2784
|
+
const rawData = await MDB_CLIENT.db()
|
|
2785
|
+
.collection('model')
|
|
2786
|
+
.find()
|
|
2787
|
+
.toArray();
|
|
2788
|
+
expect(rawData).to.be.eql([
|
|
2789
|
+
{_id: new ObjectId(id1), foo: 'test'},
|
|
2790
|
+
{_id: new ObjectId(id2), foo: 'dolor sit amet'},
|
|
2791
|
+
{_id: new ObjectId(id3), foo: 'test'},
|
|
2792
|
+
{_id: new ObjectId(id4), foo: 'sit amet'},
|
|
2793
|
+
]);
|
|
2794
|
+
});
|
|
2795
|
+
|
|
2796
|
+
it('matches by the "ilike" operator', async function () {
|
|
2797
|
+
const schema = createSchema();
|
|
2798
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2799
|
+
const rep = schema.getRepository('model');
|
|
2800
|
+
const input1 = await rep.create({foo: 'lorem ipsum'});
|
|
2801
|
+
const input2 = await rep.create({foo: 'dolor sit amet'});
|
|
2802
|
+
const input3 = await rep.create({foo: 'DOLOR SIT AMET'});
|
|
2803
|
+
const input4 = await rep.create({foo: 'sit amet'});
|
|
2804
|
+
const id1 = input1[DEF_PK];
|
|
2805
|
+
const id2 = input2[DEF_PK];
|
|
2806
|
+
const id3 = input3[DEF_PK];
|
|
2807
|
+
const id4 = input4[DEF_PK];
|
|
2808
|
+
const result = await rep.patch(
|
|
2809
|
+
{foo: 'test'},
|
|
2810
|
+
{foo: {ilike: 'sit amet'}},
|
|
2811
|
+
);
|
|
2812
|
+
expect(result).to.be.eq(3);
|
|
2813
|
+
const rawData = await MDB_CLIENT.db()
|
|
2814
|
+
.collection('model')
|
|
2815
|
+
.find()
|
|
2816
|
+
.toArray();
|
|
2817
|
+
expect(rawData).to.be.eql([
|
|
2818
|
+
{_id: new ObjectId(id1), foo: 'lorem ipsum'},
|
|
2819
|
+
{_id: new ObjectId(id2), foo: 'test'},
|
|
2820
|
+
{_id: new ObjectId(id3), foo: 'test'},
|
|
2821
|
+
{_id: new ObjectId(id4), foo: 'test'},
|
|
2822
|
+
]);
|
|
2823
|
+
});
|
|
2824
|
+
|
|
2825
|
+
it('matches by the "nilike" operator', async function () {
|
|
2826
|
+
const schema = createSchema();
|
|
2827
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2828
|
+
const rep = schema.getRepository('model');
|
|
2829
|
+
const input1 = await rep.create({foo: 'lorem ipsum'});
|
|
2830
|
+
const input2 = await rep.create({foo: 'dolor sit amet'});
|
|
2831
|
+
const input3 = await rep.create({foo: 'DOLOR SIT AMET'});
|
|
2832
|
+
const input4 = await rep.create({foo: 'sit amet'});
|
|
2833
|
+
const id1 = input1[DEF_PK];
|
|
2834
|
+
const id2 = input2[DEF_PK];
|
|
2835
|
+
const id3 = input3[DEF_PK];
|
|
2836
|
+
const id4 = input4[DEF_PK];
|
|
2837
|
+
const result = await rep.patch(
|
|
2838
|
+
{foo: 'test'},
|
|
2839
|
+
{foo: {nilike: 'sit amet'}},
|
|
2840
|
+
);
|
|
2841
|
+
expect(result).to.be.eq(1);
|
|
2842
|
+
const rawData = await MDB_CLIENT.db()
|
|
2843
|
+
.collection('model')
|
|
2844
|
+
.find()
|
|
2845
|
+
.toArray();
|
|
2846
|
+
expect(rawData).to.be.eql([
|
|
2847
|
+
{_id: new ObjectId(id1), foo: 'test'},
|
|
2848
|
+
{_id: new ObjectId(id2), foo: 'dolor sit amet'},
|
|
2849
|
+
{_id: new ObjectId(id3), foo: 'DOLOR SIT AMET'},
|
|
2850
|
+
{_id: new ObjectId(id4), foo: 'sit amet'},
|
|
2851
|
+
]);
|
|
2852
|
+
});
|
|
2853
|
+
|
|
2854
|
+
it('matches by the "regexp" operator', async function () {
|
|
2855
|
+
const schema = createSchema();
|
|
2856
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2857
|
+
const rep = schema.getRepository('model');
|
|
2858
|
+
const input1 = await rep.create({foo: 'lorem ipsum'});
|
|
2859
|
+
const input2 = await rep.create({foo: 'dolor sit amet'});
|
|
2860
|
+
const input3 = await rep.create({foo: 'DOLOR SIT AMET'});
|
|
2861
|
+
const input4 = await rep.create({foo: 'sit amet'});
|
|
2862
|
+
const id1 = input1[DEF_PK];
|
|
2863
|
+
const id2 = input2[DEF_PK];
|
|
2864
|
+
const id3 = input3[DEF_PK];
|
|
2865
|
+
const id4 = input4[DEF_PK];
|
|
2866
|
+
const result = await rep.patch(
|
|
2867
|
+
{foo: 'test'},
|
|
2868
|
+
{foo: {regexp: 'sit am+'}},
|
|
2869
|
+
);
|
|
2870
|
+
expect(result).to.be.eq(2);
|
|
2871
|
+
const rawData = await MDB_CLIENT.db()
|
|
2872
|
+
.collection('model')
|
|
2873
|
+
.find()
|
|
2874
|
+
.toArray();
|
|
2875
|
+
expect(rawData).to.be.eql([
|
|
2876
|
+
{_id: new ObjectId(id1), foo: 'lorem ipsum'},
|
|
2877
|
+
{_id: new ObjectId(id2), foo: 'test'},
|
|
2878
|
+
{_id: new ObjectId(id3), foo: 'DOLOR SIT AMET'},
|
|
2879
|
+
{_id: new ObjectId(id4), foo: 'test'},
|
|
2880
|
+
]);
|
|
2881
|
+
});
|
|
2882
|
+
|
|
2883
|
+
it('matches by the "regexp" operator with flags', async function () {
|
|
2884
|
+
const schema = createSchema();
|
|
2885
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2886
|
+
const rep = schema.getRepository('model');
|
|
2887
|
+
const input1 = await rep.create({foo: 'lorem ipsum'});
|
|
2888
|
+
const input2 = await rep.create({foo: 'dolor sit amet'});
|
|
2889
|
+
const input3 = await rep.create({foo: 'DOLOR SIT AMET'});
|
|
2890
|
+
const input4 = await rep.create({foo: 'sit amet'});
|
|
2891
|
+
const id1 = input1[DEF_PK];
|
|
2892
|
+
const id2 = input2[DEF_PK];
|
|
2893
|
+
const id3 = input3[DEF_PK];
|
|
2894
|
+
const id4 = input4[DEF_PK];
|
|
2895
|
+
const result = await rep.patch(
|
|
2896
|
+
{foo: 'test'},
|
|
2897
|
+
{foo: {regexp: 'sit am+', flags: 'i'}},
|
|
2898
|
+
);
|
|
2899
|
+
expect(result).to.be.eq(3);
|
|
2900
|
+
const rawData = await MDB_CLIENT.db()
|
|
2901
|
+
.collection('model')
|
|
2902
|
+
.find()
|
|
2903
|
+
.toArray();
|
|
2904
|
+
expect(rawData).to.be.eql([
|
|
2905
|
+
{_id: new ObjectId(id1), foo: 'lorem ipsum'},
|
|
2906
|
+
{_id: new ObjectId(id2), foo: 'test'},
|
|
2907
|
+
{_id: new ObjectId(id3), foo: 'test'},
|
|
2908
|
+
{_id: new ObjectId(id4), foo: 'test'},
|
|
2909
|
+
]);
|
|
2910
|
+
});
|
|
2911
|
+
});
|
|
2912
|
+
});
|
|
2913
|
+
|
|
1980
2914
|
describe('patchById', function () {
|
|
1981
2915
|
it('updates only provided properties by a given identifier', async function () {
|
|
1982
2916
|
const schema = createSchema();
|
|
@@ -2668,22 +3602,32 @@ describe('MongodbAdapter', function () {
|
|
|
2668
3602
|
]);
|
|
2669
3603
|
});
|
|
2670
3604
|
|
|
2671
|
-
it('matches by the "exists" operator', async function () {
|
|
3605
|
+
it('matches by the "exists" operator in case of true', async function () {
|
|
2672
3606
|
const schema = createSchema();
|
|
2673
3607
|
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
2674
3608
|
const rep = schema.getRepository('model');
|
|
2675
|
-
|
|
3609
|
+
await rep.create({});
|
|
2676
3610
|
const created2 = await rep.create({foo: undefined});
|
|
2677
3611
|
const created3 = await rep.create({foo: null});
|
|
2678
3612
|
const created4 = await rep.create({foo: 10});
|
|
2679
|
-
const
|
|
2680
|
-
|
|
2681
|
-
expect(result1).to.be.eql([
|
|
3613
|
+
const result = await rep.find({where: {foo: {exists: true}}});
|
|
3614
|
+
expect(result).to.be.eql([
|
|
2682
3615
|
{[DEF_PK]: created2[DEF_PK], foo: null},
|
|
2683
3616
|
{[DEF_PK]: created3[DEF_PK], foo: null},
|
|
2684
3617
|
{[DEF_PK]: created4[DEF_PK], foo: 10},
|
|
2685
3618
|
]);
|
|
2686
|
-
|
|
3619
|
+
});
|
|
3620
|
+
|
|
3621
|
+
it('matches by the "exists" operator in case of false', async function () {
|
|
3622
|
+
const schema = createSchema();
|
|
3623
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
3624
|
+
const rep = schema.getRepository('model');
|
|
3625
|
+
const created1 = await rep.create({});
|
|
3626
|
+
await rep.create({foo: undefined});
|
|
3627
|
+
await rep.create({foo: null});
|
|
3628
|
+
await rep.create({foo: 10});
|
|
3629
|
+
const result = await rep.find({where: {foo: {exists: false}}});
|
|
3630
|
+
expect(result).to.be.eql([{[DEF_PK]: created1[DEF_PK]}]);
|
|
2687
3631
|
});
|
|
2688
3632
|
|
|
2689
3633
|
it('matches by the "like" operator', async function () {
|
|
@@ -3018,7 +3962,7 @@ describe('MongodbAdapter', function () {
|
|
|
3018
3962
|
]);
|
|
3019
3963
|
});
|
|
3020
3964
|
|
|
3021
|
-
it('matches by the "exists" operator', async function () {
|
|
3965
|
+
it('matches by the "exists" operator in case of true', async function () {
|
|
3022
3966
|
const schema = createSchema();
|
|
3023
3967
|
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
3024
3968
|
const rep = schema.getRepository('model');
|
|
@@ -3026,13 +3970,38 @@ describe('MongodbAdapter', function () {
|
|
|
3026
3970
|
await rep.create({foo: undefined});
|
|
3027
3971
|
await rep.create({foo: null});
|
|
3028
3972
|
await rep.create({foo: 10});
|
|
3973
|
+
const id1 = created1[DEF_PK];
|
|
3029
3974
|
const result = await rep.delete({foo: {exists: true}});
|
|
3030
3975
|
expect(result).to.be.eq(3);
|
|
3031
3976
|
const rawData = await MDB_CLIENT.db()
|
|
3032
3977
|
.collection('model')
|
|
3033
3978
|
.find()
|
|
3034
3979
|
.toArray();
|
|
3035
|
-
expect(rawData).to.be.eql([{_id: new ObjectId(
|
|
3980
|
+
expect(rawData).to.be.eql([{_id: new ObjectId(id1)}]);
|
|
3981
|
+
});
|
|
3982
|
+
|
|
3983
|
+
it('matches by the "exists" operator in case of false', async function () {
|
|
3984
|
+
const schema = createSchema();
|
|
3985
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
3986
|
+
const rep = schema.getRepository('model');
|
|
3987
|
+
await rep.create({});
|
|
3988
|
+
const created2 = await rep.create({foo: undefined});
|
|
3989
|
+
const created3 = await rep.create({foo: null});
|
|
3990
|
+
const created4 = await rep.create({foo: 10});
|
|
3991
|
+
const id2 = created2[DEF_PK];
|
|
3992
|
+
const id3 = created3[DEF_PK];
|
|
3993
|
+
const id4 = created4[DEF_PK];
|
|
3994
|
+
const result = await rep.delete({foo: {exists: false}});
|
|
3995
|
+
expect(result).to.be.eq(1);
|
|
3996
|
+
const rawData = await MDB_CLIENT.db()
|
|
3997
|
+
.collection('model')
|
|
3998
|
+
.find()
|
|
3999
|
+
.toArray();
|
|
4000
|
+
expect(rawData).to.be.eql([
|
|
4001
|
+
{_id: new ObjectId(id2), foo: null},
|
|
4002
|
+
{_id: new ObjectId(id3), foo: null},
|
|
4003
|
+
{_id: new ObjectId(id4), foo: 10},
|
|
4004
|
+
]);
|
|
3036
4005
|
});
|
|
3037
4006
|
|
|
3038
4007
|
it('matches by the "like" operator', async function () {
|
|
@@ -3335,7 +4304,7 @@ describe('MongodbAdapter', function () {
|
|
|
3335
4304
|
expect(result).to.be.eq(2);
|
|
3336
4305
|
});
|
|
3337
4306
|
|
|
3338
|
-
it('matches by the "exists" operator', async function () {
|
|
4307
|
+
it('matches by the "exists" operator in case of true', async function () {
|
|
3339
4308
|
const schema = createSchema();
|
|
3340
4309
|
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
3341
4310
|
const rep = schema.getRepository('model');
|
|
@@ -3347,6 +4316,18 @@ describe('MongodbAdapter', function () {
|
|
|
3347
4316
|
expect(result1).to.be.eq(3);
|
|
3348
4317
|
});
|
|
3349
4318
|
|
|
4319
|
+
it('matches by the "exists" operator in case of false', async function () {
|
|
4320
|
+
const schema = createSchema();
|
|
4321
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
4322
|
+
const rep = schema.getRepository('model');
|
|
4323
|
+
await rep.create({});
|
|
4324
|
+
await rep.create({foo: undefined});
|
|
4325
|
+
await rep.create({foo: null});
|
|
4326
|
+
await rep.create({foo: 10});
|
|
4327
|
+
const result1 = await rep.count({foo: {exists: false}});
|
|
4328
|
+
expect(result1).to.be.eq(1);
|
|
4329
|
+
});
|
|
4330
|
+
|
|
3350
4331
|
it('matches by the "like" operator', async function () {
|
|
3351
4332
|
const schema = createSchema();
|
|
3352
4333
|
schema.defineModel({name: 'model', datasource: 'mongodb'});
|