@opengis/fastify-table 2.5.15 → 2.5.17
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/policy/funcs/checkPolicy.js +1 -1
- package/dist/server/routes/crud/controllers/table.d.ts.map +1 -1
- package/dist/server/routes/crud/controllers/table.js +15 -10
- package/dist/server/routes/table/controllers/suggest.d.ts.map +1 -1
- package/dist/server/routes/table/controllers/suggest.js +44 -22
- package/package.json +1 -1
|
@@ -13,7 +13,7 @@ export default function checkPolicy(req, jwt) {
|
|
|
13
13
|
const { role, referer, policy = [], } = (routeOptions?.config || {});
|
|
14
14
|
// role=admin|regular
|
|
15
15
|
const isRole = (role && (!user?.user_type || !role?.includes?.(user?.user_type))) ||
|
|
16
|
-
(policy.includes("admin") && user?.user_type
|
|
16
|
+
(policy.includes("admin") && !user?.user_type?.includes?.("admin"));
|
|
17
17
|
const allowExtPublic = [".png", ".jpg", ".svg"];
|
|
18
18
|
const ext = path.toLowerCase().substr(-4);
|
|
19
19
|
const isPublic = Array.isArray(policy)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../../../../server/routes/crud/controllers/table.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAqB5C,wBAA8B,QAAQ,CACpC,GAAG,EAAE,GAAG,EACR,KAAK,EAAE,YAAY,EACnB,MAAM,EAAE,GAAG,
|
|
1
|
+
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../../../../server/routes/crud/controllers/table.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAqB5C,wBAA8B,QAAQ,CACpC,GAAG,EAAE,GAAG,EACR,KAAK,EAAE,YAAY,EACnB,MAAM,EAAE,GAAG,gBAoQZ"}
|
|
@@ -47,11 +47,11 @@ export default async function tableAPI(req, reply, called) {
|
|
|
47
47
|
const table = loadTable?.table || table1;
|
|
48
48
|
const form = loadTable?.form || form1;
|
|
49
49
|
const id = hookData?.id || tokenData?.id || params.id;
|
|
50
|
-
if (tokenData && !
|
|
50
|
+
if (tokenData && !table) {
|
|
51
51
|
throw ForbiddenError("invalid token");
|
|
52
52
|
}
|
|
53
|
-
if (!table
|
|
54
|
-
throw BadRequestError("not enough params: table
|
|
53
|
+
if (!table) {
|
|
54
|
+
throw BadRequestError("not enough params: table");
|
|
55
55
|
}
|
|
56
56
|
const { actions = [], query: accessQuery, message: accessMessage, } = (await getAccess({
|
|
57
57
|
resource,
|
|
@@ -135,10 +135,12 @@ export default async function tableAPI(req, reply, called) {
|
|
|
135
135
|
.join(";");
|
|
136
136
|
return `${q};${extraQ || ""}`.replace(/\$1/g, `'${id}'`);
|
|
137
137
|
}
|
|
138
|
-
const data =
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
138
|
+
const data = id
|
|
139
|
+
? await pg
|
|
140
|
+
.query(q.replace(/{{uid}}/, user?.uid), [id])
|
|
141
|
+
.then((el) => el.rows[0])
|
|
142
|
+
: null;
|
|
143
|
+
if (id && !data) {
|
|
142
144
|
throw NotFoundError(`object not found: ${id}`);
|
|
143
145
|
}
|
|
144
146
|
Object.keys(schema)
|
|
@@ -148,7 +150,7 @@ export default async function tableAPI(req, reply, called) {
|
|
|
148
150
|
data[key] = null;
|
|
149
151
|
}
|
|
150
152
|
});
|
|
151
|
-
if (extraKeys?.length) {
|
|
153
|
+
if (id && extraKeys?.length) {
|
|
152
154
|
await Promise.all(extraKeys?.map(async (key) => {
|
|
153
155
|
const { colModel, table: extraTable, parent_id: parentId, } = schema[key];
|
|
154
156
|
const colModel1 = Array.isArray(colModel)
|
|
@@ -167,7 +169,8 @@ export default async function tableAPI(req, reply, called) {
|
|
|
167
169
|
if (user?.uid &&
|
|
168
170
|
!config.security?.disableToken &&
|
|
169
171
|
actions.includes("edit") &&
|
|
170
|
-
!called
|
|
172
|
+
!called &&
|
|
173
|
+
id) {
|
|
171
174
|
data.token = tokenData?.table
|
|
172
175
|
? params.table
|
|
173
176
|
: setToken({
|
|
@@ -176,7 +179,9 @@ export default async function tableAPI(req, reply, called) {
|
|
|
176
179
|
array: 1,
|
|
177
180
|
})?.[0];
|
|
178
181
|
}
|
|
179
|
-
|
|
182
|
+
if (id) {
|
|
183
|
+
await extraDataGet({ rows: [data], table, form }, pg);
|
|
184
|
+
}
|
|
180
185
|
const res = await applyHook("afterTable", {
|
|
181
186
|
pg,
|
|
182
187
|
table,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"suggest.d.ts","sourceRoot":"","sources":["../../../../../server/routes/table/controllers/suggest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAwBpD,wBAAsB,kBAAkB,CAAC,EACvC,KAAK,EACL,QAAQ,EACR,MAAM,EACN,UAAU,EACV,QAAQ,EACR,UAAU,EACV,GAAG,EACH,EAAqB,GACtB,EAAE;IACD,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,GAAG,CAAC;IACZ,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,EAAE,CAAC,EAAE,UAAU,CAAC;CACjB;;;;;UAwFA;AAED,wBAA8B,OAAO,CAAC,GAAG,EAAE,GAAG,
|
|
1
|
+
{"version":3,"file":"suggest.d.ts","sourceRoot":"","sources":["../../../../../server/routes/table/controllers/suggest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAwBpD,wBAAsB,kBAAkB,CAAC,EACvC,KAAK,EACL,QAAQ,EACR,MAAM,EACN,UAAU,EACV,QAAQ,EACR,UAAU,EACV,GAAG,EACH,EAAqB,GACtB,EAAE;IACD,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,GAAG,CAAC;IACZ,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,EAAE,CAAC,EAAE,UAAU,CAAC;CACjB;;;;;UAwFA;AAED,wBAA8B,OAAO,CAAC,GAAG,EAAE,GAAG,gBAuY7C"}
|
|
@@ -141,9 +141,6 @@ export default async function suggest(req) {
|
|
|
141
141
|
(metaColumns || []).find((col) => col?.name === column) ||
|
|
142
142
|
{};
|
|
143
143
|
const { dataTypeID } = (metaColumns || []).find((col) => col?.name === column) || {};
|
|
144
|
-
// const filters =
|
|
145
|
-
// hookBody?.filters ||
|
|
146
|
-
// (body ? await getFilter({ pg, table }).then((el) => el.list) : null);
|
|
147
144
|
if (table && (!column || !columnName)) {
|
|
148
145
|
throw BadRequestError("param name is invalid: 2");
|
|
149
146
|
}
|
|
@@ -239,32 +236,57 @@ export default async function suggest(req) {
|
|
|
239
236
|
data,
|
|
240
237
|
};
|
|
241
238
|
}
|
|
239
|
+
const args = [];
|
|
240
|
+
const where = [];
|
|
242
241
|
// select, with search by text support
|
|
243
|
-
|
|
242
|
+
if (query.key && typeof query.key === "string") {
|
|
243
|
+
args.push(query.key.toLowerCase());
|
|
244
|
+
where.push(searchQuery);
|
|
245
|
+
}
|
|
246
|
+
// pk
|
|
247
|
+
if (meta.pk) {
|
|
248
|
+
where.push(`${meta.pk} is not null`);
|
|
249
|
+
}
|
|
244
250
|
// filter by value
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
251
|
+
if (meta.pk && typeof query.val === "string") {
|
|
252
|
+
args.push(query.val.split(","));
|
|
253
|
+
where.push(`${meta.pk}=any($${args.length})`);
|
|
254
|
+
}
|
|
255
|
+
// filter
|
|
256
|
+
if (tableName && pg.pk?.[tableName] && columnName && dataTypeID) {
|
|
257
|
+
where.push(`id in (select ${pg.pgType[dataTypeID]?.includes("[]")
|
|
250
258
|
? `unnest(${columnName})`
|
|
251
|
-
: columnName} from ${tableName.replace(/'/g, "''")})`
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
259
|
+
: columnName} from ${tableName.replace(/'/g, "''")})`);
|
|
260
|
+
}
|
|
261
|
+
// order by value if not specified explicitly
|
|
262
|
+
const order = meta.original.toLowerCase().includes("order by")
|
|
263
|
+
? ""
|
|
264
|
+
: "order by 2";
|
|
265
|
+
const whereQuery = where.filter(Boolean).join(" and ") || "true";
|
|
266
|
+
args.push(Math.min(query.limit || meta.limit || limit, limit));
|
|
267
|
+
let sqlSuggest = tableName && column
|
|
257
268
|
? `${meta.original}
|
|
258
|
-
WHERE EXISTS ( SELECT 1 FROM ${tableName} o
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
269
|
+
WHERE EXISTS ( SELECT 1 FROM ${tableName} o
|
|
270
|
+
WHERE o.${column} IS NOT DISTINCT FROM c.id ) and ${whereQuery || "true"}
|
|
271
|
+
${order} LIMIT $${args.length}::int`
|
|
272
|
+
: `with c(id,text) as ( ${meta.original} where ${whereQuery} ${order}) select * from c LIMIT $${args.length}::int`;
|
|
273
|
+
if (sqlSuggest.includes("{{parent}}")) {
|
|
274
|
+
args.push(parent || "");
|
|
275
|
+
sqlSuggest = sqlSuggest.replace(/(['"]?){{parent}}\1/gi, `$${args.length}`);
|
|
276
|
+
}
|
|
277
|
+
if (sqlSuggest.includes("{{uid}}")) {
|
|
278
|
+
args.push(user?.uid || "0");
|
|
279
|
+
sqlSuggest = sqlSuggest.replace(/(['"]?){{uid}}\1/gi, `$${args.length}`);
|
|
280
|
+
}
|
|
264
281
|
if (query.sql && debugMode) {
|
|
265
282
|
return sqlSuggest;
|
|
266
283
|
}
|
|
267
|
-
|
|
284
|
+
if (config.trace) {
|
|
285
|
+
console.log("suggest", sqlSuggest, where, args);
|
|
286
|
+
}
|
|
287
|
+
const dataNew = await pg
|
|
288
|
+
.query(sqlSuggest, args)
|
|
289
|
+
.then((r) => r.rows || []);
|
|
268
290
|
// in case id / text = Boolean
|
|
269
291
|
const data = dataNew.filter((el) => Object.hasOwn(el, "id") && Object.hasOwn(el, "text"));
|
|
270
292
|
data
|