express-ext 0.1.24 → 0.1.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,11 +20,11 @@ var resources_1 = require("./resources");
20
20
  var view_1 = require("./view");
21
21
  var GenericController = (function (_super) {
22
22
  __extends(GenericController, _super);
23
- function GenericController(log, service, status, validate, build) {
23
+ function GenericController(log, service, status, build, validate) {
24
24
  var _this = _super.call(this, log, service) || this;
25
25
  _this.service = service;
26
- _this.validate = validate;
27
26
  _this.build = build;
27
+ _this.validate = validate;
28
28
  _this.status = edit_1.initializeStatus(status);
29
29
  if (service.metadata) {
30
30
  var m = service.metadata();
@@ -19,8 +19,8 @@ var search_1 = require("./search");
19
19
  var search_func_1 = require("./search_func");
20
20
  var GenericSearchController = (function (_super) {
21
21
  __extends(GenericSearchController, _super);
22
- function GenericSearchController(log, find, service, config, validate, dates, numbers) {
23
- var _this = _super.call(this, log, service, config, validate) || this;
22
+ function GenericSearchController(log, find, service, config, build, validate, dates, numbers) {
23
+ var _this = _super.call(this, log, service, config, build, validate) || this;
24
24
  _this.find = find;
25
25
  _this.search = _this.search.bind(_this);
26
26
  _this.config = search_1.initializeConfig(config);
@@ -67,3 +67,44 @@ var LoadSearchController = (function (_super) {
67
67
  return LoadSearchController;
68
68
  }(LoadController_1.LoadController));
69
69
  exports.LoadSearchController = LoadSearchController;
70
+ var ViewController = (function (_super) {
71
+ __extends(ViewController, _super);
72
+ function ViewController(log, query, config, dates, numbers) {
73
+ var _this = _super.call(this, log, query) || this;
74
+ _this.query = query;
75
+ _this.search = _this.search.bind(_this);
76
+ if (config) {
77
+ if (typeof config === 'boolean') {
78
+ _this.csv = config;
79
+ }
80
+ else {
81
+ _this.config = search_1.initializeConfig(config);
82
+ if (_this.config) {
83
+ _this.csv = _this.config.csv;
84
+ _this.fields = _this.config.fields;
85
+ _this.excluding = _this.config.excluding;
86
+ }
87
+ }
88
+ }
89
+ if (!_this.fields || _this.fields.length === 0) {
90
+ _this.fields = 'fields';
91
+ }
92
+ var m = search_func_1.getMetadataFunc(query, dates, numbers);
93
+ if (m) {
94
+ _this.dates = m.dates;
95
+ _this.numbers = m.numbers;
96
+ }
97
+ return _this;
98
+ }
99
+ ViewController.prototype.search = function (req, res) {
100
+ var _this = this;
101
+ var s = search_1.fromRequest(req, search_1.buildArray(this.array, this.fields, this.excluding));
102
+ var l = search_1.getParameters(s, this.config);
103
+ var s2 = search_1.format(s, this.dates, this.numbers);
104
+ this.query.search(s2, l.limit, l.skipOrRefId, l.fields)
105
+ .then(function (result) { return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config); })
106
+ .catch(function (err) { return http_1.handleError(err, res, _this.log); });
107
+ };
108
+ return ViewController;
109
+ }(LoadController_1.LoadController));
110
+ exports.ViewController = ViewController;
@@ -17,10 +17,44 @@ var GenericController_1 = require("./GenericController");
17
17
  var http_1 = require("./http");
18
18
  var search_1 = require("./search");
19
19
  var search_func_1 = require("./search_func");
20
+ var LowcodeController = (function (_super) {
21
+ __extends(LowcodeController, _super);
22
+ function LowcodeController(log, lowCodeService, config, build, validate, dates, numbers) {
23
+ var _this = _super.call(this, log, lowCodeService, config, build, validate) || this;
24
+ _this.lowCodeService = lowCodeService;
25
+ _this.search = _this.search.bind(_this);
26
+ _this.config = search_1.initializeConfig(config);
27
+ if (_this.config) {
28
+ _this.csv = _this.config.csv;
29
+ _this.fields = _this.config.fields;
30
+ _this.excluding = _this.config.excluding;
31
+ }
32
+ if (!_this.fields || _this.fields.length === 0) {
33
+ _this.fields = 'fields';
34
+ }
35
+ var m = search_func_1.getMetadataFunc(lowCodeService, dates, numbers);
36
+ if (m) {
37
+ _this.dates = m.dates;
38
+ _this.numbers = m.numbers;
39
+ }
40
+ return _this;
41
+ }
42
+ LowcodeController.prototype.search = function (req, res) {
43
+ var _this = this;
44
+ var s = search_1.fromRequest(req, search_1.buildArray(this.array, this.fields, this.excluding));
45
+ var l = search_1.getParameters(s, this.config);
46
+ var s2 = search_1.format(s, this.dates, this.numbers);
47
+ this.lowCodeService.search(s2, l.limit, l.skipOrRefId, l.fields)
48
+ .then(function (result) { return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config); })
49
+ .catch(function (err) { return http_1.handleError(err, res, _this.log); });
50
+ };
51
+ return LowcodeController;
52
+ }(GenericController_1.GenericController));
53
+ exports.LowcodeController = LowcodeController;
20
54
  var Controller = (function (_super) {
21
55
  __extends(Controller, _super);
22
- function Controller(log, lowCodeService, config, validate, dates, numbers) {
23
- var _this = _super.call(this, log, lowCodeService, config, validate) || this;
56
+ function Controller(log, lowCodeService, build, validate, config, dates, numbers) {
57
+ var _this = _super.call(this, log, lowCodeService, config, build, validate) || this;
24
58
  _this.lowCodeService = lowCodeService;
25
59
  _this.search = _this.search.bind(_this);
26
60
  _this.config = search_1.initializeConfig(config);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.1.24",
3
+ "version": "0.1.27",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -20,7 +20,7 @@ export interface GenericService<T, ID, R> {
20
20
  export class GenericController<T, ID> extends LoadController<T, ID> {
21
21
  status: StatusConfig;
22
22
  metadata?: Attributes;
23
- constructor(log: Log, public service: GenericService<T, ID, number|ResultInfo<T>>, status?: StatusConfig, public validate?: Validate<T>, public build?: Build<T>) {
23
+ constructor(log: Log, public service: GenericService<T, ID, number|ResultInfo<T>>, status?: StatusConfig, public build?: Build<T>, public validate?: Validate<T>) {
24
24
  super(log, service);
25
25
  this.status = initializeStatus(status);
26
26
  if (service.metadata) {
@@ -157,12 +157,13 @@ export function useBuild<T>(c: ModelConfig, generate?: (() => string)): Build<T>
157
157
  const b = new Builder<T>(generate, c.id ? c.id : '', c.payload ? c.payload : '', c.user ? c.user : '', c.updatedBy ? c.updatedBy : '', c.updatedAt ? c.updatedAt : '', c.createdBy ? c.createdBy : '', c.createdAt ? c.createdAt : '', c.version ? c.version : '');
158
158
  return b.build;
159
159
  }
160
+ // tslint:disable-next-line:max-classes-per-file
160
161
  export class Builder<T> {
161
162
  constructor(public generate: (() => string)|undefined, public id: string, public payload: string, public user: string, public updatedBy: string, public updatedAt: string, public createdBy: string, public createdAt: string, public version: string) {
162
163
  this.build = this.build.bind(this);
163
164
  }
164
165
  build(res: Response, obj: T, isCreate?: boolean, isPatch?: boolean): void {
165
- let o: any = obj;
166
+ const o: any = obj;
166
167
  let usr = '';
167
168
  if (this.user.length > 0) {
168
169
  if (this.payload.length > 0) {
@@ -1,6 +1,6 @@
1
1
  import {Request, Response} from 'express';
2
2
  import {ResultInfo, StatusConfig} from './edit';
3
- import {GenericController, GenericService} from './GenericController';
3
+ import {Build, GenericController, GenericService} from './GenericController';
4
4
  import {handleError, Log} from './http';
5
5
  import {ErrorMessage} from './metadata';
6
6
  import {buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
@@ -16,8 +16,8 @@ export class GenericSearchController<T, ID, S extends Filter> extends GenericCon
16
16
  fields?: string;
17
17
  excluding?: string;
18
18
  array?: string[];
19
- constructor(log: Log, public find: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>, service: GenericService<T, ID, number|ResultInfo<T>>, config?: Config, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
20
- super(log, service, config, validate);
19
+ constructor(log: Log, public find: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>, service: GenericService<T, ID, number|ResultInfo<T>>, config?: Config, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
20
+ super(log, service, config, build, validate);
21
21
  this.search = this.search.bind(this);
22
22
  this.config = initializeConfig(config);
23
23
  if (this.config) {
@@ -9,6 +9,11 @@ export interface Search {
9
9
  search(req: Request, res: Response): void;
10
10
  load(req: Request, res: Response): void;
11
11
  }
12
+ export interface Query<T, ID, S> extends ViewService<T, ID> {
13
+ search: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>;
14
+ metadata?(): Attributes|undefined;
15
+ load(id: ID, ctx?: any): Promise<T|null>;
16
+ }
12
17
  export interface SearchManager {
13
18
  search(req: Request, res: Response): void;
14
19
  load(req: Request, res: Response): void;
@@ -62,3 +67,44 @@ export class LoadSearchController<T, ID, S extends Filter> extends LoadControlle
62
67
  .catch(err => handleError(err, res, this.log));
63
68
  }
64
69
  }
70
+ export class ViewController<T, ID, S extends Filter> extends LoadController<T, ID> {
71
+ config?: SearchConfig;
72
+ csv?: boolean;
73
+ dates?: string[];
74
+ numbers?: string[];
75
+ fields?: string;
76
+ excluding?: string;
77
+ array?: string[];
78
+ constructor(log: Log, protected query: Query<T, ID, S>, config?: SearchConfig|boolean, dates?: string[], numbers?: string[]) {
79
+ super(log, query);
80
+ this.search = this.search.bind(this);
81
+ if (config) {
82
+ if (typeof config === 'boolean') {
83
+ this.csv = config;
84
+ } else {
85
+ this.config = initializeConfig(config);
86
+ if (this.config) {
87
+ this.csv = this.config.csv;
88
+ this.fields = this.config.fields;
89
+ this.excluding = this.config.excluding;
90
+ }
91
+ }
92
+ }
93
+ if (!this.fields || this.fields.length === 0) {
94
+ this.fields = 'fields';
95
+ }
96
+ const m = getMetadataFunc(query, dates, numbers);
97
+ if (m) {
98
+ this.dates = m.dates;
99
+ this.numbers = m.numbers;
100
+ }
101
+ }
102
+ search(req: Request, res: Response): void {
103
+ const s = fromRequest<S>(req, buildArray(this.array, this.fields, this.excluding));
104
+ const l = getParameters(s, this.config);
105
+ const s2 = format(s, this.dates, this.numbers);
106
+ this.query.search(s2, l.limit, l.skipOrRefId, l.fields)
107
+ .then(result => jsonResult(res, result, this.csv, l.fields, this.config))
108
+ .catch(err => handleError(err, res, this.log));
109
+ }
110
+ }
@@ -1,6 +1,6 @@
1
1
  import {Request, Response} from 'express';
2
2
  import {ResultInfo, StatusConfig} from './edit';
3
- import {GenericController, GenericService} from './GenericController';
3
+ import {Build, GenericController, GenericService} from './GenericController';
4
4
  import {handleError, Log} from './http';
5
5
  import {ErrorMessage} from './metadata';
6
6
  import {buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
@@ -11,6 +11,41 @@ export interface LowCodeConfig extends StatusConfig, SearchConfig {
11
11
  export interface Service<T, ID, R, S extends Filter> extends GenericService<T, ID, R> {
12
12
  search: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>;
13
13
  }
14
+ export class LowcodeController<T, ID, S extends Filter> extends GenericController<T, ID> {
15
+ config?: SearchConfig;
16
+ csv?: boolean;
17
+ dates?: string[];
18
+ numbers?: string[];
19
+ fields?: string;
20
+ excluding?: string;
21
+ array?: string[];
22
+ constructor(log: Log, public lowCodeService: Service<T, ID, number|ResultInfo<T>, S>, config?: LowCodeConfig, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
23
+ super(log, lowCodeService, config, build, validate);
24
+ this.search = this.search.bind(this);
25
+ this.config = initializeConfig(config);
26
+ if (this.config) {
27
+ this.csv = this.config.csv;
28
+ this.fields = this.config.fields;
29
+ this.excluding = this.config.excluding;
30
+ }
31
+ if (!this.fields || this.fields.length === 0) {
32
+ this.fields = 'fields';
33
+ }
34
+ const m = getMetadataFunc(lowCodeService, dates, numbers);
35
+ if (m) {
36
+ this.dates = m.dates;
37
+ this.numbers = m.numbers;
38
+ }
39
+ }
40
+ search(req: Request, res: Response) {
41
+ const s = fromRequest<S>(req, buildArray(this.array, this.fields, this.excluding));
42
+ const l = getParameters(s, this.config);
43
+ const s2 = format(s, this.dates, this.numbers);
44
+ this.lowCodeService.search(s2, l.limit, l.skipOrRefId, l.fields)
45
+ .then(result => jsonResult(res, result, this.csv, l.fields, this.config))
46
+ .catch(err => handleError(err, res, this.log));
47
+ }
48
+ }
14
49
  export class Controller<T, ID, S extends Filter> extends GenericController<T, ID> {
15
50
  config?: SearchConfig;
16
51
  csv?: boolean;
@@ -19,8 +54,8 @@ export class Controller<T, ID, S extends Filter> extends GenericController<T, ID
19
54
  fields?: string;
20
55
  excluding?: string;
21
56
  array?: string[];
22
- constructor(log: Log, public lowCodeService: Service<T, ID, number|ResultInfo<T>, S>, config?: LowCodeConfig, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
23
- super(log, lowCodeService, config, validate);
57
+ constructor(log: Log, public lowCodeService: Service<T, ID, number|ResultInfo<T>, S>, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, config?: LowCodeConfig, dates?: string[], numbers?: string[]) {
58
+ super(log, lowCodeService, config, build, validate);
24
59
  this.search = this.search.bind(this);
25
60
  this.config = initializeConfig(config);
26
61
  if (this.config) {