Skip to content

Commit 01eea4b

Browse files
authored
Merge pull request #95 from ianf-mongodb/DOCSP-37055
DOCSP-37055 Query Converter Triggers
2 parents 6e7769c + b24e6a9 commit 01eea4b

File tree

4 files changed

+209
-6
lines changed

4 files changed

+209
-6
lines changed

source/code-generation/query-converter.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ your SQL code.
2323
/code-generation/query-converter/enable-query-converter
2424
/code-generation/query-converter/convert-queries
2525
/code-generation/query-converter/convert-stored-procedures
26+
/code-generation/query-converter/convert-triggers
2627
/code-generation/query-converter/convert-views
2728
/code-generation/query-converter/refresh-database-objects
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
.. _rm-convert-triggers:
2+
3+
================
4+
Convert Triggers
5+
================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
You can import and convert your SQL triggers to
14+
:ref:`MongoDB Atlas Triggers <atlas-triggers>` with the query converter.
15+
The query converter considers the SQL code and relational schema defined
16+
in your project when converting your triggers.
17+
18+
About this Task
19+
---------------
20+
21+
.. include:: /includes/fact-query-converter-generic.rst
22+
23+
Before you Begin
24+
----------------
25+
26+
- Your relational database must have at least one trigger to convert.
27+
28+
- .. include:: /includes/fact-query-converter-disclaimer.rst
29+
30+
Steps
31+
-----
32+
33+
.. procedure::
34+
:style: connected
35+
36+
.. step:: Navigate to the query converter pane
37+
38+
From the :guilabel:`Code Generation` tab, click the
39+
:guilabel:`Query Converter` pane.
40+
41+
If it is your first time accessing Query Converter or your session
42+
has expired, click :guilabel:`Log In To Use Query Converter`
43+
and provide your Atlas credentials.
44+
45+
.. step:: Open the query converter view
46+
47+
- If it is your first time using the query converter in your
48+
project, click :guilabel:`Import From Database`.
49+
50+
- If your project already has converted SQL code, click the
51+
:guilabel:`Manage Database Objects` button on the left
52+
pane.
53+
54+
.. step:: Select triggers
55+
56+
a. On the :guilabel:`Import Database Objects` modal, click
57+
the :icon-fa5:`chevron-right` icon next to :guilabel:`Database`.
58+
59+
#. Click the :icon-fa5:`chevron-right` icon next to your schema.
60+
61+
#. Click the :icon-fa5:`chevron-right` icon next to
62+
:guilabel:`Triggers`.
63+
64+
You can toggle triggers for conversion by clicking
65+
the :icon-fa5:`check-square` icon next to a trigger's
66+
name. All triggers are selected by default.
67+
68+
#. Click :guilabel:`Save`.
69+
70+
The code for each trigger in your database
71+
schema is imported into your project and is visible in the
72+
left :guilabel:`Query Converter` pane under
73+
:guilabel:`Triggers`.
74+
75+
.. step:: Convert the SQL trigger
76+
77+
a. Click a trigger's name from the left pane under
78+
:guilabel:`Triggers`.
79+
80+
.. tip::
81+
82+
.. include:: /includes/fact-query-converter-filter.rst
83+
84+
The SQL trigger code displays in the
85+
:guilabel:`Imported Trigger` pane.
86+
87+
#. Click the :guilabel:`Convert` button. Wait for the query
88+
converter to convert your code.
89+
90+
The converted MongoDB code displays
91+
on the :guilabel:`Converted MongoDB Query` pane.
92+
93+
#. Click the :icon-fa5:`copy` icon on the
94+
:guilabel:`Converted MongoDB Query` pane to copy the MongoDB
95+
code to your clipboard.
96+
97+
.. step:: Create the Trigger in Atlas
98+
99+
a. Login to your `Atlas account <https://cloud.mongodb.com/v2/>`__.
100+
101+
#. From the :guilabel:`Overview` screen, click :guilabel:`Triggers`.
102+
103+
#. Click :guilabel:`Add Trigger`.
104+
105+
.. tip::
106+
107+
The converted MongoDB code contains commented lines for
108+
all the variables you must select in Atlas to create
109+
your trigger. For example:
110+
111+
.. code-block:: javascript
112+
:copyable: false
113+
114+
// Collection Name: products
115+
// Operation Type: Insert
116+
117+
#. Enter a name for the trigger in the :guilabel:`Name` text
118+
field.
119+
120+
#. Select the :guilabel:`Cluster Name`, the
121+
:guilabel:`Database Name` and the
122+
:guilabel:`Collection Name`.
123+
124+
#. Select the :guilabel:`Operation Type` as
125+
:guilabel:`Insert Document`.
126+
127+
#. Toggle the :guilabel:`Document Preimage` and
128+
:guilabel:`Full Document` switches to on.
129+
130+
#. Enter converted MongoDB code in the :guilabel:`Function`
131+
text field.
132+
133+
.. important::
134+
135+
Update the ``clusterName`` and ``databaseName`` in
136+
the generated Atlas code to match your cluster and
137+
database name.
138+
139+
#. Click :guilabel:`Save` to save the Atlas trigger.
140+
141+
Example
142+
-------
143+
144+
Convert a MySQL Trigger
145+
~~~~~~~~~~~~~~~~~~~~~~~
146+
147+
The following example shows a MySQL trigger converter to Atlas:
148+
149+
.. tabs::
150+
151+
.. tab:: Relational Input
152+
:tabid: sql-input-1
153+
154+
.. code-block:: sql
155+
:copyable: false
156+
157+
CREATE TRIGGER TRIGGER_UPPER_PRODUCTS
158+
BEFORE INSERT
159+
ON MYDATABASE.PRODUCTS
160+
FOR EACH ROW
161+
SET NEW.FULL_NAME = UPPER(new.FULL_NAME)
162+
163+
.. tab:: MongoDB Output
164+
:tabid: mongodb-output-1
165+
166+
.. code-block:: javascript
167+
:emphasize-lines: 14-15
168+
:copyable: false
169+
170+
// The relational database trigger has been converted to MongoDB Atlas Triggers format.
171+
// To create a trigger, open your Atlas project (https://cloud.mongodb.com) and choose Triggers
172+
// For more on Atlas triggers see the docs: https://www.mongodb.com/docs/atlas/triggers/
173+
// Create your trigger using the following settings and paste the code into the Function section:
174+
// Watch Against: Collection
175+
// Cluster Name: Ensure clusterName matches selection in Atlas Trigger configuration
176+
// Database Name: Ensure databaseName matches selection in Atlas Trigger configuration
177+
// Collection Name: products
178+
// Operation Type: Insert
179+
// Full Document: On
180+
// Document Preimage: Off
181+
182+
exports = async function(changeEvent) {
183+
const clusterName = "clusterName";
184+
const databaseName = "databaseName";
185+
186+
const { fullDocument } = changeEvent;
187+
const db = context.services.get(clusterName).db(databaseName);
188+
const collection = db.collection('products');
189+
190+
if (fullDocument && fullDocument.fullName) {
191+
fullDocument.fullName = fullDocument.fullName.toUpperCase();
192+
await collection.updateOne({ _id: fullDocument._id }, { $set: { fullName: fullDocument.fullName } });
193+
}
194+
};
195+
196+
Learn More
197+
----------
198+
199+
- :ref:`rm-convert-views`
200+
- :ref:`rm-convert-queries`
201+
- :ref:`rm-convert-stored-procedures`
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
You can use the :guilabel:`Filter` text box to filter queries,
2-
stored procedures, and views based on object name and SQL syntax.
2+
stored procedures, triggers, and views based on object name and SQL
3+
syntax.
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
- The query converter uses :abbr:`AI (Artificial Intelligence)`
2-
technology which may not be able to convert long or complex queries
3-
or stored procedures. Some queries may not be converted correctly
4-
while others may not be converted at all.
2+
technology which may not be able to convert long or complex queries,
3+
triggers, or stored procedures. Some queries may not be converted
4+
correctly while others may not be converted at all.
55

66
- The query converter uses the relational schema, the MongoDB schema,
77
and the mapping rules in your current project to determine how the
88
queries should be converted. Conversions may fail or be incorrect if
99
the queries reference tables that are not in your relational schema
1010
or if they are not mapped to MongoDB collections.
1111

12-
- Converted queries, views, and stored procedures are saved in your
13-
project and persist through project import and exports.
12+
- Converted queries, triggers, views, and stored procedures are saved in
13+
your project and persist through project import and exports.
1414

1515
- SQL queries are limited to 10,000 text characters.

0 commit comments

Comments
 (0)