mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-04-24 16:00:53 +08:00
feat(connection-mongodb): Add MongoDBBulkWrite request type to the MongoDB connection.
This commit is contained in:
parent
a2bf785c98
commit
143c83e7dd
.changeset
packages
docs/connections
plugins/connections/connection-mongodb
package.json
src/connections/MongoDBCollection/MongoDBBulkWrite
5
.changeset/lazy-bikes-suffer.md
Normal file
5
.changeset/lazy-bikes-suffer.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
'@lowdefy/connection-mongodb': minor
|
||||
---
|
||||
|
||||
Add request type `MongoDBBulkWrite` to the MongoDB connection.
|
@ -106,6 +106,7 @@ _ref:
|
||||
|
||||
Request types:
|
||||
- MongoDBAggregation
|
||||
- MongoDBBulkWrite
|
||||
- MongoDBDeleteMany
|
||||
- MongoDBDeleteOne
|
||||
- MongoDBFind
|
||||
@ -171,6 +172,85 @@ _ref:
|
||||
score: 1
|
||||
```
|
||||
|
||||
### MongoDBBulkWrite
|
||||
|
||||
The `MongoDBBulkWrite` request executes [bulkWrite operations](https://www.mongodb.com/docs/bulk-write-operations/#bulk-write-operations) in the collection specified in the connectionId.
|
||||
|
||||
#### Properties
|
||||
- `operations: object[]`: __Required__ - Array containing all the bulkWrite operations for the execution.
|
||||
- `insertOne: object`:
|
||||
- `document: object`: The document to be inserted.
|
||||
- `deleteOne: object`:
|
||||
- `filter: object`: __Required__ - The filter used to select the document to delete.
|
||||
- `collation: object`: Specify collation settings for update operation.
|
||||
- `deleteMany: object`:
|
||||
- `filter: object`: __Required__ - The filter used to select the documents to delete.
|
||||
- `collation: object`: Specify collation settings for update operation.
|
||||
- `updateOne: object`:
|
||||
- `filter: object`: __Required__ - The filter used to select the document to update.
|
||||
- `update: object | object[]`: __Required__ - The update operations to be applied to the document.
|
||||
- `upsert: object`: Insert document if no match is found.
|
||||
- `arrayFilters: string[]`: Array filters for the [`$[<identifier>]`](https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/) array update operator.
|
||||
- `collation: object`: Specify collation settings for update operation.
|
||||
- `hint: object | string`: 'An optional hint for query optimization.'
|
||||
- `updateMany: object`:
|
||||
- `filter: object`: __Required__ - The filter used to select the documents to update.
|
||||
- `update: object | object[]`: __Required__ - The update operations to be applied to the documents.
|
||||
- `upsert: object`: Insert document if no match is found.
|
||||
- `arrayFilters: string[]`: Array filters for the [`$[<identifier>]`](https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/) array update operator.
|
||||
- `collation: object`: Specify collation settings for update operation.
|
||||
- `hint: object | string`: An optional hint for query optimization.
|
||||
- `replaceOne: object`:
|
||||
- `filter: object`: __Required__ - The filter used to select the document to replace.
|
||||
- `replacement: object`: __Required__ - The document to be inserted.
|
||||
- `upsert: object`: Insert document if no match is found.
|
||||
- `collation: object`: Specify collation settings for update operation.
|
||||
- `hint: object | string`: An optional hint for query optimization.
|
||||
- `options: object`: Optional settings. See the [driver documentation](https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#aggregate) for more information. Supported settings are:
|
||||
- `ordered: boolean`: Default: `true` - A boolean specifying whether the mongod instance should perform an ordered or unordered operation execution.
|
||||
- `writeConcern: object`: An object that expresses the write concern to use.
|
||||
|
||||
#### Examples
|
||||
|
||||
###### Update pizzas:
|
||||
|
||||
```yaml
|
||||
requests:
|
||||
- id: update_pizzas
|
||||
type: MongoDBBulkWrite
|
||||
connectionId: my_mongodb_collection_id
|
||||
properties:
|
||||
operations:
|
||||
- insertOne:
|
||||
document:
|
||||
_id: 3
|
||||
type: "beef"
|
||||
size: "medium"
|
||||
price: 6
|
||||
- insertOne:
|
||||
document:
|
||||
_id: 4
|
||||
type: "sausage"
|
||||
size: "large"
|
||||
price: 10
|
||||
- updateOne:
|
||||
filter:
|
||||
type: "cheese"
|
||||
update:
|
||||
$set:
|
||||
price: 8
|
||||
- deleteOne:
|
||||
filter:
|
||||
type: "pepperoni"
|
||||
- replaceOne:
|
||||
filter:
|
||||
type: "vegan"
|
||||
replacement:
|
||||
type: "tofu"
|
||||
size: "small"
|
||||
price: 4
|
||||
```
|
||||
|
||||
### MongoDBDeleteMany
|
||||
|
||||
The `MongoDBDeleteMany` request deletes multiple documents in the collection specified in the connectionId. It requires a filter, which is written in the query syntax, to select a documents to delete.
|
||||
|
@ -20,6 +20,10 @@
|
||||
{
|
||||
"name": "Gerrie van Wyk",
|
||||
"url": "https://github.com/Gervwyk"
|
||||
},
|
||||
{
|
||||
"name": "Stephanie Smit",
|
||||
"url": "https://github.com/StephanieJKS"
|
||||
}
|
||||
],
|
||||
"repository": {
|
||||
|
42
packages/plugins/connections/connection-mongodb/src/connections/MongoDBCollection/MongoDBBulkWrite/MongoDBBulkWrite.js
Normal file
42
packages/plugins/connections/connection-mongodb/src/connections/MongoDBCollection/MongoDBBulkWrite/MongoDBBulkWrite.js
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
Copyright 2020-2024 Lowdefy, Inc
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import getCollection from '../getCollection.js';
|
||||
import { serialize, deserialize } from '../serialize.js';
|
||||
import schema from './schema.js';
|
||||
|
||||
async function MongodbBulkWrite({ connection, request }) {
|
||||
const deserializedRequest = deserialize(request);
|
||||
const { operations, options } = deserializedRequest;
|
||||
const { collection, client } = await getCollection({ connection });
|
||||
let response;
|
||||
try {
|
||||
response = await collection.bulkWrite(operations, options);
|
||||
} catch (error) {
|
||||
await client.close();
|
||||
throw error;
|
||||
}
|
||||
await client.close();
|
||||
return serialize(response);
|
||||
}
|
||||
|
||||
MongodbBulkWrite.schema = schema;
|
||||
MongodbBulkWrite.meta = {
|
||||
checkRead: false,
|
||||
checkWrite: true,
|
||||
};
|
||||
|
||||
export default MongodbBulkWrite;
|
475
packages/plugins/connections/connection-mongodb/src/connections/MongoDBCollection/MongoDBBulkWrite/MongoDBulkWrite.test.js
Normal file
475
packages/plugins/connections/connection-mongodb/src/connections/MongoDBCollection/MongoDBBulkWrite/MongoDBulkWrite.test.js
Normal file
@ -0,0 +1,475 @@
|
||||
/*
|
||||
Copyright 2020-2024 Lowdefy, Inc
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { validate } from '@lowdefy/ajv';
|
||||
import MongoDBBulkWrite from './MongoDBBulkWrite.js';
|
||||
import populateTestMongoDb from '../../../../test/populateTestMongoDb.js';
|
||||
|
||||
const { checkRead, checkWrite } = MongoDBBulkWrite.meta;
|
||||
const schema = MongoDBBulkWrite.schema;
|
||||
|
||||
const operations = [{}];
|
||||
|
||||
const databaseUri = process.env.MONGO_URL;
|
||||
const databaseName = 'test';
|
||||
const collection = 'bulkWrite';
|
||||
const documents = [
|
||||
{ _id: 'deleteMany' },
|
||||
{ _id: 'deleteMany_1' },
|
||||
{ _id: 'deleteMany_2' },
|
||||
{ _id: 'deleteMany_3' },
|
||||
{ _id: 'deleteMany_4', f: 'deleteMany' },
|
||||
{ _id: 'deleteMany_5', f: 'deleteMany' },
|
||||
{ _id: 'deleteMany_6', f: 'deleteMany' },
|
||||
{ _id: 'deleteOne' },
|
||||
{ _id: 'updateMany', v: 'before' },
|
||||
{ _id: 'updateMany_1', v: 'before' },
|
||||
{ _id: 'updateMany_2', v: 'before' },
|
||||
{ _id: 'updateMany_3', v: 'before' },
|
||||
{ _id: 'updateMany_4', v: 'before', f: 'updateMany' },
|
||||
{ _id: 'updateMany_5', v: 'before', f: 'updateMany' },
|
||||
{ _id: 'updateMany_6', v: 'before', f: 'updateMany' },
|
||||
{ _id: 'updateOne', v: 'before' },
|
||||
{ _id: 'replaceOne', v: 'before' },
|
||||
];
|
||||
|
||||
beforeAll(() => {
|
||||
return populateTestMongoDb({ collection, documents });
|
||||
});
|
||||
|
||||
test('bulkWrite connection error', async () => {
|
||||
const request = { operations };
|
||||
const connection = {
|
||||
databaseUri: 'bad_uri',
|
||||
databaseName,
|
||||
collection,
|
||||
read: true,
|
||||
};
|
||||
await expect(MongoDBBulkWrite({ request, connection })).rejects.toThrow(
|
||||
'Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"'
|
||||
);
|
||||
});
|
||||
|
||||
test('checkRead should be false', async () => {
|
||||
expect(checkRead).toBe(false);
|
||||
});
|
||||
|
||||
test('checkWrite should be true', async () => {
|
||||
expect(checkWrite).toBe(true);
|
||||
});
|
||||
|
||||
test('request not an object', async () => {
|
||||
const request = 'request';
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'MongoDBBulkWrite request properties should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('bulkWrite no operations', async () => {
|
||||
const request = {};
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'MongoDBBulkWrite request should have required property "operations".'
|
||||
);
|
||||
});
|
||||
|
||||
test('bulkWrite operations not an array of objects', async () => {
|
||||
const request = { operations: 'operations' };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'MongoDBBulkWrite request property "operations" should be an array.'
|
||||
);
|
||||
});
|
||||
|
||||
test('bulkWrite operations not an array', async () => {
|
||||
const request = { operations: ['operations'] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'MongoDBBulkWrite request property "operations" should be an array of write operation objects.'
|
||||
);
|
||||
});
|
||||
|
||||
test('bulkWrite operation not a valid write operation', async () => {
|
||||
const request = { operations: [{ notValidOperation: 'notValidOperation' }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'MongoDBBulkWrite operation should be a write operation.'
|
||||
);
|
||||
});
|
||||
|
||||
test('bulkWrite options not an object', async () => {
|
||||
const request = { operations, options: 'options' };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'MongoDBBulkWrite request property "options" should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('insertOne operation not an object', async () => {
|
||||
const request = { operations: [{ insertOne: 'insertOne' }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'insertOne operation should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('insertOne operation document not an object', async () => {
|
||||
const request = { operations: [{ insertOne: { document: 'document' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'insertOne operation property "document" should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('insertOne operation no document', async () => {
|
||||
const request = { operations: [{ insertOne: { property: 'property' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'insertOne operation should have required property "document".'
|
||||
);
|
||||
});
|
||||
|
||||
test('updateOne operation not an object', async () => {
|
||||
const request = { operations: [{ updateOne: 'updateOne' }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'updateOne operation should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('updateOne operation filter not an object', async () => {
|
||||
const request = { operations: [{ updateOne: { filter: 'filter' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'updateOne operation property "filter" should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('updateOne operation update not an object', async () => {
|
||||
const request = { operations: [{ updateOne: { filter: { _id: 1 }, update: 'update' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'updateOne operation property "update" should be an object or an array.'
|
||||
);
|
||||
});
|
||||
|
||||
test('updateOne operation no filter', async () => {
|
||||
const request = { operations: [{ updateOne: { property: 'property' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'updateOne operation should have required property "filter".'
|
||||
);
|
||||
});
|
||||
|
||||
test('updateOne operation no update', async () => {
|
||||
const request = { operations: [{ updateOne: { filter: { _id: 1 } } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'updateOne operation should have required property "update".'
|
||||
);
|
||||
});
|
||||
|
||||
test('updateMany operation not an object', async () => {
|
||||
const request = { operations: [{ updateMany: 'updateMany' }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'updateMany operation should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('updateMany operation filter not an object', async () => {
|
||||
const request = { operations: [{ updateMany: { filter: 'filter' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'updateMany operation property "filter" should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('updateMany operation update not an object', async () => {
|
||||
const request = { operations: [{ updateMany: { filter: { _id: 1 }, update: 'update' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'updateMany operation property "update" should be an object or an array.'
|
||||
);
|
||||
});
|
||||
|
||||
test('updateMany operation no filter', async () => {
|
||||
const request = { operations: [{ updateMany: { property: 'property' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'updateMany operation should have required property "filter".'
|
||||
);
|
||||
});
|
||||
|
||||
test('updateMany operation no update', async () => {
|
||||
const request = { operations: [{ updateMany: { filter: { _id: 1 } } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'updateMany operation should have required property "update".'
|
||||
);
|
||||
});
|
||||
|
||||
test('deleteOne operation not an object', async () => {
|
||||
const request = { operations: [{ deleteOne: 'deleteOne' }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'deleteOne operation should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('deleteOne operation filter not an object', async () => {
|
||||
const request = { operations: [{ deleteOne: { filter: 'filter' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'deleteOne operation property "filter" should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('deleteOne operation no filter', async () => {
|
||||
const request = { operations: [{ deleteOne: { property: 'property' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'deleteOne operation should have required property "filter".'
|
||||
);
|
||||
});
|
||||
|
||||
test('deleteMany operation not an object', async () => {
|
||||
const request = { operations: [{ deleteMany: 'deleteMany' }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'deleteMany operation should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('deleteMany operation filter not an object', async () => {
|
||||
const request = { operations: [{ deleteMany: { filter: 'filter' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'deleteMany operation property "filter" should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('deleteMany operation no filter', async () => {
|
||||
const request = { operations: [{ deleteMany: { property: 'property' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'deleteMany operation should have required property "filter".'
|
||||
);
|
||||
});
|
||||
|
||||
test('replaceOne operation not an object', async () => {
|
||||
const request = { operations: [{ replaceOne: 'replaceOne' }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'replaceOne operation should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('replaceOne operation filter not an object', async () => {
|
||||
const request = { operations: [{ replaceOne: { filter: 'filter' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'replaceOne operation property "filter" should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('replaceOne operation update not an object', async () => {
|
||||
const request = {
|
||||
operations: [{ replaceOne: { filter: { _id: 1 }, replacement: 'replacement' } }],
|
||||
};
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'replaceOne operation property "replacement" should be an object.'
|
||||
);
|
||||
});
|
||||
|
||||
test('replaceOne operation no filter', async () => {
|
||||
const request = { operations: [{ replaceOne: { property: 'property' } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'replaceOne operation should have required property "filter".'
|
||||
);
|
||||
});
|
||||
|
||||
test('replaceOne operation no replacement', async () => {
|
||||
const request = { operations: [{ replaceOne: { filter: { _id: 1 } } }] };
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'replaceOne operation should have required property "replacement".'
|
||||
);
|
||||
});
|
||||
|
||||
test('insertOne', async () => {
|
||||
const request = { operations: [{ insertOne: { document: { _id: 'insertOne' } } }] };
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBBulkWrite({ request, connection });
|
||||
expect(res).toEqual({
|
||||
insertedCount: 1,
|
||||
insertedIds: { 0: 'insertOne' },
|
||||
matchedCount: 0,
|
||||
modifiedCount: 0,
|
||||
deletedCount: 0,
|
||||
upsertedCount: 0,
|
||||
upsertedIds: {},
|
||||
});
|
||||
});
|
||||
|
||||
test('deleteMany', async () => {
|
||||
const request = {
|
||||
operations: [
|
||||
{
|
||||
deleteMany: { filter: { _id: { $in: ['deleteMany_1', 'deleteMany_2', 'deleteMany_3'] } } },
|
||||
},
|
||||
],
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBBulkWrite({ request, connection });
|
||||
expect(res).toEqual({
|
||||
insertedCount: 0,
|
||||
insertedIds: {},
|
||||
matchedCount: 0,
|
||||
modifiedCount: 0,
|
||||
deletedCount: 3,
|
||||
upsertedCount: 0,
|
||||
upsertedIds: {},
|
||||
});
|
||||
});
|
||||
|
||||
test('deleteOne', async () => {
|
||||
const request = {
|
||||
operations: [
|
||||
{
|
||||
deleteOne: {
|
||||
filter: { _id: 'deleteOne' },
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBBulkWrite({ request, connection });
|
||||
expect(res).toEqual({
|
||||
insertedCount: 0,
|
||||
insertedIds: {},
|
||||
matchedCount: 0,
|
||||
modifiedCount: 0,
|
||||
deletedCount: 1,
|
||||
upsertedCount: 0,
|
||||
upsertedIds: {},
|
||||
});
|
||||
});
|
||||
|
||||
test('updateMany - Multiple Documents', async () => {
|
||||
const request = {
|
||||
operations: [
|
||||
{
|
||||
updateMany: {
|
||||
filter: { _id: { $in: ['updateMany_1', 'updateMany_2', 'updateMany_3'] } },
|
||||
update: { $set: { v: 'after' } },
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBBulkWrite({ request, connection });
|
||||
expect(res).toEqual({
|
||||
insertedCount: 0,
|
||||
insertedIds: {},
|
||||
matchedCount: 3,
|
||||
modifiedCount: 3,
|
||||
deletedCount: 0,
|
||||
upsertedCount: 0,
|
||||
upsertedIds: {},
|
||||
});
|
||||
});
|
||||
|
||||
test('updateOne', async () => {
|
||||
const request = {
|
||||
operations: [
|
||||
{
|
||||
updateOne: {
|
||||
filter: { _id: 'updateOne' },
|
||||
update: { $set: { v: 'after' } },
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBBulkWrite({ request, connection });
|
||||
expect(res).toEqual({
|
||||
insertedCount: 0,
|
||||
insertedIds: {},
|
||||
matchedCount: 1,
|
||||
modifiedCount: 1,
|
||||
deletedCount: 0,
|
||||
upsertedCount: 0,
|
||||
upsertedIds: {},
|
||||
});
|
||||
});
|
||||
|
||||
test('updateOne upsert', async () => {
|
||||
const request = {
|
||||
operations: [
|
||||
{
|
||||
updateOne: {
|
||||
filter: { _id: 'updateOne_upsert' },
|
||||
update: { $set: { v: 'after' } },
|
||||
upsert: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBBulkWrite({ request, connection });
|
||||
expect(res).toEqual({
|
||||
insertedCount: 0,
|
||||
insertedIds: {},
|
||||
matchedCount: 0,
|
||||
modifiedCount: 0,
|
||||
deletedCount: 0,
|
||||
upsertedCount: 1,
|
||||
upsertedIds: { 0: 'updateOne_upsert' },
|
||||
});
|
||||
});
|
||||
|
||||
test('replaceOne', async () => {
|
||||
const request = {
|
||||
operations: [
|
||||
{
|
||||
replaceOne: {
|
||||
filter: { _id: 'replaceOne' },
|
||||
replacement: { v: 'after' },
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBBulkWrite({ request, connection });
|
||||
expect(res).toEqual({
|
||||
insertedCount: 0,
|
||||
insertedIds: {},
|
||||
matchedCount: 1,
|
||||
modifiedCount: 1,
|
||||
deletedCount: 0,
|
||||
upsertedCount: 0,
|
||||
upsertedIds: {},
|
||||
});
|
||||
});
|
279
packages/plugins/connections/connection-mongodb/src/connections/MongoDBCollection/MongoDBBulkWrite/schema.js
Normal file
279
packages/plugins/connections/connection-mongodb/src/connections/MongoDBCollection/MongoDBBulkWrite/schema.js
Normal file
@ -0,0 +1,279 @@
|
||||
/*
|
||||
Copyright 2020-2024 Lowdefy, Inc
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { type } from '@lowdefy/helpers';
|
||||
|
||||
export default {
|
||||
$schema: 'http://json-schema.org/draft-07/schema#',
|
||||
title: 'Lowdefy Request Schema - MongoDBBulkWrite',
|
||||
type: 'object',
|
||||
required: ['operations'],
|
||||
errorMessage: {
|
||||
type: 'MongoDBBulkWrite request properties should be an object.',
|
||||
required: 'MongoDBBulkWrite request should have required property "operations".',
|
||||
},
|
||||
properties: {
|
||||
operations: {
|
||||
type: 'array',
|
||||
description: 'Array containing all the write operations for the execution.',
|
||||
errorMessage: {
|
||||
type: 'MongoDBBulkWrite request property "operations" should be an array.',
|
||||
},
|
||||
items: {
|
||||
type: 'object',
|
||||
errorMessage: {
|
||||
type: 'MongoDBBulkWrite request property "operations" should be an array of write operation objects.',
|
||||
additionalProperties: 'MongoDBBulkWrite operation should be a write operation.',
|
||||
maxProperties: 'MongoDBBulkWrite operation should be a write operation.',
|
||||
},
|
||||
additionalProperties: false,
|
||||
maxProperties: 1,
|
||||
properties: {
|
||||
insertOne: {
|
||||
type: 'object',
|
||||
required: ['document'],
|
||||
errorMessage: {
|
||||
type: 'insertOne operation should be an object.',
|
||||
required: 'insertOne operation should have required property "document".',
|
||||
},
|
||||
properties: {
|
||||
document: {
|
||||
type: 'object',
|
||||
description: 'The document to be inserted.',
|
||||
errorMessage: {
|
||||
type: 'insertOne operation property "document" should be an object.',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
updateOne: {
|
||||
type: 'object',
|
||||
required: ['filter', 'update'],
|
||||
errorMessage: {
|
||||
type: 'updateOne operation should be an object.',
|
||||
required: {
|
||||
filter: 'updateOne operation should have required property "filter".',
|
||||
update: 'updateOne operation should have required property "update".',
|
||||
},
|
||||
},
|
||||
properties: {
|
||||
filter: {
|
||||
type: 'object',
|
||||
description: 'The filter used to select the document to update.',
|
||||
errorMessage: {
|
||||
type: 'updateOne operation property "filter" should be an object.',
|
||||
},
|
||||
},
|
||||
update: {
|
||||
type: ['object', 'array'],
|
||||
description: 'The update operations to be applied to the document.',
|
||||
errorMessage: {
|
||||
type: 'updateOne operation property "update" should be an object or an array.',
|
||||
},
|
||||
},
|
||||
upsert: {
|
||||
type: 'boolean',
|
||||
description: 'Insert document if no match is found.',
|
||||
errorMessage: {
|
||||
type: 'updateOne operation property "upsert" should be a boolean.',
|
||||
},
|
||||
},
|
||||
arrayFilters: {
|
||||
type: 'array',
|
||||
description: 'Array filters for the `$[<identifier>]` array update operator.',
|
||||
errorMessage: {
|
||||
type: 'updateOne operation property "arrayFilters" should be an array.',
|
||||
},
|
||||
},
|
||||
collation: {
|
||||
type: 'object',
|
||||
description: 'Specify collation settings for update operation.',
|
||||
errorMessage: {
|
||||
type: 'updateOne operation property "collation" should be an object.',
|
||||
},
|
||||
},
|
||||
hint: {
|
||||
type: ['object', 'string'],
|
||||
description: 'An optional hint for query optimization.',
|
||||
errorMessage: {
|
||||
type: 'updateOne operation property "hint" should be an object or a string.',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
updateMany: {
|
||||
type: 'object',
|
||||
required: ['filter', 'update'],
|
||||
errorMessage: {
|
||||
type: 'updateMany operation should be an object.',
|
||||
required: {
|
||||
filter: 'updateMany operation should have required property "filter".',
|
||||
update: 'updateMany operation should have required property "update".',
|
||||
},
|
||||
},
|
||||
properties: {
|
||||
filter: {
|
||||
type: 'object',
|
||||
description: 'The filter used to select the documents to update.',
|
||||
errorMessage: {
|
||||
type: 'updateMany operation property "filter" should be an object.',
|
||||
},
|
||||
},
|
||||
update: {
|
||||
type: ['object', 'array'],
|
||||
description: 'The update operations to be applied to the document.',
|
||||
errorMessage: {
|
||||
type: 'updateMany operation property "update" should be an object or an array.',
|
||||
},
|
||||
},
|
||||
upsert: {
|
||||
type: 'boolean',
|
||||
description: 'Insert document if no match is found.',
|
||||
errorMessage: {
|
||||
type: 'updateMany operation property "upsert" should be a boolean.',
|
||||
},
|
||||
},
|
||||
arrayFilters: {
|
||||
type: 'array',
|
||||
description: 'Array filters for the `$[<identifier>]` array update operator.',
|
||||
errorMessage: {
|
||||
type: 'updateMany operation property "arrayFilters" should be an array.',
|
||||
},
|
||||
},
|
||||
collation: {
|
||||
type: 'object',
|
||||
description: 'Specify collation settings for update operation.',
|
||||
errorMessage: {
|
||||
type: 'updateMany operation property "collation" should be an object.',
|
||||
},
|
||||
},
|
||||
hint: {
|
||||
type: ['object', 'string'],
|
||||
description: 'An optional hint for query optimization.',
|
||||
errorMessage: {
|
||||
type: 'updateMany operation property "hint" should be an object or a string.',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
deleteOne: {
|
||||
type: 'object',
|
||||
required: ['filter'],
|
||||
errorMessage: {
|
||||
type: 'deleteOne operation should be an object.',
|
||||
required: 'deleteOne operation should have required property "filter".',
|
||||
},
|
||||
properties: {
|
||||
filter: {
|
||||
type: 'object',
|
||||
description: 'The filter used to select the document to update.',
|
||||
errorMessage: {
|
||||
type: 'deleteOne operation property "filter" should be an object.',
|
||||
},
|
||||
},
|
||||
collation: {
|
||||
type: 'object',
|
||||
description: 'Specify collation settings for update operation.',
|
||||
errorMessage: {
|
||||
type: 'deleteOne operation property "collation" should be an object.',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
deleteMany: {
|
||||
type: 'object',
|
||||
required: ['filter'],
|
||||
errorMessage: {
|
||||
type: 'deleteMany operation should be an object.',
|
||||
required: 'deleteMany operation should have required property "filter".',
|
||||
},
|
||||
properties: {
|
||||
filter: {
|
||||
type: 'object',
|
||||
description: 'The filter used to select the documents to delete.',
|
||||
errorMessage: {
|
||||
type: 'deleteMany operation property "filter" should be an object.',
|
||||
},
|
||||
},
|
||||
collation: {
|
||||
type: 'object',
|
||||
description: 'Specify collation settings for update operation.',
|
||||
errorMessage: {
|
||||
type: 'deleteMany operation property "collation" should be an object.',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
replaceOne: {
|
||||
type: 'object',
|
||||
required: ['filter', 'replacement'],
|
||||
errorMessage: {
|
||||
type: 'replaceOne operation should be an object.',
|
||||
required: {
|
||||
filter: 'replaceOne operation should have required property "filter".',
|
||||
replacement: 'replaceOne operation should have required property "replacement".',
|
||||
},
|
||||
},
|
||||
properties: {
|
||||
filter: {
|
||||
type: 'object',
|
||||
description: 'The filter used to select the documents to update.',
|
||||
errorMessage: {
|
||||
type: 'replaceOne operation property "filter" should be an object.',
|
||||
},
|
||||
},
|
||||
replacement: {
|
||||
type: 'object',
|
||||
description: 'The document to be inserted.',
|
||||
errorMessage: {
|
||||
type: 'replaceOne operation property "replacement" should be an object.',
|
||||
},
|
||||
},
|
||||
upsert: {
|
||||
type: 'boolean',
|
||||
description: 'Insert document if no match is found.',
|
||||
errorMessage: {
|
||||
type: 'replaceOne operation property "upsert" should be a boolean.',
|
||||
},
|
||||
},
|
||||
collation: {
|
||||
type: 'object',
|
||||
description: 'Specify collation settings for update operation.',
|
||||
errorMessage: {
|
||||
type: 'replaceOne operation property "collation" should be an object.',
|
||||
},
|
||||
},
|
||||
hint: {
|
||||
type: ['object', 'string'],
|
||||
description: 'An optional hint for query optimization.',
|
||||
errorMessage: {
|
||||
type: 'replaceOne operation property "hint" should be an object or a string.',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
options: {
|
||||
type: 'object',
|
||||
description: 'Optional settings.',
|
||||
errorMessage: {
|
||||
type: 'MongoDBBulkWrite request property "options" should be an object.',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user