Skip to content

Commit 38cfee7

Browse files
DOCSP-39185 Require local files in playgrounds (#85)
1 parent 94aa98e commit 38cfee7

File tree

5 files changed

+211
-9
lines changed

5 files changed

+211
-9
lines changed

snooty.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ intersphinx = ["https://www.mongodb.com/docs/manual/objects.inv",
55
"https://www.mongodb.com/docs/atlas/objects.inv"
66
]
77

8-
toc_landing_pages = ["/playgrounds", "/crud-ops", "/playground-databases"]
8+
toc_landing_pages = [
9+
"/playgrounds",
10+
"/crud-ops",
11+
"/playground-databases",
12+
"/require-playgrounds"
13+
]
914

1015
[constants]
1116

source/playgrounds.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,4 @@ Consideration for Authentication
266266
/run-agg-pipelines
267267
/enable-autocomplete-for-string-literals
268268
/export-to-language
269-
/require-modules
269+
/require-playgrounds

source/require-playgrounds.txt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
.. _vsce-require-landing:
2+
3+
============================
4+
Use require() in Playgrounds
5+
============================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
You can use the `require()
14+
<https://nodejs.org/api/modules.html#modules_require_id>`__ function in
15+
your playgrounds to reuse code in your playgrounds. The code you
16+
include with ``require()`` can be either:
17+
18+
- A native Node module (such as `fs
19+
<https://nodejs.org/api/fs.html#fs_file_system>`__)
20+
21+
- An external Node module downloaded from `npm
22+
<https://www.npmjs.com/>`__
23+
24+
- A local JavaScript file
25+
26+
Use Cases
27+
---------
28+
29+
Using ``require()`` to import code helps keep your playgrounds
30+
organized. If you are often performing the same tasks in your
31+
playgrounds, you can use ``require()`` to reuse code and avoid writing
32+
full functions in each of your playgrounds.
33+
34+
For example, consider the following scenarios where reusing code can
35+
simplify logic and improve productivity:
36+
37+
- You need to ensure that documents are inserted with a consistent
38+
structure. You can import a JavaScript file to validate the data
39+
format before the playground script performs the insert.
40+
41+
- You store monetary data and want to convert values between different
42+
currencies. You can import a JavaScript function to perform conversion
43+
calculations.
44+
45+
- You need to import data from files on your local computer. You can use
46+
the native Node module ``fs`` to read data from local files.
47+
48+
Get Started
49+
-----------
50+
51+
To learn how to use ``require()`` in your playgrounds, see the following
52+
tutorials:
53+
54+
- :ref:`vsce-require`
55+
56+
- :ref:`vsce-require-local`
57+
58+
.. toctree::
59+
:titlesonly:
60+
61+
/require-playgrounds/require-modules
62+
/require-playgrounds/require-local
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
.. _vsce-require-local:
2+
3+
=================================
4+
Use require() to Load Local Files
5+
=================================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
You can use the `require()
14+
<https://nodejs.org/api/modules.html#modules_require_id>`__ function in
15+
your MongoDB Playgrounds to include code from local files. You can store
16+
your code in a single location and reuse that code in different
17+
playgrounds.
18+
19+
About this Task
20+
---------------
21+
22+
This tutorial shows how to use ``require()`` to load local scripts. You
23+
can also use ``require()`` to load Node modules, like those downloaded
24+
from `npm <https://www.npmjs.com/>`__. For more information, see
25+
:ref:`vsce-require`.
26+
27+
Steps
28+
-----
29+
30+
.. procedure::
31+
:style: normal
32+
33+
.. step:: Create a script file
34+
35+
The following script file validates documents to ensure that the
36+
required fields are present. Save the script to your local
37+
filesystem as ``validate.js``:
38+
39+
.. code-block:: javascript
40+
41+
// validate.js
42+
43+
const required_fields = [ 'name', 'email' ]
44+
45+
const validate_data = (document) => {
46+
47+
let is_valid = true;
48+
49+
for (const field of required_fields) {
50+
if (document[field] == null) {
51+
is_valid = false;
52+
}
53+
};
54+
return is_valid;
55+
};
56+
57+
module.exports = validate_data;
58+
59+
.. step:: Create a playground that uses the validation script
60+
61+
The following playground uses ``require()`` to call the
62+
``validate_data`` function specified in ``validate.js``. The
63+
``validate_data`` function is called on two sample documents. If
64+
the document contains the required fields ``name`` and ``email``,
65+
it is inserted into the ``people`` collection.
66+
67+
.. important::
68+
69+
Update the first line of the playground with the path to the
70+
``validate.js`` file:
71+
72+
.. code-block:: javascript
73+
:emphasize-lines: 3
74+
75+
// playground-1.mongodb.js
76+
77+
const validate = require('/path/to/validate.js');
78+
79+
use('mongodbVSCodePlaygroundDB');
80+
81+
const doc1 = { _id: 1, 'name': 'Taylor', 'email': '[email protected]' };
82+
83+
const doc2 = { _id: 2, 'name': 'Taylor' };
84+
85+
const docs = [ doc1, doc2 ];
86+
87+
let inserted_count = 0;
88+
89+
for (const doc of docs) {
90+
if (validate(doc)) {
91+
db.getCollection('people').insertOne(doc);
92+
inserted_count++;
93+
}
94+
};
95+
96+
console.log("Inserted " + inserted_count + " documents");
97+
98+
.. step:: Run the playground
99+
100+
.. include:: /includes/run-playground.rst
101+
102+
Results
103+
-------
104+
105+
Only ``doc1`` contains both required fields and is inserted into the
106+
collection. ``doc2`` does not contain the required field ``email``, and
107+
is not inserted.
108+
109+
To confirm that the correct document was inserted, query the ``people``
110+
collection:
111+
112+
.. code-block:: javascript
113+
114+
use mongodbVSCodePlaygroundDB
115+
116+
db.people.find()
117+
118+
Output:
119+
120+
.. code-block:: javascript
121+
:copyable: false
122+
123+
[
124+
{ _id: 1, name: 'Taylor', email: '[email protected]' }
125+
]
126+
127+
Learn More
128+
----------
129+
130+
- :ref:`schema-validation-overview`
131+
132+
- :ref:`vsce-require`
133+
134+
- :ref:`vsce-playground-databases`

source/require-modules.txt renamed to source/require-playgrounds/require-modules.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.. _vsce-require:
22

3-
=============================================
4-
Use ``require()`` to Include External Modules
5-
=============================================
3+
========================================
4+
Use require() to Include Node.js Modules
5+
========================================
66

77
.. default-domain:: mongodb
88

@@ -19,10 +19,11 @@ Use ``require()`` to Include External Modules
1919
function is out of scope for this tutorial. To learn more, refer to
2020
the `Node.js Documentation <https://nodejs.org/api/modules.html>`__.
2121

22-
You can use the
23-
`require() <https://nodejs.org/api/modules.html#modules_require_id>`__
24-
function in your MongoDB Playgrounds to
25-
include modules which exist in separate files.
22+
You can use the `require()
23+
<https://nodejs.org/api/modules.html#modules_require_id>`__ function in
24+
your MongoDB Playgrounds to include functionality from Node.js modules.
25+
You can use modules to import reusable code to simplify your
26+
playgrounds.
2627

2728
Require Native Modules
2829
----------------------

0 commit comments

Comments
 (0)