UNPKG

4.16 kBJavaScriptView Raw
1import { isDev } from '@lskjs/env';
2import Err from '@lskjs/err';
3import tryJSONparse from '@lskjs/utils/tryJSONparse';
4import forEach from 'lodash/forEach';
5import get from 'lodash/get';
6import mapValues from 'lodash/mapValues';
7import pick from 'lodash/pick';
8import set from 'lodash/set';
9
10import BaseApi from './Api';
11
12export class ListApi extends BaseApi {
13 getRoutes() {
14 return {
15 ...super.getRoutes(),
16 '/count': this.count.bind(this),
17 '/find': this.find.bind(this),
18 '/list': this.find.bind(this),
19 '/findOne': this.findOne.bind(this),
20 '/get': this.findOne.bind(this),
21 '/create': this.create.bind(this),
22 '/update': this.update.bind(this),
23 '/edit': this.update.bind(this),
24 '/remove': this.remove.bind(this),
25 '/delete': this.remove.bind(this),
26 };
27 }
28
29 cache(key, cb) {
30 return cb();
31 }
32 getListParams(req) {
33 const { data } = req;
34 const params = mapValues(pick(data, ['filter', 'sort', 'skip', 'limit', 'select', 'view', 'operation']), (a) =>
35 tryJSONparse(a),
36 );
37
38 if (!params.filter) params.filter = {};
39 if (req.data) {
40 forEach(req.data, (val, key) => {
41 if (key.substr(0, 'filter.'.length) === 'filter.') {
42 set(params, key, val);
43 }
44 if (key.substr(0, 'sort.'.length) === 'sort.') {
45 set(params, key, val);
46 }
47 });
48 }
49 if (params.limit > 100) params.limit = 100;
50 if (!params.select) params.select = [];
51 if (typeof params.select === 'string') {
52 params.select = params.select
53 .trim()
54 .split(',')
55 .map((a) => a.trim());
56 }
57 if (!Array.isArray(params.select)) throw new Err('select not array');
58 if (!params.view) params.view = 'default';
59 // params.operation = req.data.operation;
60
61 return params;
62 }
63
64 countByParams(Model, incomeParams = {}, systemParams = {}) {
65 const params = this.__getParams(Model, incomeParams, systemParams);
66 return Model.countDocuments(params.filter);
67 }
68 __getSelect(Model, params = {}) {
69 let select = get(params, 'select', get(params, 'req.data.select', []));
70 const view = get(params, 'view', get(params, 'req.data.view', 'default'));
71 if (typeof select === 'string') select = [select];
72 const { views = {} } = Model;
73 return [...(views[view] || []), ...(select || [])];
74 }
75 __getParams(Model, incomeParams = {}, systemParams = {}) {
76 const params = {
77 ...(Model.defaultParams || {}),
78 ...pick(incomeParams, ['filter', 'sort', 'skip', 'limit', 'select', 'view']),
79 };
80 params.select = this.__getSelect(params);
81 if (isDev && (incomeParams.debug || params.select.includes('*'))) {
82 delete params.select;
83 }
84 if (params.limit > 100) {
85 console.log('params.limit > 100'); //eslint-disable-line
86 params.limit = 100;
87 }
88 return {
89 ...params,
90 ...systemParams,
91 };
92 }
93 findByParams(Model, incomeParams = {}, systemParams = {}) {
94 const params = this.__getParams(Model, incomeParams, systemParams);
95 let res = Model.find(params.filter);
96 if (params.sort) {
97 res = res.sort(params.sort);
98 }
99 if (params.skip && +params.skip) {
100 res = res.skip(+params.skip);
101 }
102 if (params.limit && +params.limit) {
103 res = res.limit(+params.limit);
104 }
105 if (params.select) {
106 res = res.select(params.select);
107 }
108 // if (params.then) {
109 // res = params.then(res);
110 // }
111 // if (params.populate) {
112 // res = res.populate(params.populate);
113 // }
114 // if (params.prepare) {
115 // return this.prepare(res, params.prepare);
116 // }
117 return res;
118 }
119
120 async count() {
121 throw new Err('api.notImplemented', { status: 500 });
122 }
123 async find() {
124 throw new Err('api.notImplemented', { status: 500 });
125 }
126 async findOne() {
127 throw new Err('api.notImplemented', { status: 500 });
128 }
129 async create() {
130 throw new Err('api.notImplemented', { status: 500 });
131 }
132 async update() {
133 throw new Err('api.notImplemented', { status: 500 });
134 }
135 async remove() {
136 throw new Err('api.notImplemented', { status: 500 });
137 }
138}
139
140export default ListApi;