Skip to main content


Framework based on mongoose library and provide direct access to mongoose.

It uses ES6 class variant of mongoose to init. Also you can access framework

Model take care about database connection


Models files part of framework inheritance process .

Access mongoose instance

Inside the class mongoose avaialable as


Basic model

import AbstractModel from '@adaptivestone/framework/modules/AbstractModel.js';

class SomeModel extends AbstractModel {
constructor(app) {
// you can put some init stuff

initHooks() {
// place to init plugins, indexes, etc.
// As it happens after loaded class into mongoos, but before mongoose inited class
// this.mongooseSchema.plugin(PLUGIN_NAME);

// here mongoose scheme go
// this is a fullu mongoose scheme
// Please reffer to mongoose documentation
get modelSchema() {
return {
someString: { type: String, required: true },
firstName: String,
lastName: String,
email: String

// Static method will be part on monngose class
// TODO access to APP instance from static methods
static async someStaticMethod() {
const somedata = await this.findByIdAndUpdate(
return somedata;

// any methods will be part on instance method
// await'SomeModel').findById(124).someInstanceMethod()
async someInstanceMethod(){
// you can access app into the instance method
const { app } = this.getSuper();

// inside of instance method you can access model data


// any getters will became a mongoose virtual
// const SomeModels ="SomeModel").
// const someModelInstance = await SomeModel().create({ email: '' });;
// `domain` is now a property on SomeModels documents.
// someModelInstance.domain; // ''
get domain() {
return'@') + 1);


// setters also be an virtual
// const SomeModels ="SomeModel").
// const someModelInstance = new SomeModel();
// Vanilla JavaScript assignment triggers the setter
// someModelInstance.fullName = 'Jean-Luc Picard';
set fullName(v) {
// `v` is the value being set, so use the value to set
// `firstName` and `lastName`.
const firstName = v.substring(0, v.indexOf(' '));
const lastName = v.substring(v.indexOf(' ') + 1);
this.set({ firstName, lastName });


export default SomeModel;


If you have some relations ("ref") on a mongoose model that you should care to load schema. As mongoose can only build relationships with schemas in memory. Google place to do that - inside constructor. Be aware on loop linking models


Models united onse per process and then united instances cached. Do NOT expect constructor or init hook calls on every model loading


Please do not name the model in plural form.

Bad - Coins

Good - Coin


getModel(modelName: string): MongooseModel<any>;


const UserModel ="User");
const userInstace = await UserModel.findOne({email:""})


Main configuration variable "MONGO_DSN" environment variable. Based on it model will made connection to the database

Built-in models

Framework came with few built-in models.


It's a part of the authorization system. It takes care of storing users, hash passwords and provides some basic functions for token generation and getting users.

If you want to have your own user implementation you should overwrite it or disable.

Auth controller depends on this model


const UserModel ="User");
const user = await UserModel.getUserByEmailAndPassword("email","password"):
const userToken = await user.generateToken(); // generate and store token on databse
const userPublic = await user.getPublic();
const hashedPassword = await UserModel.hashPassword("password");
const sameUser = await UserModel.getUserByToken(userToken);
const sameUserAgain = await UserModel.getUserByEmail(;
const recoveryToken = await UserModel.generateUserPasswordRecoveryToken(user);
const sameUSerAgain2 = await UserModel.getUserByPasswordRecoveryToken(recoveryToken);
const isSuccess = await user.sendPasswordRecoveryEmail(i18n);
const verificationToken = await UserModel.generateUserVerificationToken(user);
const sameUSerAgain3 = awaitUserModel.getUserByVerificationToken(verificationToken);
await UserModel.removeVerificationToken(verificationToken);
const isSuccess2 = await user.sendVerificationEmail(i18n);


Migration model it's helper for migration subsystems. It stores migrated files to make sure that migrated executed once

Please refer to CLI/migrations for more details

You probably should not use this model directly


Sequence allows you to generate sequences by name. This is cross server safe method to generate sequences in distributed environment

const SequenceModel ='Sequence');
// will be 1
const someTypeSequence = await SequenceModel.getSequence('someType');
// will be 2
const someTypeSequence2 = await SequenceModel.getSequence('someType');
// will be 1 as type is another
const someAnotherTypeSequence = await SequenceModel.getSequence(