In this demonstration, I'd like to show a very efficient way of generating Mongo schemas from sequel databases. One of the frustrations of MongoDB is that the documents are, do not really have a data definition by default. That's, that's the definition and that's the way MongoDB and been designed to accept documents at.

In Node. js a lot of hacks have been created using simple schema and other methods totypecast every document.

Typing those documents is pretty laborious, and to a fair bit there is a
dearth generating database documents based on user interfaces.

So as a hack I've discovered this method to create MongoDB documents and simple schema documents or definitions through, through using sqlite as an interface. So here to demonstrate that

In this particular demonstration I've got Navicat Premium open which allows me to connect to sqlite databases, I'm going to use the Ghost development sqlite database, as a sample.

I'll be using the post table to generate the schema that I want.

So it is very simple I click on the table in Navicat. Go to design. Next go to SQL preview. And I converted to "save as" to be able to get definitions on the
UI.

I simply copy over the script to my generator. The generator that accepts DDL, this is the data Definition Language that generated by Navicat, and once I save it.

I simply go to my node script node file and run it. Very straightforward!

Once I get that I'm able to generate the scheme I want or the definitions as shown below.

I can simply put in definitions of very very quickly to my MongoDB is in the schema, make it very quickly, and I'm able to improve my productivity for creating more collections.

Very straightforward and really useful.

Files

  • Video demo: technical-blog-efficiently-creating-simple-schema-definitions-for-mongodb-from-sqlite.mov
  • Script to run: nodejs/my-generate-schema/sql-ddl-to-json-schema-example.js

Bonus

To generate gif from mov credits:

# Install dependencies

brew install ffmpeg imagemagick gifsicle

bash

# input
mov="technical-blog-efficiently-creating-simple-schema-definitions-for-mongodb-from-sqlite.mov"

# output
gif_file="technical-blog-efficiently-creating-simple-schema-definitions-for-mongodb-from-sqlite.gif"

#!/bin/bash

# Convert a .MOV to an animated .GIF using FFMPEG and Gifsicle.
#
# Dependencies:
# ffmpeg
# gifsicle
#
# Example:
#
# $ ./mov2gif.sh -i screencast.mov -o output.gif
#

inputFile="technical-blog-efficiently-creating-simple-schema-definitions-for-mongodb-from-sqlite.mov" # input filename

outputFile="technical-blog-efficiently-creating-simple-schema-definitions-for-mongodb-from-sqlite.gif" # output filename

frameRate=10 # frames per second
optimize=3 # file size optimization. 3 being the highest
delay=100 # time between each gif in milliseconds. 3 = 30ms

while getopts "i:o:f:" opt; do
  case $opt in
  i)  inputFile="$OPTARG";;
  o)  outputFile="$OPTARG";;
  f)  frameRate="$OPTARG";;
  op) optimize="$OPTARG";;
  d)  delay="$OPTARG";;
  esac
done

shift $((OPTIND - 1))

printf "Input: $inputFile\n"
printf "Output: $outputFile\n\n"

CMD="ffmpeg -i $inputFile -pix_fmt rgb24 -r $frameRate -f gif - | gifsicle --optimize=$optimize --delay=$delay > $outputFile"

printf "Executing command:\n"
printf "$CMD\n\n"

eval $CMD