Skip to content

Commit c79d91d

Browse files
committed
Cleanups.
1 parent 10f5de1 commit c79d91d

13 files changed

Lines changed: 265 additions & 266 deletions

File tree

js/charts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const schema = '"' + (config.schema || "evolutility") + '"',
2222

2323
// - returns data for a single charts
2424
// - sample REST url: http://localhost:2000/api/v1/todo/chart/category
25-
function chartField(req, res) {
25+
export function chartField(req, res) {
2626
logger.logReq("GET CHARTS", req);
2727

2828
const m = getModel(req.params.entity),

js/crud.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import { getModel } from "./utils/model-manager.js";
1010
import { systemFields } from "./utils/dico.js";
1111
import sqls from "./utils/sql-select.js";
12-
import query from "./utils/query.js";
13-
import errors from "./utils/errors.js";
12+
import { runQuery, promiseQuery } from "./utils/query.js";
13+
import { badRequest } from "./utils/errors.js";
1414
import logger from "./utils/logger.js";
1515
import config from "../config.js";
1616

@@ -39,7 +39,7 @@ function SQLgetOne(id, m, res) {
3939
sql += " WHERE t1." + m.pKey + "=$1";
4040
} else {
4141
const invalidID = 'Invalid id: "' + id + '".';
42-
return res ? errors.badRequest(res, invalidID) : "ERROR: " + invalidID;
42+
return res ? badRequest(res, invalidID) : "ERROR: " + invalidID;
4343
}
4444
sql += " LIMIT 1;";
4545
return {
@@ -63,9 +63,9 @@ function getOne(req, res) {
6363
}
6464
if (m.collections && !req.query.shallow) {
6565
const qCollecs = m.collections.map((collec) =>
66-
query.promiseQuery(SQLCollecOne(collec), [id], false)
66+
promiseQuery(SQLCollecOne(collec), [id], false)
6767
);
68-
qCollecs.unshift(query.promiseQuery(sql, sqlParams, true));
68+
qCollecs.unshift(promiseQuery(sql, sqlParams, true));
6969
Promise.all(qCollecs)
7070
.then((data) => {
7171
if (data && data.length) {
@@ -86,10 +86,10 @@ function getOne(req, res) {
8686
res.json(err);
8787
});
8888
} else {
89-
query.runQuery(res, sql, sqlParams, true);
89+
runQuery(res, sql, sqlParams, true);
9090
}
9191
} else {
92-
errors.badRequest(res, 'Invalid model: "' + mid + '".');
92+
badRequest(res, 'Invalid model: "' + mid + '".');
9393
}
9494
}
9595

@@ -102,7 +102,7 @@ function insertOne(req, res) {
102102
logger.logReq("INSERT ONE", req);
103103
const m = getModel(req.params.entity);
104104
if (!m) {
105-
return errors.badRequest(res);
105+
return badRequest(res);
106106
} else {
107107
const pKey = m.pKey || "id",
108108
q = sqls.namedValues(m, req, "insert");
@@ -125,9 +125,9 @@ function insertOne(req, res) {
125125
sqls.select(m.fields, false, null, "C") +
126126
";";
127127

128-
query.runQuery(res, sql, q.values, true);
128+
runQuery(res, sql, q.values, true);
129129
} else {
130-
errors.badRequest(res);
130+
badRequest(res);
131131
}
132132
}
133133
}
@@ -171,9 +171,9 @@ function updateOne(req, res) {
171171
" as id, " +
172172
sqls.select(m.fields, false, null, "U") +
173173
";";
174-
query.runQuery(res, sql, q.values, true);
174+
runQuery(res, sql, q.values, true);
175175
} else {
176-
errors.badRequest(res);
176+
badRequest(res);
177177
}
178178
}
179179

@@ -206,13 +206,13 @@ function deleteX(req, res) {
206206
"::integer AS id";
207207
params = ids;
208208
} else {
209-
errors.badRequest(res);
209+
badRequest(res);
210210
return;
211211
}
212212
}
213-
query.runQuery(res, sql, params, ids.length === 1);
213+
runQuery(res, sql, params, ids.length === 1);
214214
} else {
215-
errors.badRequest(res);
215+
badRequest(res);
216216
}
217217
}
218218

