Skip to content

This repository demonstrates how data trapped in an RDBMS can be liberated by migrating to MongoDB Atlas, powering with search, graphql and mobile clients. This demo was created for AWS re:Invent 2022 and presented at the MongoDB booth area at the Venetian expo hall.

Notifications You must be signed in to change notification settings

ClusterDB/liberate-data

 
 

Repository files navigation

Liberate your data: From RDBMS to Search, GraphQL & Mobile ... in minutes!

Reduce the time it takes to modernize your applications by freeing the data trapped in your relational database and migrating to the next-gen fully transactional DB of MongoDB Atlas. Powering it with advanced lucene-based search, enable API consumption via fully-managed GraphQL, and expose to mobile and edge consumers via the Realm mobile DB and SDK’s.

Demo Architecture

Steps

PostgreSQL

  1. Restore the northwind PostgreSQL database using the restore.sh script into your postgres host of choice. The instructions assume localhost running on the default port of 5432 with a user named "demo".
  2. Verify that the following tables with all data are correctly imported by running this script:
WITH tbl AS
  (SELECT table_schema,
          TABLE_NAME
   FROM information_schema.tables
   WHERE TABLE_NAME not like 'pg_%'
     AND table_schema in ('northwind'))
SELECT table_schema,
       TABLE_NAME,
       (xpath('/row/c/text()', query_to_xml(format('select count(*) as c from %I.%I', table_schema, TABLE_NAME), FALSE, TRUE, '')))[1]::text::int AS rows_n
FROM tbl
ORDER BY rows_n DESC;

output
-----------------------------------------
"table_schema","table_name","rows_n"
"northwind","order_details",2155
"northwind","orders",830
"northwind","customers",91
"northwind","products",77
"northwind","territories",53
"northwind","us_states",51
"northwind","employee_territories",49
"northwind","suppliers",29
"northwind","employees",9
"northwind","categories",8
"northwind","shippers",6
"northwind","region",4
"northwind","customer_customer_demo",0
"northwind","customer_demographics",0

MongoDB Atlas

  1. Sign up for Atlas and create an Atlas Project named Liberate Data with an Atlas cluster named production.
  2. Create a user named demo with readWriteAnyDatabase permissions.
  3. Add 0.0.0.0 to your Atlas IP Access List. NOTE: This is not recommneded for production or professional work environments.

MongoDB Relational Migrator

  1. Install MongoDB Relational Migrator.
  2. Import the project liberate-data.relmig.
  3. Inspect the Relational and MDB diagrams. Notice how the Orders collection uses the Subset schema design pattern to store most frequently accessed data together.
  4. The destination Orders collection shoud look like this: Orders collection mapping
  5. Perform the data migration by entering your Postgres and Atlas credentials.
  6. When done, navigate to Atlas and ensure all collections were migrated. Inspect the orders collection. A subset of the data from orderDetails, product, customer & employee should be nested.

MongoDB Atlas Search

  1. Create a default search index with dynamic mappings on the orders and categories collections. See search-indexes.json for their definition.

MongoDB Atlas App Services

  1. Import the Atlas Application production-app into the Atlas project.
  2. Linked Data Sources: Inspect that the production cluster is linked as the data source.
  3. Rules: The orders collection should have the readAndWriteAll role. All other collections should have the readAll role.
  4. Schema: Ensure the schema for all collections is defined. The schema for the orders collection should define required fields as below in addition to their bson types:
{
  "title": "order",
  "required": [
    "_id",
    "customerId",
    "employeeId",
    "freight",
    "orderDate",
    "shipAddress",
    "shipCity",
    "shipCountry",
    "shipName",
    "shipPostalCode",
    "shipRegion",
    "shipVia",
    "shippedDate"
  ],
  ...
}
  1. Authentication: Two authentication providers should be enabled: email/password and API Keys. An API key named demo should be (re)created by you.
  2. Device Sync: Flexible device sync should be enabled, set to the linked atlas cluster and the northwind database.
  3. GraphQL: All entity types should be defined along with a custom resolver named searchOrders, which itself is linked to an Atlas Function named funcSearchOrders.

Postman

  1. Install Postman and import the liberate-data - GraphQL postman collection.
  2. In the collection variables, enter the api_key and atlas_app_id values. Obtain the GraphQL API endpoint from the GraphQL section in Atlas App Services and set in the graphql_api variable. Postman variable
  3. Execute the 1st POST operation Auth: Get Bearer & Access Token to authenticate and obtain tokens.
  4. Execute all other operations in any order. Feel free to change query values.
  5. The Search: Orders by search string operation uses a custom resolver which in turn executes an Atlas Search pipeline. This pipeline is implemented in the funcSearchOrders function and performs a fuzzy text search on the orders collection, plus a union ($unionWith) and join ($lookup) to the categories collection, thus performing a text search on orders and categories.

Swift mobile app with Realm SDK

  1. Install XCode with the Swift SDK, and open the swift app under the app-swift folder.
  2. Open the Realm object and replace the appId and appUrl. Compile and run. Swift Realm config
  3. In the mobile app, register with a new user via an email and password.
  4. Browse orders. For the purpose of this demo, all users have access to all orders.

Atlas Device Sync

  1. Modify an order using the mobile app.
  2. Open the same Order document in Atlas or Compass and notice the changes. Now modify the same order and the changes will be reflected on the mobile app. Atlas Device Sync works.
  3. Finally, run the Mutation: Change a Sales Order GraphQL operation in postman. Change the Order ID and any fields in the order body. The changes should reflect in the mobile app.

About

This repository demonstrates how data trapped in an RDBMS can be liberated by migrating to MongoDB Atlas, powering with search, graphql and mobile clients. This demo was created for AWS re:Invent 2022 and presented at the MongoDB booth area at the Venetian expo hall.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 78.0%
  • JavaScript 20.7%
  • Shell 1.3%