What is moltyjs?
A tiny ODM for MongoDB with multy tenancy support.
Install
$ npm install moltyjs --save
Usage
const { connect } = require('moltys');
import { connect } from ('moltyjs');
Connect to a DB
To connect to a database use the "connect()" function passing trough 'options' payload all the settings required:
const { connect } = require('moltys');
const options = {
engine: 'mongodb',
uri: 'mongodb://localhost:27017/test',
max: 100,
min: 1,
};
const connection = connect(options);
"connect()" will return a connection instance which will allow you to perform all the actions availables on the DB.
Note: For the time being MoltyJS only support Mongo Databases.
Drop a DB
const res = await connection.dropDatabase('test');
Create a new Schema
Molty Schema are based on Mongoose Schema structure with some changes on the declaration of the inherit schema options. I even keep some field options name to make the Molty integration as easier as posible in those project are currently running Mongoose.
To create a new Schema use the "Schema()" constructor passing the schema and the options:
const { Schema } = require('moltys');
const newSchema = Schema(
{
email: {
type: String,
required: true,
unique: true,
maxlength: 100,
},
password: {
type: String,
required: true,
},
firstName: {
type: String,
default: '',
},
lastName: {
type: String,
default: 'LEMES',
},
},
{
timestamps: true,
inheritOptions: {
discriminatorKey: '__kind',
},
},
);
The schema field properties alowed are:
type: Mandatory [String, Number, Boolean, Buffer, Date, Array, Object]
required: Optional {Boolean}
unique: Optional {Boolean}
default: Optional
match: Optional
enum: Optional
min: Optional
max: Optional
maxlength: Optional
validate: Optional
And the schema options allowed are:
timestamps: Optional
inheritOptions: Optional
--discriminatorKey: Required once "inheritOptions" is set
--merge: Optional ['methods', 'preHooks', 'postHooks']
Create a new Model
Once we have created our schema we need to register as a model so we can start to create, find, updete and delete documents. To do this you must provide the a proper schema and a model name. The model name will be the collection name on the DB so use the criteria you want since Molty does not make any accomodation on them like auto plurilize.
const { Model } = require('moltys');
const TestModel = model(newSchema, 'TestModel');
TODO
- Improove documentation
- Populate
- find()
- update()
- delete()
- Add embedded documents and ref document support
- The mquery query builder