mongoplusplus 1.0.4 → 1.0.5

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/README.md CHANGED
@@ -1,170 +1,188 @@
1
- # mongoplusplus
2
-
3
- ## Overview
4
-
5
- `mongoplusplus` is a Node.js package designed to facilitate load balancing of read and write operations across multiple MongoDB databases. It simplifies database connection management, schema definition, model building, and CRUD operations execution.
6
-
7
- ## Installation
8
-
9
- Install mongoplusplus via npm:
10
-
11
- ```bash
12
- npm install mongoplusplus
13
- ```
14
-
15
- importing
16
- ```node
17
- const mongoplusplus = require('mongoplusplus');
18
- ```
19
- ##Initialing
20
- ```node
21
- const dbname = 'testforUP';
22
-
23
- const mongoURI1 = `mongodb+srv://xxxxx:xxxxxx@cluster0.xxxxx.mongodb.net/${dbname}?retryWrites=true&w=majority`;
24
- const mongoURI2 = `readonly:mongodb+srv://xxxxxxx:xxxxxx@cluster0.xxxxxx.mongodb.net/${dbname}?retryWrites=true&w=majority`;
25
- const mongodb = new mongoplusplus([mongoURI1, mongoURI2]);
26
-
27
- ```
28
- ##connecting database
29
- this is the top level thing in your main page (in this test code, under mongodb variable declaration )
30
- ```node
31
- (async () => {
32
- await mongodb.connectToAll();
33
- })();
34
- ```
35
- ##Schema defining
36
- it is very similar to Mongoose schema definition.but only one mandatory field must be there in the schema which will act as a db identifier for that document
37
- ```node
38
- const likeSH = mongodb.Schema({
39
- user_id: { type: mongoose.Schema.Types.ObjectId, required: true, ref: "users" },
40
- postId: { type: mongoose.Schema.Types.ObjectId, required: true },
41
- dbIndex: { type: Number, required: true },
42
-
43
- like_time: { type: Date, default: Date.now }
44
- }
45
- )
46
- ```
47
- ### model building
48
- ```node
49
- const likes = mongodb.buildModel("likes", likeSH)
50
- ```
51
-
52
-
53
- ##### Note that `dbIndex` is used to indicate which MongoDB server it belongs to.
54
- ###### it is a must otherwise it will throw an error.
55
- ```node
56
- throw new Error(`[!]Error : < dbIndex > must be present in your schema like dbIndex:{
57
- type: Number,
58
- required: true
59
- } `)
60
- ```
61
-
62
-
63
- ### usage methods
64
- ```node
65
- findInAllDatabase(filter, chain = {})
66
- ```
67
- Usage: Finds documents matching the filter in all MongoDB databases.
68
-
69
- ```node
70
- const result = await likes.findInAllDatabase({ name: 'John' }, { limit: 10, skip: 0 });
71
- Returns: An array of documents matching the filter from all databases.
72
- ```
73
- ```node
74
- writeInAllDatabase(data)
75
-
76
- Usage: Writes data to all MongoDB databases.
77
-
78
- const result = await likes.writeInAllDatabase({ name: 'John', age: 30 });
79
- Returns: An array of written documents from all databases.
80
- ```
81
- ```node
82
- UpdateOneInAllDatabase(filter, update)
83
- Usage: Updates a single document matching the filter in all MongoDB databases.
84
- const updatedDocument = await likes.UpdateOneInAllDatabase({ name: 'John' }, { age: 35 });
85
- Returns: The updated document.
86
- ```
87
- ```node
88
- UpdateByIdInAllDatabase(id, update)
89
- Usage: Updates a document by ID in all MongoDB databases.
90
- const updatedDocument = await likes.UpdateByIdInAllDatabase('123456', { age: 35 });
91
- Returns: The updated document.
92
- ```
93
- ```node
94
- findByIdInAllDatabaseAndDelete(id)
95
- Usage: Finds a document by ID in all MongoDB databases and deletes it.
96
- const deletedDocument = await likes.findByIdInAllDatabaseAndDelete('123456');
97
- Returns: The deleted document.
98
- ```
99
-
100
- ```node
101
- findOneInAllDatabaseAndDelete(filter)
102
- Usage: Finds a single document matching the filter in all MongoDB databases and deletes it.
103
- const deletedDocument = await likes.findOneInAllDatabaseAndDelete({ name: 'John' });
104
- Returns: The deleted document.
105
- ```
106
- ```node
107
- write(data)
108
- Usage: Writes data to a MongoDB database.
109
- const result = await likes.write({ name: 'John', age: 30 });
110
- Returns: The written document.
111
- ```
112
-
113
- ```node
114
- findOne(dbIndex, filter, chain = {})
115
- Usage: Finds a single document matching the filter in a specific MongoDB database.
116
- const document = await likes.findOne(0, { name: 'John' }, { limit: 1 });
117
- Returns: The found document.
118
- ```
119
- ```node
120
- find(dbIndex, filter, chain = {})
121
- Usage: Finds documents matching the filter in a specific MongoDB database.
122
- const documents = await likes.find(0, { age: { $gt: 18 } }, { limit: 10 });
123
- Returns: An array of found documents.
124
- ```
125
-
126
- ```node
127
- findById(dbIndex, id, chain = {})
128
- Usage: Finds a document by ID in a specific MongoDB database.
129
- const document = await likes.findById(0, '123456');
130
- Returns: The found document.
131
- ```
132
- ```node
133
- findByIdAndUpdate(dbIndex, id, update)
134
- Usage: Finds a document by ID in a specific MongoDB database and updates it.
135
- const updatedDocument = await likes.findByIdAndUpdate(0, '123456', { age: 35 });
136
- Returns: The updated document.
137
- ```
138
- ```node
139
- findByIdAndDelete(dbIndex, id)
140
- Usage: Finds a document by ID in a specific MongoDB database and deletes it.
141
- const deletedDocument = await likes.findByIdAndDelete(0, '123456');
142
- Returns: The deleted document.
143
- ```
144
- ```node
145
- findOneAndUpdate(dbIndex, filter, update)
146
- Usage: Finds a single document matching the filter in a specific MongoDB database and updates it.
147
-
148
- const updatedDocument = await likes.findOneAndUpdate(0, { name: 'John' }, { age: 35 });
149
- Returns: The updated document.
150
- ```
151
- ```node
152
- aggregate(dbIndex, filter)
153
- Usage: Aggregates documents in a specific MongoDB database based on the filter.
154
- const aggregationResult = await likes.aggregate(0, [{ $group: { _id: '$name', total: { $sum: '$age' } } }]);
155
- Returns: The aggregation result.
156
- ```
157
- ```node
158
- watch(dbIndex)
159
- Usage: Starts watching for changes in a specific MongoDB database.
160
- const watcher = await likes.watch(0);
161
- Returns: A watcher object for the database.
162
- ```
163
-
164
- > now most of the functionality is as same as mongoose so you can use them directly like indexing a schema
165
- ```node
166
- likeSH.index({ user_id: 1, postId: 1 }, { unique: true });
167
- ```
168
- # Contributing
169
- there are many things that are not ported. feel free to contribute!
1
+ # mongoplusplus
2
+
3
+ ## Overview
4
+
5
+ `mongoplusplus` is a Node.js package designed to facilitate load balancing of read and write operations across multiple MongoDB databases. It simplifies database connection management, schema definition, model building, and CRUD operations execution.
6
+
7
+ ## Installation
8
+
9
+ Install mongoplusplus via npm:
10
+
11
+ ```bash
12
+ npm install mongoplusplus
13
+ ```
14
+
15
+ importing
16
+ ```node
17
+ const mongoplusplus = require('mongoplusplus');
18
+ ```
19
+ ##Initialing
20
+ ```node
21
+ const dbname = 'testforUP';
22
+
23
+ const mongoURI1 = `mongodb+srv://xxxxx:xxxxxx@cluster0.xxxxx.mongodb.net/${dbname}?retryWrites=true&w=majority`;
24
+ const mongoURI2 = `readonly:mongodb+srv://xxxxxxx:xxxxxx@cluster0.xxxxxx.mongodb.net/${dbname}?retryWrites=true&w=majority`;
25
+ const mongodb = new mongoplusplus([mongoURI1, mongoURI2]);
26
+
27
+ ```
28
+ ##connecting database
29
+ this is the top level thing in your main page (in this test code, under mongodb variable declaration )
30
+ ```node
31
+ (async () => {
32
+ await mongodb.connectToAll();
33
+ })();
34
+ ```
35
+ ##Schema defining
36
+ it is very similar to Mongoose schema definition.but only one mandatory field must be there in the schema which will act as a db identifier for that document
37
+ ```node
38
+ const likeSH = mongodb.Schema({
39
+ user_id: { type: mongoose.Schema.Types.ObjectId, required: true, ref: "users" },
40
+ postId: { type: mongoose.Schema.Types.ObjectId, required: true },
41
+ dbIndex: { type: Number, required: true },
42
+
43
+ like_time: { type: Date, default: Date.now }
44
+ }
45
+ )
46
+ ```
47
+ ### model building
48
+ ```node
49
+ const likes = mongodb.buildModel("likes", likeSH)
50
+ ```
51
+
52
+
53
+ ##### Note that `dbIndex` is used to indicate which MongoDB server it belongs to.
54
+ ###### it is a must otherwise it will throw an error.
55
+ ```node
56
+ throw new Error(`[!]Error : < dbIndex > must be present in your schema like dbIndex:{
57
+ type: Number,
58
+ required: true
59
+ } `)
60
+ ```
61
+
62
+
63
+ ### usage methods
64
+ ```node
65
+ findInAllDatabase(filter, chain = {})
66
+ ```
67
+ Usage: Finds documents matching the filter in all MongoDB databases.
68
+
69
+ ```node
70
+ const result = await likes.findInAllDatabase({ name: 'John' }, { limit: 10, skip: 0 });
71
+ Returns: An array of documents matching the filter from all databases.
72
+ ```
73
+ ```node
74
+ writeInAllDatabase(data)
75
+
76
+ Usage: Writes data to all MongoDB databases.
77
+
78
+ const result = await likes.writeInAllDatabase({ name: 'John', age: 30 });
79
+ Returns: An array of written documents from all databases.
80
+ ```
81
+ ```node
82
+ UpdateOneInAllDatabase(filter, update)
83
+ Usage: Updates a single document matching the filter in all MongoDB databases.
84
+ const updatedDocument = await likes.UpdateOneInAllDatabase({ name: 'John' }, { age: 35 });
85
+ Returns: The updated document.
86
+ ```
87
+ ```node
88
+ UpdateByIdInAllDatabase(id, update)
89
+ Usage: Updates a document by ID in all MongoDB databases.
90
+ const updatedDocument = await likes.UpdateByIdInAllDatabase('123456', { age: 35 });
91
+ Returns: The updated document.
92
+ ```
93
+ ```node
94
+ findByIdInAllDatabaseAndDelete(id)
95
+ Usage: Finds a document by ID in all MongoDB databases and deletes it.
96
+ const deletedDocument = await likes.findByIdInAllDatabaseAndDelete('123456');
97
+ Returns: The deleted document.
98
+ ```
99
+
100
+ ```node
101
+ findOneInAllDatabaseAndDelete(filter)
102
+ Usage: Finds a single document matching the filter in all MongoDB databases and deletes it.
103
+ const deletedDocument = await likes.findOneInAllDatabaseAndDelete({ name: 'John' });
104
+ Returns: The deleted document.
105
+ ```
106
+ ```node
107
+ write(data)
108
+ Usage: Writes data to a MongoDB database.
109
+ const result = await likes.write({ name: 'John', age: 30 });
110
+ Returns: The written document.
111
+ ```
112
+
113
+ ```node
114
+ bulkWrite(data)
115
+ Usage: Bulk writes data to a MongoDB database with transaction inbuilt.
116
+
117
+ const testData = Array.from({ length: 10000 }, (_, i) => ({
118
+ name: `User ${i}`,
119
+ email: `user${i}@example.com`,
120
+ age: 20 + (i % 50)
121
+ }));
122
+
123
+ // Test 1: Large batch, concurrent
124
+ console.time('Large batch concurrent');
125
+ await UserModel.bulkWrite(testData, {
126
+ batchSize: 5000,
127
+ concurrentBatches: true
128
+ });
129
+ ```
130
+
131
+ ```node
132
+ findOne(dbIndex, filter, chain = {})
133
+ Usage: Finds a single document matching the filter in a specific MongoDB database.
134
+ const document = await likes.findOne(0, { name: 'John' }, { limit: 1 });
135
+ Returns: The found document.
136
+ ```
137
+ ```node
138
+ find(dbIndex, filter, chain = {})
139
+ Usage: Finds documents matching the filter in a specific MongoDB database.
140
+ const documents = await likes.find(0, { age: { $gt: 18 } }, { limit: 10 });
141
+ Returns: An array of found documents.
142
+ ```
143
+
144
+ ```node
145
+ findById(dbIndex, id, chain = {})
146
+ Usage: Finds a document by ID in a specific MongoDB database.
147
+ const document = await likes.findById(0, '123456');
148
+ Returns: The found document.
149
+ ```
150
+ ```node
151
+ findByIdAndUpdate(dbIndex, id, update)
152
+ Usage: Finds a document by ID in a specific MongoDB database and updates it.
153
+ const updatedDocument = await likes.findByIdAndUpdate(0, '123456', { age: 35 });
154
+ Returns: The updated document.
155
+ ```
156
+ ```node
157
+ findByIdAndDelete(dbIndex, id)
158
+ Usage: Finds a document by ID in a specific MongoDB database and deletes it.
159
+ const deletedDocument = await likes.findByIdAndDelete(0, '123456');
160
+ Returns: The deleted document.
161
+ ```
162
+ ```node
163
+ findOneAndUpdate(dbIndex, filter, update)
164
+ Usage: Finds a single document matching the filter in a specific MongoDB database and updates it.
165
+
166
+ const updatedDocument = await likes.findOneAndUpdate(0, { name: 'John' }, { age: 35 });
167
+ Returns: The updated document.
168
+ ```
169
+ ```node
170
+ aggregate(dbIndex, filter)
171
+ Usage: Aggregates documents in a specific MongoDB database based on the filter.
172
+ const aggregationResult = await likes.aggregate(0, [{ $group: { _id: '$name', total: { $sum: '$age' } } }]);
173
+ Returns: The aggregation result.
174
+ ```
175
+ ```node
176
+ watch(dbIndex)
177
+ Usage: Starts watching for changes in a specific MongoDB database.
178
+ const watcher = await likes.watch(0);
179
+ Returns: A watcher object for the database.
180
+ ```
181
+
182
+ > now most of the functionality is as same as mongoose so you can use them directly like indexing a schema
183
+ ```node
184
+ likeSH.index({ user_id: 1, postId: 1 }, { unique: true });
185
+ ```
186
+ # Contributing
187
+ there are many things that are not ported. feel free to contribute!
170
188
  Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update.and also
package/index.d.ts ADDED
@@ -0,0 +1,144 @@
1
+ import mongoose = require('mongoose');
2
+
3
+ /**
4
+ * Mongoplus manages multiple mongoose connections and builds distributed models.
5
+ *
6
+ * Usage example:
7
+ * ```js
8
+ * const Mongoplus = require('mongoplus');
9
+ * const mp = new Mongoplus(['mongodb://host1/db', 'readonly:mongodb://host2/db']);
10
+ * await mp.connectToAll();
11
+ * const User = mp.buildModel('User', new mp.Schema({ name: String, dbIndex: { type: Number, required: true } }));
12
+ * await User.write({ name: 'Alice' });
13
+ * ```
14
+ */
15
+ declare class Mongoplus {
16
+ /** Array of connection URIs. Prefix a URI with `readonly:` to mark it read-only. */
17
+ mongoURI: string[];
18
+ /** All mongoose connections created by `connectToAll`. */
19
+ allConnections: mongoose.Connection[];
20
+ /** Internal rotation index used by `getNextMongoURI`. */
21
+ currentIndex: number;
22
+ /** Connections that were created from `readonly:` URIs. */
23
+ static readonlydbs: mongoose.Connection[];
24
+ /** Models corresponding to readonly connections. */
25
+ static readonlymodels: any[];
26
+
27
+ /**
28
+ * Create a Mongoplus manager.
29
+ * @param mongoURI - Array of MongoDB URIs. Use `readonly:` prefix for read-only replicas.
30
+ */
31
+ constructor(mongoURI: string[]);
32
+
33
+ /** Create a mongoose Schema from a plain definition. */
34
+ Schema(schema: mongoose.SchemaDefinition | mongoose.Schema): mongoose.Schema;
35
+
36
+ /** Add an index to a schema. Delegates to `schema.index(...)`. */
37
+ addIndex(schema: mongoose.Schema, indextype: any): any;
38
+
39
+ /** Return next MongoDB URI in rotation (without `readonly:` prefix). */
40
+ getNextMongoURI(): string;
41
+
42
+ /**
43
+ * Establish all connections from `mongoURI`. Returns an array of mongoose.Connection.
44
+ * Call this before `buildModel`.
45
+ */
46
+ connectToAll(): mongoose.Connection[];
47
+
48
+ /**
49
+ * Build a distributed model across all connections.
50
+ * The provided `schema` must include a required numeric `dbIndex` field.
51
+ */
52
+ buildModel(name: string, schema: mongoose.Schema): MongoModel;
53
+ }
54
+
55
+ /**
56
+ * MongoModel represents the model instances distributed across multiple databases.
57
+ * Methods with `InAllDatabase` perform the operation on every connection and return aggregated results.
58
+ */
59
+ declare class MongoModel {
60
+ /** Array of mongoose models (one per connection). */
61
+ model: any[];
62
+ /** Read-only model references. */
63
+ readonlydbs: any[];
64
+ /** Original schema used to create the models. */
65
+ s: mongoose.Schema;
66
+ /** Rotation index for `write` balancing. */
67
+ static currentIndex: number;
68
+
69
+ /**
70
+ * @param model - array of mongoose Model instances (one per connection)
71
+ * @param s - mongoose Schema used to create models
72
+ * @param readonlydbs - list of models that are read-only
73
+ */
74
+ constructor(model: any[], s: mongoose.Schema, readonlydbs: any[]);
75
+
76
+ /** Find matching documents in all databases.
77
+ * @param filter - mongoose filter
78
+ * @param chain - optional chaining options (skip, limit, sort)
79
+ */
80
+ findInAllDatabase(filter: any, chain?: any): Promise<any>;
81
+
82
+ /** Run aggregation pipeline on all databases. */
83
+ aggregateInAllDatabase(filter: any, chain?: any): Promise<any>;
84
+
85
+ /** Write the same document to all writable databases. Returns an array of saved docs. */
86
+ writeInAllDatabase(data: any): Promise<any[]>;
87
+
88
+ /** Update one matching document across all databases. */
89
+ UpdateOneInAllDatabase(filter: any, update: any): Promise<any>;
90
+
91
+ /** Update by id across all databases. */
92
+ UpdateByIdInAllDatabase(id: any, update: any): Promise<any>;
93
+
94
+ /** Find by id in all DBs and delete. */
95
+ findByIdInAllDatabaseAndDelete(id: any): Promise<any>;
96
+
97
+ /** Find one in all DBs and delete. */
98
+ findOneInAllDatabaseAndDelete(filter: any): Promise<any>;
99
+
100
+ /** Write a single document using round-robin balancing across writable DBs. */
101
+ write(data: any): Promise<any>;
102
+
103
+ /**
104
+ * Perform efficient bulk upserts across writable DBs.
105
+ * @param data - array of objects to upsert
106
+ * @param options - optional settings: `batchSize` and `concurrentBatches`
107
+ */
108
+ bulkWrite(data: any[], options?: { batchSize?: number; concurrentBatches?: boolean }): Promise<any[]>;
109
+
110
+ /** Find a single document on a specific DB index. */
111
+ findOne(dbIndex: number, filter: any, chain?: any): Promise<any>;
112
+
113
+ /** Find documents on a specific DB index. */
114
+ find(dbIndex: number, filter: any, chain?: any): Promise<any>;
115
+
116
+ /** Find by id on a specific DB index. */
117
+ findById(dbIndex: number, filter: any, chain?: any): Promise<any>;
118
+
119
+ /** Find by id and update on a specific DB index. */
120
+ findByIdAndUpdate(dbIndex: number, id: any, update: any): Promise<any>;
121
+
122
+ /** Find by id and delete on a specific DB index. */
123
+ findByIdAndDelete(dbIndex: number, id: any, update?: any): Promise<any>;
124
+
125
+ /** Find one and update on a specific DB index. */
126
+ findOneAndUpdate(dbIndex: number, filter: any, update: any): Promise<any>;
127
+
128
+ /** Run aggregation on a specific DB index. */
129
+ aggregate(dbIndex: number, filter: any): Promise<any>;
130
+
131
+ /** Watch change stream on a specific DB index. */
132
+ watch(dbIndex: number): any;
133
+
134
+ /** Return the next model and its index for round-robin writes. */
135
+ getNextModel(): [any, number];
136
+
137
+ /** Internal runner to execute many queries concurrently and return results with timing. */
138
+ runLargeComputations(computationPairs: any[]): Promise<{ results: any[]; totalTime: number }>;
139
+ }
140
+
141
+ declare namespace Mongoplus { }
142
+
143
+ export = Mongoplus;
144
+