mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-04-12 15:40:30 +08:00
Merge remote-tracking branch 'origin/develop' into docs-todos
This commit is contained in:
commit
9041c47b94
@ -67,9 +67,6 @@ _ref:
|
||||
- `read: boolean`: Default: `true` - Allow read operations like find on the collection.
|
||||
- `write: boolean`: Default: `false` - Allow write operations like update on the collection.
|
||||
- `options: object`: See the [driver documentation](https://mongodb.github.io/node-mongodb-native/4.0/interfaces/mongoclientoptions.html) for more information.
|
||||
- `logCollection: object`: Optional log collection for logging changes.
|
||||
- `collection: string`: __Required__ - The name of the MongoDB log collection.
|
||||
- `meta: object`: Optional attributes object for user and other information.
|
||||
|
||||
#### Examples
|
||||
|
||||
|
@ -18,26 +18,13 @@ import getCollection from '../getCollection.js';
|
||||
import { serialize, deserialize } from '../serialize.js';
|
||||
import schema from './schema.js';
|
||||
|
||||
async function MongodbDeleteMany({ blockId, connection, pageId, request, requestId, payload }) {
|
||||
async function MongodbDeleteMany({ connection, request }) {
|
||||
const deserializedRequest = deserialize(request);
|
||||
const { filter, options } = deserializedRequest;
|
||||
const { collection, client, logCollection } = await getCollection({ connection });
|
||||
const { collection, client } = await getCollection({ connection });
|
||||
let response;
|
||||
try {
|
||||
response = await collection.deleteMany(filter, options);
|
||||
if (logCollection) {
|
||||
await logCollection.insertOne({
|
||||
args: { filter, options },
|
||||
blockId,
|
||||
pageId,
|
||||
payload,
|
||||
requestId,
|
||||
response,
|
||||
timestamp: new Date(),
|
||||
type: 'MongoDBDeleteMany',
|
||||
meta: connection.changeLog?.meta,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
await client.close();
|
||||
throw error;
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
import { validate } from '@lowdefy/ajv';
|
||||
import MongoDBDeleteMany from './MongoDBDeleteMany.js';
|
||||
import findLogCollectionRecordTestMongoDb from '../../../../test/findLogCollectionRecordTestMongoDb.js';
|
||||
import populateTestMongoDb from '../../../../test/populateTestMongoDb.js';
|
||||
|
||||
const { checkRead, checkWrite } = MongoDBDeleteMany.meta;
|
||||
@ -25,7 +24,6 @@ const schema = MongoDBDeleteMany.schema;
|
||||
const databaseUri = process.env.MONGO_URL;
|
||||
const databaseName = 'test';
|
||||
const collection = 'deleteMany';
|
||||
const logCollection = 'logCollection';
|
||||
const documents = [
|
||||
{ _id: 'deleteMany' },
|
||||
{ _id: 'deleteMany_1' },
|
||||
@ -34,13 +32,6 @@ const documents = [
|
||||
{ _id: 'deleteMany_4', f: 'deleteMany' },
|
||||
{ _id: 'deleteMany_5', f: 'deleteMany' },
|
||||
{ _id: 'deleteMany_6', f: 'deleteMany' },
|
||||
{ _id: 'deleteMany_log' },
|
||||
{ _id: 'deleteMany_1_log' },
|
||||
{ _id: 'deleteMany_2_log' },
|
||||
{ _id: 'deleteMany_3_log' },
|
||||
{ _id: 'deleteMany_4_log', f: 'deleteMany_log' },
|
||||
{ _id: 'deleteMany_5_log', f: 'deleteMany_log' },
|
||||
{ _id: 'deleteMany_6_log', f: 'deleteMany_log' },
|
||||
];
|
||||
|
||||
beforeAll(() => {
|
||||
@ -64,43 +55,6 @@ test('deleteMany - Single Document', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('deleteMany logCollection - Single Document', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'deleteMany_log' },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBDeleteMany({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'deleteMany_log',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
acknowledged: true,
|
||||
deletedCount: 1,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'deleteMany_log',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'deleteMany_log',
|
||||
type: 'MongoDBDeleteMany',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('deleteMany - Multiple Documents', async () => {
|
||||
const request = {
|
||||
filter: { _id: { $in: ['deleteMany_1', 'deleteMany_2', 'deleteMany_3'] } },
|
||||
@ -118,43 +72,6 @@ test('deleteMany - Multiple Documents', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('deleteMany logCollection - Multiple Documents', async () => {
|
||||
const request = {
|
||||
filter: { _id: { $in: ['deleteMany_1_log', 'deleteMany_2_log', 'deleteMany_3_log'] } },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBDeleteMany({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'deleteMany_multiple',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
acknowledged: true,
|
||||
deletedCount: 3,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'deleteMany_multiple',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'deleteMany_multiple',
|
||||
type: 'MongoDBDeleteMany',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('deleteMany - Multiple Documents one field', async () => {
|
||||
const request = {
|
||||
filter: { f: 'deleteMany' },
|
||||
@ -172,43 +89,6 @@ test('deleteMany - Multiple Documents one field', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('deleteMany logCollection - Multiple Documents one field', async () => {
|
||||
const request = {
|
||||
filter: { f: 'deleteMany_log' },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBDeleteMany({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'deleteMany_multiple_one_field',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
acknowledged: true,
|
||||
deletedCount: 3,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'deleteMany_multiple_one_field',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'deleteMany_multiple_one_field',
|
||||
type: 'MongoDBDeleteMany',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('deleteMany connection error', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'test' },
|
||||
|
@ -18,29 +18,13 @@ import getCollection from '../getCollection.js';
|
||||
import { serialize, deserialize } from '../serialize.js';
|
||||
import schema from './schema.js';
|
||||
|
||||
async function MongodbDeleteOne({ blockId, connection, pageId, request, requestId, payload }) {
|
||||
async function MongodbDeleteOne({ connection, request }) {
|
||||
const deserializedRequest = deserialize(request);
|
||||
const { filter, options } = deserializedRequest;
|
||||
const { collection, client, logCollection } = await getCollection({ connection });
|
||||
const { collection, client } = await getCollection({ connection });
|
||||
let response;
|
||||
try {
|
||||
if (logCollection) {
|
||||
const { value, ...responseWithoutValue } = await collection.findOneAndDelete(filter, options);
|
||||
response = responseWithoutValue;
|
||||
await logCollection.insertOne({
|
||||
args: { filter, options },
|
||||
blockId,
|
||||
pageId,
|
||||
payload,
|
||||
requestId,
|
||||
before: value,
|
||||
timestamp: new Date(),
|
||||
type: 'MongoDBDeleteOne',
|
||||
meta: connection.changeLog?.meta,
|
||||
});
|
||||
} else {
|
||||
response = await collection.deleteOne(filter, options);
|
||||
}
|
||||
response = await collection.deleteOne(filter, options);
|
||||
} catch (error) {
|
||||
await client.close();
|
||||
throw error;
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
import { validate } from '@lowdefy/ajv';
|
||||
import MongoDBDeleteOne from './MongoDBDeleteOne.js';
|
||||
import findLogCollectionRecordTestMongoDb from '../../../../test/findLogCollectionRecordTestMongoDb.js';
|
||||
import populateTestMongoDb from '../../../../test/populateTestMongoDb.js';
|
||||
|
||||
const { checkRead, checkWrite } = MongoDBDeleteOne.meta;
|
||||
@ -25,7 +24,6 @@ const schema = MongoDBDeleteOne.schema;
|
||||
const databaseUri = process.env.MONGO_URL;
|
||||
const databaseName = 'test';
|
||||
const collection = 'deleteOne';
|
||||
const logCollection = 'logCollection';
|
||||
const documents = [{ _id: 'deleteOne' }, { _id: 'deleteOne_log' }];
|
||||
|
||||
beforeAll(() => {
|
||||
@ -49,46 +47,6 @@ test('deleteOne', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('deleteOne logCollection', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'deleteOne_log' },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBDeleteOne({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'deleteOne_log',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
lastErrorObject: {
|
||||
n: 1,
|
||||
},
|
||||
ok: 1,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'deleteOne_log',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'deleteOne_log',
|
||||
before: { _id: 'deleteOne_log' },
|
||||
type: 'MongoDBDeleteOne',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('deleteOne connection error', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'test' },
|
||||
|
@ -18,26 +18,13 @@ import getCollection from '../getCollection.js';
|
||||
import { serialize, deserialize } from '../serialize.js';
|
||||
import schema from './schema.js';
|
||||
|
||||
async function MongodbInsertMany({ blockId, connection, pageId, request, requestId, payload }) {
|
||||
async function MongodbInsertMany({ connection, request }) {
|
||||
const deserializedRequest = deserialize(request);
|
||||
const { docs, options } = deserializedRequest;
|
||||
const { collection, client, logCollection } = await getCollection({ connection });
|
||||
const { collection, client } = await getCollection({ connection });
|
||||
let response;
|
||||
try {
|
||||
response = await collection.insertMany(docs, options);
|
||||
if (logCollection) {
|
||||
await logCollection.insertOne({
|
||||
args: { docs, options },
|
||||
blockId,
|
||||
pageId,
|
||||
payload,
|
||||
requestId,
|
||||
response,
|
||||
timestamp: new Date(),
|
||||
type: 'MongoDBInsertMany',
|
||||
meta: connection.changeLog?.meta,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
await client.close();
|
||||
throw error;
|
||||
|
@ -17,7 +17,6 @@
|
||||
import { validate } from '@lowdefy/ajv';
|
||||
import MongoDBInsertMany from './MongoDBInsertMany.js';
|
||||
import clearTestMongoDb from '../../../../test/clearTestMongoDb.js';
|
||||
import findLogCollectionRecordTestMongoDb from '../../../../test/findLogCollectionRecordTestMongoDb.js';
|
||||
|
||||
const { checkRead, checkWrite } = MongoDBInsertMany.meta;
|
||||
const schema = MongoDBInsertMany.schema;
|
||||
@ -25,7 +24,6 @@ const schema = MongoDBInsertMany.schema;
|
||||
const databaseUri = process.env.MONGO_URL;
|
||||
const databaseName = 'test';
|
||||
const collection = 'insertMany';
|
||||
const logCollection = 'logCollection';
|
||||
|
||||
beforeAll(() => {
|
||||
return clearTestMongoDb({ collection });
|
||||
@ -48,47 +46,6 @@ test('insertMany', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('insertMany logCollection', async () => {
|
||||
const request = {
|
||||
docs: [
|
||||
{ _id: 'insertMany1-1_log' },
|
||||
{ _id: 'insertMany1-2_log' },
|
||||
{ _id: 'insertMany1-3_log' },
|
||||
],
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBInsertMany({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'insertMany_log',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
acknowledged: true,
|
||||
insertedCount: 3,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'insertMany_log',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'insertMany_log',
|
||||
type: 'MongoDBInsertMany',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('insertMany options', async () => {
|
||||
const request = {
|
||||
docs: [{ _id: 'insertMany2-1' }, { _id: 'insertMany2-2' }],
|
||||
@ -107,44 +64,6 @@ test('insertMany options', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('insertMany logCollection options', async () => {
|
||||
const request = {
|
||||
docs: [{ _id: 'insertMany2-1_log' }, { _id: 'insertMany2-2_log' }],
|
||||
options: { writeConcern: { w: 'majority' } },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBInsertMany({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'insertMany_options_log',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
acknowledged: true,
|
||||
insertedCount: 2,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'insertMany_options_log',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'insertMany_options_log',
|
||||
type: 'MongoDBInsertMany',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('insertMany connection error', async () => {
|
||||
const request = { docs: [{ _id: 'insertMany8-1' }, { _id: 'insertMany8-2' }] };
|
||||
const connection = {
|
||||
|
@ -18,26 +18,13 @@ import getCollection from '../getCollection.js';
|
||||
import { serialize, deserialize } from '../serialize.js';
|
||||
import schema from './schema.js';
|
||||
|
||||
async function MongodbInsertOne({ blockId, connection, pageId, request, requestId, payload }) {
|
||||
async function MongodbInsertOne({ connection, request }) {
|
||||
const deserializedRequest = deserialize(request);
|
||||
const { doc, options } = deserializedRequest;
|
||||
const { collection, client, logCollection } = await getCollection({ connection });
|
||||
const { collection, client } = await getCollection({ connection });
|
||||
let response;
|
||||
try {
|
||||
response = await collection.insertOne(doc, options);
|
||||
if (logCollection) {
|
||||
await logCollection.insertOne({
|
||||
args: { doc, options },
|
||||
blockId,
|
||||
pageId,
|
||||
payload,
|
||||
requestId,
|
||||
response,
|
||||
timestamp: new Date(),
|
||||
type: 'MongoDBInsertOne',
|
||||
meta: connection.changeLog?.meta,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
await client.close();
|
||||
throw error;
|
||||
|
@ -18,7 +18,6 @@ import { validate } from '@lowdefy/ajv';
|
||||
import { MongoClient } from 'mongodb';
|
||||
import MongoDBInsertOne from './MongoDBInsertOne.js';
|
||||
import clearTestMongoDb from '../../../../test/clearTestMongoDb.js';
|
||||
import findLogCollectionRecordTestMongoDb from '../../../../test/findLogCollectionRecordTestMongoDb.js';
|
||||
|
||||
const { checkRead, checkWrite } = MongoDBInsertOne.meta;
|
||||
const schema = MongoDBInsertOne.schema;
|
||||
@ -26,7 +25,6 @@ const schema = MongoDBInsertOne.schema;
|
||||
const databaseUri = process.env.MONGO_URL;
|
||||
const databaseName = 'test';
|
||||
const collection = 'insertOne';
|
||||
const logCollection = 'logCollection';
|
||||
|
||||
beforeAll(() => {
|
||||
return clearTestMongoDb({ collection });
|
||||
@ -47,41 +45,6 @@ test('insertOne', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('insertOne logCollection', async () => {
|
||||
const request = { doc: { _id: 'insertOne_log' } };
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBInsertOne({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'insertOne_log',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
acknowledged: true,
|
||||
insertedId: 'insertOne_log',
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'insertOne_log',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'insertOne_log',
|
||||
type: 'MongoDBInsertOne',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('insertOne options', async () => {
|
||||
const request = {
|
||||
doc: { _id: 'insertOne_options' },
|
||||
@ -100,44 +63,6 @@ test('insertOne options', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('insertOne logCollection options', async () => {
|
||||
const request = {
|
||||
doc: { _id: 'insertOne_options_log' },
|
||||
options: { writeConcern: { w: 'majority' } },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBInsertOne({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'insertOne_options_log',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
acknowledged: true,
|
||||
insertedId: 'insertOne_options_log',
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'insertOne_options_log',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'insertOne_options_log',
|
||||
type: 'MongoDBInsertOne',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('insertOne connection error', async () => {
|
||||
const request = { doc: { _id: 'insertOne_connection_error' } };
|
||||
const connection = {
|
||||
|
@ -18,26 +18,13 @@ import getCollection from '../getCollection.js';
|
||||
import { serialize, deserialize } from '../serialize.js';
|
||||
import schema from './schema.js';
|
||||
|
||||
async function MongodbUpdateMany({ blockId, connection, pageId, request, requestId, payload }) {
|
||||
async function MongodbUpdateMany({ connection, request }) {
|
||||
const deserializedRequest = deserialize(request);
|
||||
const { filter, update, options } = deserializedRequest;
|
||||
const { collection, client, logCollection } = await getCollection({ connection });
|
||||
const { collection, client } = await getCollection({ connection });
|
||||
let response;
|
||||
try {
|
||||
response = await collection.updateMany(filter, update, options);
|
||||
if (logCollection) {
|
||||
await logCollection.insertOne({
|
||||
args: { filter, update, options },
|
||||
blockId,
|
||||
pageId,
|
||||
payload,
|
||||
requestId,
|
||||
response,
|
||||
timestamp: new Date(),
|
||||
type: 'MongoDBUpdateMany',
|
||||
meta: connection.changeLog?.meta,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
await client.close();
|
||||
throw error;
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
import { validate } from '@lowdefy/ajv';
|
||||
import MongoDBUpdateMany from './MongoDBUpdateMany.js';
|
||||
import findLogCollectionRecordTestMongoDb from '../../../../test/findLogCollectionRecordTestMongoDb.js';
|
||||
import populateTestMongoDb from '../../../../test/populateTestMongoDb.js';
|
||||
|
||||
const { checkRead, checkWrite } = MongoDBUpdateMany.meta;
|
||||
@ -25,7 +24,6 @@ const schema = MongoDBUpdateMany.schema;
|
||||
const databaseUri = process.env.MONGO_URL;
|
||||
const databaseName = 'test';
|
||||
const collection = 'updateMany';
|
||||
const logCollection = 'logCollection';
|
||||
const documents = [
|
||||
{ _id: 'updateMany', v: 'before' },
|
||||
{ _id: 'updateMany_1', v: 'before' },
|
||||
@ -60,46 +58,6 @@ test('updateMany - Single Document', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('updateMany logCollection - Single Document', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateMany' },
|
||||
update: { $set: { v: 'afterLog' } },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBUpdateMany({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateMany',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
modifiedCount: 1,
|
||||
upsertedId: null,
|
||||
upsertedCount: 0,
|
||||
matchedCount: 1,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'updateMany',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateMany',
|
||||
type: 'MongoDBUpdateMany',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('updateMany - Multiple Documents', async () => {
|
||||
const request = {
|
||||
filter: { _id: { $in: ['updateMany_1', 'updateMany_2', 'updateMany_3'] } },
|
||||
@ -120,46 +78,6 @@ test('updateMany - Multiple Documents', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('updateMany logCollection - Multiple Documents', async () => {
|
||||
const request = {
|
||||
filter: { _id: { $in: ['updateMany_1', 'updateMany_2', 'updateMany_3'] } },
|
||||
update: { $set: { v: 'afterLog' } },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBUpdateMany({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateMany_multiple',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
modifiedCount: 3,
|
||||
upsertedId: null,
|
||||
upsertedCount: 0,
|
||||
matchedCount: 3,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'updateMany_multiple',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateMany_multiple',
|
||||
type: 'MongoDBUpdateMany',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('updateMany - Multiple Documents one field', async () => {
|
||||
const request = {
|
||||
filter: { f: 'updateMany' },
|
||||
@ -180,46 +98,6 @@ test('updateMany - Multiple Documents one field', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('updateMany logCollection - Multiple Documents one field', async () => {
|
||||
const request = {
|
||||
filter: { f: 'updateMany' },
|
||||
update: { $set: { v: 'afterLog' } },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBUpdateMany({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateMany_multiple_one_field',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
modifiedCount: 3,
|
||||
upsertedId: null,
|
||||
upsertedCount: 0,
|
||||
matchedCount: 3,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'updateMany_multiple_one_field',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateMany_multiple_one_field',
|
||||
type: 'MongoDBUpdateMany',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('updateMany upsert', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateMany_upsert' },
|
||||
@ -241,47 +119,6 @@ test('updateMany upsert', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('updateMany logCollection upsert', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateMany_upsert_log' },
|
||||
update: { $set: { v: 'after' } },
|
||||
options: { upsert: true },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBUpdateMany({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateMany_upsert_log',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
modifiedCount: 0,
|
||||
upsertedId: 'updateMany_upsert_log',
|
||||
upsertedCount: 1,
|
||||
matchedCount: 0,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'updateMany_upsert_log',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateMany_upsert_log',
|
||||
type: 'MongoDBUpdateMany',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('updateMany upsert false', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateMany_upsert_false' },
|
||||
@ -303,47 +140,6 @@ test('updateMany upsert false', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('updateMany logCollection upsert false', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateMany_upsert_false_log' },
|
||||
update: { $set: { v: 'after' } },
|
||||
options: { upsert: false },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBUpdateMany({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateMany_upsert_false_log',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
modifiedCount: 0,
|
||||
upsertedId: null,
|
||||
upsertedCount: 0,
|
||||
matchedCount: 0,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'updateMany_upsert_false_log',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateMany_upsert_false_log',
|
||||
type: 'MongoDBUpdateMany',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('updateMany upsert default false', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateMany_upsert_default_false' },
|
||||
@ -364,46 +160,6 @@ test('updateMany upsert default false', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('updateMany logCollection upsert default false', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateMany_upsert_default_false_log' },
|
||||
update: { $set: { v: 'after' } },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBUpdateMany({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateMany_upsert_default_false_log',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
modifiedCount: 0,
|
||||
upsertedId: null,
|
||||
upsertedCount: 0,
|
||||
matchedCount: 0,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'updateMany_upsert_default_false_log',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateMany_upsert_default_false_log',
|
||||
type: 'MongoDBUpdateMany',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('updateMany connection error', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateMany_connection_error' },
|
||||
|
@ -18,37 +18,13 @@ import getCollection from '../getCollection.js';
|
||||
import { serialize, deserialize } from '../serialize.js';
|
||||
import schema from './schema.js';
|
||||
|
||||
async function MongodbUpdateOne({ blockId, connection, pageId, request, requestId, payload }) {
|
||||
async function MongodbUpdateOne({ connection, request }) {
|
||||
const deserializedRequest = deserialize(request);
|
||||
const { filter, update, options } = deserializedRequest;
|
||||
const { collection, client, logCollection } = await getCollection({ connection });
|
||||
const { collection, client } = await getCollection({ connection });
|
||||
let response;
|
||||
try {
|
||||
if (logCollection) {
|
||||
const { value, ...responseWithoutValue } = await collection.findOneAndUpdate(
|
||||
filter,
|
||||
update,
|
||||
options
|
||||
);
|
||||
response = responseWithoutValue;
|
||||
const after = await collection.findOne({
|
||||
_id: value ? value._id : response.lastErrorObject?.upserted,
|
||||
});
|
||||
await logCollection.insertOne({
|
||||
args: { filter, update, options },
|
||||
blockId,
|
||||
pageId,
|
||||
payload,
|
||||
requestId,
|
||||
before: value,
|
||||
after,
|
||||
timestamp: new Date(),
|
||||
type: 'MongoDBUpdateOne',
|
||||
meta: connection.changeLog?.meta,
|
||||
});
|
||||
} else {
|
||||
response = await collection.updateOne(filter, update, options);
|
||||
}
|
||||
response = await collection.updateOne(filter, update, options);
|
||||
} catch (error) {
|
||||
await client.close();
|
||||
throw error;
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
import { validate } from '@lowdefy/ajv';
|
||||
import MongoDBUpdateOne from './MongoDBUpdateOne.js';
|
||||
import findLogCollectionRecordTestMongoDb from '../../../../test/findLogCollectionRecordTestMongoDb.js';
|
||||
import populateTestMongoDb from '../../../../test/populateTestMongoDb.js';
|
||||
|
||||
const { checkRead, checkWrite } = MongoDBUpdateOne.meta;
|
||||
@ -25,7 +24,6 @@ const schema = MongoDBUpdateOne.schema;
|
||||
const databaseUri = process.env.MONGO_URL;
|
||||
const databaseName = 'test';
|
||||
const collection = 'updateOne';
|
||||
const logCollection = 'logCollection';
|
||||
const documents = [{ _id: 'updateOne', v: 'before' }];
|
||||
|
||||
beforeAll(() => {
|
||||
@ -53,49 +51,6 @@ test('updateOne', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('updateOne logCollection', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateOne' },
|
||||
update: { $set: { v: 'afterLog' } },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBUpdateOne({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateOne',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
lastErrorObject: {
|
||||
n: 1,
|
||||
updatedExisting: true,
|
||||
},
|
||||
ok: 1,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'updateOne',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateOne',
|
||||
before: { _id: 'updateOne', v: 'after' },
|
||||
after: { _id: 'updateOne', v: 'afterLog' },
|
||||
type: 'MongoDBUpdateOne',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('updateOne upsert', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateOne_upsert' },
|
||||
@ -118,51 +73,6 @@ test('updateOne upsert', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('updateOne upsert logCollection', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateOne_upsert_log' },
|
||||
update: { $set: { v: 'after' } },
|
||||
options: { upsert: true },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBUpdateOne({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateOne_upsert_log',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
lastErrorObject: {
|
||||
n: 1,
|
||||
updatedExisting: false,
|
||||
upserted: 'updateOne_upsert_log',
|
||||
},
|
||||
ok: 1,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'updateOne_upsert_log',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateOne_upsert_log',
|
||||
before: null,
|
||||
after: { _id: 'updateOne_upsert_log', v: 'after' },
|
||||
type: 'MongoDBUpdateOne',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('updateOne upsert false', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateOne_upsert_false' },
|
||||
@ -185,50 +95,6 @@ test('updateOne upsert false', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('updateOne upsert false logCollection', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateOne_upsert_false' },
|
||||
update: { $set: { v: 'after' } },
|
||||
options: { upsert: false },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBUpdateOne({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateOne_upsert_false',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
lastErrorObject: {
|
||||
n: 0,
|
||||
updatedExisting: false,
|
||||
},
|
||||
ok: 1,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'updateOne_upsert_false',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateOne_upsert_false',
|
||||
before: null,
|
||||
after: null,
|
||||
type: 'MongoDBUpdateOne',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('updateOne upsert default false', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateOne_upsert_default_false' },
|
||||
@ -250,49 +116,6 @@ test('updateOne upsert default false', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('updateOne upsert default false logCollection', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateOne_upsert_default_false' },
|
||||
update: { $set: { v: 'after' } },
|
||||
};
|
||||
const connection = {
|
||||
databaseUri,
|
||||
databaseName,
|
||||
collection,
|
||||
changeLog: { collection: logCollection, meta: { meta: true } },
|
||||
write: true,
|
||||
};
|
||||
const res = await MongoDBUpdateOne({
|
||||
request,
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateOne_upsert_default_false',
|
||||
connection,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
lastErrorObject: {
|
||||
n: 0,
|
||||
updatedExisting: false,
|
||||
},
|
||||
ok: 1,
|
||||
});
|
||||
const logged = await findLogCollectionRecordTestMongoDb({
|
||||
logCollection,
|
||||
requestId: 'updateOne_upsert_default_false',
|
||||
});
|
||||
expect(logged).toMatchObject({
|
||||
blockId: 'blockId',
|
||||
pageId: 'pageId',
|
||||
payload: { payload: true },
|
||||
requestId: 'updateOne_upsert_default_false',
|
||||
before: null,
|
||||
after: null,
|
||||
type: 'MongoDBUpdateOne',
|
||||
meta: { meta: true },
|
||||
});
|
||||
});
|
||||
|
||||
test('updateOne connection error', async () => {
|
||||
const request = {
|
||||
filter: { _id: 'updateOne_connection_error' },
|
||||
|
@ -18,7 +18,7 @@ import { MongoClient } from 'mongodb';
|
||||
|
||||
async function getCollection({ connection }) {
|
||||
let client;
|
||||
const { collection, databaseName, databaseUri, changeLog, options } = connection;
|
||||
const { collection, databaseName, databaseUri, options } = connection;
|
||||
client = new MongoClient(databaseUri, options);
|
||||
await client.connect();
|
||||
try {
|
||||
@ -26,7 +26,6 @@ async function getCollection({ connection }) {
|
||||
return {
|
||||
client,
|
||||
collection: db.collection(collection),
|
||||
logCollection: changeLog?.collection && db.collection(changeLog.collection),
|
||||
};
|
||||
} catch (error) {
|
||||
await client.close();
|
||||
|
@ -29,14 +29,14 @@ export default {
|
||||
},
|
||||
databaseName: {
|
||||
type: 'string',
|
||||
description: 'AWS IAM secret access key with s3 access.',
|
||||
description: 'Database name.',
|
||||
errorMessage: {
|
||||
type: 'MongoDBCollection connection property "databaseName" should be a string.',
|
||||
},
|
||||
},
|
||||
collection: {
|
||||
type: 'string',
|
||||
description: 'AWS region the bucket is located in.',
|
||||
description: 'Collection name.',
|
||||
errorMessage: {
|
||||
type: 'MongoDBCollection connection property "collection" should be a string.',
|
||||
},
|
||||
|
@ -1,31 +0,0 @@
|
||||
/*
|
||||
Copyright 2020-2023 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 { MongoClient } from 'mongodb';
|
||||
|
||||
async function findLogCollectionRecordTestMongoDb({ logCollection, requestId }) {
|
||||
const client = new MongoClient(process.env.MONGO_URL, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
});
|
||||
await client.connect();
|
||||
const db = client.db();
|
||||
const logged = await db.collection(logCollection).findOne({ requestId });
|
||||
await client.close();
|
||||
return logged;
|
||||
}
|
||||
|
||||
export default findLogCollectionRecordTestMongoDb;
|
Loading…
x
Reference in New Issue
Block a user