mongoose2pojo
Converts MongooseJS schemas to POJO (Plain Old Java Object)
Example usage
my@comp:~$ mkdir schemas && cd schemas# COPY SCHEMA FILES TO THE ~/schemas DIRECTORY my@comp:~/schemas$ npm install mongoosemy@comp:~/schemas$ sudo npm install mongoose2pojo -gmy@comp:~/schemas$ mongoose2pojo *.js
mongoose2pojo expects that your schema files are nodejs modules exposing Schema
object to module.exports
.
Using as command line executable
my@comp:~/code/schemas$ mongoose2pojo image-schema.jsCreated POJO file. Size: 1797 /home/my/schemas/ImagePojo.java
File image-schema.js
.
var Schema = require("mongoose").Schema; module.exports = new Schema({ ref: { type: Schema.ObjectId, ref: 'Asset', index: true }, time: { type: Date, index: true }, type: String}, { collection: 'myimage' });
File ImagePojo.java
gets created:
Using as library
var Schema = require("mongoose").Schema;var m2p = require("mongoose2pojo");var converter = m2p({ className: "User"});var javaCodeString = converter.parse({ name: String, login: { type: String, required: true }, registeredBy: { type: Schema.ObjectId, ref: 'User', index: true }});
The resulting javaCodeString
is:
Alternatively you can generate POJO text from js schema file. No files will be created.
var contents = m2p().parseFile(fileName);
Or you can generate POJO file from js schema file.
A file ImagePojo.java
will be created right next to the existing schema file.
m2p.convertFile("image-schema.js");