molti

A self documenting extensible framework powered by express and knex

View on GitHub

NPM Version NPM Downloads Coveralls branchTravis branch AppVeyor branch

Molti

A self documenting extensible framework powered by express and knex

Installation

$ npm install molti

Basic Usage

As a server

const { Parameter, Response, Application, Handler, Controller, Generics } = require('molti');

const sampleController = new Controller({
  basePath: '/'
});

sampleController.get(new Handler({
  path: '/some_path/:id',
  params: [Generics.params.id],
  responses: [Generics.responses.success],
  handler({ id }, { success }) {
    return success({ message: `Found ${id}` });
  }
}));

const sample = new Application({
  controllers: [sampleController]
});

sample.listen(3000);

As an ORM

const { Schema, ModelFactory, Registry } = require('molti');

const parentSchema = new Schema({
  name: {
    type: Schema.Types.String
  },
  children: {
    type: Schema.Types.Models
  }
});

class Parent extends ModelFactory(parentSchema) {
  feedChildren() {
    return this.children.map(child => child.eat());
  }
}

const childSchema = new Schema({
  name: {
    type: Schema.Types.String
  },
  parent: {
    type: Schema.Types.Model
  }
});

class Child extends ModelFactory(childSchema) {
  eat() {
    return `I, ${this.name}, ate ${this.parent.name}'s food`;
  }
}

const registry = new Registry({
  client: 'sqlite',
  connection: {
    filename: ':memory:'
  }
}, [
  Child,
  Parent
]);

Parent.findById(1, { withRelated: ['children'] })
  .then(parent => console.log(parent.feedChildren()));