@lowdefy/connection-mongodb 5.3.0 → 5.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/adapters/MultiAppMongoDBAdapter/MultiAppMongoDBAdapter.js +155 -0
- package/dist/auth/adapters/MultiAppMongoDBAdapter/createDatabaseUser.js +39 -0
- package/dist/auth/adapters/MultiAppMongoDBAdapter/createDatabaseUserFromContact.js +63 -0
- package/dist/auth/adapters/MultiAppMongoDBAdapter/createDatabaseUserWithoutContact.js +59 -0
- package/dist/auth/adapters/MultiAppMongoDBAdapter/getUserFromDbByEmail.js +28 -0
- package/dist/auth/adapters/MultiAppMongoDBAdapter/getUserFromDbById.js +28 -0
- package/dist/auth/adapters/MultiAppMongoDBAdapter/transformContactToAdapterUser.js +29 -0
- package/dist/auth/adapters/MultiAppMongoDBAdapter/updateDatabaseUser.js +26 -0
- package/dist/auth/adapters.js +2 -1
- package/dist/connections/MongoDBCollection/MongoDBAggregation/MongoDBAggregation.js +3 -10
- package/dist/connections/MongoDBCollection/MongoDBBulkWrite/MongoDBBulkWrite.js +2 -9
- package/dist/connections/MongoDBCollection/MongoDBCollection.js +7 -1
- package/dist/connections/MongoDBCollection/MongoDBDeleteMany/MongoDBDeleteMany.js +19 -9
- package/dist/connections/MongoDBCollection/MongoDBDeleteOne/MongoDBDeleteOne.js +30 -7
- package/dist/connections/MongoDBCollection/MongoDBFind/MongoDBFind.js +3 -10
- package/dist/connections/MongoDBCollection/MongoDBFindOne/MongoDBFindOne.js +2 -9
- package/dist/connections/MongoDBCollection/MongoDBInsertConsecutiveId/MongoDBInsertConsecutiveId.js +84 -0
- package/dist/connections/MongoDBCollection/MongoDBInsertConsecutiveId/schema.js +60 -0
- package/dist/connections/MongoDBCollection/MongoDBInsertMany/MongoDBInsertMany.js +22 -11
- package/dist/connections/MongoDBCollection/MongoDBInsertManyConsecutiveIds/MongoDBInsertManyConsecutiveIds.js +86 -0
- package/dist/connections/MongoDBCollection/MongoDBInsertManyConsecutiveIds/schema.js +66 -0
- package/dist/connections/MongoDBCollection/MongoDBInsertOne/MongoDBInsertOne.js +19 -9
- package/dist/connections/MongoDBCollection/MongoDBUpdateMany/MongoDBUpdateMany.js +20 -9
- package/dist/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.js +48 -8
- package/dist/connections/MongoDBCollection/MongoDBUpdateOne/schema.js +7 -0
- package/dist/connections/MongoDBCollection/MongoDBVersionedUpdateOne/MongoDBVersionedUpdateOne.js +96 -0
- package/dist/connections/MongoDBCollection/MongoDBVersionedUpdateOne/schema.js +86 -0
- package/dist/connections/MongoDBCollection/getClient.js +44 -0
- package/dist/connections/MongoDBCollection/getCollection.js +18 -20
- package/dist/connections/MongoDBCollection/getConsecutiveIdIndex.js +50 -0
- package/dist/connections/MongoDBCollection/schema.js +29 -0
- package/dist/types.js +6 -2
- package/package.json +5 -4
package/dist/connections/MongoDBCollection/MongoDBInsertConsecutiveId/MongoDBInsertConsecutiveId.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import getCollection from '../getCollection.js';
|
|
16
|
+
import getConsecutiveIdIndex from '../getConsecutiveIdIndex.js';
|
|
17
|
+
import { serialize, deserialize } from '../serialize.js';
|
|
18
|
+
import schema from './schema.js';
|
|
19
|
+
async function MongoDBInsertConsecutiveId({ blockId, connection, connectionId, pageId, payload, request, requestId }) {
|
|
20
|
+
const deserializedRequest = deserialize(request);
|
|
21
|
+
const { doc, options, prefix, length } = deserializedRequest;
|
|
22
|
+
const { client, collection, logCollection } = await getCollection({
|
|
23
|
+
connection
|
|
24
|
+
});
|
|
25
|
+
// The id read and insert run in a transaction so concurrent requests cannot
|
|
26
|
+
// claim the same id. Transactions require MongoDB to run as a replica set.
|
|
27
|
+
const session = client.startSession();
|
|
28
|
+
const transactionOptions = {
|
|
29
|
+
readPreference: 'primary',
|
|
30
|
+
readConcern: {
|
|
31
|
+
level: 'local'
|
|
32
|
+
},
|
|
33
|
+
writeConcern: {
|
|
34
|
+
w: 'majority'
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
let response = {};
|
|
38
|
+
try {
|
|
39
|
+
await session.withTransaction(async ()=>{
|
|
40
|
+
const index = await getConsecutiveIdIndex({
|
|
41
|
+
collection,
|
|
42
|
+
prefix,
|
|
43
|
+
session
|
|
44
|
+
});
|
|
45
|
+
doc._id = `${prefix}${String(index).padStart(length, '0')}`;
|
|
46
|
+
response = await collection.insertOne(doc, {
|
|
47
|
+
...options,
|
|
48
|
+
session
|
|
49
|
+
});
|
|
50
|
+
if (logCollection) {
|
|
51
|
+
await logCollection.insertOne({
|
|
52
|
+
args: {
|
|
53
|
+
doc,
|
|
54
|
+
options
|
|
55
|
+
},
|
|
56
|
+
blockId,
|
|
57
|
+
connectionId,
|
|
58
|
+
pageId,
|
|
59
|
+
payload,
|
|
60
|
+
requestId,
|
|
61
|
+
response,
|
|
62
|
+
timestamp: new Date(),
|
|
63
|
+
type: 'MongoDBInsertConsecutiveId',
|
|
64
|
+
meta: connection.changeLog?.meta
|
|
65
|
+
}, {
|
|
66
|
+
session
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}, transactionOptions);
|
|
70
|
+
} finally{
|
|
71
|
+
await session.endSession();
|
|
72
|
+
}
|
|
73
|
+
const { acknowledged, insertedId } = serialize(response);
|
|
74
|
+
return {
|
|
75
|
+
acknowledged,
|
|
76
|
+
insertedId
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
MongoDBInsertConsecutiveId.schema = schema;
|
|
80
|
+
MongoDBInsertConsecutiveId.meta = {
|
|
81
|
+
checkRead: false,
|
|
82
|
+
checkWrite: true
|
|
83
|
+
};
|
|
84
|
+
export default MongoDBInsertConsecutiveId;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ export default {
|
|
16
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
17
|
+
title: 'Lowdefy Request Schema - MongoDBInsertConsecutiveId',
|
|
18
|
+
type: 'object',
|
|
19
|
+
required: [
|
|
20
|
+
'doc',
|
|
21
|
+
'prefix'
|
|
22
|
+
],
|
|
23
|
+
properties: {
|
|
24
|
+
doc: {
|
|
25
|
+
type: 'object',
|
|
26
|
+
description: 'The document to be inserted.',
|
|
27
|
+
errorMessage: {
|
|
28
|
+
type: 'MongoDBInsertConsecutiveId request property "doc" should be an object.'
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
options: {
|
|
32
|
+
type: 'object',
|
|
33
|
+
description: 'Optional settings.',
|
|
34
|
+
errorMessage: {
|
|
35
|
+
type: 'MongoDBInsertConsecutiveId request property "options" should be an object.'
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
prefix: {
|
|
39
|
+
type: 'string',
|
|
40
|
+
description: 'Prefix to add to _id.',
|
|
41
|
+
errorMessage: {
|
|
42
|
+
type: 'MongoDBInsertConsecutiveId request property "prefix" should be a string.'
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
length: {
|
|
46
|
+
type: 'number',
|
|
47
|
+
description: 'The numeric ID will be padded to this number of digits.',
|
|
48
|
+
errorMessage: {
|
|
49
|
+
type: 'MongoDBInsertConsecutiveId request property "length" should be a number.'
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
errorMessage: {
|
|
54
|
+
type: 'MongoDBInsertConsecutiveId request properties should be an object.',
|
|
55
|
+
required: {
|
|
56
|
+
doc: 'MongoDBInsertConsecutiveId request should have required property "doc".',
|
|
57
|
+
prefix: 'MongoDBInsertConsecutiveId request should have required property "prefix".'
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
@@ -15,24 +15,35 @@
|
|
|
15
15
|
*/ import getCollection from '../getCollection.js';
|
|
16
16
|
import { serialize, deserialize } from '../serialize.js';
|
|
17
17
|
import schema from './schema.js';
|
|
18
|
-
async function MongodbInsertMany({ connection, request }) {
|
|
18
|
+
async function MongodbInsertMany({ blockId, connection, connectionId, pageId, payload, request, requestId }) {
|
|
19
19
|
const deserializedRequest = deserialize(request);
|
|
20
20
|
const { docs, options } = deserializedRequest;
|
|
21
|
-
const { collection,
|
|
21
|
+
const { collection, logCollection } = await getCollection({
|
|
22
22
|
connection
|
|
23
23
|
});
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
const response = await collection.insertMany(docs, options);
|
|
25
|
+
if (logCollection) {
|
|
26
|
+
await logCollection.insertOne({
|
|
27
|
+
args: {
|
|
28
|
+
docs,
|
|
29
|
+
options
|
|
30
|
+
},
|
|
31
|
+
blockId,
|
|
32
|
+
connectionId,
|
|
33
|
+
pageId,
|
|
34
|
+
payload,
|
|
35
|
+
requestId,
|
|
36
|
+
response,
|
|
37
|
+
timestamp: new Date(),
|
|
38
|
+
type: 'MongoDBInsertMany',
|
|
39
|
+
meta: connection.changeLog?.meta
|
|
40
|
+
});
|
|
30
41
|
}
|
|
31
|
-
|
|
32
|
-
const { acknowledged, insertedCount } = serialize(response);
|
|
42
|
+
const { acknowledged, insertedCount, insertedIds } = serialize(response);
|
|
33
43
|
return {
|
|
34
44
|
acknowledged,
|
|
35
|
-
insertedCount
|
|
45
|
+
insertedCount,
|
|
46
|
+
insertedIds
|
|
36
47
|
};
|
|
37
48
|
}
|
|
38
49
|
MongodbInsertMany.schema = schema;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import getCollection from '../getCollection.js';
|
|
16
|
+
import getConsecutiveIdIndex from '../getConsecutiveIdIndex.js';
|
|
17
|
+
import { serialize, deserialize } from '../serialize.js';
|
|
18
|
+
import schema from './schema.js';
|
|
19
|
+
async function MongoDBInsertManyConsecutiveIds({ blockId, connection, connectionId, pageId, payload, request, requestId }) {
|
|
20
|
+
const deserializedRequest = deserialize(request);
|
|
21
|
+
const { docs, options, prefix, length } = deserializedRequest;
|
|
22
|
+
const { client, collection, logCollection } = await getCollection({
|
|
23
|
+
connection
|
|
24
|
+
});
|
|
25
|
+
// The id read and insert run in a transaction so concurrent requests cannot
|
|
26
|
+
// claim the same ids. Transactions require MongoDB to run as a replica set.
|
|
27
|
+
const session = client.startSession();
|
|
28
|
+
const transactionOptions = {
|
|
29
|
+
readPreference: 'primary',
|
|
30
|
+
readConcern: {
|
|
31
|
+
level: 'local'
|
|
32
|
+
},
|
|
33
|
+
writeConcern: {
|
|
34
|
+
w: 'majority'
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
let response = {};
|
|
38
|
+
try {
|
|
39
|
+
await session.withTransaction(async ()=>{
|
|
40
|
+
const firstIndex = await getConsecutiveIdIndex({
|
|
41
|
+
collection,
|
|
42
|
+
prefix,
|
|
43
|
+
session
|
|
44
|
+
});
|
|
45
|
+
docs.forEach((doc, index)=>{
|
|
46
|
+
doc._id = `${prefix}${String(firstIndex + index).padStart(length, '0')}`;
|
|
47
|
+
});
|
|
48
|
+
response = await collection.insertMany(docs, {
|
|
49
|
+
...options,
|
|
50
|
+
session
|
|
51
|
+
});
|
|
52
|
+
if (logCollection) {
|
|
53
|
+
await logCollection.insertOne({
|
|
54
|
+
args: {
|
|
55
|
+
docs,
|
|
56
|
+
options
|
|
57
|
+
},
|
|
58
|
+
blockId,
|
|
59
|
+
connectionId,
|
|
60
|
+
pageId,
|
|
61
|
+
payload,
|
|
62
|
+
requestId,
|
|
63
|
+
response,
|
|
64
|
+
timestamp: new Date(),
|
|
65
|
+
type: 'MongoDBInsertManyConsecutiveIds',
|
|
66
|
+
meta: connection.changeLog?.meta
|
|
67
|
+
}, {
|
|
68
|
+
session
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}, transactionOptions);
|
|
72
|
+
} finally{
|
|
73
|
+
await session.endSession();
|
|
74
|
+
}
|
|
75
|
+
const { acknowledged, insertedIds } = serialize(response);
|
|
76
|
+
return {
|
|
77
|
+
acknowledged,
|
|
78
|
+
insertedIds
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
MongoDBInsertManyConsecutiveIds.schema = schema;
|
|
82
|
+
MongoDBInsertManyConsecutiveIds.meta = {
|
|
83
|
+
checkRead: false,
|
|
84
|
+
checkWrite: true
|
|
85
|
+
};
|
|
86
|
+
export default MongoDBInsertManyConsecutiveIds;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ export default {
|
|
16
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
17
|
+
title: 'Lowdefy Request Schema - MongoDBInsertManyConsecutiveIds',
|
|
18
|
+
type: 'object',
|
|
19
|
+
required: [
|
|
20
|
+
'docs',
|
|
21
|
+
'prefix'
|
|
22
|
+
],
|
|
23
|
+
properties: {
|
|
24
|
+
docs: {
|
|
25
|
+
type: 'array',
|
|
26
|
+
description: 'The documents to be inserted.',
|
|
27
|
+
items: {
|
|
28
|
+
type: 'object',
|
|
29
|
+
errorMessage: {
|
|
30
|
+
type: 'MongoDBInsertManyConsecutiveIds request property "docs" should be an array of objects.'
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
errorMessage: {
|
|
34
|
+
type: 'MongoDBInsertManyConsecutiveIds request property "docs" should be an array.'
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
options: {
|
|
38
|
+
type: 'object',
|
|
39
|
+
description: 'Optional settings.',
|
|
40
|
+
errorMessage: {
|
|
41
|
+
type: 'MongoDBInsertManyConsecutiveIds request property "options" should be an object.'
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
prefix: {
|
|
45
|
+
type: 'string',
|
|
46
|
+
description: 'Prefix to add to _id.',
|
|
47
|
+
errorMessage: {
|
|
48
|
+
type: 'MongoDBInsertManyConsecutiveIds request property "prefix" should be a string.'
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
length: {
|
|
52
|
+
type: 'number',
|
|
53
|
+
description: 'The numeric ID will be padded to this number of digits.',
|
|
54
|
+
errorMessage: {
|
|
55
|
+
type: 'MongoDBInsertManyConsecutiveIds request property "length" should be a number.'
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
errorMessage: {
|
|
60
|
+
type: 'MongoDBInsertManyConsecutiveIds request properties should be an object.',
|
|
61
|
+
required: {
|
|
62
|
+
docs: 'MongoDBInsertManyConsecutiveIds request should have required property "docs".',
|
|
63
|
+
prefix: 'MongoDBInsertManyConsecutiveIds request should have required property "prefix".'
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
@@ -15,20 +15,30 @@
|
|
|
15
15
|
*/ import getCollection from '../getCollection.js';
|
|
16
16
|
import { serialize, deserialize } from '../serialize.js';
|
|
17
17
|
import schema from './schema.js';
|
|
18
|
-
async function MongodbInsertOne({ connection, request }) {
|
|
18
|
+
async function MongodbInsertOne({ blockId, connection, connectionId, pageId, payload, request, requestId }) {
|
|
19
19
|
const deserializedRequest = deserialize(request);
|
|
20
20
|
const { doc, options } = deserializedRequest;
|
|
21
|
-
const { collection,
|
|
21
|
+
const { collection, logCollection } = await getCollection({
|
|
22
22
|
connection
|
|
23
23
|
});
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
const response = await collection.insertOne(doc, options);
|
|
25
|
+
if (logCollection) {
|
|
26
|
+
await logCollection.insertOne({
|
|
27
|
+
args: {
|
|
28
|
+
doc,
|
|
29
|
+
options
|
|
30
|
+
},
|
|
31
|
+
blockId,
|
|
32
|
+
connectionId,
|
|
33
|
+
pageId,
|
|
34
|
+
payload,
|
|
35
|
+
requestId,
|
|
36
|
+
response,
|
|
37
|
+
timestamp: new Date(),
|
|
38
|
+
type: 'MongoDBInsertOne',
|
|
39
|
+
meta: connection.changeLog?.meta
|
|
40
|
+
});
|
|
30
41
|
}
|
|
31
|
-
await client.close();
|
|
32
42
|
const { acknowledged, insertedId } = serialize(response);
|
|
33
43
|
return {
|
|
34
44
|
acknowledged,
|
|
@@ -15,20 +15,31 @@
|
|
|
15
15
|
*/ import getCollection from '../getCollection.js';
|
|
16
16
|
import { serialize, deserialize } from '../serialize.js';
|
|
17
17
|
import schema from './schema.js';
|
|
18
|
-
async function MongodbUpdateMany({ connection, request }) {
|
|
18
|
+
async function MongodbUpdateMany({ blockId, connection, connectionId, pageId, payload, request, requestId }) {
|
|
19
19
|
const deserializedRequest = deserialize(request);
|
|
20
20
|
const { filter, update, options } = deserializedRequest;
|
|
21
|
-
const { collection,
|
|
21
|
+
const { collection, logCollection } = await getCollection({
|
|
22
22
|
connection
|
|
23
23
|
});
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
const response = await collection.updateMany(filter, update, options);
|
|
25
|
+
if (logCollection) {
|
|
26
|
+
await logCollection.insertOne({
|
|
27
|
+
args: {
|
|
28
|
+
filter,
|
|
29
|
+
update,
|
|
30
|
+
options
|
|
31
|
+
},
|
|
32
|
+
blockId,
|
|
33
|
+
connectionId,
|
|
34
|
+
pageId,
|
|
35
|
+
payload,
|
|
36
|
+
requestId,
|
|
37
|
+
response,
|
|
38
|
+
timestamp: new Date(),
|
|
39
|
+
type: 'MongoDBUpdateMany',
|
|
40
|
+
meta: connection.changeLog?.meta
|
|
41
|
+
});
|
|
30
42
|
}
|
|
31
|
-
await client.close();
|
|
32
43
|
const { modifiedCount, upsertedId, upsertedCount, matchedCount } = serialize(response);
|
|
33
44
|
return {
|
|
34
45
|
modifiedCount,
|
|
@@ -15,20 +15,60 @@
|
|
|
15
15
|
*/ import getCollection from '../getCollection.js';
|
|
16
16
|
import { serialize, deserialize } from '../serialize.js';
|
|
17
17
|
import schema from './schema.js';
|
|
18
|
-
async function MongodbUpdateOne({ connection, request }) {
|
|
18
|
+
async function MongodbUpdateOne({ blockId, connection, connectionId, pageId, payload, request, requestId }) {
|
|
19
19
|
const deserializedRequest = deserialize(request);
|
|
20
|
-
const { filter, update, options } = deserializedRequest;
|
|
21
|
-
const { collection,
|
|
20
|
+
const { filter, update, options, disableNoMatchError } = deserializedRequest;
|
|
21
|
+
const { collection, logCollection } = await getCollection({
|
|
22
22
|
connection
|
|
23
23
|
});
|
|
24
24
|
let response;
|
|
25
|
-
|
|
25
|
+
if (logCollection) {
|
|
26
|
+
// findOneAndUpdate instead of updateOne to capture before and after
|
|
27
|
+
// documents for the change log. The response shape matches the updateOne
|
|
28
|
+
// response so it is invariant to the connection having a changeLog.
|
|
29
|
+
const before = await collection.findOne(filter);
|
|
30
|
+
const result = await collection.findOneAndUpdate(filter, update, {
|
|
31
|
+
...options,
|
|
32
|
+
includeResultMetadata: true,
|
|
33
|
+
returnDocument: 'after'
|
|
34
|
+
});
|
|
35
|
+
const after = result.value ?? null;
|
|
36
|
+
const upsertedId = result.lastErrorObject?.upserted ?? null;
|
|
37
|
+
const matched = result.lastErrorObject?.updatedExisting ? 1 : 0;
|
|
38
|
+
response = {
|
|
39
|
+
acknowledged: true,
|
|
40
|
+
matchedCount: matched,
|
|
41
|
+
modifiedCount: matched,
|
|
42
|
+
upsertedId,
|
|
43
|
+
upsertedCount: upsertedId ? 1 : 0
|
|
44
|
+
};
|
|
45
|
+
// Throw before writing the log record so a no-match update never logs.
|
|
46
|
+
if (!disableNoMatchError && !options?.upsert && matched === 0 && !upsertedId) {
|
|
47
|
+
throw new Error('No matching record to update.');
|
|
48
|
+
}
|
|
49
|
+
await logCollection.insertOne({
|
|
50
|
+
args: {
|
|
51
|
+
filter,
|
|
52
|
+
update,
|
|
53
|
+
options
|
|
54
|
+
},
|
|
55
|
+
blockId,
|
|
56
|
+
connectionId,
|
|
57
|
+
pageId,
|
|
58
|
+
payload,
|
|
59
|
+
requestId,
|
|
60
|
+
before,
|
|
61
|
+
after,
|
|
62
|
+
timestamp: new Date(),
|
|
63
|
+
type: 'MongoDBUpdateOne',
|
|
64
|
+
meta: connection.changeLog?.meta
|
|
65
|
+
});
|
|
66
|
+
} else {
|
|
26
67
|
response = await collection.updateOne(filter, update, options);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
68
|
+
if (!disableNoMatchError && !options?.upsert && response.matchedCount === 0) {
|
|
69
|
+
throw new Error('No matching record to update.');
|
|
70
|
+
}
|
|
30
71
|
}
|
|
31
|
-
await client.close();
|
|
32
72
|
return serialize(response);
|
|
33
73
|
}
|
|
34
74
|
MongodbUpdateOne.schema = schema;
|
|
@@ -44,6 +44,13 @@
|
|
|
44
44
|
errorMessage: {
|
|
45
45
|
type: 'MongoDBUpdateOne request property "options" should be an object.'
|
|
46
46
|
}
|
|
47
|
+
},
|
|
48
|
+
disableNoMatchError: {
|
|
49
|
+
type: 'boolean',
|
|
50
|
+
description: 'Do not throw an error when no document matches the filter. By default the request throws "No matching record to update." when nothing matched and upsert is not set.',
|
|
51
|
+
errorMessage: {
|
|
52
|
+
type: 'MongoDBUpdateOne request property "disableNoMatchError" should be a boolean.'
|
|
53
|
+
}
|
|
47
54
|
}
|
|
48
55
|
},
|
|
49
56
|
errorMessage: {
|
package/dist/connections/MongoDBCollection/MongoDBVersionedUpdateOne/MongoDBVersionedUpdateOne.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import getCollection from '../getCollection.js';
|
|
16
|
+
import { serialize, deserialize } from '../serialize.js';
|
|
17
|
+
import schema from './schema.js';
|
|
18
|
+
async function MongoDBVersionedUpdateOne({ blockId, connection, connectionId, pageId, payload, request, requestId }) {
|
|
19
|
+
const deserializedRequest = deserialize(request);
|
|
20
|
+
const { filter, update, options, disableNoMatchError } = deserializedRequest;
|
|
21
|
+
const { collection, logCollection } = await getCollection({
|
|
22
|
+
connection
|
|
23
|
+
});
|
|
24
|
+
const findOptions = options?.find;
|
|
25
|
+
const insertOptions = options?.insert;
|
|
26
|
+
const updateOptions = options?.update;
|
|
27
|
+
// The matched document is re-inserted under a new _id so the previous
|
|
28
|
+
// version is preserved, then the update is applied to the new copy.
|
|
29
|
+
const document = await collection.findOne(filter, {
|
|
30
|
+
...findOptions
|
|
31
|
+
});
|
|
32
|
+
let insertedDocument;
|
|
33
|
+
if (document) {
|
|
34
|
+
delete document._id;
|
|
35
|
+
insertedDocument = await collection.insertOne(document, {
|
|
36
|
+
...insertOptions
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
let response;
|
|
40
|
+
if (logCollection) {
|
|
41
|
+
const result = await collection.findOneAndUpdate(insertedDocument ? {
|
|
42
|
+
_id: insertedDocument.insertedId
|
|
43
|
+
} : filter, update, {
|
|
44
|
+
...updateOptions,
|
|
45
|
+
includeResultMetadata: true,
|
|
46
|
+
returnDocument: 'after'
|
|
47
|
+
});
|
|
48
|
+
const after = result.value ?? null;
|
|
49
|
+
const upsertedId = result.lastErrorObject?.upserted ?? null;
|
|
50
|
+
const matched = result.lastErrorObject?.updatedExisting ? 1 : 0;
|
|
51
|
+
response = {
|
|
52
|
+
acknowledged: true,
|
|
53
|
+
matchedCount: matched,
|
|
54
|
+
modifiedCount: matched,
|
|
55
|
+
upsertedId,
|
|
56
|
+
upsertedCount: upsertedId ? 1 : 0
|
|
57
|
+
};
|
|
58
|
+
// Throw before writing the log record so a no-match update never logs.
|
|
59
|
+
if (!disableNoMatchError && !updateOptions?.upsert && matched === 0 && !upsertedId) {
|
|
60
|
+
throw new Error('No matching record to update.');
|
|
61
|
+
}
|
|
62
|
+
await logCollection.insertOne({
|
|
63
|
+
args: {
|
|
64
|
+
filter,
|
|
65
|
+
update,
|
|
66
|
+
options
|
|
67
|
+
},
|
|
68
|
+
blockId,
|
|
69
|
+
connectionId,
|
|
70
|
+
pageId,
|
|
71
|
+
payload,
|
|
72
|
+
requestId,
|
|
73
|
+
before: document,
|
|
74
|
+
after,
|
|
75
|
+
timestamp: new Date(),
|
|
76
|
+
type: 'MongoDBVersionedUpdateOne',
|
|
77
|
+
meta: connection.changeLog?.meta
|
|
78
|
+
});
|
|
79
|
+
} else {
|
|
80
|
+
response = await collection.updateOne(insertedDocument ? {
|
|
81
|
+
_id: insertedDocument.insertedId
|
|
82
|
+
} : filter, update, {
|
|
83
|
+
...updateOptions
|
|
84
|
+
});
|
|
85
|
+
if (!disableNoMatchError && !updateOptions?.upsert && response.matchedCount === 0) {
|
|
86
|
+
throw new Error('No matching record to update.');
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return serialize(response);
|
|
90
|
+
}
|
|
91
|
+
MongoDBVersionedUpdateOne.schema = schema;
|
|
92
|
+
MongoDBVersionedUpdateOne.meta = {
|
|
93
|
+
checkRead: false,
|
|
94
|
+
checkWrite: true
|
|
95
|
+
};
|
|
96
|
+
export default MongoDBVersionedUpdateOne;
|