lemon-core
Version:
Lemon Serverless Micro-Service Platform
107 lines • 4.49 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DummyController = void 0;
/**
* API: `/dummy-controller`
* - public service api w/ dummy data
* - use `data/dummy.file.yml` to serve CRUD operation
*
*
* @author Steve Jung <steve@lemoncloud.io>
* @date 2019-12-10 initial version
*
* @copyright (C) 2019 LemonCloud Co Ltd. - All Rights Reserved.
*/
/** ********************************************************************************************************************
* Common Headers
** ********************************************************************************************************************/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const engine_1 = require("../engine/");
const dynamo_1 = require("../cores/dynamo/");
const general_controller_1 = require("./general-controller");
/** ********************************************************************************************************************
* MAIN IMPLEMENTATION.
** ********************************************************************************************************************/
/**
* class: `DummyController`
* - to serve basic CRUD with `dummy-<type>-data.yml`
*/
class DummyController extends general_controller_1.GeneralController {
/**
* create dummy-controller with type
*
* @param type type of resource
* @param name name of dummy data (default equal to type)
* @param idName name of id (default as 'id')
*/
constructor(type, name, idName = 'id') {
super(type);
/**
* name of this resource.
*/
this.hello = () => `dummy-controller:${this.type()}/${this._name}`;
/**
* get list of data.
*/
this.do_list = (id, param, body, ctx) => __awaiter(this, void 0, void 0, function* () {
param = param || {};
const page = engine_1.$U.N(param.page, 1);
const limit = engine_1.$U.N(param.limit, 1);
return this.service.listItems(page, limit);
});
/**
* read item.
*/
this.do_get = (id, param, body, ctx) => __awaiter(this, void 0, void 0, function* () {
return this.service.readItem(id);
});
/**
* update item.
*/
this.do_put = (id, param, body, ctx) => __awaiter(this, void 0, void 0, function* () {
return this.service.updateItem(id, null, body);
});
/**
* save item.
*/
this.do_post = (id, param, body, ctx) => __awaiter(this, void 0, void 0, function* () {
return this.service.saveItem(id, body);
});
/**
* delete item.
*/
this.do_delete = (id, param, body, ctx) => __awaiter(this, void 0, void 0, function* () {
return this.service.deleteItem(id);
});
this._name = name || type;
//* prepare dynamo options.
const tableName = `dummy-${this._name}`;
const fileName = `dummy-${this._name}-data.yml`;
this.service = new dynamo_1.DummyDynamoService(fileName, { tableName, idName });
}
/**
* decode to target `next-handler`
* - use pattern `do_<mode>_<cmd?>`
*/
decode(mode, id, cmd) {
//WARN - if like to use `super.` in here. must use function style.
const fx = super.decode(mode, id, cmd);
if (fx)
return fx;
//* decode as `do_<mode>_<cmd?>` style.
const funcName = (cmd ? `do_${mode}_${cmd}` : `do_${mode}`).toLowerCase();
const handler = this[funcName];
return typeof handler == 'function' ? handler : null;
}
}
exports.DummyController = DummyController;
//# sourceMappingURL=dummy-controller.js.map
;