@lowdefy/connection-mongodb 5.6.0 → 6.1.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.
@@ -12,8 +12,8 @@
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 { MongoDBAdapter as NextAuthMongoDBAdapter } from '@next-auth/mongodb-adapter';
15
+ */ import { MongoDBAdapter as AuthJsMongoDBAdapter } from '@auth/mongodb-adapter';
16
+ import getClient from '../../../connections/MongoDBCollection/getClient.js';
17
17
  /*
18
18
  Default collections are:
19
19
  {
@@ -25,7 +25,15 @@ Default collections are:
25
25
  */ // TODO: Docs: MongoDB database name should be in databaseUri
26
26
  function MongoDBAdapter({ properties }) {
27
27
  const { databaseUri, mongoDBClientOptions, options } = properties;
28
- const clientPromise = new MongoClient(databaseUri, mongoDBClientOptions).connect();
29
- return NextAuthMongoDBAdapter(clientPromise, options);
28
+ // A single client promise would stay rejected after a failed first connect. Auth.js
29
+ // calls a client function per operation, and getClient evicts failed connects, so
30
+ // the next operation connects afresh.
31
+ function getMongoClient() {
32
+ return getClient({
33
+ databaseUri,
34
+ options: mongoDBClientOptions
35
+ });
36
+ }
37
+ return AuthJsMongoDBAdapter(getMongoClient, options);
30
38
  }
31
39
  export default MongoDBAdapter;
@@ -12,11 +12,11 @@
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 createDatabaseUser from './createDatabaseUser.js';
15
+ */ import createDatabaseUser from './createDatabaseUser.js';
17
16
  import getUserFromDbByEmail from './getUserFromDbByEmail.js';
18
17
  import getUserFromDbById from './getUserFromDbById.js';
19
18
  import updateDatabaseUser from './updateDatabaseUser.js';
