@opengis/fastify-table 1.4.48 → 1.4.49
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/index.js +11 -1
- package/package.json +3 -1
- package/server/plugins/logger/index.js +7 -0
- package/server/plugins/sqlite/funcs/getSqlite.js +32 -0
- package/server/plugins/sqlite/funcs/init.js +51 -0
- package/server/plugins/sqlite/index.js +11 -0
- package/server/plugins/sqlite/sqliteClients.js +19 -0
- package/server/plugins/util/funcs/unflattenObject.js +4 -1
- package/server/routes/logger/index.js +1 -1
- package/server/routes/table/index.js +10 -1
- package/utils.js +8 -0
package/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
3
|
-
import fp from 'fastify-plugin';
|
|
4
3
|
import { fileURLToPath } from 'node:url';
|
|
5
4
|
|
|
5
|
+
import fp from 'fastify-plugin';
|
|
6
|
+
|
|
6
7
|
import config from './config.js';
|
|
7
8
|
|
|
8
9
|
const { maxFileSize = 512 } = config;
|
|
@@ -15,6 +16,7 @@ import cronPlugin from './server/plugins/cron/index.js';
|
|
|
15
16
|
import crudPlugin from './server/plugins/crud/index.js';
|
|
16
17
|
|
|
17
18
|
import pgPlugin from './server/plugins/pg/index.js';
|
|
19
|
+
import sqlitePlugin from './server/plugins/sqlite/index.js';
|
|
18
20
|
import policyPlugin from './server/plugins/policy/index.js';
|
|
19
21
|
import metricPlugin from './server/plugins/metric/index.js';
|
|
20
22
|
import redisPlugin from './server/plugins/redis/index.js';
|
|
@@ -95,12 +97,20 @@ async function plugin(fastify, opt) {
|
|
|
95
97
|
redisPlugin(fastify);
|
|
96
98
|
// helperPlugin(fastify);
|
|
97
99
|
await pgPlugin(fastify, opt);
|
|
100
|
+
await sqlitePlugin(fastify, opt);
|
|
98
101
|
tablePlugin(fastify, opt);
|
|
99
102
|
crudPlugin(fastify, opt);
|
|
100
103
|
utilPlugin(fastify, opt);
|
|
101
104
|
cronPlugin(fastify, opt);
|
|
102
105
|
loggerPlugin(fastify, opt);
|
|
103
106
|
|
|
107
|
+
await fastify.register(import('@fastify/rate-limit'), {
|
|
108
|
+
max: 100,
|
|
109
|
+
timeWindow: '1 minute',
|
|
110
|
+
global: true,
|
|
111
|
+
keyGenerator: (req) => `${req.ip}-${req.raw.url.split('?')[0]}`,
|
|
112
|
+
});
|
|
113
|
+
|
|
104
114
|
if (config.dblist) {
|
|
105
115
|
dblistRoutes(fastify, opt);
|
|
106
116
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opengis/fastify-table",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.49",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "core-plugins",
|
|
6
6
|
"keywords": [
|
|
@@ -34,8 +34,10 @@
|
|
|
34
34
|
"@aws-sdk/client-s3": "3.554.0",
|
|
35
35
|
"@fastify/http-proxy": "11.1.2",
|
|
36
36
|
"@fastify/multipart": "9.0.3",
|
|
37
|
+
"@fastify/rate-limit": "10.3.0",
|
|
37
38
|
"@grpc/grpc-js": "1.10.6",
|
|
38
39
|
"@grpc/proto-loader": "0.7.12",
|
|
40
|
+
"better-sqlite3": "12.2.0",
|
|
39
41
|
"dotenv": "16.5.0",
|
|
40
42
|
"fastify": "5.3.3",
|
|
41
43
|
"fastify-plugin": "5.0.1",
|
|
@@ -3,6 +3,12 @@ import logger from './getLogger.js';
|
|
|
3
3
|
import pgClients from '../pg/pgClients.js';
|
|
4
4
|
|
|
5
5
|
async function plugin(fastify) {
|
|
6
|
+
fastify.addHook('onResponse', async (req, reply) => {
|
|
7
|
+
if (reply?.statusCode && reply.statusCode >= 400) {
|
|
8
|
+
logger.metrics(`error-count:${reply.statusCode}`);
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
|
|
6
12
|
fastify.setErrorHandler(async (error, request, reply) => {
|
|
7
13
|
// validation not error
|
|
8
14
|
if (error.validation) {
|
|
@@ -19,6 +25,7 @@ async function plugin(fastify) {
|
|
|
19
25
|
|
|
20
26
|
return reply.status(error.statusCode || 500).send(msg);
|
|
21
27
|
});
|
|
28
|
+
|
|
22
29
|
fastify.addHook('onListen', async () => {
|
|
23
30
|
logger.file('init', { db: pgClients.client?.options?.database });
|
|
24
31
|
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import Database from 'better-sqlite3';
|
|
2
|
+
|
|
3
|
+
import config from '../../../../config.js';
|
|
4
|
+
import sqliteClients from '../sqliteClients.js';
|
|
5
|
+
import init from './init.js';
|
|
6
|
+
|
|
7
|
+
function getSqliteAsync({
|
|
8
|
+
name,
|
|
9
|
+
readonly = false,
|
|
10
|
+
fileMustExist = false,
|
|
11
|
+
statement_timeout: timeout = 10000,
|
|
12
|
+
} = {}) {
|
|
13
|
+
if (!config.sqlite) return null;
|
|
14
|
+
|
|
15
|
+
if (sqliteClients.client?.tlist) {
|
|
16
|
+
return sqliteClients.client;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const dbConfig = {
|
|
20
|
+
readonly,
|
|
21
|
+
fileMustExist,
|
|
22
|
+
timeout,
|
|
23
|
+
verbose: config.trace ? console.log : undefined,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
sqliteClients.client = new Database(name || ':memory:', dbConfig);
|
|
27
|
+
init(sqliteClients.client);
|
|
28
|
+
|
|
29
|
+
return sqliteClients.client;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export default getSqliteAsync;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
function init(client) {
|
|
2
|
+
if (!client) { return; }
|
|
3
|
+
|
|
4
|
+
const rows = client.prepare(`WITH tables AS (
|
|
5
|
+
SELECT name AS table_name
|
|
6
|
+
FROM sqlite_master
|
|
7
|
+
WHERE type = 'table' AND name NOT LIKE 'sqlite_%'
|
|
8
|
+
),
|
|
9
|
+
pk_columns AS (
|
|
10
|
+
SELECT
|
|
11
|
+
m.name AS table_name,
|
|
12
|
+
ti.name AS column_name,
|
|
13
|
+
ti.pk
|
|
14
|
+
FROM sqlite_master m
|
|
15
|
+
JOIN pragma_table_info(m.name) AS ti
|
|
16
|
+
WHERE ti.pk = 1
|
|
17
|
+
)
|
|
18
|
+
SELECT table_name, column_name
|
|
19
|
+
FROM pk_columns
|
|
20
|
+
WHERE table_name IN (
|
|
21
|
+
SELECT table_name
|
|
22
|
+
FROM pk_columns
|
|
23
|
+
GROUP BY table_name
|
|
24
|
+
HAVING COUNT(*) = 1
|
|
25
|
+
)
|
|
26
|
+
ORDER BY table_name;`).all();
|
|
27
|
+
|
|
28
|
+
const pk = Object.fromEntries(
|
|
29
|
+
rows.map(row => [row.table_name, row.column_name]),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const tlist = client.prepare('SELECT name FROM sqlite_master WHERE type=\'table\' AND name NOT LIKE \'sqlite_%\';').all().reduce((acc, curr) => {
|
|
33
|
+
acc.push(curr.name);
|
|
34
|
+
return acc;
|
|
35
|
+
}, []);
|
|
36
|
+
|
|
37
|
+
async function query(q, args = []) {
|
|
38
|
+
const data = client.prepare(q.replace(/\$\d+/g, '?')).all(...args);
|
|
39
|
+
return Promise.resolve({ rows: data, rowCount: data.length });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
Object.assign(client, {
|
|
43
|
+
query,
|
|
44
|
+
pk,
|
|
45
|
+
tlist,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
console.log('db connected', client.name || ':memory:');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export default init;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import fp from 'fastify-plugin';
|
|
2
|
+
|
|
3
|
+
import sqliteClients from './sqliteClients.js';
|
|
4
|
+
|
|
5
|
+
async function dbPlugin(app, opts) {
|
|
6
|
+
app.addHook('onClose', async () => {
|
|
7
|
+
Object.keys(sqliteClients).forEach(key => sqliteClients[key].close());
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default fp(dbPlugin);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import Database from 'better-sqlite3';
|
|
2
|
+
|
|
3
|
+
import config from '../../../config.js';
|
|
4
|
+
import init from './funcs/init.js';
|
|
5
|
+
|
|
6
|
+
const sqliteClients = {};
|
|
7
|
+
|
|
8
|
+
if (config.sqlite) {
|
|
9
|
+
const client = new Database(config.sqlite?.name || ':memory:', {
|
|
10
|
+
readonly: config.sqlite?.readonly || false,
|
|
11
|
+
fileMustExist: config.sqlite?.fileMustExist || false,
|
|
12
|
+
timeout: config.sqlite?.statement_timeout || 10000,
|
|
13
|
+
verbose: config.trace ? console.log : undefined,
|
|
14
|
+
});
|
|
15
|
+
client.init = () => init(client);
|
|
16
|
+
client.init();
|
|
17
|
+
sqliteClients.client = client;
|
|
18
|
+
}
|
|
19
|
+
export default sqliteClients;
|
|
@@ -14,8 +14,11 @@ export default function unflattenObject(flatObj) {
|
|
|
14
14
|
nestedObj[part] = flatObj[key]; // fallback to original value if parsing fails
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
+
else if (['true', 'false'].includes(flatObj[key]) || !isNaN(flatObj[key])) {
|
|
18
|
+
nestedObj[part] = JSON.parse(flatObj[key]);
|
|
19
|
+
}
|
|
17
20
|
else {
|
|
18
|
-
nestedObj[part] =
|
|
21
|
+
nestedObj[part] = flatObj[key];
|
|
19
22
|
}
|
|
20
23
|
}
|
|
21
24
|
else {
|
|
@@ -16,7 +16,7 @@ const loggerSchema = {
|
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
async function plugin(app) {
|
|
19
|
-
app.get('/logger-file/*', { config: { policy: ['public'] }, schema: loggerSchema }, loggerFile);
|
|
19
|
+
app.get('/logger-file/*', { config: { policy: ['public'], rateLimit: false }, schema: loggerSchema }, loggerFile);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export default plugin;
|
|
@@ -27,7 +27,16 @@ async function plugin(app, config = {}) {
|
|
|
27
27
|
app.get(`${prefix}/table-filter/:table`, { config: { policy }, schema: filterSchema }, filter);
|
|
28
28
|
|
|
29
29
|
app.get(`${prefix}/table-info/:table/:id?`, { config: { policy }, schema: tableDataSchema }, tableInfo);
|
|
30
|
-
app.get(`${prefix}/suggest/:data`, {
|
|
30
|
+
app.get(`${prefix}/suggest/:data`, {
|
|
31
|
+
config: {
|
|
32
|
+
policy,
|
|
33
|
+
rateLimit: {
|
|
34
|
+
max: 10000,
|
|
35
|
+
timeWindow: '1 minute',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
schema: suggestSchema,
|
|
39
|
+
}, suggest);
|
|
31
40
|
app.get(`${prefix}/data/:table/:id?`, { config: { policy: ['public', 'no-sql'] }, schema: tableSchema }, data);
|
|
32
41
|
app.get(`${prefix}/table-data/:table`, { config: { policy: ['user', 'no-sql'] }, schema: tableDataSchema }, data);
|
|
33
42
|
app.get(`${prefix}/table-data/:table/:id`, { config: { policy }, schema: tableDataIdSchema }, cardData);
|
package/utils.js
CHANGED
|
@@ -7,6 +7,9 @@ import { handlebars, handlebarsSync } from './server/helpers/index.js';
|
|
|
7
7
|
// pg
|
|
8
8
|
import getPG from './server/plugins/pg/funcs/getPG.js';
|
|
9
9
|
import getPGAsync from './server/plugins/pg/funcs/getPGAsync.js';
|
|
10
|
+
import getSqlite from './server/plugins/sqlite/funcs/getSqlite.js';
|
|
11
|
+
import initSqlite from './server/plugins/sqlite/funcs/init.js';
|
|
12
|
+
import sqliteClients from './server/plugins/sqlite/sqliteClients.js';
|
|
10
13
|
import getDBParams from './server/plugins/pg/funcs/getDBParams.js';
|
|
11
14
|
import initPG from './server/plugins/pg/funcs/init.js';
|
|
12
15
|
import pgClients from './server/plugins/pg/pgClients.js';
|
|
@@ -170,6 +173,11 @@ export {
|
|
|
170
173
|
pgClients,
|
|
171
174
|
getDBParams,
|
|
172
175
|
|
|
176
|
+
// sqlite
|
|
177
|
+
getSqlite,
|
|
178
|
+
initSqlite,
|
|
179
|
+
sqliteClients,
|
|
180
|
+
|
|
173
181
|
// select
|
|
174
182
|
getSelectVal,
|
|
175
183
|
getSelectMeta,
|