Created
June 10, 2019 22:07
-
-
Save kjimenezdev/dd1ac1dd3588fa64d6b663d0932116fa to your computer and use it in GitHub Desktop.
Convert JSON to mongoose models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import json | |
import sys, getopt | |
def parse_dictionary(outfile, iter, key, intern_dict): | |
for i in range(iter): | |
outfile.write("\t") | |
key2write = '' | |
if key != '': | |
key2write = key + ':' | |
outfile.write(key2write + '{\n') | |
for i_index, i_key in enumerate(intern_dict): | |
key_type = type(intern_dict[i_key]) | |
if key_type is dict: | |
newiter = iter + 1 | |
parse_dictionary(outfile, newiter, i_key, intern_dict[i_key]) | |
if i_index != len(intern_dict) - 1: | |
outfile.write(',\n') | |
elif key_type is list: | |
newiter = iter + 1 | |
parse_list(outfile, newiter, i_key, intern_dict[i_key]) | |
else: | |
for i in range(iter + 1): | |
outfile.write("\t") | |
outfile.write(i_key + ': ' + intern_dict[i_key]) | |
if i_index != len(intern_dict) - 1: | |
outfile.write(',\n') | |
else: | |
outfile.write('\n') | |
for i in range(iter): | |
outfile.write("\t") | |
outfile.write('}') | |
def parse_list(outfile, iter, key, intern_list): | |
for i in range(iter): | |
outfile.write("\t") | |
outfile.write(key + ': [\n') | |
for x in range(len(intern_list)): | |
objx = intern_list[x] | |
key_type = type(objx) | |
if key_type is dict: | |
newiter = iter+1 | |
parse_dictionary(outfile, newiter, '', objx) | |
outfile.write('\n') | |
for i in range(iter): | |
outfile.write("\t") | |
outfile.write('],\n') | |
def main(argv): | |
input_file = '' | |
try: | |
opts, args = getopt.getopt(argv, "hi:o:", ["ifile="]) | |
except getopt.GetoptError: | |
print 'test.py -i <inputfile>' | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-h': | |
print 'test.py -i <inputfile>' | |
sys.exit() | |
elif opt in ("-i", "--ifile"): | |
input_file = arg | |
point_pos = input_file.find('.') | |
file_name = input_file[0:0 + point_pos] | |
lower_case_name = file_name | |
outfile_name = file_name + '.js' | |
outfile = open(outfile_name, "w") | |
schema_title = file_name.title().replace('_', '') | |
data_file = open(input_file) | |
data = json.load(data_file) | |
outfile.write("var mongoose = require('mongoose');\n") | |
outfile.write("var Schema = mongoose.Schema;\n\n") | |
outfile.write("var " + schema_title + "Schema = new Schema({\n") | |
for index, key in enumerate(data): | |
key_type = type(data[key]) | |
if key_type is dict: | |
intern_dict = data[key] | |
parse_dictionary(outfile, 1, key, intern_dict) | |
if index != len(data) - 1: | |
outfile.write(',\n') | |
else: | |
outfile.write('\n') | |
elif key_type is list: | |
intern_list = data[key] | |
parse_list(outfile, 1, key, intern_list) | |
else: | |
outfile.write("\t" + key + ': ' + data[key]) | |
if index != len(data) - 1: | |
outfile.write(',\n') | |
else: | |
outfile.write('\n') | |
outfile.write(" }, { collection: '" + lower_case_name + "' });\n\n") | |
outfile.write(schema_title + "Schema.pre('save', function(next) {\n") | |
outfile.write("\tthis.increment();\n") | |
outfile.write("\treturn next();\n") | |
outfile.write("});\n\n") | |
outfile.write("module.exports = mongoose.model('" + schema_title + "Schema', " + schema_title + "Schema);\n") | |
outfile.close() | |
print "Result saved on file named " + outfile_name | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment