lemon-core
Version:
Lemon Serverless Micro-Service Platform
155 lines • 7.24 kB
JavaScript
"use strict";
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.GeneralAPIController = void 0;
/**
* API: `general-api-controllers.ts`
* - common controller class
*
*
* @author Steve Jung <steve@lemoncloud.io>
* @date 2019-12-03 initial version
* @date 2020-01-07 refactoring with `GeneralWEBController`
*
* @copyright (C) 2020 LemonCloud Co Ltd. - All Rights Reserved.
*/
/** ********************************************************************************************************************
* Common Headers
** ********************************************************************************************************************/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const engine_1 = require("../engine/");
const test_helper_1 = require("../common/test-helper");
const general_controller_1 = require("./general-controller");
/**
* class: `APIController`
* - support basic CRUD with TypedManager
*/
class GeneralAPIController extends general_controller_1.GeneralWEBController {
/**
* default constructor
* @param type type of REST API
* @param storage storage-service
* @param search search-service
* @param uniqueField (optional) field in unique to lookup the origin id.
*/
constructor(type, storage, search, uniqueField) {
super(type);
/**
* name of this resource.
*/
this.hello = () => `general-api-controller:${this.type()}`;
/**
* search node
*
*/
this.listBase = (id, param, body, context) => __awaiter(this, void 0, void 0, function* () {
(0, engine_1._log)(this.NS, `! listBase(${id})..`);
param = param || {};
//! translate page + ipp => page & limit.
const $page = engine_1.$U.N(param.page, 0);
const $limit = engine_1.$U.N(param.limit, engine_1.$U.N(param.ipp, 12)); // compartible for ipp
const $param = Object.assign(Object.assign({}, param), { $page, $limit, page: undefined, ipp: undefined, limit: undefined }); // clear paging param.
$param.$O = $param.$O || 'id.keyword'; // order by id
//! base filter masking.
if (param.type === undefined)
$param.type = this.type();
if (param.deletedAt === undefined)
$param.deletedAt = 0;
if (this.unique && param.stereo === undefined)
$param['!stereo'] = '#'; //! `.stereo` is not like '#'
//! call search.
return this.search.searchSimple($param);
});
/**
* read node
*/
this.getBase = (id, param, body, context) => __awaiter(this, void 0, void 0, function* () {
(0, engine_1._log)(this.NS, `! getBase(${id})..`);
return this.storage.read(id);
});
/**
* update node.
* - throw error if not found.
*/
this.putBase = (id, param, body, context) => __awaiter(this, void 0, void 0, function* () {
(0, engine_1._log)(this.NS, `! putBase(${id})..`);
id = `${id || ''}`.trim();
if (!id)
throw new Error('@id (string) is required!');
const $org = yield this.storage.read(id);
//! if try to update 'unique-field', then update lookup.
const field = this.unique ? this.unique.field : '';
const $base = field && body[field] ? yield this.unique.updateLookup($org, body[field]) : {};
const res = yield this.storage.save(id, body);
return Object.assign(Object.assign(Object.assign({}, $base), res), { id });
});
/**
* save (or create) node.
*/
this.postBase = (id, param, body, context) => __awaiter(this, void 0, void 0, function* () {
(0, engine_1._log)(this.NS, `! postBase(${id})..`);
id = `${id || ''}`.trim();
if (!id)
throw new Error('@id (string) is required!');
id = id == '0' ? '' : id; // clear id.
//! if try to update 'unique-field', then update lookup.
const field = this.unique ? this.unique.field : '';
const $base = field && body[field] ? yield this.unique.updateLookup({ id, [field]: body[field] }) : {};
id = id || $base.id;
//! if no id found, then try to make new one.
if (!id) {
const $id = yield this.storage.insert({ type: this.storage.type });
const res = yield this.storage.save($id.id, body);
return Object.assign(Object.assign({}, res), $id);
}
else {
const res = yield this.storage.save(id, body);
return Object.assign(Object.assign(Object.assign({}, $base), res), { id });
}
});
/**
* delete (or destroy) node
*/
this.deleteBase = (id, param, body, context) => __awaiter(this, void 0, void 0, function* () {
(0, engine_1._log)(this.NS, `! deleteBase(${id})..`);
param = param || {};
const destroy = engine_1.$U.N(param.destroy, param.destroy === '' ? 1 : 0) ? true : false;
//! destroy for lookup-id (TBD - need to destory lookup-key really?)
const field = this.unique ? this.unique.field : '';
if (destroy && this.unique) {
const $org = yield this.storage.read(id).catch(test_helper_1.NUL404);
const old = $org ? $org[field] : '';
if (old)
yield this.storage.delete(this.unique.asLookupId(old), destroy);
}
//! destroy key...
return this.storage.delete(id, destroy);
});
this.NS = engine_1.$U.NS(`*${type}`, 'yellow'); // NAMESPACE TO BE PRINTED.
this.storage = storage;
this.search = search;
this.unique = uniqueField ? storage.makeUniqueFieldManager(uniqueField) : null;
}
/**
* override decoder of function-name.
* 1. try to find default.
* 2. use 'base' function.
*/
asFuncName(mode, type, cmd) {
const func1 = super.asFuncName(mode, type, cmd); // use origin.
if (this[func1])
return func1;
const func2 = super.asFuncName(mode, 'base', cmd); // use like `getBase()`
return func2;
}
}
exports.GeneralAPIController = GeneralAPIController;
//# sourceMappingURL=general-api-controller.js.map