@paralect/hive 0.1.48 → 0.1.50-alpha.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/.cursor/commands/deslop.md +12 -0
- package/.hive/.babelrc +3 -0
- package/.hive/.cursor/commands/add-endpoint.md +262 -0
- package/.hive/.cursor/commands/add-handler.md +137 -0
- package/.hive/.cursor/commands/add-middleware.md +95 -0
- package/.hive/.cursor/commands/add-resource.md +71 -0
- package/.hive/.cursor/commands/add-scheduler.md +138 -0
- package/.hive/.cursor/commands/add-service.md +188 -0
- package/.hive/.cursor/skills/hive-auth/SKILL.md +134 -0
- package/.hive/.cursor/skills/hive-database/SKILL.md +103 -0
- package/.hive/.cursor/skills/hive-endpoint/SKILL.md +103 -0
- package/.hive/.cursor/skills/hive-handler/SKILL.md +88 -0
- package/.hive/.cursor/skills/hive-mapping/SKILL.md +85 -0
- package/.hive/.cursor/skills/hive-middleware/SKILL.md +104 -0
- package/.hive/.cursor/skills/hive-overview/SKILL.md +50 -0
- package/.hive/.cursor/skills/hive-scheduler/SKILL.md +94 -0
- package/.hive/.cursor/skills/hive-schema/SKILL.md +73 -0
- package/.hive/.cursor/skills/hive-service/SKILL.md +90 -0
- package/.hive/.dockerignore +1 -0
- package/.hive/Dockerfile +22 -0
- package/.hive/Dockerfile.dev +33 -0
- package/.hive/Dockerfile.prod +29 -0
- package/.hive/README.md +11 -0
- package/.hive/bin/deploy.sh +5 -0
- package/.hive/bin/start.sh +2 -0
- package/.hive/bootstrap-hive.js +118 -0
- package/.hive/deploy/api/Chart.yaml +6 -0
- package/.hive/deploy/api/staging.yaml +3 -0
- package/.hive/deploy/api/templates/deployment.yaml +44 -0
- package/.hive/deploy/api/templates/ingress.yaml +26 -0
- package/.hive/deploy/api/templates/service.yaml +14 -0
- package/.hive/deploy/script/Dockerfile +39 -0
- package/.hive/deploy/script/package-lock.json +1499 -0
- package/.hive/deploy/script/package.json +12 -0
- package/.hive/deploy/script/src/config.js +48 -0
- package/.hive/deploy/script/src/index.js +108 -0
- package/.hive/deploy/script/src/util.js +19 -0
- package/.hive/initial-data.json +176 -0
- package/.hive/package-lock.json +10242 -0
- package/.hive/package.json +98 -0
- package/.hive/ship_logo.png +0 -0
- package/.hive/src/app-config/app.js +3 -0
- package/.hive/src/app-config/assertEnv.js +15 -0
- package/.hive/src/app-config/index.js +62 -0
- package/.hive/src/app.js +69 -0
- package/.hive/src/assets/emails/components/header.mjml +13 -0
- package/.hive/src/assets/emails/dist/.gitkeep +0 -0
- package/.hive/src/assets/emails/signup-welcome.mjml +34 -0
- package/.hive/src/assets/emails/styles/index.mjml +77 -0
- package/.hive/src/autoMap/addHandlers.js +142 -0
- package/.hive/src/autoMap/getDependentFields.js +37 -0
- package/.hive/src/autoMap/mapSchema.js +99 -0
- package/.hive/src/autoMap/schemaMappings.js +13 -0
- package/.hive/src/autoMap/schemaMappings.json +3 -0
- package/.hive/src/bullMqBus.js +21 -0
- package/.hive/src/bullMqWrapper.js +23 -0
- package/.hive/src/db.js +52 -0
- package/.hive/src/emails/MyEmailComponent.jsx +14 -0
- package/.hive/src/emails/compiled/MyEmailComponent.js +18 -0
- package/.hive/src/emails/compiled/compiled/MyEmailComponent.js +18 -0
- package/.hive/src/helpers/db/ifUpdated.js +22 -0
- package/.hive/src/helpers/getMiddlewares.js +38 -0
- package/.hive/src/helpers/getResourceEndpoints.js +28 -0
- package/.hive/src/helpers/getResources.js +32 -0
- package/.hive/src/helpers/getSchemas.js +50 -0
- package/.hive/src/helpers/importHandlers.js +29 -0
- package/.hive/src/helpers/isZodArray.js +13 -0
- package/.hive/src/helpers/prettierFormat.js +8 -0
- package/.hive/src/helpers/schema/db.schema.js +9 -0
- package/.hive/src/helpers/schema/pagination.schema.js +14 -0
- package/.hive/src/ioEmitter.js +9 -0
- package/.hive/src/jsconfig.json +5 -0
- package/.hive/src/lib/node-mongo/.github/workflows/npm-publish.yml +32 -0
- package/.hive/src/lib/node-mongo/API.md +654 -0
- package/.hive/src/lib/node-mongo/CHANGELOG.md +98 -0
- package/.hive/src/lib/node-mongo/README.md +97 -0
- package/.hive/src/lib/node-mongo/package-lock.json +3682 -0
- package/.hive/src/lib/node-mongo/package.json +74 -0
- package/.hive/src/lib/node-mongo/src/index.js +64 -0
- package/.hive/src/lib/node-mongo/src/mongo-query-service.js +78 -0
- package/.hive/src/lib/node-mongo/src/mongo-service-error.js +15 -0
- package/.hive/src/lib/node-mongo/src/mongo-service.js +303 -0
- package/.hive/src/logger.js +43 -0
- package/.hive/src/middlewares/allowNoAuth.js +9 -0
- package/.hive/src/middlewares/attachUser.js +41 -0
- package/.hive/src/middlewares/global/extractUserTokens.js +15 -0
- package/.hive/src/middlewares/global/tryToAttachUser.js +33 -0
- package/.hive/src/middlewares/isAuthorized.js +18 -0
- package/.hive/src/middlewares/shouldExist.js +37 -0
- package/.hive/src/middlewares/shouldNotExist.js +19 -0
- package/.hive/src/middlewares/uploadFile.js +5 -0
- package/.hive/src/middlewares/validate.js +32 -0
- package/.hive/src/migrations/migration.js +8 -0
- package/.hive/src/migrations/migration.service.js +73 -0
- package/.hive/src/migrations/migrations/1.js +22 -0
- package/.hive/src/migrations/migrations-log/migration-log.schema.js +13 -0
- package/.hive/src/migrations/migrations-log/migration-log.service.js +50 -0
- package/.hive/src/migrations/migrations.schema.js +6 -0
- package/.hive/src/migrations/migrator.js +75 -0
- package/.hive/src/migrator.js +4 -0
- package/.hive/src/resources/_dev/endpoints/triggerSchedulerHandler.js +32 -0
- package/.hive/src/resources/health/endpoints/get.js +19 -0
- package/.hive/src/resources/schemaMappings/schemaMappings.schema.js +6 -0
- package/.hive/src/resources/tokens/methods/generateSecureToken.js +9 -0
- package/.hive/src/resources/tokens/methods/setToken.js +8 -0
- package/.hive/src/resources/tokens/methods/storeToken.js +35 -0
- package/.hive/src/resources/tokens/tokens.schema.js +11 -0
- package/.hive/src/resources/users/endpoints/getCurrentUser.js +14 -0
- package/.hive/src/resources/users/endpoints/getUserProfile.js +19 -0
- package/.hive/src/resources/users/handlers/test.js +1 -0
- package/.hive/src/resources/users/methods/ensureUserCreated.js +68 -0
- package/.hive/src/resources/users/users.schema.js +16 -0
- package/.hive/src/routes/index.js +172 -0
- package/.hive/src/routes/middlewares/attachCustomErrors.js +28 -0
- package/.hive/src/routes/middlewares/routeErrorHandler.js +27 -0
- package/.hive/src/scheduler/handlers/sendDailyReport.example.js +7 -0
- package/.hive/src/scheduler.js +32 -0
- package/.hive/src/security.util.js +38 -0
- package/.hive/src/services/emailService.js +15 -0
- package/.hive/src/services/globalTest.js +0 -0
- package/.hive/src/services/setCookie.js +21 -0
- package/.hive/src/socketIo.js +99 -0
- package/.hive/tsconfig.json +31 -0
- package/AGENTS.md +96 -0
- package/README.md +271 -0
- package/cli/helpers/docker.js +59 -0
- package/cli/helpers/envCheck.js +123 -0
- package/cli/helpers/findPort.js +32 -0
- package/cli/hive.js +155 -15
- package/package.json +1 -1
- package/starter/.cursor/commands/add-endpoint.md +262 -0
- package/starter/.cursor/commands/add-handler.md +137 -0
- package/starter/.cursor/commands/add-middleware.md +95 -0
- package/starter/.cursor/commands/add-resource.md +71 -0
- package/starter/.cursor/commands/add-scheduler.md +138 -0
- package/starter/.cursor/commands/add-service.md +188 -0
- package/starter/.cursor/skills/hive-auth/SKILL.md +134 -0
- package/starter/.cursor/skills/hive-database/SKILL.md +103 -0
- package/starter/.cursor/skills/hive-endpoint/SKILL.md +103 -0
- package/starter/.cursor/skills/hive-handler/SKILL.md +88 -0
- package/starter/.cursor/skills/hive-mapping/SKILL.md +85 -0
- package/starter/.cursor/skills/hive-middleware/SKILL.md +104 -0
- package/starter/.cursor/skills/hive-overview/SKILL.md +50 -0
- package/starter/.cursor/skills/hive-scheduler/SKILL.md +94 -0
- package/starter/.cursor/skills/hive-schema/SKILL.md +73 -0
- package/starter/.cursor/skills/hive-service/SKILL.md +90 -0
- package/starter/src/app.js +4 -3
- package/test-app/.cursor/commands/add-endpoint.md +262 -0
- package/test-app/.cursor/commands/add-handler.md +137 -0
- package/test-app/.cursor/commands/add-middleware.md +95 -0
- package/test-app/.cursor/commands/add-resource.md +71 -0
- package/test-app/.cursor/commands/add-scheduler.md +138 -0
- package/test-app/.cursor/commands/add-service.md +188 -0
- package/test-app/.cursor/skills/hive-auth/SKILL.md +134 -0
- package/test-app/.cursor/skills/hive-database/SKILL.md +103 -0
- package/test-app/.cursor/skills/hive-endpoint/SKILL.md +103 -0
- package/test-app/.cursor/skills/hive-handler/SKILL.md +88 -0
- package/test-app/.cursor/skills/hive-mapping/SKILL.md +85 -0
- package/test-app/.cursor/skills/hive-middleware/SKILL.md +104 -0
- package/test-app/.cursor/skills/hive-overview/SKILL.md +50 -0
- package/test-app/.cursor/skills/hive-scheduler/SKILL.md +94 -0
- package/test-app/.cursor/skills/hive-schema/SKILL.md +73 -0
- package/test-app/.cursor/skills/hive-service/SKILL.md +90 -0
- package/test-app/package-lock.json +8684 -0
- package/test-app/package.json +21 -0
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
# 2.0.0 API Reference
|
|
2
|
+
|
|
3
|
+
- [Node Mongo](#node-mongo)
|
|
4
|
+
- [connect](#connect)
|
|
5
|
+
- [Manager](#manager)
|
|
6
|
+
- [createService](#createservice)
|
|
7
|
+
- [setServiceMethod](#setservicemethod)
|
|
8
|
+
- [createQueryService](#createqueryservice)
|
|
9
|
+
- [setQueryServiceMethod](#setqueryservicemethod)
|
|
10
|
+
- [Query Service](#query-service)
|
|
11
|
+
- [name](#name)
|
|
12
|
+
- [exists](#exists)
|
|
13
|
+
- [find](#find)
|
|
14
|
+
- [findOne](#findone)
|
|
15
|
+
- [aggregate](#aggregate)
|
|
16
|
+
- [count](#count)
|
|
17
|
+
- [distinct](#distinct)
|
|
18
|
+
- [geoHaystackSearch](#geohaystacksearch)
|
|
19
|
+
- [indexes](#indexes)
|
|
20
|
+
- [mapReduce](#mapreduce)
|
|
21
|
+
- [stats](#stats)
|
|
22
|
+
- [Service](#service)
|
|
23
|
+
- [on](#on)
|
|
24
|
+
- [once](#once)
|
|
25
|
+
- [onPropertiesUpdated](#onpropertiesupdated)
|
|
26
|
+
- [generateId](#generateid)
|
|
27
|
+
- [create](#create)
|
|
28
|
+
- [updateOne](#updateone)
|
|
29
|
+
- [updateMany](#updatemany)
|
|
30
|
+
- [remove](#remove)
|
|
31
|
+
- [performTransaction](#performtransaction)
|
|
32
|
+
- [atomic.bulkWrite](#atomicbulkwrite)
|
|
33
|
+
- [atomic.createIndex](#atomiccreateindex)
|
|
34
|
+
- [atomic.drop](#atomicdrop)
|
|
35
|
+
- [atomic.dropIndex](#atomicdropindex)
|
|
36
|
+
- [atomic.dropIndexes](#atomicdropindexes)
|
|
37
|
+
- [atomic.findOneAndDelete](#atomicfindoneanddelete)
|
|
38
|
+
- [atomic.findOneAndUpdate](#atomicfindoneandupdate)
|
|
39
|
+
- [atomic.insert](#atomicinsert)
|
|
40
|
+
- [atomic.remove](#atomicremove)
|
|
41
|
+
- [atomic.update](#atomicupdate)
|
|
42
|
+
|
|
43
|
+
## Node Mongo
|
|
44
|
+
|
|
45
|
+
### connect
|
|
46
|
+
|
|
47
|
+
Connect to MongoDB.
|
|
48
|
+
|
|
49
|
+
#### Arguments:
|
|
50
|
+
|
|
51
|
+
- `connectionString: String` - [connection string](https://docs.mongodb.com/manual/reference/connection-string/).
|
|
52
|
+
- `connectionSettings: Object` - optional [connection settings](http://mongodb.github.io/node-mongodb-native/2.1/reference/connecting/connection-settings/).
|
|
53
|
+
|
|
54
|
+
#### Returns:
|
|
55
|
+
|
|
56
|
+
A [Manager](#manager) instance.
|
|
57
|
+
|
|
58
|
+
#### Example:
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
const db = require("node-mongo").connect("mongodb://localhost:27017/home", {
|
|
62
|
+
poolSize: 10,
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Manager
|
|
67
|
+
|
|
68
|
+
### Methods:
|
|
69
|
+
|
|
70
|
+
- [createQueryService](#createqueryservice)
|
|
71
|
+
- [setQueryServiceMethod](#setqueryservicemethod)
|
|
72
|
+
- [createService](#createservice)
|
|
73
|
+
- [setServiceMethod](#setservicemethod)
|
|
74
|
+
|
|
75
|
+
### createQueryService
|
|
76
|
+
|
|
77
|
+
Create and return [Query Service](#query-service) instance.
|
|
78
|
+
|
|
79
|
+
#### Arguments:
|
|
80
|
+
|
|
81
|
+
- `collectionName: String` - name of the MongoDB collection.
|
|
82
|
+
|
|
83
|
+
#### Returns:
|
|
84
|
+
|
|
85
|
+
A [Query Service](#query-service) instance.
|
|
86
|
+
|
|
87
|
+
#### Example:
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
const usersQueryService = db.createQueryService("users");
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### setQueryServiceMethod
|
|
94
|
+
|
|
95
|
+
Add custom method for [Query Service](#query-service).
|
|
96
|
+
|
|
97
|
+
#### Arguments:
|
|
98
|
+
|
|
99
|
+
- `name: String` - name of the method, that will be used to call method.
|
|
100
|
+
- `method: (QueryService, ...args) => any` - custom function in which we can manipulate the collection. The custom function takes the service itself as the first parameter, and the remaining parameters are the parameters that are passed when this custom function is called.
|
|
101
|
+
|
|
102
|
+
#### Example:
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
const db = require("node-mongo").connect(connectionString);
|
|
106
|
+
|
|
107
|
+
db.setQueryServiceMethod("findByName", (service, name, options) => {
|
|
108
|
+
return service.findOne({ name }, { collation: "en", ...options });
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const userService = db.createQueryService("users");
|
|
112
|
+
|
|
113
|
+
const user = userService.findByName("Bob", { projection: { name: 1, age: 1 } });
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### createService
|
|
117
|
+
|
|
118
|
+
Create and return [Service](#service) instance.
|
|
119
|
+
|
|
120
|
+
#### Arguments:
|
|
121
|
+
|
|
122
|
+
- `collectionName: String` - the name of the collection with which the service will work.
|
|
123
|
+
- `options: Object` - optional object with options of the service.
|
|
124
|
+
- `addCreatedOnField: Boolean = true` - if `true`, we add the `createdOn` field for each document to be created using the [create](#create) method.
|
|
125
|
+
- `addUpdatedOnField: Boolean = true` - if `true`, we add and update the `updatedOn` field for each document to be updated using [updateOne](#updateone) or [updateMany](#updatemany) methods.
|
|
126
|
+
- `useStringId: Boolean = true` - if `true`, we replace `_id` ([ObjectId](https://docs.mongodb.com/manual/reference/method/ObjectId/) by default) with a string that is generated using the [generateId](#generateid) method.
|
|
127
|
+
- `validate: (doc) => Promise<{ error, value }>` - optional function that accepts a collection document and returns the result of the validation of this document. Result should be an object with `value` and `error` fields. The error will be thrown if `error` is a truthy value.
|
|
128
|
+
- `emitter: Emitter = new EventEmitter()` - optional instance of Emitter, which partially implements the [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter) interface ([emit](https://nodejs.org/api/events.html#events_emitter_emit_eventname_args), [on](https://nodejs.org/api/events.html#events_emitter_on_eventname_listener), [once](https://nodejs.org/api/events.html#events_emitter_once_eventname_listener) methods are enough).
|
|
129
|
+
|
|
130
|
+
#### Returns:
|
|
131
|
+
|
|
132
|
+
A [Service](#service) instance.
|
|
133
|
+
|
|
134
|
+
#### Example:
|
|
135
|
+
|
|
136
|
+
`user.schema.js`
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
const Joi = require("Joi");
|
|
140
|
+
|
|
141
|
+
const userSchema = Joi.object({
|
|
142
|
+
_id: Joi.string(),
|
|
143
|
+
createdOn: Joi.date(),
|
|
144
|
+
updatedOn: Joi.date(),
|
|
145
|
+
name: Joi.string(),
|
|
146
|
+
status: Joi.string().valid("active", "inactive"),
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// you can use validate method from Joi
|
|
150
|
+
module.validate = (obj) => userSchema.validate(obj);
|
|
151
|
+
|
|
152
|
+
// or it could be your custom function
|
|
153
|
+
module.validate = (obj) => {
|
|
154
|
+
if (!obj.name) {
|
|
155
|
+
return {
|
|
156
|
+
value: obj,
|
|
157
|
+
error: {
|
|
158
|
+
details: [{ message: "Name is required" }],
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
return { value: obj };
|
|
163
|
+
};
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
`user.service.js`
|
|
167
|
+
|
|
168
|
+
```js
|
|
169
|
+
const { validate } = require("./user.schema");
|
|
170
|
+
|
|
171
|
+
const userService = db.createService("users", {
|
|
172
|
+
useStringId: false,
|
|
173
|
+
validate,
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### setServiceMethod
|
|
178
|
+
|
|
179
|
+
Add custom method for [Service](#service).
|
|
180
|
+
|
|
181
|
+
#### Arguments:
|
|
182
|
+
|
|
183
|
+
- `name: String` - name of the method, that will be used to call method.
|
|
184
|
+
- `method: (Service, ...args) => any` - custom function in which we can manipulate the collection. The custom function takes the service itself as the first parameter, and the remaining parameters are the parameters that are passed when this custom function is called.
|
|
185
|
+
|
|
186
|
+
#### Example:
|
|
187
|
+
|
|
188
|
+
```js
|
|
189
|
+
const db = require("node-mongo").connect(connectionString);
|
|
190
|
+
|
|
191
|
+
db.setServiceMethod("createByName", (service, name) => {
|
|
192
|
+
return service.create({ name });
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const userService = db.createService("users");
|
|
196
|
+
|
|
197
|
+
const user = userService.createByName("Bob");
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## Query Service
|
|
201
|
+
|
|
202
|
+
Query Service allows you to make requests to the database to get needed data, but this service not allow to modify data in the database.
|
|
203
|
+
|
|
204
|
+
### Properties
|
|
205
|
+
|
|
206
|
+
- [name](#name)
|
|
207
|
+
|
|
208
|
+
### Methods
|
|
209
|
+
|
|
210
|
+
- [exists](#exists)
|
|
211
|
+
- [find](#find)
|
|
212
|
+
- [findOne](#findone)
|
|
213
|
+
- [aggregate](#aggregate)
|
|
214
|
+
- [count](#count)
|
|
215
|
+
- [distinct](#distinct)
|
|
216
|
+
- [geoHaystackSearch](#geohaystacksearch)
|
|
217
|
+
- [indexes](#indexes)
|
|
218
|
+
- [mapReduce](#mapreduce)
|
|
219
|
+
- [stats](#stats)
|
|
220
|
+
|
|
221
|
+
### name
|
|
222
|
+
|
|
223
|
+
Name of the collection for which service was created.
|
|
224
|
+
|
|
225
|
+
#### Example:
|
|
226
|
+
|
|
227
|
+
```js
|
|
228
|
+
const db = require("node-mongo").connect(connectionString);
|
|
229
|
+
|
|
230
|
+
const userQueryService = db.createQueryService("users");
|
|
231
|
+
|
|
232
|
+
console.log(userQueryService.name); // users
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### exists
|
|
236
|
+
|
|
237
|
+
Gets existence of the documents matching the filter. Under the hood, [count](#count) method is used.
|
|
238
|
+
|
|
239
|
+
#### Arguments:
|
|
240
|
+
|
|
241
|
+
- `query: Object` - query for [count](#count) operation.
|
|
242
|
+
- `options: Object` - optional settings for [count](#count) operation.
|
|
243
|
+
|
|
244
|
+
#### Returns:
|
|
245
|
+
|
|
246
|
+
Boolean value.
|
|
247
|
+
|
|
248
|
+
#### Example:
|
|
249
|
+
|
|
250
|
+
```js
|
|
251
|
+
const userService = db.createService("users");
|
|
252
|
+
const userExists = await userService.exists({ name: "Bob" });
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### find
|
|
256
|
+
|
|
257
|
+
Gets documents matching the filter. Under the hood, monk's [find](https://automattic.github.io/monk/docs/collection/find.html) method is used.
|
|
258
|
+
|
|
259
|
+
#### Arguments:
|
|
260
|
+
|
|
261
|
+
- `query: Object` - object, according to which we receive documents.
|
|
262
|
+
- `options: Object` - optional object with options for query.
|
|
263
|
+
- `perPage: Number = 100` - optional number of returned documents.
|
|
264
|
+
- `page: Number = 0` - optional page number with results.
|
|
265
|
+
- `rawCursor: Boolean` - optional parameter to get the raw [mongo cursor](http://mongodb.github.io/node-mongodb-native/3.2/api/Cursor.html). You can find more usage examples in [monk docs](https://automattic.github.io/monk/docs/collection/find.html).
|
|
266
|
+
- [...default mongo options](https://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#find)
|
|
267
|
+
|
|
268
|
+
#### Returns:
|
|
269
|
+
|
|
270
|
+
An object with following fields:
|
|
271
|
+
|
|
272
|
+
- `results: Object[]` - array of documents.
|
|
273
|
+
|
|
274
|
+
Additional fields will be added, if the `page` option exists and is greater than zero:
|
|
275
|
+
|
|
276
|
+
- `pagesCount: Number` - total number of pages.
|
|
277
|
+
- `count: Number` - total number of documents that satisfy the condition.
|
|
278
|
+
|
|
279
|
+
#### Example:
|
|
280
|
+
|
|
281
|
+
```js
|
|
282
|
+
const db = require("node-mongo").connect(connectionString);
|
|
283
|
+
|
|
284
|
+
const userQueryService = db.createQueryService("users");
|
|
285
|
+
|
|
286
|
+
const { results, pagesCount, count } = await userQueryService.find(
|
|
287
|
+
{ name: "Bob" },
|
|
288
|
+
{ page: 1, perPage: 30 }
|
|
289
|
+
);
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
### findOne
|
|
293
|
+
|
|
294
|
+
Get one document that satisfies the specified condition. Under the hood, [find](#find) method is used.
|
|
295
|
+
|
|
296
|
+
#### Arguments:
|
|
297
|
+
|
|
298
|
+
- `query: Object` - query for [find](#find) operation.
|
|
299
|
+
- `options: Object` - optional settings for [find](#find) operation.
|
|
300
|
+
|
|
301
|
+
#### Returns:
|
|
302
|
+
|
|
303
|
+
A document or `null`. If several documents satisfy the condition, then we throw an error.
|
|
304
|
+
|
|
305
|
+
#### Example:
|
|
306
|
+
|
|
307
|
+
```js
|
|
308
|
+
const userService = db.createService("users");
|
|
309
|
+
try {
|
|
310
|
+
const user = await userService.findOne({ name: "Bob" });
|
|
311
|
+
} catch (error) {
|
|
312
|
+
console.error("Several users were found");
|
|
313
|
+
}
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
### aggregate
|
|
317
|
+
|
|
318
|
+
Calculates aggregate values for the data in a collection.
|
|
319
|
+
|
|
320
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/aggregate.html). Under the hood, native [aggregate](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#aggregate) method is used.
|
|
321
|
+
|
|
322
|
+
### count
|
|
323
|
+
|
|
324
|
+
Gets the number of documents matching the filter.
|
|
325
|
+
|
|
326
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/count.html). Under the hood, native [countDocuments](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#countDocuments) method is used.
|
|
327
|
+
|
|
328
|
+
### distinct
|
|
329
|
+
|
|
330
|
+
The distinct command returns a list of distinct values for the given key across a collection.
|
|
331
|
+
|
|
332
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/distinct.html). Under the hood, native [distinct](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#distinct) method is used.
|
|
333
|
+
|
|
334
|
+
### geoHaystackSearch
|
|
335
|
+
|
|
336
|
+
Execute a geo search using a geo haystack index on a collection.
|
|
337
|
+
|
|
338
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/geoHaystackSearch.html). Under the hood, native [geoHaystackSearch](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#geoHaystackSearch) method is used.
|
|
339
|
+
|
|
340
|
+
### indexes
|
|
341
|
+
|
|
342
|
+
Returns an array that holds a list of documents that identify and describe the existing indexes on the collection.
|
|
343
|
+
|
|
344
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/indexes.html). Under the hood, native [indexes](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#indexes) method is used.
|
|
345
|
+
|
|
346
|
+
### mapReduce
|
|
347
|
+
|
|
348
|
+
Run Map Reduce across a collection. Be aware that the inline option for out will return an array of results not a collection.
|
|
349
|
+
|
|
350
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/mapReduce.html). Under the hood, native [mapReduce](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#mapReduce) method is used.
|
|
351
|
+
|
|
352
|
+
### stats
|
|
353
|
+
|
|
354
|
+
Get all the collection statistics.
|
|
355
|
+
|
|
356
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/stats.html). Under the hood, native [stats](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#stats) method is used.
|
|
357
|
+
|
|
358
|
+
## Service
|
|
359
|
+
|
|
360
|
+
Service extends [Query Service](#query-service), therefore instance of this service has all methods of the [Query Service](#query-service).
|
|
361
|
+
|
|
362
|
+
Service emits events that you can subscribe to. **Please note that only the methods mentioned below can emit events**.
|
|
363
|
+
|
|
364
|
+
#### Events:
|
|
365
|
+
|
|
366
|
+
- `created` — emits when you create a document with [create](#create) method.
|
|
367
|
+
- `updated` — emits when you create a document with [updateOne](#updateone) or [updateMany](#updatemany) methods.
|
|
368
|
+
- `removed` — emits when you remove a document with [remove](#remove) method.
|
|
369
|
+
|
|
370
|
+
Methods in the `atomic` namespace are ordinary monk's methods. They don't emit any events and don't validate data.
|
|
371
|
+
|
|
372
|
+
#### Methods:
|
|
373
|
+
|
|
374
|
+
- [on](#on)
|
|
375
|
+
- [once](#once)
|
|
376
|
+
- [onPropertiesUpdated](#onpropertiesupdated)
|
|
377
|
+
- [generateId](#generateid)
|
|
378
|
+
- [create](#create)
|
|
379
|
+
- [updateOne](#updateone)
|
|
380
|
+
- [updateMany](#updatemany)
|
|
381
|
+
- [remove](#remove)
|
|
382
|
+
- [performTransaction](#performtransaction)
|
|
383
|
+
- [atomic.bulkWrite](#atomicbulkwrite)
|
|
384
|
+
- [atomic.createIndex](#atomiccreateindex)
|
|
385
|
+
- [atomic.drop](#atomicdrop)
|
|
386
|
+
- [atomic.dropIndex](#atomicdropindex)
|
|
387
|
+
- [atomic.dropIndexes](#atomicdropindexes)
|
|
388
|
+
- [atomic.findOneAndDelete](#atomicfindoneanddelete)
|
|
389
|
+
- [atomic.findOneAndUpdate](#atomicfindoneandupdate)
|
|
390
|
+
- [atomic.insert](#atomicinsert)
|
|
391
|
+
- [atomic.remove](#atomicremove)
|
|
392
|
+
- [atomic.update](#atomicupdate)
|
|
393
|
+
|
|
394
|
+
### on
|
|
395
|
+
|
|
396
|
+
Subscribes to database change events.
|
|
397
|
+
|
|
398
|
+
#### Arguments:
|
|
399
|
+
|
|
400
|
+
- `eventName: String` - name of the database [event](#events).
|
|
401
|
+
- `handler: ({ doc, prevDoc }) => any` - event handler.
|
|
402
|
+
|
|
403
|
+
#### Returns:
|
|
404
|
+
|
|
405
|
+
A reference to the `EventEmitter`.
|
|
406
|
+
|
|
407
|
+
#### Example:
|
|
408
|
+
|
|
409
|
+
```js
|
|
410
|
+
const userService = db.createService("users");
|
|
411
|
+
userService.on("updated", ({ doc, prevDoc }) => {});
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
### once
|
|
415
|
+
|
|
416
|
+
Subscribe to database change events only once. The first time evenName is triggered listener handler is removed and then invoked.
|
|
417
|
+
|
|
418
|
+
#### Arguments:
|
|
419
|
+
|
|
420
|
+
- `eventName: String` - name of the database [event](#events).
|
|
421
|
+
- `handler: ({ doc, prevDoc }) => any` - event handler.
|
|
422
|
+
|
|
423
|
+
#### Returns:
|
|
424
|
+
|
|
425
|
+
Returns a reference to the `EventEmitter`.
|
|
426
|
+
|
|
427
|
+
#### Example:
|
|
428
|
+
|
|
429
|
+
```js
|
|
430
|
+
const userService = db.createService("users");
|
|
431
|
+
userService.once("updated", ({ doc, prevDoc }) => {});
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
### onPropertiesUpdated
|
|
435
|
+
|
|
436
|
+
Deep compare doc and prevDoc from `updated` event. When something changed - executes callback.
|
|
437
|
+
|
|
438
|
+
#### Arguments:
|
|
439
|
+
|
|
440
|
+
- `properties: String[] | Object` - properties to compare
|
|
441
|
+
- `handler: ({ doc, prevDoc }) => any` - event handler.
|
|
442
|
+
|
|
443
|
+
#### Returns:
|
|
444
|
+
|
|
445
|
+
A reference to the `EventEmitter`.
|
|
446
|
+
|
|
447
|
+
#### Example:
|
|
448
|
+
|
|
449
|
+
```js
|
|
450
|
+
const userService = db.createService("users");
|
|
451
|
+
|
|
452
|
+
// Callback executed only if user lastName or firstName are different in current or updated document
|
|
453
|
+
userService.onPropertiesUpdated(
|
|
454
|
+
["user.firstName", "user.lastName"],
|
|
455
|
+
({ doc, prevDoc }) => {}
|
|
456
|
+
);
|
|
457
|
+
|
|
458
|
+
// Callback executed only if user first name changes to `Bob`
|
|
459
|
+
userService.onPropertiesUpdated(
|
|
460
|
+
{ "user.firstName": "Bob" },
|
|
461
|
+
({ doc, prevDoc }) => {}
|
|
462
|
+
);
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
### generateId
|
|
466
|
+
|
|
467
|
+
Get ID for mongoDB documents.
|
|
468
|
+
|
|
469
|
+
#### Returns:
|
|
470
|
+
|
|
471
|
+
ID string.
|
|
472
|
+
|
|
473
|
+
#### Example:
|
|
474
|
+
|
|
475
|
+
```js
|
|
476
|
+
const userService = db.createService("users");
|
|
477
|
+
const id = userService.generateId();
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
### create
|
|
481
|
+
|
|
482
|
+
Inserts one object or array of the objects to the database. Validates the documents before creation if service was created with `validate` option. Adds `createdOn` field to the document. Publishes the `created` event.
|
|
483
|
+
|
|
484
|
+
#### Arguments:
|
|
485
|
+
|
|
486
|
+
- `documents: Object | Object[]` - object or array of objects to create.
|
|
487
|
+
|
|
488
|
+
#### Returns:
|
|
489
|
+
|
|
490
|
+
Object or array of created objects.
|
|
491
|
+
|
|
492
|
+
#### Example:
|
|
493
|
+
|
|
494
|
+
```js
|
|
495
|
+
const userService = db.createService("users");
|
|
496
|
+
const users = await userService.create([{ name: "Bob" }, { name: "Alice" }]);
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
### updateOne
|
|
500
|
+
|
|
501
|
+
Updates entity found by query in the database. Validates the document before save if service was created with `validate` option. Updates `updatedOn` field in the document. Publishes the `updated` event. **Throws out an error if more than one document is found or if no document is found**.
|
|
502
|
+
|
|
503
|
+
#### Arguments:
|
|
504
|
+
|
|
505
|
+
- `query: Object` - query for [findOne](#findone) operation.
|
|
506
|
+
- `updateFn: (doc) => doc` - update function that recieves old document and should return updated one.
|
|
507
|
+
- `options: Object` - optional options for [findOne](#findone) operation.
|
|
508
|
+
|
|
509
|
+
#### Returns:
|
|
510
|
+
|
|
511
|
+
Updated document.
|
|
512
|
+
|
|
513
|
+
#### Example:
|
|
514
|
+
|
|
515
|
+
```js
|
|
516
|
+
const userService = db.createService("users");
|
|
517
|
+
try {
|
|
518
|
+
const updatedUser = await userService.updateOne({ _id: "1" }, (doc) => ({
|
|
519
|
+
...name,
|
|
520
|
+
name: "Alex",
|
|
521
|
+
}));
|
|
522
|
+
} catch (error) {
|
|
523
|
+
console.error(error.message);
|
|
524
|
+
}
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
### updateMany
|
|
528
|
+
|
|
529
|
+
Updates entity found by query in the database. Validates the documents before save if service was created with `validate` option. Updates `updatedOn` field in the every the document. Publishes the `updated` event for every document.
|
|
530
|
+
|
|
531
|
+
#### Arguments:
|
|
532
|
+
|
|
533
|
+
- `query: Object` - query for [find](#find) operation.
|
|
534
|
+
- `updateFn: (doc) => doc` - update function that recieves old document and should return updated one.
|
|
535
|
+
- `options: Object` - optional options for [find](#find) operation.
|
|
536
|
+
|
|
537
|
+
#### Returns:
|
|
538
|
+
|
|
539
|
+
Array of updated documents.
|
|
540
|
+
|
|
541
|
+
#### Example:
|
|
542
|
+
|
|
543
|
+
```js
|
|
544
|
+
const userService = db.createService("users");
|
|
545
|
+
const updatedUsers = await userService.updateMany({ age: "27" }, (doc) => ({
|
|
546
|
+
...name,
|
|
547
|
+
alive: false,
|
|
548
|
+
}));
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
### remove
|
|
552
|
+
|
|
553
|
+
Removes documents found by query. Publishes the `removed` event for every document.
|
|
554
|
+
|
|
555
|
+
#### Arguments:
|
|
556
|
+
|
|
557
|
+
- `query: Object` - query for [find](#find) operation.
|
|
558
|
+
- `options: Object` - optional options for [find](#find) operation.
|
|
559
|
+
|
|
560
|
+
#### Returns:
|
|
561
|
+
|
|
562
|
+
Array of removed documents.
|
|
563
|
+
|
|
564
|
+
#### Example:
|
|
565
|
+
|
|
566
|
+
```js
|
|
567
|
+
const userService = db.createService("users");
|
|
568
|
+
const removedUsers = await userService.remove({ name: "Alex" });
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
### performTransaction
|
|
572
|
+
|
|
573
|
+
Starts a new session, performs transaction and ends this session.
|
|
574
|
+
|
|
575
|
+
#### Arguments:
|
|
576
|
+
|
|
577
|
+
- `transactionFn: (Session) => Promise<any>` - function to be performed within a transaction. **It must return Promise**.
|
|
578
|
+
- `options: Object` - optional settings for [startSession](http://mongodb.github.io/node-mongodb-native/3.2/api/MongoClient.html#startSession) operation.
|
|
579
|
+
|
|
580
|
+
#### Returns:
|
|
581
|
+
|
|
582
|
+
Resulting Promise of operations run within transaction.
|
|
583
|
+
|
|
584
|
+
#### Example:
|
|
585
|
+
|
|
586
|
+
```js
|
|
587
|
+
const userService = db.createService("users");
|
|
588
|
+
const teamService = db.createService("teams");
|
|
589
|
+
|
|
590
|
+
await userService.performTransaction(async (session) => {
|
|
591
|
+
await userService.create({}, { session });
|
|
592
|
+
await teamService.create({}, { session });
|
|
593
|
+
});
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
### atomic.bulkWrite
|
|
597
|
+
|
|
598
|
+
Perform a bulkWrite operation without a fluent API.
|
|
599
|
+
|
|
600
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/bulkWrite.html). Under the hood, native [bulkWrite](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#bulkWrite) method is used.
|
|
601
|
+
|
|
602
|
+
### atomic.createIndex
|
|
603
|
+
|
|
604
|
+
Creates an index on the db and collection (will not create if already exists).
|
|
605
|
+
|
|
606
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/createIndex.html). Under the hood, native [createIndex](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#createIndex) method is used.
|
|
607
|
+
|
|
608
|
+
### atomic.drop
|
|
609
|
+
|
|
610
|
+
Drop the collection from the database, removing it permanently. New accesses will create a new collection.
|
|
611
|
+
|
|
612
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/drop.html). Under the hood, native [drop](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#drop) method is used.
|
|
613
|
+
|
|
614
|
+
### atomic.dropIndex
|
|
615
|
+
|
|
616
|
+
Drops indexes from this collection.
|
|
617
|
+
|
|
618
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/dropIndex.html). Under the hood, native [dropIndex](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#dropIndex) method is used.
|
|
619
|
+
|
|
620
|
+
### atomic.dropIndexes
|
|
621
|
+
|
|
622
|
+
Drops all indexes from this collection.
|
|
623
|
+
|
|
624
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/dropIndexes.html). Under the hood, native [dropIndexes](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#dropIndexes) method is used.
|
|
625
|
+
|
|
626
|
+
### atomic.findOneAndDelete
|
|
627
|
+
|
|
628
|
+
Find a document and delete it in one atomic operation.
|
|
629
|
+
|
|
630
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/findOneAndDelete.html). Under the hood, native [findOneAndDelete](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#findOneAndDelete) method is used.
|
|
631
|
+
|
|
632
|
+
### atomic.findOneAndUpdate
|
|
633
|
+
|
|
634
|
+
Find a document and update it in one atomic operation.
|
|
635
|
+
|
|
636
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/findOneAndUpdate.html). Under the hood, native [findOneAndUpdate](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#findOneAndUpdate) method is used.
|
|
637
|
+
|
|
638
|
+
### atomic.insert
|
|
639
|
+
|
|
640
|
+
Inserts a single document or a an array of documents into MongoDB.
|
|
641
|
+
|
|
642
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/insert.html). Under the hood, native [insertOne](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#insertOne) and [insertMany](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#insertMany) methods are used.
|
|
643
|
+
|
|
644
|
+
### atomic.remove
|
|
645
|
+
|
|
646
|
+
Remove documents.
|
|
647
|
+
|
|
648
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/remove.html). Under the hood, native [deleteOne](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#deleteOne) and [deleteMany](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#deleteMany) methods are used.
|
|
649
|
+
|
|
650
|
+
### atomic.update
|
|
651
|
+
|
|
652
|
+
Modifies an existing document or documents in a collection.
|
|
653
|
+
|
|
654
|
+
[Monk's method](https://automattic.github.io/monk/docs/collection/update.html). Under the hood, native [updateOne](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#updateOne) and [updateMany](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#updateMany) methods are used.
|