@lowdefy/connection-mongodb 5.4.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.
Files changed (33) hide show
  1. package/dist/auth/adapters/MultiAppMongoDBAdapter/MultiAppMongoDBAdapter.js +155 -0
  2. package/dist/auth/adapters/MultiAppMongoDBAdapter/createDatabaseUser.js +39 -0
  3. package/dist/auth/adapters/MultiAppMongoDBAdapter/createDatabaseUserFromContact.js +63 -0
  4. package/dist/auth/adapters/MultiAppMongoDBAdapter/createDatabaseUserWithoutContact.js +59 -0
  5. package/dist/auth/adapters/MultiAppMongoDBAdapter/getUserFromDbByEmail.js +28 -0
  6. package/dist/auth/adapters/MultiAppMongoDBAdapter/getUserFromDbById.js +28 -0
  7. package/dist/auth/adapters/MultiAppMongoDBAdapter/transformContactToAdapterUser.js +29 -0
  8. package/dist/auth/adapters/MultiAppMongoDBAdapter/updateDatabaseUser.js +26 -0
  9. package/dist/auth/adapters.js +2 -1
  10. package/dist/connections/MongoDBCollection/MongoDBAggregation/MongoDBAggregation.js +3 -10
  11. package/dist/connections/MongoDBCollection/MongoDBBulkWrite/MongoDBBulkWrite.js +2 -9
  12. package/dist/connections/MongoDBCollection/MongoDBCollection.js +7 -1
  13. package/dist/connections/MongoDBCollection/MongoDBDeleteMany/MongoDBDeleteMany.js +19 -9
  14. package/dist/connections/MongoDBCollection/MongoDBDeleteOne/MongoDBDeleteOne.js +30 -7
  15. package/dist/connections/MongoDBCollection/MongoDBFind/MongoDBFind.js +3 -10
  16. package/dist/connections/MongoDBCollection/MongoDBFindOne/MongoDBFindOne.js +2 -9
  17. package/dist/connections/MongoDBCollection/MongoDBInsertConsecutiveId/MongoDBInsertConsecutiveId.js +84 -0
  18. package/dist/connections/MongoDBCollection/MongoDBInsertConsecutiveId/schema.js +60 -0
  19. package/dist/connections/MongoDBCollection/MongoDBInsertMany/MongoDBInsertMany.js +22 -11
  20. package/dist/connections/MongoDBCollection/MongoDBInsertManyConsecutiveIds/MongoDBInsertManyConsecutiveIds.js +86 -0
  21. package/dist/connections/MongoDBCollection/MongoDBInsertManyConsecutiveIds/schema.js +66 -0
  22. package/dist/connections/MongoDBCollection/MongoDBInsertOne/MongoDBInsertOne.js +19 -9
  23. package/dist/connections/MongoDBCollection/MongoDBUpdateMany/MongoDBUpdateMany.js +20 -9
  24. package/dist/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.js +48 -8
  25. package/dist/connections/MongoDBCollection/MongoDBUpdateOne/schema.js +7 -0
  26. package/dist/connections/MongoDBCollection/MongoDBVersionedUpdateOne/MongoDBVersionedUpdateOne.js +96 -0
  27. package/dist/connections/MongoDBCollection/MongoDBVersionedUpdateOne/schema.js +86 -0
  28. package/dist/connections/MongoDBCollection/getClient.js +44 -0
  29. package/dist/connections/MongoDBCollection/getCollection.js +18 -20
  30. package/dist/connections/MongoDBCollection/getConsecutiveIdIndex.js +50 -0
  31. package/dist/connections/MongoDBCollection/schema.js +29 -0
  32. package/dist/types.js +6 -2
  33. package/package.json +5 -4
