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.
Steps
- Restore the
northwind
PostgreSQL database using therestore.sh
script into your postgres host of choice. The instructions assume localhost running on the default port of 5432 with a user named "demo". - 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
- Sign up for Atlas and create an Atlas Project named
Liberate Data
with an Atlas cluster namedproduction
. - Create a user named
demo
withreadWriteAnyDatabase
permissions. - Add
0.0.0.0
to your Atlas IP Access List. NOTE: This is not recommneded for production or professional work environments.
- Install MongoDB Relational Migrator.
- Import the project liberate-data.relmig.
- Inspect the Relational and MDB diagrams. Notice how the
Orders
collection uses the Subset schema design pattern to store most frequently accessed data together. - The destination Orders collection shoud look like this:
- Perform the data migration by entering your Postgres and Atlas credentials.
- 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.
- Create a default search index with dynamic mappings on the
orders
andcategories
collections. See search-indexes.json for their definition.
- Import the Atlas Application production-app into the Atlas project.
- Linked Data Sources: Inspect that the
production
cluster is linked as the data source. - Rules: The
orders
collection should have thereadAndWriteAll
role. All other collections should have thereadAll
role. - 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"
],
...
}
- Authentication: Two authentication providers should be enabled:
email/password
andAPI Keys
. An API key nameddemo
should be (re)created by you. - Device Sync: Flexible device sync should be enabled, set to the linked atlas cluster and the northwind database.
- GraphQL: All entity types should be defined along with a custom resolver named
searchOrders
, which itself is linked to an Atlas Function namedfuncSearchOrders
.
- Install Postman and import the liberate-data - GraphQL postman collection.
- In the collection variables, enter the
api_key
andatlas_app_id
values. Obtain the GraphQL API endpoint from the GraphQL section in Atlas App Services and set in thegraphql_api
variable. - Execute the 1st POST operation
Auth: Get Bearer & Access Token
to authenticate and obtain tokens. - Execute all other operations in any order. Feel free to change query values.
- The
Search: Orders by search string
operation uses a custom resolver which in turn executes an Atlas Search pipeline. This pipeline is implemented in thefuncSearchOrders
function and performs a fuzzy text search on theorders
collection, plus a union ($unionWith
) and join ($lookup
) to thecategories
collection, thus performing a text search on orders and categories.
- Install XCode with the Swift SDK, and open the swift app under the app-swift folder.
- Open the Realm object and replace the
appId
andappUrl
. Compile and run. - In the mobile app, register with a new user via an email and password.
- Browse orders. For the purpose of this demo, all users have access to all orders.
- Modify an order using the mobile app.
- 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.
- 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.