@mongodb-js/compass-aggregations 0.0.0-experimental-1c53312ce4905234885618d41ae95fde9c21aa18

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 ADDED
@@ -0,0 +1,268 @@
1
+ # compass-aggregations
2
+
3
+ > Compass Plugin for the MongoDB Aggregation Pipeline Builder.
4
+
5
+ [![](https://docs.mongodb.com/compass/master/_images/agg-builder-export-dropdown.png)](https://docs.mongodb.com/compass/master/aggregation-pipeline-builder/)
6
+
7
+ ## Code Tour
8
+
9
+ - `src/components/aggregations` The primary export is connected component
10
+ - `src/modules/` is where action happens
11
+ - action creators components call
12
+ - reducers that call dataService, window.open, emit to other plugins, etc.
13
+ - follows the `ducks` pattern
14
+ - `src/stores/store` is where plugin listens+responds to events of interest from other plugins
15
+ - store is global state instantiated via `configureStore()`
16
+ - All tests are local to their module e.g. `src/*/<module>.spec.js`
17
+
18
+ ### Note: Cross-plugin Communication
19
+
20
+ > How does clicking on export to language button in aggregation builder show the modal that has code in it?
21
+
22
+ The aggregation builder in Compass actually involves 2 Compass plugins:
23
+
24
+ - [`<Aggregation />`](https://github.com/mongodb-js/compass-aggregations) Plugin for primary UI
25
+ - [`<ExportToLanguage />`](https://github.com/mongodb-js/compass-export-to-language) Modal plugin that connects `<Aggregation />` to `bson-transpilers` [for actual compiling to another language](https://github.com/mongodb-js/bson-transpilers)
26
+
27
+ Here's how these 2 plugins come together:
28
+
29
+ - `./src/modules/export-to-language.js` `appRegistry.emit('open-aggregation-export-to-language', generatePipelineAsString())`
30
+ - [`compass-export-to-language/src/stores/store.js`](https://github.com/mongodb-js/compass-export-to-language/blob/master/src/stores/store.js#L16) Listener for 'export to lang' event via appRegistry and renders its modal.
31
+ - [`compass-export-to-language/src/modules/export-query.js`](https://github.com/mongodb-js/compass-export-to-language/blob/master/src/modules/export-query.js#L56) has reducer for calling `bson-transpilers.compile()` which populates the code in the modal dialog.
32
+
33
+ ### Usage with a `mongodb-data-service` Provider
34
+
35
+ See `./examples-data-service-provider.js` for details on what `data-service` functions are used and the applicable options for each.
36
+
37
+ ## Contributing
38
+
39
+ If you're interested in helping with the Aggregation Builder plugin, we'd be over the moon excited! Here are a few ideas if you're interested but not sure where to start:
40
+
41
+ - [Add a new example aggregation](https://github.com/mongodb-js/compass-aggregations/#adding-new-examples)
42
+ - Additions/clarifications/improvements to `README`'s
43
+ - More tests (especially edge cases!)
44
+ - Generate `jsdoc` html to include in GitHub pages
45
+ - Improve build times (e.g. audit our webpack configs)
46
+
47
+ ## Related
48
+
49
+ - [`<ExportToLanguage />`](https://github.com/mongodb-js/compass-export-to-language) Modal plugin that connects `<Aggregation />` to `bson-transpilers` [for actual compiling to another language](https://github.com/mongodb-js/bson-transpilers)
50
+ - [`mongodb-js/stage-validator`](https://github.com/mongodb-js/stage-validator) Aggregation Pipeline Stage grammar.
51
+ - [`bson-transpilers`](https://github.com/mongodb-js/bson-transpilers) Read the amazing: [Compiler in JavaScript using ANTLR](https://medium.com/dailyjs/compiler-in-javascript-using-antlr-9ec53fd2780f)
52
+ - [`mongodb-js/ace-mode`](https://github.com/mongodb-js/ace-mode) MongoDB highlighting rules for ACE.
53
+ - [`mongodb-js/ace-autocompleter`](https://github.com/mongodb-js/ace-autocompleter) Makes ACE autocompletion aware of MongoDB Aggregation Pipeline [operators, expressions, and fields](https://github.com/mongodb-js/ace-autocompleter/tree/master/lib/constants).
54
+
55
+ ## Usage
56
+
57
+ This plugin uses an instance of a Redux store and not the traditional singleton,
58
+ so the store instance must be passed to the plugin. The plugin exports a function
59
+ to initialise the store instance, which decorates it with various methods to
60
+ conveniently set any values it uses.
61
+
62
+ This is for:
63
+ - `@mongodb-js/compass-aggregations 4.0.0-beta.11`
64
+ - `@mongodb-js/compass-export-to-language 4.0.2`
65
+
66
+ ### Browser
67
+
68
+ Setting values via configure:
69
+
70
+ ```js
71
+ import AppRegistry from 'hadron-app-registry';
72
+ import AggregationsPlugin, {
73
+ configureStore as configureAggregationsStore
74
+ } from '@mongodb-js/compass-aggregations';
75
+ import ExportToLanguagePlugin, {
76
+ configureStore as configureExportToLanguageStore
77
+ } from '@mongodb-js/compass-export-to-language';
78
+
79
+ const handleOut = (namespace) => {
80
+ window.open(`https://cloud.mongodb.com/${namespace}`, '_new');
81
+ };
82
+
83
+ const handleCopy = (query) => {
84
+ alert(query);
85
+ };
86
+
87
+ const appRegistry = new AppRegistry();
88
+
89
+ const aggregationsStore = configureAggregationsStore({
90
+ dataProvider: {
91
+ error: null,
92
+ dataProvider: dataProvider
93
+ },
94
+ namespace: 'db.coll',
95
+ serverVersion: '4.2.0',
96
+ fields: [],
97
+ isAtlasDeployed: true,
98
+ outResultsFn: handleOut,
99
+ env: 'atlas',
100
+ localAppRegistry: appRegistry
101
+ });
102
+
103
+ const exportToLanguageStore = configureExportToLanguageStore({
104
+ localAppRegistry: appRegistry,
105
+ copyToClipboardFn: handleCopy
106
+ });
107
+
108
+ <AggregationsPlugin store={aggregationsStore} />
109
+ <ExportToLanguagePlugin store={exportToLanguageStore} />
110
+ ```
111
+
112
+ ### Hadron/Electron
113
+
114
+ ```js
115
+ const role = appRegistry.getRole('Collection.Tab')[0];
116
+ const Plugin = role.component;
117
+ const configureStore = role.configureStore;
118
+ const store = configureStore({
119
+ globalAppRegistry: appRegistry,
120
+ localAppRegistry: localAppRegistry,
121
+ dataProvider: {
122
+ error: null,
123
+ dataProvider: dataProvider
124
+ },
125
+ env: 'on-prem',
126
+ namespace: 'db.coll',
127
+ serverVersion: '4.2.0',
128
+ fields: []
129
+ });
130
+
131
+ <Plugin store={store} />
132
+ ```
133
+
134
+ ### Fields
135
+
136
+ The fields array must be an array of objects that the ACE editor autocompleter understands. See
137
+ [This example](https://github.com/mongodb-js/ace-autocompleter/blob/master/lib/constants/accumulators.js)
138
+ for what that array looks like.
139
+
140
+ ### Data Provider
141
+
142
+ The data provider is an object that must adhere to the following interface:
143
+
144
+ ```js
145
+ /**
146
+ * Get a count of documents.
147
+ *
148
+ * @param {String} namespace - The namespace in "db.collection" format.
149
+ * @param {Object} filter - The MQL query filter.
150
+ * @param {Object} options - The query options.
151
+ * @param {Function} callback - Gets error and integer count as params.
152
+ */
153
+ provider.count(namespace, filter, options, callback);
154
+
155
+ /**
156
+ * Execute an aggregation pipeline.
157
+ *
158
+ * @param {String} namespace - The namespace in "db.collection" format.
159
+ * @param {Array} pipeline - The pipeline.
160
+ * @param {Object} options - The agg options.
161
+ * @param {Function} callback - Gets error and cursor as params. Cursor must
162
+ * implement #toArray (which takes a callback with error and an array of result docs)
163
+ * and #close
164
+ */
165
+ provider.aggregate(namespace, pipeline, options, callback);
166
+ ```
167
+
168
+ ### App Registry Events Emmitted
169
+ Various actions within this plugin will emit events for other parts of the
170
+ application can be listened to via [hadron-app-registry][hadron-app-registry].
171
+ `Local` events are scoped to a `Tab`.
172
+ `Global` events are scoped to the whole Compass application.
173
+
174
+ #### Global
175
+ - **'open-create-view'**: Indicated `Create View` is to be opened.
176
+ - **'compass:export-to-language:opened', source**: Indicates
177
+ `export-to-language` was opened. `source` refers to the module it is opened
178
+ from, in this case `Aggregations`.
179
+ - **'compass:aggregations:pipeline-imported'**: Indicates a pipeline ws
180
+ imported, either from pasting the pipeline in, or from using the import
181
+ functionality. Sends data to metrics.
182
+ - **'compass:aggregations:create-view', numOfStages**: Indicates `Create View` was
183
+ successful. `numOfStages` refers to pipeline length. Sends data to metrics.
184
+ - **'compass:aggregations:pipeline-opened'**: Indicates a saved pipeline was
185
+ opened. Sends pipeline data to metrics.
186
+ - **'open-namespace-in-new-tab'**: Indicates current pipeline's namespace is to
187
+ be opened in a new tab. Called when `Create View` is successful, when
188
+ `$merge` are to be shown, when `$out` results are to be shown.
189
+ - **'compass:aggregations:update-view', numOfStages**: Indicates a pipeline view
190
+ was updated. `numOfStages` refers to the length of the pipeline. Sends data to
191
+ metrics.
192
+ - **'compass:aggregations:settings-applied', settings**: Indicates pipeline
193
+ settings are to be applied. `settings` include: `isExpanded`, `isCommentMode`,
194
+ `isDirty`, `sampleSize`, `maxTimeMS`, `limit`.
195
+ - **'refresh-data'**: Indicates a data refresh is required within Compass.
196
+ - **'select-namespace', metadata**: Indicates a namespace is being selected.
197
+ Emitted when updating a collection. `metadata` refers to information about the
198
+ pipeline.
199
+ - **'agg-pipeline-deleted'**: Indicates a pipeline was deleted. Sends pipeline
200
+ data to metrics.
201
+ - **'agg-pipeline-saved', pipelineName**: Indicates a pipeline was saved
202
+ locally. Sens pipeline data to analytics.
203
+ - **'agg-pipeline-executed', metadata**: Indicates a pipeline was executed.
204
+ `metadata` refers to data about the pipeline. Sends pipeline data to metrics.
205
+ - **'agg-pipeline-out-executed', pipelineId**: Indicates a pipeline was executed
206
+ with a `$out`. Sends pipeline data to metrics.
207
+
208
+ #### Local
209
+ - **'open-aggregation-export-to-language', pipeline**: Indicates
210
+ `export-to-language` plugin is to opened. `pipeline` refers to the pipeline to
211
+ be exported.
212
+ - **'open-create-view', { meta: { source, pipeline }}**: Indicates `Create
213
+ View` is being opened.
214
+
215
+ ### App Registry Events Received
216
+ #### Local
217
+ - **'import-finished'**: When import data was successful, refresh plugin's input
218
+ data.
219
+ - **'fields-changed', fields**: Received when schema fields change. Updates
220
+ plugin's fields.
221
+ - **'refresh-data'**: Received when Compass data was refreshed. Refreshes input
222
+ data in the plugin.
223
+ - **'open-create-view', { meta: { source, pipeline }}**: Received when `Create
224
+ View` is to be opened. Opens a Create View modal.
225
+
226
+ #### Global
227
+ - **'refresh-data'**: Received when Input data is to be refreshed on Compass
228
+ level. Refreshes plugin's input.
229
+
230
+ ### Metrics Events
231
+ - `refresh-data`
232
+ - `open-create-view`
233
+ - `agg-pipeline-saved`
234
+ - `agg-pipeline-deleted`
235
+ - `agg-pipeline-executed`
236
+ - `agg-pipeline-out-executed`
237
+ - `compass:aggregations:update-view`
238
+ - `compass:aggregations:create-view`
239
+ - `compass:aggregations:pipeline-opened`
240
+ - `compass:aggregations:settings-applied`
241
+ - `compass:aggregations:pipeline-imported`
242
+
243
+ ## Development
244
+
245
+ ### Tests
246
+
247
+ ```shell
248
+ npm run test
249
+ ```
250
+
251
+ ### Electron
252
+
253
+ ```shell
254
+ npm start
255
+ ```
256
+
257
+ ### Analyze Build
258
+
259
+ ```shell
260
+ npm run analyze
261
+ ```
262
+
263
+ ## Install
264
+ ```shell
265
+ npm i -S @mongodb-js/compass-aggregations
266
+ ```
267
+
268
+ [hadron-app-registry]: https://github.com/mongodb-js/hadron-app-registry