In this article we will show you how to convert classic Northwind Database to NoSQL world i.e. Restdb.io. we chosen the most popular database from Microsoft i.e. Northwind as an example to show the migration from a traditional relational database to NoSQL cloud enabled database.
The Northwind database contains the sales data from a company called Northwind Traders, which involves in import/export of foods across the globe.
In this example mentioned will be explained:
- Import the original SQL data files
- Map Relations between tables (Collections)
- Use the REST API to the new Northwind NoSQL DB
Original Data Model of Northwind
The Northwind relational data schema is reflected in the picture below, if you observe there are Thirteen tables having Thirteen relations, our goal for this article is to re-create the same schema in a NoSQL document schema:
Upload the Raw Data
Luckily, a GitHub project we found, we save time and get a nice set of CSV files from a data dump from the Northwind database.
Importing the data is easy. Just click the Import button on the restdb.io database dashboard.
The screen shot below shows the import of orders and the mapping of the data fields:
After we’ve imported all data files, we can see the newly created collections and the count of their data records. All in all, it counts 6,635 records (or Documents, as we say in NoSQL speak).
When we navigate to the Orders collection, we can see that the data has been imported correctly. We can also see the CustomerID column that refers to a column in the Customer collection.
The next step is to start the mapping of the relations.
Mapping of Relations Between Collections
In order to create relations, we must change the schema of our database. First, we switch to Developer mode (button in the upper right corner) and then navigate to Orders > Settings.
The screen shot below shows all the fields on the Orders collection. We then select the CustomerID field, and change it from a textdata type to a Lookup to Customers. Note that we also need to select the advanced option and then pick the foreign key field from the Customers collection (CustomerID).
The mapping process described here is repeated for all the relations we wish to map.
For example, to create a relation between Orders and Order-details, we navigate to the Order-detail collection and change the OrderIDfield to a Lookupthat refers to Orders with the foreign key OrderID.
The screenshot below shows the Order-detail view with the finished mappings against both Orders and Customers.
As we now have a data schema that “knows” about the relations between our collections, restdb.io provides us with some really useful productivity features.
Navigate to any Product shows all Order-details for that particular product.
And if you click on the Order-Details tab, you will see all associated records. The same “automagic” navigation tools are provided for all the relations in our data model.
The Rest API
What does the Northwind data look like in our NoSQL database?
Let’s run a cURL command line query to get a particular Order.
curl -k -i -H “Content-Type: application/json”\-H “x-apikey: <some key>”\-X GET ‘https://northwindnosql-39ba.restdb.io/rest/orders/594a3c65f8b84a36000016be’
We get back the following JSON document.
We see that the fields CustomerID and EmployeeID which used to be just text, has been replaced with the actual documents (records) they refer.
This differs from SQL databases, where we would have to join the three collections to produce a similar result.
In the NoSQL world, we embed and duplicate — and that’s okay.