@@ -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
+ */ export default {
16
+ $schema: 'http://json-schema.org/draft-07/schema#',
17
+ title: 'Lowdefy Request Schema - MongoDBVersionedUpdateOne',
18
+ type: 'object',
19
+ required: [
20
+ 'filter',
21
+ 'update'
22
+ ],
23
+ properties: {
24
+ filter: {
25
+ type: 'object',
26
+ description: 'The filter used to select the document to find and insert.',
27
+ errorMessage: {
28
+ type: 'MongoDBVersionedUpdateOne request property "filter" should be an object.'
29
+ }
30
+ },
31
+ update: {
32
+ type: [
33
+ 'object',
34
+ 'array'
35
+ ],
36
+ description: 'The update operations to be applied to the new inserted document.',
37
+ errorMessage: {
38
+ type: 'MongoDBVersionedUpdateOne request property "update" should be an object.'
39
+ }
40
+ },
41
+ options: {
42
+ type: 'object',
43
+ description: 'Optional settings for each mongodb operation.',
44
+ errorMessage: {
45
+ type: 'MongoDBVersionedUpdateOne request property "options" should be an object.'
46
+ },
47
+ properties: {
48
+ find: {
49
+ type: 'object',
50
+ description: 'Find options for mongodb find one.',
51
+ errorMessage: {
52
+ type: 'MongoDBVersionedUpdateOne request property option "find" should be an object.'
53
+ }
54
+ },
55
+ insert: {
56
+ type: 'object',
57
+ description: 'Insert options for mongodb insert one.',
58
+ errorMessage: {
59
+ type: 'MongoDBVersionedUpdateOne request property option "insert" should be an object.'
60
+ }
61
+ },
62
+ update: {
63
+ type: 'object',
64
+ description: 'Update options for mongodb find one and update.',
65
+ errorMessage: {
66
+ type: 'MongoDBVersionedUpdateOne request property option "update" should be an object.'
67
+ }
68
+ }
69
+ }
70
+ },
71
+ disableNoMatchError: {
72
+ type: 'boolean',
73
+ 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.',
74
+ errorMessage: {
75
+ type: 'MongoDBVersionedUpdateOne request property "disableNoMatchError" should be a boolean.'
76
+ }
77
+ }
78
+ },
79
+ errorMessage: {
80
+ type: 'MongoDBVersionedUpdateOne request properties should be an object.',
81
+ required: {
82
+ filter: 'MongoDBVersionedUpdateOne request should have required property "filter".',
83
+ update: 'MongoDBVersionedUpdateOne request should have required property "update".'
84
+ }
85
+ }
86
+ };
@@ -0,0 +1,44 @@
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 { MongoClient } from 'mongodb';
16
+ // Clients are cached for the lifetime of the process so requests share the driver's
17
+ // connection pool instead of paying a full connect (DNS, TLS, auth) per request.
18
+ const clientPromises = new Map();
19
+ function getClient({ databaseUri, options }) {
20
+ const key = `${databaseUri}:${JSON.stringify(options ?? {})}`;
21
+ if (!clientPromises.has(key)) {
22
+ const clientPromise = new MongoClient(databaseUri, options).connect();
23
+ // Evict failed connects so the next request retries instead of replaying the rejection.
24
+ clientPromise.catch(()=>clientPromises.delete(key));
25
+ clientPromises.set(key, clientPromise);
26
+ }
27
+ return clientPromises.get(key);
28
+ }
29
+ async function closeClients() {
30
+ const promises = [
31
+ ...clientPromises.values()
32
+ ];
33
+ clientPromises.clear();
34
+ await Promise.all(promises.map(async (clientPromise)=>{
35
+ try {
36
+ const client = await clientPromise;
37
+ await client.close();
38
+ } catch (error) {
39
+ // Clients that failed to connect have nothing to close.
40
+ }
41
+ }));
42
+ }
43
+ export { closeClients };
44
+ export default getClient;
@@ -12,30 +12,28 @@
12
12
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
- */ import { MongoClient } from 'mongodb';
16
- import { type } from '@lowdefy/helpers';
15
+ */ import { type } from '@lowdefy/helpers';
16
+ import getClient from './getClient.js';
17
17
  async function getCollection({ connection }) {
18
- const { collection, databaseName, databaseUri, options } = connection;
18
+ const { changeLog, collection, databaseName, databaseUri, options } = connection;
19
19
  if (!type.isString(databaseUri)) {
20
20
  throw new Error('Database URI must be a string');
21
21
  }
22
- const client = new MongoClient(databaseUri, options);
23
- await client.connect();
24
- try {
25
- if (!type.isString(databaseName) && !type.isNone(databaseName)) {
26
- throw new Error('Database name must be a string');
27
- }
28
- const db = client.db(databaseName);
29
- if (!type.isString(collection)) {
30
- throw new Error('Collection name must be a string');
31
- }
32
- return {
33
- client,
34
- collection: db.collection(collection)
35
- };
36
- } catch (error) {
37
- await client.close();
38
- throw error;
22
+ if (!type.isString(databaseName) && !type.isNone(databaseName)) {
23
+ throw new Error('Database name must be a string');
39
24
  }
25
+ if (!type.isString(collection)) {
26
+ throw new Error('Collection name must be a string');
27
+ }
28
+ const client = await getClient({
29
+ databaseUri,
30
+ options
31
+ });
32
+ const db = client.db(databaseName);
33
+ return {
34
+ client,
35
+ collection: db.collection(collection),
36
+ logCollection: changeLog?.collection ? db.collection(changeLog.collection) : undefined
37
+ };
40
38
  }
41
39
  export default getCollection;
@@ -0,0 +1,50 @@
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
+ */ async function getConsecutiveIdIndex({ collection, prefix, session }) {
16
+ const previous = await collection.aggregate([
17
+ {
18
+ $match: {
19
+ _id: {
20
+ $regex: `^${prefix}\\d+$`
21
+ }
22
+ }
23
+ },
24
+ {
25
+ $project: {
26
+ index: {
27
+ $toInt: {
28
+ $replaceOne: {
29
+ input: '$_id',
30
+ find: prefix,
31
+ replacement: ''
32
+ }
33
+ }
34
+ }
35
+ }
36
+ },
37
+ {
38
+ $sort: {
39
+ index: -1
40
+ }
41
+ },
42
+ {
43
+ $limit: 1
44
+ }
45
+ ], {
46
+ session
47
+ }).toArray();
48
+ return (previous?.[0]?.index ?? 0) + 1;
49
+ }
50
+ export default getConsecutiveIdIndex;
@@ -42,6 +42,35 @@
42
42
  type: 'MongoDBCollection connection property "collection" should be a string.'
43
43
  }
