Installation
This is a Node.js module available through the npm registry.
Before installing, download and install Node.js. Node.js 0.10 or higher is required.
Installation is done using the
npm install
command:
$ npm install -g lesrobertsframework
Quick Start
Install the executable:
$ npm install -g lesrobertsframework
Create the app:
$ roberts init|i
Create new model (go in the previously created project)):
$ roberts model:create|m:c
Install dependencies:
$ npm install
Configure your project ...
Start the server:
$ roberts start
Access swagger on : host/api-doc#/
Project structure
myProject_orm
│ main.ts
│ package-lock.json
│ package.json
│ README.md
│ tsconfig.json
│
├───config
│ datasource.ts
│ server-config.ts
│ swagger-config.ts
│
├───models
│ FooModel.ts
│
└───route
router-Foo.ts
router.ts
Configuration example
serveur-config.ts
import { ServerConfig } from 'lesrobertsframeWork'; // Importing the interface from the frameWork
const serverConfig: ServerConfig = {
id: 1,
name: "serverConfig",
port: 3001,
}
export default serverConfig;
datasource.ts
export const dataSourceRest = new DataSource(
RestAdapter,
1,
"datasource1",
{
restBaseUrl: 'http://localhost:3000'
}
);
export const dataSourceSql = new DataSource(
SqlAdapter,
1,
"dataSource2",
{
host : "db347118-robert.sql-pro.online.net",
user: "db114324",
password: "Azerty123",
database: "db347118_robert"
}
);
swagger-config.ts
const options = {
swaggerDefinition: {
info: {
description: 'This is a sample server',
title: 'Swagger',
version: '1.0.0',
},
host: 'localhost:3001',
basePath: '',
produces: [
"application/json",
"application/xml"
],
schemes: ['http', 'https'],
securityDefinitions: {
JWT: {
type: 'apiKey',
in: 'header',
name: 'Authorization',
description: "",
}
}
},
basedir: __dirname, //app absolute path
files: ['../route/router*.ts'] //Path to the API handle folder
};
export default options;
Customisation
This module has some automaticaly generated route and adapter. You can add your own :
My custom adapter :
import Foo from '../model/foo'
import fetch from 'node-fetch';
// Test de la création d'une requête pour un utilisateur de notre framework
export default class Test {
public static async findById() : Promise<User> {
const url = `http://localhost:3000/foo/1`;
const promiseModel = await fetch(url).then(
async (res: any) => {
const model = new User(await res.json());
return model;
},
);
return promiseModel;
}
}
Modify your model :
import { Model } from 'lesrobertsframeWork';
import DataSource from '../config/datasources';
import { endPoint } from './restConfig/foo-restConfig';
import Test from '../testAdapter/testFindFooById';
class Foo extends Model {
static dataSource = DataSource;
static restConfig = {
endpoint: endPoint,
}
constructor(data: Record<string, any>) {
super(data);
}
public static testGet() {
return Test.findById();
}
}
export default Foo;
my custom route :
router.get('/id/:id', async function(req, res, next) {
try {
res.send(await Foo.testGet());
} catch(err) {
next(err)
}
});