19
+ import getClient from '../../../connections/MongoDBCollection/getClient.js';
20
20
  function from({ _id, ...data }) {
21
21
  return {
22
22
  id: _id,
@@ -31,7 +31,14 @@ function to({ id, ...data }) {
31
31
  }
32
32
  function MultiAppMongoDBAdapter({ properties }) {
33
33
  const { appName, collections, databaseUri, mongoDBClientOptions } = properties;
34
- const mongoClient = new MongoClient(databaseUri, mongoDBClientOptions);
34
+ // Resolved per operation: getClient evicts failed connects, so the operation after
35
+ // a failed first connect connects afresh instead of reusing a dead client.
36
+ function getMongoClient() {
37
+ return getClient({
38
+ databaseUri,
39
+ options: mongoDBClientOptions
40
+ });
41
+ }
35
42
  const collectionNames = {
36
43
  accounts: collections?.accounts ?? 'user-accounts',
37
44
  contacts: collections?.contacts ?? 'user-contacts',
@@ -40,6 +47,7 @@ function MultiAppMongoDBAdapter({ properties }) {
40
47
  };
41
48
  return {
42
49
  async createUser (adapterUserData) {
50
+ const mongoClient = await getMongoClient();
43
51
  return createDatabaseUser({
44
52
  adapterUserData,
45
53
  appName,
@@ -49,6 +57,7 @@ function MultiAppMongoDBAdapter({ properties }) {
49
57
  });
50
58
  },
51
59
  async getUser (userId) {
60
+ const mongoClient = await getMongoClient();
52
61
  return getUserFromDbById({
53
62
  appName,
54
63
  collectionNames,
@@ -57,6 +66,7 @@ function MultiAppMongoDBAdapter({ properties }) {
57
66
  });
58
67
  },
59
68
  async getUserByEmail (email) {
69
+ const mongoClient = await getMongoClient();
60
70
  return getUserFromDbByEmail({
61
71
  appName,
62
72
  collectionNames,
@@ -65,6 +75,7 @@ function MultiAppMongoDBAdapter({ properties }) {
65
75
  });
66
76
  },
67
77
  async getUserByAccount (provider_providerAccountId) {
78
+ const mongoClient = await getMongoClient();
68
79
  const account = await mongoClient.db().collection(collectionNames.accounts).findOne(provider_providerAccountId);
69
80
  if (!account) return null;
70
81
  return getUserFromDbById({
@@ -75,6 +86,7 @@ function MultiAppMongoDBAdapter({ properties }) {
75
86
  });
76
87
  },
77
88
  async updateUser (adapterUserData) {
89
+ const mongoClient = await getMongoClient();
78
90
  await updateDatabaseUser({
79
91
  adapterUserData,
80
92
  collectionNames,
@@ -92,14 +104,17 @@ function MultiAppMongoDBAdapter({ properties }) {
92
104
  // ]);
93
105
  // },
94
106
  async linkAccount (account) {
107
+ const mongoClient = await getMongoClient();
95
108
  await mongoClient.db().collection(collectionNames.accounts).insertOne(to(account));
96
109
  return from(account);
97
110
  },
98
111
  async unlinkAccount (provider_providerAccountId) {
112
+ const mongoClient = await getMongoClient();
99
113
  const account = await mongoClient.db().collection(collectionNames.accounts).findOneAndDelete(provider_providerAccountId);
100
114
  return from(account);
101
115
  },
102
116
  async getSessionAndUser (sessionToken) {
117
+ const mongoClient = await getMongoClient();
103
118
  // eslint-disable-next-line no-unused-vars
104
119
  const session = await mongoClient.db().collection(collectionNames.sessions).findOne({
105
120
  sessionToken
@@ -117,10 +132,12 @@ function MultiAppMongoDBAdapter({ properties }) {
117
132
  };
118
133
  },
119
134
  async createSession (session) {
135
+ const mongoClient = await getMongoClient();
120
136
  await mongoClient.db().collection(collectionNames.sessions).insertOne(to(session));
121
137
  return session;
122
138
  },
123
139
  async updateSession (data) {
140
+ const mongoClient = await getMongoClient();
124
141
  // eslint-disable-next-line no-unused-vars
125
142
  const { _id, ...session } = to(data);
126
143
  const result = await mongoClient.db().collection(collectionNames.sessions).findOneAndUpdate({
@@ -133,12 +150,14 @@ function MultiAppMongoDBAdapter({ properties }) {
133
150
  return from(result);
134
151
  },
135
152
  async deleteSession (sessionToken) {
153
+ const mongoClient = await getMongoClient();
136
154
  const session = await mongoClient.db().collection(collectionNames.sessions).findOneAndDelete({
137
155
  sessionToken
138
156
  });
139
157
  return from(session);
140
158
  },
141
159
  async createVerificationToken (data) {
160
+ const mongoClient = await getMongoClient();
142
161
  const tokens = Array.from({
143
162
  length: properties?.verificationTokens?.uses ?? 1
144
163
  }, ()=>to(data));
@@ -146,6 +165,7 @@ function MultiAppMongoDBAdapter({ properties }) {
146
165
  return data;
147
166
  },
148
167
  async useVerificationToken (identifier_token) {
168
+ const mongoClient = await getMongoClient();
149
169
  const verificationToken = await mongoClient.db().collection(collectionNames.verificationTokens).findOneAndDelete(identifier_token);
150
170
  if (!verificationToken) return null;
151
171
  return from(verificationToken);
@@ -0,0 +1,64 @@
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
+ // Websocket source resolver — opens a MongoDB change stream and publishes
19
+ // each change event to the channel's subscribers. Runs from the first
20
+ // subscriber until the last one leaves (abort signal). Requires MongoDB to
21
+ // run as a replica set (change streams are not available on standalone
22
+ // deployments).
23
+ async function MongoDBChangeStream({ connection, properties, publish, signal }) {
24
+ const { fullDocument, pipeline } = deserialize(properties ?? {});
25
+ // The client is cached and shared with all other requests on this connection,
26
+ // so only the stream may be closed here, never the client.
27
+ const collection = await getCollection({
28
+ connection
29
+ });
30
+ if (signal.aborted) {
31
+ return;
32
+ }
33
+ const stream = collection.watch(pipeline ?? [], {
34
+ fullDocument: fullDocument ?? 'updateLookup'
35
+ });
36
+ signal.addEventListener('abort', ()=>{
37
+ stream.close().catch(()=>{
38
+ // Already closed — nothing to clean up.
39
+ });
40
+ }, {
41
+ once: true
42
+ });
43
+ try {
44
+ for await (const change of stream){
45
+ publish({
46
+ data: serialize(change)
47
+ });
48
+ }
49
+ } catch (error) {
50
+ // Closing the stream on abort surfaces as an error on the iterator.
51
+ if (!signal.aborted) {
52
+ throw error;
53
+ }
54
+ } finally{
55
+ await stream.close().catch(()=>{
56
+ // Already closed — nothing to clean up.
57
+ });
58
+ }
59
+ }
60
+ MongoDBChangeStream.schema = schema;
61
+ MongoDBChangeStream.meta = {
62
+ publish: false
63
+ };
64
+ export default MongoDBChangeStream;
@@ -0,0 +1,33 @@
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 Websocket Schema - MongoDBChangeStream',
18
+ type: 'object',
19
+ properties: {
20
+ pipeline: {
21
+ type: 'array',
22
+ description: 'Aggregation pipeline stages applied to the change stream (e.g. $match on operationType or fullDocument fields).'
23
+ },
24
+ fullDocument: {
25
+ type: 'string',
26
+ default: 'updateLookup',
27
+ description: 'The fullDocument option for the change stream. "updateLookup" includes the current document on updates.'
28
+ }
29
+ },
30
+ errorMessage: {
31
+ type: 'MongoDBChangeStream properties should be an object.'
32
+ }
33
+ };
package/dist/types.js CHANGED
@@ -36,5 +36,8 @@
36
36
  'MongoDBAdapter',
37
37
  'MultiAppMongoDBAdapter'
38
38
  ]
39
- }
39
+ },
40
+ websockets: [
41
+ 'MongoDBChangeStream'
42
+ ]
40
43
  };
@@ -0,0 +1,15 @@
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 as MongoDBChangeStream } from './connections/MongoDBCollection/MongoDBChangeStream/MongoDBChangeStream.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/connection-mongodb",
3
- "version": "5.6.0",
3
+ "version": "6.1.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -34,28 +34,28 @@
34
34
  "exports": {
35
35
  "./auth/adapters": "./dist/auth/adapters.js",
36
36
  "./connections": "./dist/connections.js",
37
- "./types": "./dist/types.js"
37
+ "./types": "./dist/types.js",
38
+ "./websockets": "./dist/websockets.js"
38
39
  },
39
40
  "files": [
40
41
  "dist/*"
41
42
  ],
42
43
  "dependencies": {
43
- "@lowdefy/helpers": "5.6.0",
44
+ "@auth/mongodb-adapter": "3.11.3",
45
+ "@lowdefy/helpers": "6.1.0",
44
46
  "@next-auth/mongodb-adapter": "1.1.3",
45
- "mongodb": "6.3.0",
47
+ "mongodb": "7.2.0",
46
48
  "saslprep": "1.0.3",
47
- "uuid": "13.0.0"
49
+ "uuid": "13.0.1"
48
50
  },
49
51
  "devDependencies": {
50
- "@lowdefy/ajv": "5.6.0",
52
+ "@lowdefy/ajv": "6.1.0",
51
53
  "@shelf/jest-mongodb": "4.3.2",
52
- "@swc/cli": "0.8.0",
53
- "@swc/core": "1.15.18",
54
+ "@swc/cli": "0.8.1",
55
+ "@swc/core": "1.15.32",
54
56
  "@swc/jest": "0.2.39",
55
57
  "jest": "28.1.3",
56
58
  "jest-environment-node": "28.1.3",
57
- "next": "16.1.6",
58
- "next-auth": "4.24.10",
59
59
  "react": "18.2.0",
60
60
  "react-dom": "18.2.0"
61
61
  },