-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
210 lines (178 loc) · 4.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* @author Vishal Gauba
* @desc Generate Spark StructType schema JSON from Mongoose model
* @example node index.js --model=./path-to-mongoose-model.js
* @example node index.js --model=./path-to-mongoose-model.js --output=./path-to-output-schema.json
*/
const fs = require('fs')
const fsPromises = fs.promises
class Main {
// mapping mongoose.js types to spark structtype types
static TypeMappings = {
String: 'string',
Date: 'timestamp',
ObjectId: 'string',
Boolean: 'boolean',
Number: 'float',
}
static BaseBlock = {
metadata: {},
nullable: true,
name: null,
type: null,
}
constructor(args){
if (!args.model || !fs.existsSync(args.model)) {
console.log('ERROR: One or more required arguments is either missing or invalid (--model)')
process.exit(1)
}
this.model_path = args.model
this.output_path = args.output
}
async generateSchema(){
try {
var model = require(this.model_path)
} catch (err) {
console.log(`ERROR: Could not import mongoose model file. Manually remove all dependencies (plugins, hooks) except 'mongoose' in ${this.model_path} and re-run.`)
process.exit(1)
}
const schema_tree = model.schema.tree
// recursively parse mongoose schema to spark schema
const spark_schema_fields = this._parseObject({ value: schema_tree })
const spark_schema_json = {
collection_name: model.modelName,
spark_schema_json: {
type: "struct",
fields: spark_schema_fields
}
}
const output = JSON.stringify(spark_schema_json, null, 4)
if (!this.output_path){
console.log(output)
} else {
await Helpers.writeFile(this.output_path, output)
}
return this.output
}
/**
* Recursively parse mongoose schema
*/
_parseObject({key, value}){
// {name: String, age: Number}
if (!key){
let fields = []
for (let [k, v] of Object.entries(value)) {
let result = this._parseObject({ key: k, value: v })
if (result) fields.push(result)
}
return fields
}
// ignore
if (key === '__v' || value.constructor.name === 'VirtualType' || value === false) {
return
}
// mongoose subdocuments
// users: mongoose.Schema()
if (value.constructor.name === 'Schema') {
value = value.tree
}
// _id: ObjectId
if (value === 'ObjectId') {
return {
...Main.BaseBlock,
type: this._getSparkType(key, value),
name: key,
}
}
// age: Number
if (value.constructor.name === 'Function') {
return {
...Main.BaseBlock,
type: this._getSparkType(key, value.name),
name: key,
}
}
if (value.constructor.name === 'Object') {
// age: {type: Number, default: 0, enum: []}
// _id: { auto: true, type: 'ObjectId' }
if (value.type
// handle fields with key name 'type'
&& (value.type.constructor.name === 'Function' || value.type.constructor.name === 'String')
) {
return {
...Main.BaseBlock,
...this._parseObject({key, value: value.type}),
name: key,
}
}
// user: {name: String, age: Number}
return {
...Main.BaseBlock,
type: {
type: "struct",
fields: this._parseObject({ value })
},
name: key,
}
}
if (value.constructor.name === 'Array') {
// mongoose subdocuments
// users: [mongoose.Schema()]
if (value[0].constructor.name === 'Schema') {
value[0] = value[0].tree
}
// users: [{name: String, age: Number}]
if (value[0].constructor.name === 'Object'
// handle fields with key name 'type'
&& !(value[0].type && (value[0].type.constructor.name === 'Function' || value[0].type.constructor.name === 'String'))
) {
return {
...Main.BaseBlock,
name: key,
type: {
containsNull: true,
elementType: {
type: "struct",
fields: this._parseObject({value: value[0]})
},
type: "array"
}
}
}
// names: [String]
return this._parseObject({key, value: value[0]})
}
console.log(`ERROR: Could not parse model due to ${JSON.stringify({[key]: value}, null, 4)}. Manually fix such fields in ${this.model_path} and re-run.`)
process.exit(1)
}
_getSparkType(key, mongooseType){
const sparkType = Main.TypeMappings[mongooseType]
if (!sparkType){
console.log(`ERROR: Mongoose type ${mongooseType} (at model path ${key}) is not supported yet. Manually fix such fields in ${this.model_path} and re-run.`)
process.exit(1)
}
return sparkType
}
}
class Helpers {
static getArgs() {
const args = {};
process.argv.slice(2, process.argv.length).forEach(arg => {
// long arg
if (arg.slice(0, 2) === '--') {
const longArg = arg.split('=');
const longArgFlag = longArg[0].slice(2, longArg[0].length);
const longArgValue = longArg.length > 1 ? longArg[1] : true;
args[longArgFlag] = longArgValue;
}
});
return args;
}
static writeFile(fileName, content) {
return fsPromises.writeFile(fileName, content)
}
}
new Main(Helpers.getArgs())
.generateSchema()
.catch(console.error)
// TODO: convert to module, export function