44
44
  },
45
+ changeLog: {
46
+ type: 'object',
47
+ required: [
48
+ 'collection'
49
+ ],
50
+ description: 'Log all changes made by write requests to a log collection.',
51
+ properties: {
52
+ collection: {
53
+ type: 'string',
54
+ description: 'Name of the collection change log records are written to.',
55
+ errorMessage: {
56
+ type: 'MongoDBCollection connection property "changeLog.collection" should be a string.'
57
+ }
58
+ },
59
+ meta: {
60
+ type: 'object',
61
+ description: 'Additional data to include in every change log record.',
62
+ errorMessage: {
63
+ type: 'MongoDBCollection connection property "changeLog.meta" should be an object.'
64
+ }
65
+ }
66
+ },
67
+ errorMessage: {
68
+ type: 'MongoDBCollection connection property "changeLog" should be an object.',
69
+ required: {
70
+ collection: 'MongoDBCollection connection property "changeLog" should have required property "collection".'
71
+ }
72
+ }
73
+ },
45
74
  read: {
46
75
  type: 'boolean',
47
76
  default: true,
package/dist/types.js CHANGED
@@ -23,14 +23,18 @@
23
23
  'MongoDBDeleteOne',
24
24
  'MongoDBFind',
25
25
  'MongoDBFindOne',
26
+ 'MongoDBInsertConsecutiveId',
26
27
  'MongoDBInsertMany',
28
+ 'MongoDBInsertManyConsecutiveIds',
27
29
  'MongoDBInsertOne',
28
30
  'MongoDBUpdateMany',
29
- 'MongoDBUpdateOne'
31
+ 'MongoDBUpdateOne',
32
+ 'MongoDBVersionedUpdateOne'
30
33
  ],
31
34
  auth: {
32
35
  adapters: [
33
- 'MongoDBAdapter'
36
+ 'MongoDBAdapter',
37
+ 'MultiAppMongoDBAdapter'
34
38
  ]
35
39
  }
36
40
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/connection-mongodb",
3
- "version": "5.4.0",
3
+ "version": "5.5.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -40,13 +40,14 @@
40
40
  "dist/*"
41
41
  ],
42
42
  "dependencies": {
43
- "@lowdefy/helpers": "5.4.0",
43
+ "@lowdefy/helpers": "5.5.0",
44
44
  "@next-auth/mongodb-adapter": "1.1.3",
45
45
  "mongodb": "6.3.0",
46
- "saslprep": "1.0.3"
46
+ "saslprep": "1.0.3",
47
+ "uuid": "13.0.0"
47
48
  },
48
49
  "devDependencies": {
49
- "@lowdefy/ajv": "5.4.0",
50
+ "@lowdefy/ajv": "5.5.0",
50
51
  "@shelf/jest-mongodb": "4.3.2",
51
52
  "@swc/cli": "0.8.0",
52
53
  "@swc/core": "1.15.18",