@opengis/fastify-table 2.0.55 → 2.0.57
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.
- package/dist/server/plugins/auth/index.d.ts +3 -1
- package/dist/server/plugins/auth/index.d.ts.map +1 -1
- package/dist/server/plugins/auth/index.js +86 -77
- package/dist/server/routes/file/controllers/export.d.ts.map +1 -1
- package/dist/server/routes/file/controllers/export.js +11 -6
- package/dist/server/routes/table/functions/getData.d.ts.map +1 -1
- package/dist/server/routes/table/functions/getData.js +9 -6
- package/package.json +1 -1
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { FastifyInstance } from "fastify";
|
|
1
|
+
import { FastifyInstance, FastifyReply } from "fastify";
|
|
2
|
+
import { ExtendedRequest } from "../../types/core.js";
|
|
3
|
+
export declare function onRequest(req: ExtendedRequest, reply: FastifyReply): Promise<null>;
|
|
2
4
|
declare function plugin(fastify: FastifyInstance): void;
|
|
3
5
|
export default plugin;
|
|
4
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../server/plugins/auth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../server/plugins/auth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAYxD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAMtD,wBAAsB,SAAS,CAAC,GAAG,EAAE,eAAe,EAAE,KAAK,EAAE,YAAY,iBA6GxE;AAED,iBAAS,MAAM,CAAC,OAAO,EAAE,eAAe,QA6BvC;AAED,eAAe,MAAM,CAAC"}
|
|
@@ -7,6 +7,91 @@ import config from "../../../config.js";
|
|
|
7
7
|
import getRedis from "../redis/funcs/getRedis.js";
|
|
8
8
|
const fastifyPassport = new Authenticator();
|
|
9
9
|
const { prefix = "/api" } = config;
|
|
10
|
+
export async function onRequest(req, reply) {
|
|
11
|
+
const { hostname, headers, routeOptions } = req;
|
|
12
|
+
const { policy = [] } = routeOptions?.config || {};
|
|
13
|
+
// proxy from old apps to editor, bi etc.
|
|
14
|
+
const validToken = (req.ip === "193.239.152.181" ||
|
|
15
|
+
req.ip === "127.0.0.1" ||
|
|
16
|
+
req.ip?.startsWith?.("192.168.") ||
|
|
17
|
+
config.debug) &&
|
|
18
|
+
req.headers?.token &&
|
|
19
|
+
config.auth?.tokens?.includes?.(headers.token);
|
|
20
|
+
if (validToken && !req?.user?.uid) {
|
|
21
|
+
req.user = {
|
|
22
|
+
uid: req.headers?.uid?.toString?.(),
|
|
23
|
+
user_type: req.headers?.user_type?.toString?.() || "regular",
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const isAdmin = process.env.NODE_ENV === "admin" ||
|
|
27
|
+
hostname?.split?.(":")?.shift?.() === config.adminDomain ||
|
|
28
|
+
config.admin ||
|
|
29
|
+
hostname?.startsWith?.("admin");
|
|
30
|
+
const isPublic = Array.isArray(policy)
|
|
31
|
+
? policy.includes("public")
|
|
32
|
+
: policy === "L0";
|
|
33
|
+
if (!req.session?.passport?.user?.uid &&
|
|
34
|
+
(config.auth?.disable || config.auth?.user) &&
|
|
35
|
+
!isPublic) {
|
|
36
|
+
req.session = req.session || {};
|
|
37
|
+
req.session.passport = req.session.passport || {}; // ensure passport session exists
|
|
38
|
+
req.session.passport.user = {
|
|
39
|
+
...(config.auth?.user || {}),
|
|
40
|
+
uid: config.auth?.user?.uid?.toString?.() || "1",
|
|
41
|
+
user_rnokpp: config.auth?.user?.rnokpp,
|
|
42
|
+
user_type: config.auth?.user?.type || "regular",
|
|
43
|
+
};
|
|
44
|
+
req.user = req.session.passport.user;
|
|
45
|
+
}
|
|
46
|
+
// ! intentional: null || undefined > undefined
|
|
47
|
+
req.user = req.user || req.session?.passport?.user || undefined; // fix for user.uid errors, by default user is null, while with express passport it was {}, unauthorized user does not trigger serializer
|
|
48
|
+
// currently 2factor + auth with passwd file not supported
|
|
49
|
+
const ispasswd = (existsSync("passwd") && !config.auth?.["2factor"]) || config.auth?.passwd;
|
|
50
|
+
const loginPageUrl = config.auth?.link?.core?.login || config?.auth?.redirect || "/login";
|
|
51
|
+
if (!req.user?.uid &&
|
|
52
|
+
!config.auth?.disable &&
|
|
53
|
+
isAdmin &&
|
|
54
|
+
!isPublic &&
|
|
55
|
+
!config.auth?.disableRedirect &&
|
|
56
|
+
!req.url.startsWith(prefix) &&
|
|
57
|
+
!req.url.startsWith("/api") &&
|
|
58
|
+
!req.url.startsWith(loginPageUrl) &&
|
|
59
|
+
!req.url.includes(".") &&
|
|
60
|
+
!req.url.includes("@")) {
|
|
61
|
+
return reply.redirect(`${loginPageUrl}` + `?redirect=${req.url}`);
|
|
62
|
+
}
|
|
63
|
+
// by default, disable 2factor for id.gov.ua auth
|
|
64
|
+
const check = req.user?.auth_type === "govid" ? config.auth?.["2factor"]?.govid : true;
|
|
65
|
+
const login2faPage = config.auth?.link?.["2fa"]?.login || "/2factor";
|
|
66
|
+
// example: 2factor for admin env only, while public env does not require it
|
|
67
|
+
const checkEnv = () => {
|
|
68
|
+
if (!config.auth?.["2factorEnv"])
|
|
69
|
+
return true;
|
|
70
|
+
if ((config.auth?.["2factorEnv"] &&
|
|
71
|
+
process.env.NODE_ENV === config.auth?.["2factorEnv"]) ||
|
|
72
|
+
(config.auth?.["2factorEnv"] === "admin" && isAdmin)) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
};
|
|
77
|
+
// if 2factor is enabled globally + for user and secondFactorPassed not true => redirect to 2factor login page
|
|
78
|
+
if (req.user?.uid &&
|
|
79
|
+
req.user?.twofa &&
|
|
80
|
+
config.auth?.["2factor"] &&
|
|
81
|
+
!isPublic &&
|
|
82
|
+
(routeOptions?.method || "GET") === "GET" &&
|
|
83
|
+
!req.session?.secondFactorPassed &&
|
|
84
|
+
!ispasswd &&
|
|
85
|
+
!config.auth?.disableRedirect &&
|
|
86
|
+
!config.auth?.disable &&
|
|
87
|
+
check &&
|
|
88
|
+
checkEnv()) {
|
|
89
|
+
if (!req.url.startsWith(login2faPage)) {
|
|
90
|
+
return reply.redirect(login2faPage);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
10
95
|
function plugin(fastify) {
|
|
11
96
|
if (!config.redis) {
|
|
12
97
|
return;
|
|
@@ -27,82 +112,6 @@ function plugin(fastify) {
|
|
|
27
112
|
fastifyPassport.registerUserSerializer(async (user) => ({ user }));
|
|
28
113
|
// deserialize user used to add user info from session store to req
|
|
29
114
|
fastifyPassport.registerUserDeserializer(async (passport) => passport?.user || passport);
|
|
30
|
-
fastify.addHook("onRequest",
|
|
31
|
-
const { pg, hostname, headers, routeOptions } = req;
|
|
32
|
-
const { policy = [] } = routeOptions?.config || {};
|
|
33
|
-
// proxy from old apps to editor, bi etc.
|
|
34
|
-
const validToken = (req.ip === "193.239.152.181" ||
|
|
35
|
-
req.ip === "127.0.0.1" ||
|
|
36
|
-
req.ip?.startsWith?.("192.168.") ||
|
|
37
|
-
config.debug) &&
|
|
38
|
-
req.headers?.token &&
|
|
39
|
-
config.auth?.tokens?.includes?.(headers.token);
|
|
40
|
-
if (validToken && !req?.user?.uid) {
|
|
41
|
-
req.user = {
|
|
42
|
-
uid: req.headers?.uid?.toString?.(),
|
|
43
|
-
user_type: req.headers?.user_type?.toString?.() || "regular",
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
const isAdmin = process.env.NODE_ENV === "admin" ||
|
|
47
|
-
hostname?.split?.(":")?.shift?.() === config.adminDomain ||
|
|
48
|
-
config.admin ||
|
|
49
|
-
hostname?.startsWith?.("admin");
|
|
50
|
-
const isPublic = Array.isArray(policy)
|
|
51
|
-
? policy.includes("public")
|
|
52
|
-
: policy === "L0";
|
|
53
|
-
// if 2factor is enabled globally + for user and secondFactorPassed not true => redirect to 2factor login page
|
|
54
|
-
const { secondFactorPassed, passport = {} } = req.session || {}; // base login +
|
|
55
|
-
if (!passport.user?.uid &&
|
|
56
|
-
(config.auth?.disable || config.auth?.user) &&
|
|
57
|
-
!isPublic) {
|
|
58
|
-
req.session = req.session || {};
|
|
59
|
-
req.session.passport = req.session.passport || {}; // ensure passport session exists
|
|
60
|
-
req.session.passport.user = {
|
|
61
|
-
...(config.auth?.user || {}),
|
|
62
|
-
uid: config.auth?.user?.uid?.toString?.() || "1",
|
|
63
|
-
user_rnokpp: config.auth?.user?.rnokpp,
|
|
64
|
-
user_type: config.auth?.user?.type || "regular",
|
|
65
|
-
};
|
|
66
|
-
req.user = req.session.passport.user;
|
|
67
|
-
}
|
|
68
|
-
// ! intentional: null || undefined > undefined
|
|
69
|
-
req.user = req.user || req.session?.passport?.user || undefined; // fix for user.uid errors, by default user is null, while with express passport it was {}, unauthorized user does not trigger serializer
|
|
70
|
-
if (config.trace && false) {
|
|
71
|
-
console.log("req.user?.uid", req.user?.uid, "req.session?.passport?.user?.uid", req.session?.passport?.user?.uid, "config.auth", config.auth);
|
|
72
|
-
}
|
|
73
|
-
// currently 2factor + auth with passwd file not supported
|
|
74
|
-
const ispasswd = (existsSync("passwd") && !config.auth?.["2factor"]) ||
|
|
75
|
-
config.auth?.passwd;
|
|
76
|
-
if (!passport.user?.uid &&
|
|
77
|
-
!config.auth?.disable &&
|
|
78
|
-
isAdmin &&
|
|
79
|
-
!isPublic &&
|
|
80
|
-
!config.auth?.disableRedirect &&
|
|
81
|
-
!req.url.startsWith(prefix) &&
|
|
82
|
-
!req.url.startsWith("/api") &&
|
|
83
|
-
!req.url.startsWith("/login") &&
|
|
84
|
-
!req.url.includes('.') &&
|
|
85
|
-
!req.url.includes('@')) {
|
|
86
|
-
return reply.redirect(`${config?.auth?.redirect || "/login"}` + `?redirect=${req.url}`);
|
|
87
|
-
}
|
|
88
|
-
// by default, disable 2factor for id.gov.ua auth
|
|
89
|
-
const check = passport.user?.auth_type === "govid"
|
|
90
|
-
? config.auth?.["2factor"]?.govid
|
|
91
|
-
: true;
|
|
92
|
-
if (passport.user?.uid &&
|
|
93
|
-
passport.user?.twofa &&
|
|
94
|
-
config.auth?.["2factor"] &&
|
|
95
|
-
!isPublic &&
|
|
96
|
-
(routeOptions?.method || "GET") === "GET" &&
|
|
97
|
-
!secondFactorPassed &&
|
|
98
|
-
!ispasswd &&
|
|
99
|
-
check) {
|
|
100
|
-
const href = config.auth?.["2factorRedirect"] || "/2factor";
|
|
101
|
-
if (!href.includes(req.url)) {
|
|
102
|
-
return reply.redirect(href);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return null;
|
|
106
|
-
});
|
|
115
|
+
fastify.addHook("onRequest", onRequest);
|
|
107
116
|
}
|
|
108
117
|
export default plugin;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"export.d.ts","sourceRoot":"","sources":["../../../../../server/routes/file/controllers/export.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AA0B5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAKzD;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,wBAA8B,WAAW,CACvC,EACE,EAAqB,EACrB,IAAI,EACJ,QAAQ,EACR,OAAO,EAAE,QAAQ,EACjB,GAAG,EACH,KAAU,EACV,IAAkB,EAClB,QAAQ,EACR,UAAU,GACX,EAAE;IACD,EAAE,EAAE,UAAU,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,GAAG,CAAC,EAAE,GAAG,CAAC;IACV,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,EACD,KAAK,EAAE,YAAY,
|
|
1
|
+
{"version":3,"file":"export.d.ts","sourceRoot":"","sources":["../../../../../server/routes/file/controllers/export.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AA0B5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAKzD;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,wBAA8B,WAAW,CACvC,EACE,EAAqB,EACrB,IAAI,EACJ,QAAQ,EACR,OAAO,EAAE,QAAQ,EACjB,GAAG,EACH,KAAU,EACV,IAAkB,EAClB,QAAQ,EACR,UAAU,GACX,EAAE;IACD,EAAE,EAAE,UAAU,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,GAAG,CAAC,EAAE,GAAG,CAAC;IACV,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,EACD,KAAK,EAAE,YAAY,gBAgWpB"}
|
|
@@ -51,7 +51,7 @@ export default async function exportTable({ pg = pgClients.client, user, unittes
|
|
|
51
51
|
return reply.status(400).send("param format is invalid");
|
|
52
52
|
}
|
|
53
53
|
const date = new Date();
|
|
54
|
-
const sufixName = `${filter}-${cols || "all"}-${search}-${query.limit || "unlimited"}`;
|
|
54
|
+
const sufixName = `${filter}-${cols || "all"}-${search}-${query.limit || "unlimited"}-${id}`;
|
|
55
55
|
const sufixDate = [
|
|
56
56
|
date.getFullYear(),
|
|
57
57
|
date.getMonth(),
|
|
@@ -91,7 +91,8 @@ export default async function exportTable({ pg = pgClients.client, user, unittes
|
|
|
91
91
|
}
|
|
92
92
|
const loadTable = await getTemplate("table", table);
|
|
93
93
|
const meta = await getMeta({ pg, table: loadTable?.table || table });
|
|
94
|
-
|
|
94
|
+
const viewSql = await getTemplate("view", loadTable?.table || table);
|
|
95
|
+
if (!meta?.pk && !meta?.view && !tableSql && !viewSql) {
|
|
95
96
|
return reply.status(404).send("table not found");
|
|
96
97
|
}
|
|
97
98
|
if (format === "geojson" && !meta?.geom) {
|
|
@@ -110,9 +111,11 @@ export default async function exportTable({ pg = pgClients.client, user, unittes
|
|
|
110
111
|
sufix: false,
|
|
111
112
|
};
|
|
112
113
|
// check total count, debug sql etc.
|
|
113
|
-
const result = tableSql
|
|
114
|
+
const result = tableSql || viewSql
|
|
114
115
|
? await pg
|
|
115
|
-
.query(`select count(*) as total, json_agg(row_to_json(q)) as rows from (${tableSql})q
|
|
116
|
+
.query(`select count(*) as total, json_agg(row_to_json(q)) as rows from (${tableSql || viewSql})q where ${loadTable?.key && id
|
|
117
|
+
? `${loadTable?.key}::text = '${id}'`
|
|
118
|
+
: "true"}`)
|
|
116
119
|
.then((el) => el.rows?.[0] || {})
|
|
117
120
|
: await getData(options, reply, true);
|
|
118
121
|
if (sql)
|
|
@@ -174,8 +177,10 @@ export default async function exportTable({ pg = pgClients.client, user, unittes
|
|
|
174
177
|
while (+filtered - offset > 0 && !res?.error) {
|
|
175
178
|
try {
|
|
176
179
|
send(`Оброблено: ${offset}/${filtered}`);
|
|
177
|
-
const { rows = [] } = tableSql
|
|
178
|
-
? await pg.query(`select * from (${tableSql})q
|
|
180
|
+
const { rows = [] } = tableSql || viewSql
|
|
181
|
+
? await pg.query(`select * from (${tableSql || viewSql})q where ${loadTable?.key && id
|
|
182
|
+
? `${loadTable?.key}::text = '${id}'`
|
|
183
|
+
: "true"} limit ${options.limit} offset ${offset}`)
|
|
179
184
|
: await getData({ ...options, page }, reply, true);
|
|
180
185
|
send(`seq: ${++seq}`);
|
|
181
186
|
send(`Обробка ${rows.length} об'єктів...`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getData.d.ts","sourceRoot":"","sources":["../../../../../server/routes/table/functions/getData.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AA4DzD,wBAA8B,OAAO,CACnC,EACE,EAAqB,EACrB,MAAM,EACN,KAAK,EACL,EAAE,EACF,OAAY,EACZ,KAAU,EACV,IAAS,EACT,YAAY,EACZ,KAAY,EACZ,UAAU,EACV,OAAO,EAAE,YAAY,GACtB,EAAE;IACD,EAAE,EAAE,UAAU,CAAC;IACf,MAAM,CAAC,EAAE;QAAE,EAAE,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IACxD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,GAAG,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB,EACD,KAAK,EAAE,YAAY,EACnB,MAAM,CAAC,EAAE,GAAG,
|
|
1
|
+
{"version":3,"file":"getData.d.ts","sourceRoot":"","sources":["../../../../../server/routes/table/functions/getData.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AA4DzD,wBAA8B,OAAO,CACnC,EACE,EAAqB,EACrB,MAAM,EACN,KAAK,EACL,EAAE,EACF,OAAY,EACZ,KAAU,EACV,IAAS,EACT,YAAY,EACZ,KAAY,EACZ,UAAU,EACV,OAAO,EAAE,YAAY,GACtB,EAAE;IACD,EAAE,EAAE,UAAU,CAAC;IACf,MAAM,CAAC,EAAE;QAAE,EAAE,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IACxD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,GAAG,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB,EACD,KAAK,EAAE,YAAY,EACnB,MAAM,CAAC,EAAE,GAAG,gBAwzBb"}
|
|
@@ -137,13 +137,16 @@ export default async function dataAPI({ pg = pgClients.client, params, table, id
|
|
|
137
137
|
user,
|
|
138
138
|
}, null)) || {};
|
|
139
139
|
const tableMeta = await getMeta({ pg, table: table1 });
|
|
140
|
+
const viewSql = await getTemplate("view", table1);
|
|
140
141
|
timeArr.push(Date.now());
|
|
141
142
|
if (tableMeta?.view) {
|
|
142
143
|
if (!loadTable?.key && !tokenData?.key)
|
|
143
144
|
return { message: `key not found: ${table1}`, status: 404 };
|
|
144
145
|
Object.assign(tableMeta, { pk: loadTable?.key || tokenData?.key });
|
|
145
146
|
}
|
|
146
|
-
const { pk, columns: dbColumns = [] } =
|
|
147
|
+
const { pk, columns: dbColumns = [] } = (viewSql
|
|
148
|
+
? { pk: loadTable?.key, columns: loadTable?.columns }
|
|
149
|
+
: tableMeta) || {};
|
|
147
150
|
const columns1 = columns ||
|
|
148
151
|
dbColumns.map(({ name, title, dataTypeID }) => ({
|
|
149
152
|
name,
|
|
@@ -176,9 +179,9 @@ export default async function dataAPI({ pg = pgClients.client, params, table, id
|
|
|
176
179
|
?.filter?.((el) => el.inline)
|
|
177
180
|
?.map((el) => `,(${el.sql})`)
|
|
178
181
|
?.join("") || "";
|
|
179
|
-
const { fields = [] } =
|
|
180
|
-
? await pg.
|
|
181
|
-
: {};
|
|
182
|
+
const { fields = [] } = !viewSql
|
|
183
|
+
? await pg.query(`select * ${sqlInline} from ${table1} t ${sqlTable} ${cardSqlTable} limit 0`)
|
|
184
|
+
: await pg.query(`select * from (${viewSql})q limit 0`);
|
|
182
185
|
const dbColumnsTable = fields.map((el) => el.name);
|
|
183
186
|
const cols = columns
|
|
184
187
|
.filter((el) => el.name !== "geom" && dbColumnsTable.includes(el.name))
|
|
@@ -295,7 +298,7 @@ export default async function dataAPI({ pg = pgClients.client, params, table, id
|
|
|
295
298
|
from (select * ${sql
|
|
296
299
|
?.filter((el) => el.inline)
|
|
297
300
|
.map((el) => `,(${el.sql})`)
|
|
298
|
-
.join("") || ""} from ${table1} t ${sqlTable} ) t
|
|
301
|
+
.join("") || ""} from ${viewSql ? `(${viewSql})` : table1} t ${sqlTable} ) t
|
|
299
302
|
|
|
300
303
|
${objectId ? cardSqlTable : ""}
|
|
301
304
|
where ${where.join(" and ") || "true"}
|
|
@@ -373,7 +376,7 @@ export default async function dataAPI({ pg = pgClients.client, params, table, id
|
|
|
373
376
|
from (select * ${sql
|
|
374
377
|
?.filter((el) => el.inline)
|
|
375
378
|
.map((el) => `,(${el.sql})`)
|
|
376
|
-
.join("") || ""} from ${table1} t ${sqlTable})q
|
|
379
|
+
.join("") || ""} from ${viewSql ? `(${viewSql})` : table1} t ${sqlTable})q
|
|
377
380
|
where ${[loadTable?.query, tokenData?.query, accessQuery, contextQuery]
|
|
378
381
|
.filter(Boolean)
|
|
379
382
|
.filter((el) => checkQuery(el))
|