Simple Usage - nodejayes/molly GitHub Wiki

There are always 2 steps to do to use Molly. First, type script classes have to be created and decorated and then a server instance has to be configured.

import {BaseTypes, collection, ExpressServer, validation} from 'molly';

// Define a Class and decorate it
// this Decoration defines a MongoDb Collection with CRUD Support
@collection({
  allow: 'CUD'
})
class User {
  // this Decoration defines a Validation Rule for the Property _id
  // it must be a MongoDb Object Id as String
  @validation({type: BaseTypes.mongoDbObjectId})
  _id?: string;
  @validation({type: BaseTypes.stringDefaultLength})
  name: string;
  @validation({type: BaseTypes.hexadecimal})
  password: string;
  @validation({type: BaseTypes.integer.min(0).max(120)})
  age: string;
}

// get a Instance of the WebServer
const SERVER = new ExpressServer();
// Configure the WebServer and start them
SERVER.start({
  binding: 'localhost',
  port: 8086,
  mongoUrl: 'mongodb connection url',
  mongoDatabase: 'database name',
  mongoAuthDatabase: 'admin database', // for Authentication only and admin is default
  mongoReplicaSet: 'name of the replica set',
  clear: true,
  models: [User], // register the defines Models
});

When we start this script, a connection to MongoDb is established in the background and the database and collections are created. Also an Express Server is started, which can be reached under the address localhost:8086.

Now you can send the following POST request to the server and a user object will be created in the MongoDb.

POST /create/User HTTP/1.1
Host: localhost:8086
Content-Type: application/json
Cache-Control: no-cache

{
	"params": {
		"name": "John",
		"password": "d88807c3c0af47d4b2379262a513b441",
		"age": 25
	}
}

To read the created users the following POST request is used

POST /read/User HTTP/1.1
Host: localhost:8086
Content-Type: application/json
Cache-Control: no-cache

{
	"params": {}
}

Back to Index