|
| 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` |
0 commit comments