@@ -236,9 +236,9 @@ function collecOne(req, res) {
236236

237237
if (m && collec) {
238238
const sqlParams = [parseInt(req.query.id, 10)];
239-
query.runQuery(res, SQLCollecOne(collec), sqlParams, false);
239+
runQuery(res, SQLCollecOne(collec), sqlParams, false);
240240
} else {
241-
errors.badRequest(res);
241+
badRequest(res);
242242
}
243243
}
244244

js/designer.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
* (c) 2023 Olivier Giulieri
77
*/
88

9-
import query from "./utils/query.js";
9+
import { db, promiseQuery } from "./utils/query.js";
1010
import logger from "./utils/logger.js";
11-
import errors from "./utils/errors.js";
11+
import { badRequest } from "./utils/errors.js";
1212
import config from "../config.js";
1313

14-
const db = query.db;
15-
1614
const schema = '"' + (config.schema || "evolutility") + '"';
1715
const camelProp = {
1816
nameplural: "namePlural",
@@ -243,9 +241,9 @@ function getModel(req, res) {
243241
qModel = dataM;
244242
const sqls = sqlModelCollecs(modelType, true, sqlWhere);
245243
return Promise.all([
246-
query.promiseQuery(sqls.fields, sqlParams, false),
247-
query.promiseQuery(sqls.groups, sqlParams, false),
248-
query.promiseQuery(sqls.collections, sqlParams, false),
244+
promiseQuery(sqls.fields, sqlParams, false),
245+
promiseQuery(sqls.groups, sqlParams, false),
246+
promiseQuery(sqls.collections, sqlParams, false),
249247
]);
250248
} else {
251249
return null;
@@ -267,12 +265,12 @@ function getModel(req, res) {
267265
}
268266
return res.json(qModel);
269267
} else {
270-
return errors.badRequest(res, "Invalid model ID.");
268+
return badRequest(res, "Invalid model ID.");
271269
}
272270
})
273271
.catch((error) => {
274272
console.log(error);
275-
return errors.badRequest(res, 'Error querying for model "' + id + '".');
273+
return badRequest(res, 'Error querying for model "' + id + '".');
276274
});
277275
}
278276

