Skip to content

Commit

Permalink
update v1.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
vovikhangcdv committed Oct 6, 2022
1 parent 933a8df commit 8c5d513
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 119 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nosqldbm-converter",
"version": "1.0.4",
"version": "1.0.5",
"description": "Convert https://nosqldbm.ru/ JSON data to Moongose Schema.",
"main": "index.js",
"directories": {
Expand Down
8 changes: 4 additions & 4 deletions source/convert.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const convertElement = require("./convertElement.js");
const convertCollection = require("./convertCollection.js");

module.exports = function convert(data) {
if (Array.isArray(data)) {
var arraySchemas = [];
data.forEach(function (element) {
arraySchemas.push(convertElement(element));
data.forEach(function (Collection) {
arraySchemas.push(convertCollection(Collection));
})
return arraySchemas;
}
if (typeof (data) === "object") {
return convertElement(data);
return convertCollection(data);
}
}
17 changes: 17 additions & 0 deletions source/convertCollection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const convertType = require('./convertType.js');

module.exports = function convertElement(obj) {
var collection = {};
if (obj["fields"]) {
obj["fields"].forEach(function (field) {
if (field["name"] == "_id") return;
collection[field["name"]] = {
"type": convertType(field["type"]),
"required": Boolean(field["required"]),
"unique": Boolean(field["unique"]),
"default" : field["defaultValue"],
}
})
}
return { [obj["name"]]: collection };
}
37 changes: 0 additions & 37 deletions source/convertElement.js

This file was deleted.

42 changes: 26 additions & 16 deletions source/convertType.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
module.exports = function convertType(type) {
const arrayRegex = /Array<(.+)>/;
const enumRegex = /Enum{(.+)}/;
if (type.match(arrayRegex)) {
return [[convertType(type.match(arrayRegex)[1])[0]], null];
module.exports = function convertType(type){
if (type.match(/\[(.+)\]/)){
return [convertType(type.match(/\[(.+)\]/)[1])];
}
if (type.match(enumRegex)) {
return ["String", type.match(enumRegex)[1].split(",")];
switch (type) {
case "String":
return "String";
case "Integer":
return "Number";
case "Date":
return "Date";
case "Boolean":
return "Boolean";
case "ObjectId":
return "ObjectId";
case "Array":
return [];
case "[]":
return [];
case "Object":
return "Object";
case "Double":
return "Double";
case "Mixed":
return "Mixed";
default:
return type;
}
if (type === "Integer") {
return ["Number", null];
}

if (type === "Array") {
return [[], null];
}

return [type, null];
}
122 changes: 61 additions & 61 deletions test/nosqldbm-converter.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,56 @@
const assert = require('assert')
const convert = require('../source/convert.js');
const convertType = require('../source/convertType.js');
const convertElement = require('../source/convertElement.js');
const convertCollection = require('../source/convertCollection.js');


describe("Convert https://nosqldbm.ru/ JSON data to Moongose Schema.", function () {
describe("ConvertType", function () {
it("should return [type, null] if type is not Array or Enum", function () {
assert.deepEqual(convertType("String"), ["String", null]);
describe("convertType", function () {
it("should return String", function () {
assert.deepEqual(convertType("String"), "String");
});
it("should return [[type], null] if type is Array", function () {
assert.deepEqual(convertType("Array<String>"), [["String"], null]);
it("should return Number", function () {
assert.deepEqual(convertType("Integer"), "Number");
});
it("should return [type, enumData] if type is Enum", function () {
assert.deepEqual(convertType("Enum{a,b,c}"), ["String", ["a", "b", "c"]]);
it("should return Date", function () {
assert.deepEqual(convertType("Date"), "Date");
});
it("should return Boolean", function () {
assert.deepEqual(convertType("Boolean"), "Boolean");
});
it("should return ObjectId", function () {
assert.deepEqual(convertType("ObjectId"), "ObjectId");
});
it("should return Array", function () {
assert.deepEqual(convertType("Array"), []);
});
it("should return []", function () {
assert.deepEqual(convertType("[]"), []);
});
it("should return Object", function () {
assert.deepEqual(convertType("Object"), "Object");
});
it("should return Double", function () {
assert.deepEqual(convertType("Double"), "Double");
});
it("should return Mixed", function () {
assert.deepEqual(convertType("Mixed"), "Mixed");
});
it("should return Array of type", function () {
assert.deepEqual(convertType("[String]"), ["String"]);
});
it("should return Array of array", function () {
assert.deepEqual(convertType("[[]]"), [[]]);
});
it("should return Array of array string", function () {
assert.deepEqual(convertType("[[String]]"), [["String"]]);
});
it("should return whatever type", function () {
assert.deepEqual(convertType("Whatever"), "Whatever");
});
});

describe("ConvertElement", function () {
describe("convertCollection", function () {
it("should ignore _id field", function () {
const input = {
"schemaType": "Collection",
Expand All @@ -43,7 +76,7 @@ describe("Convert https://nosqldbm.ru/ JSON data to Moongose Schema.", function
},
]
}
assert.deepEqual(convertElement(input), { User: {} });
assert.deepEqual(convertCollection(input), { User: {} });
});

it("should convert correct type, required and unique value", function () {
Expand Down Expand Up @@ -84,11 +117,11 @@ describe("Convert https://nosqldbm.ru/ JSON data to Moongose Schema.", function
}
const expect = {
User: {
name: { type: 'String', required: true, unique: false },
email: { type: 'String', required: true, unique: true }
name: { type: 'String', required: true, unique: false, default: "" },
email: { type: 'String', required: true, unique: true, default: "" }
}
}
assert.deepEqual(convertElement(input), expect);
assert.deepEqual(convertCollection(input), expect);
});

it("should convert correct defaultValue", function () {
Expand Down Expand Up @@ -121,40 +154,7 @@ describe("Convert https://nosqldbm.ru/ JSON data to Moongose Schema.", function
name: { type: 'String', required: true, unique: false, default: 'John' },
}
}
assert.deepEqual(convertElement(input), expect);
});

it("should convert correct enum value", function () {
const input = {
"schemaType": "Collection",
"name": "User",
"type": "ObjectId",
"required": true,
"unique": true,
"defaultValue": "",
"description": "",
"index": 0,
"customProps": [],
"fields": [
{
"schemaType": "Field",
"name": "type",
"type": "Enum{Public,Private}",
"required": true,
"unique": true,
"defaultValue": "",
"description": "",
"index": 0,
"customProps": []
}
]
}
const expect = {
User: {
type: { type: 'String', required: true, unique: true, enum: ['Public', 'Private'] }
}
}
assert.deepEqual(convertElement(input), expect);
assert.deepEqual(convertCollection(input), expect);
});

it("should convert correct array value", function () {
Expand All @@ -172,7 +172,7 @@ describe("Convert https://nosqldbm.ru/ JSON data to Moongose Schema.", function
{
"schemaType": "Field",
"name": "type",
"type": "Array<String>",
"type": "[String]",
"required": true,
"unique": true,
"defaultValue": "",
Expand All @@ -183,7 +183,7 @@ describe("Convert https://nosqldbm.ru/ JSON data to Moongose Schema.", function
{
"schemaType": "Field",
"name": "type2",
"type": "Array<Array>",
"type": "[[]]",
"required": true,
"unique": true,
"defaultValue": "",
Expand All @@ -195,16 +195,16 @@ describe("Convert https://nosqldbm.ru/ JSON data to Moongose Schema.", function
}
const expect = {
Task: {
type: { type: ['String'], required: true, unique: true },
type2: { type: [[]], required: true, unique: true }
type: { type: ['String'], required: true, unique: true, default: "" },
type2: { type: [[]], required: true, unique: true, default: "" }
}
}
assert.deepEqual(convertElement(input), expect);
assert.deepEqual(convertCollection(input), expect);
}
);
});

describe("Convert", function () {
describe("convert", function () {
it("should convert correct seperated object", function () {
const input = {
"schemaType": "Collection",
Expand Down Expand Up @@ -243,8 +243,8 @@ describe("Convert https://nosqldbm.ru/ JSON data to Moongose Schema.", function
}
const expect = {
User: {
name: { type: 'String', required: true, unique: false },
email: { type: 'String', required: true, unique: true }
name: { type: 'String', required: true, unique: false, default: "" },
email: { type: 'String', required: true, unique: true, default: "" }
}
}
assert.deepEqual(convert(input), expect);
Expand Down Expand Up @@ -301,7 +301,7 @@ describe("Convert https://nosqldbm.ru/ JSON data to Moongose Schema.", function
{
"schemaType": "Field",
"name": "type",
"type": "Array<String>",
"type": "[String]",
"required": true,
"unique": true,
"defaultValue": "",
Expand All @@ -312,7 +312,7 @@ describe("Convert https://nosqldbm.ru/ JSON data to Moongose Schema.", function
{
"schemaType": "Field",
"name": "type2",
"type": "Array<Array>",
"type": "[[]]",
"required": true,
"unique": true,
"defaultValue": "",
Expand All @@ -326,14 +326,14 @@ describe("Convert https://nosqldbm.ru/ JSON data to Moongose Schema.", function
const expect = [
{
User: {
name: { type: 'String', required: true, unique: false },
email: { type: 'String', required: true, unique: true }
name: { type: 'String', required: true, unique: false, default: "" },
email: { type: 'String', required: true, unique: true, default: "" }
}
},
{
Task: {
type: { type: ['String'], required: true, unique: true },
type2: { type: [[]], required: true, unique: true }
type: { type: ['String'], required: true, unique: true, default: "" },
type2: { type: [[]], required: true, unique: true, default: "" }
}
}
]
Expand Down

0 comments on commit 8c5d513

Please sign in to comment.