@@ -322,15 +320,15 @@ function getModels(req, res, callback) {
322320
const sqlGetFields = SQLmodelFields(modelType, true, sqlWhere);
323321
logger.logSQL(sqlGetFields);
324322
return Promise.all([
325-
query.promiseQuery(sqlGetFields, null, false),
326-
query.promiseQuery(
323+
promiseQuery(sqlGetFields, null, false),
324+
promiseQuery(
327325
'SELECT gid as "id", object_id, label, width, css, header, footer, fields FROM evolutility.evol_object_group WHERE ' +
328326
sqlWhere +
329327
sqlOrderBy,
330328
null,
331329
false
332330
),
333-
query.promiseQuery(
331+
promiseQuery(
334332
'SELECT cid as "id", object_id, label, dbcolumn as "column", fields FROM evolutility.evol_object_collec WHERE ' +
335333
sqlWhere +
336334
sqlOrderBy,
@@ -384,12 +382,12 @@ function getModels(req, res, callback) {
384382
});
385383
return res.json(qModel);
386384
} else {
387-
return errors.badRequest(res, "Invalid model ID.");
385+
return badRequest(res, "Invalid model ID.");
388386
}
389387
})
390388
.catch((error) => {
391389
console.log(error);
392-
return errors.badRequest(res, "Invalid model ID.");
390+
return badRequest(res, "Invalid model ID.");
393391
});
394392
}
395393

js/info.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,27 @@
66
*/
77

88
import path from "path";
9-
import { fieldInCharts, fieldTypes } from "./utils/dico.js";
9+
import { fieldInCharts, fieldTypes as ft } from "./utils/dico.js";
1010
import logger from "./utils/logger.js";
1111
import pkg from "../package.json" assert { type: "json" };
1212
import { models } from "./utils/model-manager.js";
1313
import config from "../config.js";
1414

15-
const ft = fieldTypes;
16-
1715
function getFieldsAPIs(model, protocol, baseUrl) {
18-
const apis = {
19-
charts: [],
20-
lovs: [],
21-
};
16+
const charts = [];
17+
const lovs = [];
2218
model.fields.forEach(function (f) {
2319
if (fieldInCharts(f)) {
24-
apis.charts.push(protocol + path.join(baseUrl, model.id, "chart", f.id));
20+
charts.push(protocol + path.join(baseUrl, model.id, "chart", f.id));
2521
}
2622
if (f.type === ft.lov) {
27-
apis.lovs.push(protocol + path.join(baseUrl, model.id, "lov", f.id));
23+
lovs.push(protocol + path.join(baseUrl, model.id, "lov", f.id));
2824
}
2925
});
30-
return apis;
26+
return {
27+
charts,
28+
lovs,
29+
};
3130
}
3231

3332
function baseURL(req) {
@@ -42,11 +41,13 @@ const entityAPIs = (model, protocol, baseUrl, fullDescription) => {
4241
id: model.id,
4342
title: model.title || model.label,
4443
list: pathToModel,
45-
lovs,
4644
charts,
47-
stats: pathToModel + "/stats",
4845
csv: pathToModel + "?format=csv",
46+
lovs,
4947
};
48+
if (!model.noStats) {
49+
mi.stats = pathToModel + "/stats";
50+
}
5051
if (fullDescription) {
5152
mi.crud = {
5253
create: {

js/list.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import dico, { fieldTypes as ft } from "./utils/dico.js";
1010
import { getModel } from "./utils/model-manager.js";
1111
import sqls from "./utils/sql-select.js";
1212
import { runQuery } from "./utils/query.js";
13-
import errors from "./utils/errors.js";
13+
import { badRequest } from "./utils/errors.js";
1414
import logger from "./utils/logger.js";
1515
import config from "../config.js";
1616

@@ -282,7 +282,7 @@ export function getMany(req, res) {
282282
isCSV ? csvHeader(m.fields) : null
283283
);
284284
} else {
285-
errors.badRequest(res, 'Invalid model: "' + mid + '".');
285+
badRequest(res, 'Invalid model: "' + mid + '".');
286286
}
287287
}
288288

js/lov.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { getModel } from "./utils/model-manager.js";
1010
import { runQuery } from "./utils/query.js";
11-
import errors from "./utils/errors.js";
11+
import { badRequest } from "./utils/errors.js";
1212
import logger from "./utils/logger.js";
1313
import config from "../config.js";
1414

@@ -20,7 +20,7 @@ const searchParam = (search) =>
2020

2121
// - returns list of possible values for a field (usually for dropdown)
2222
// - sample url: http://localhost:2000/api/v1/todo/lov/category
23-
function lovOne(req, res) {
23+
export function lovOne(req, res) {
2424
logger.logReq("LOV ONE", req);
2525
const mid = req.params.entity,
2626
m = getModel(mid),
@@ -49,7 +49,7 @@ function lovOne(req, res) {
4949
res.json(logger.errorMsg('Invalid field "' + fid + '".', "lovOne"));
5050
}
5151
} else {
52-
errors.badRequest(res);
52+
badRequest(res);
5353
}
5454
}
5555

js/routes.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
import express from "express";
99
import logger from "./utils/logger.js";
10-
import upload from "./utils/upload.js";
10+
import { uploadOne } from "./utils/upload.js";
1111
import config from "../config.js";
1212
import crud from "./crud.js";
1313
import list from "./list.js";
14-
import lov from "./lov.js";
14+
import { lovOne } from "./lov.js";
1515
import stats from "./stats.js";
1616
import charts from "./charts.js";
1717
import info from "./info.js";
@@ -68,14 +68,14 @@ router.post(apiPath + ":entity", crud.insertOne);
6868
// - UPDATE ONE -
6969
router.patch(apiPath + ":entity/:id", crud.updateOne);
7070
router.put(apiPath + ":entity/:id", crud.updateOne);
71-
router.post(apiPath + ":entity/upload/:id", upload.uploadOne);
71+
router.post(apiPath + ":entity/upload/:id", uploadOne);
7272
// - DELETE ONE -
7373
router.delete(apiPath + ":entity/:id", crud.deleteX);
7474
// - SUB-COLLECTIONS -
7575
router.get(apiPath + ":entity/collec/:collec", crud.getCollec);
7676

7777
// ====== LOV ====================================
78-
router.get(apiPath + ":entity/lov/:field", lov.lovOne);
78+
router.get(apiPath + ":entity/lov/:field", lovOne);
7979

8080
// ====== GET CHARTS ====================================
8181
router.get(apiPath + ":entity/chart/:field", charts.chartField);

js/utils/db-structure.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
*/
88

99
import { runQuery } from "./query.js";
10-
import errors from "./errors.js";
10+
import { badRequest } from "./errors.js";
1111
import logger from "./logger.js";
1212
import config from "../../config.js";
1313

1414
// - Get list of schema tables
1515
function getTables(req, res) {
1616
logger.logHeader("REST", "DB", "tables");
17-
var sql =
17+
const sql =
1818
"SELECT table_name as table, table_type as type" +
1919
", CASE WHEN is_insertable_into='YES' THEN false ELSE true END as \"readOnly\"" +
2020
" FROM information_schema.tables" +
@@ -24,10 +24,10 @@ function getTables(req, res) {
2424

2525
// - Get list of columns for the specified table
2626
function getColumns(req, res) {
27-
var table = req.params.table;
27+
const table = req.params.table;
2828
logger.logHeader("REST", "DB", table + " columns");
2929
if (table) {
30-
var sql =
30+
const sql =
3131
"SELECT column_name as column, data_type as type" +
3232
", CASE WHEN is_nullable='YES' THEN false ELSE true END as \"required\"" +
3333
" FROM information_schema.columns AS t1" +
@@ -38,7 +38,7 @@ function getColumns(req, res) {
3838
//sql = 'SELECT * FROM information_schema.constraint_column_usage '+
3939
//' WHERE table_name=$1 AND table_schema=$2';
4040
} else {
41-
errors.badRequest(res, "No table specified.");
41+
badRequest(res, "No table specified.");
4242
}
4343
}
4444

js/utils/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logger from "./logger.js";
22

3-
function badRequest(res, msg, errorCode = 400) {
3+
export function badRequest(res, msg, errorCode = 400) {
44
const errorMsg = msg || "Bad request";
55

66
logger.logError(msg);

js/utils/logger.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ function maskedConnection() {
5252
}
5353
return "N/A";
5454
}
55+
5556
const pubConnection = maskedConnection();
5657

5758
export default {
58-
ascii_art: asciiArt,
59-
6059
startupMessage() {
6160
if (consoleLog) {
6261
console.log(asciiArt);
@@ -150,11 +149,9 @@ export default {
150149
return green(msg);
151150
},
152151

153-
green: green,
152+
green,
154153

155-
logSuccess(msg) {
156-
green(msg);
157-
},
154+
logSuccess: green,
158155

159156
logError(err, moreInfo) {
160157
if (consoleLog) {

0 commit comments

Comments
 (0)