@opengis/fastify-table 1.0.0 → 1.0.2
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/Changelog.md +12 -0
- package/README.md +26 -26
- package/crud/funcs/dataInsert.js +3 -2
- package/crud/funcs/dataUpdate.js +4 -2
- package/crud/funcs/getOpt.js +3 -1
- package/crud/funcs/setOpt.js +3 -1
- package/crud/index.js +6 -6
- package/helper.js +1 -1
- package/package.json +1 -1
- package/pg/funcs/getMeta.js +4 -8
- package/pg/funcs/getPG.js +8 -7
- package/pg/funcs/init.js +2 -1
- package/pg/index.js +3 -9
- package/redis/client.js +6 -12
- package/redis/funcs/getRedis.js +23 -0
- package/redis/funcs/redisClients.js +2 -0
- package/redis/index.js +4 -1
- package/table/controllers/data.js +2 -1
- package/table/controllers/utils/getSelectMeta.js +6 -4
- package/table/index.js +2 -5
- package/test/funcs/crud.test.js +1 -1
- package/test/funcs/pg.test.js +1 -1
- package/test/funcs/redis.test.js +1 -1
- package/test/funcs/table.test.js +1 -1
- /package/test/{confg.js → config.example} +0 -0
package/Changelog.md
ADDED
package/README.md
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
# fastify-table
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/@opengis/fastify-table)
|
|
4
|
-
[](http://standardjs.com/)
|
|
5
|
-
|
|
6
|
-
It standardizes the entire form building process, while taking care of everything from rendering to validation and processing:
|
|
7
|
-
|
|
8
|
-
- pg
|
|
9
|
-
- redis
|
|
10
|
-
- crud
|
|
11
|
-
|
|
12
|
-
## Install
|
|
13
|
-
|
|
14
|
-
```bash
|
|
15
|
-
npm i @opengis/fastify-table
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
## Usage
|
|
19
|
-
|
|
20
|
-
```js
|
|
21
|
-
fastify.register(import('@opengis/fastify-table'), config);
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## Documenation
|
|
25
|
-
|
|
26
|
-
For a detailed understanding fastify-table, its features, and how to use them, refer to our [Documentation](https://apidocs.softpro.ua/gis.storage/).
|
|
1
|
+
# fastify-table
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@opengis/fastify-table)
|
|
4
|
+
[](http://standardjs.com/)
|
|
5
|
+
|
|
6
|
+
It standardizes the entire form building process, while taking care of everything from rendering to validation and processing:
|
|
7
|
+
|
|
8
|
+
- pg
|
|
9
|
+
- redis
|
|
10
|
+
- crud
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm i @opengis/fastify-table
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
fastify.register(import('@opengis/fastify-table'), config);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Documenation
|
|
25
|
+
|
|
26
|
+
For a detailed understanding fastify-table, its features, and how to use them, refer to our [Documentation](https://apidocs.softpro.ua/gis.storage/).
|
package/crud/funcs/dataInsert.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import getPG from '../../pg/funcs/getPG.js';
|
|
2
2
|
import getMeta from '../../pg/funcs/getMeta.js';
|
|
3
3
|
|
|
4
|
-
export default async function dataInsert({ table, data
|
|
4
|
+
export default async function dataInsert({ table, data }) {
|
|
5
|
+
const pg = getPG({ name: 'client' });
|
|
5
6
|
if (!data) return null;
|
|
6
7
|
const { columns } = await getMeta(table);
|
|
7
8
|
if (!columns) return null;
|
package/crud/funcs/dataUpdate.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import getPG from '../../pg/funcs/getPG.js';
|
|
2
|
+
|
|
2
3
|
import getMeta from '../../pg/funcs/getMeta.js';
|
|
3
4
|
|
|
4
5
|
export default async function dataUpdate({
|
|
5
|
-
table, id, data,
|
|
6
|
+
table, id, data,
|
|
6
7
|
}) {
|
|
8
|
+
const pg = getPG({ name: 'client' });
|
|
7
9
|
const { columns, pk } = await getMeta(table);
|
|
8
10
|
|
|
9
11
|
const names = columns.map((el) => el.name);
|
package/crud/funcs/getOpt.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import getRedis from '../../redis/funcs/getRedis.js';
|
|
2
2
|
|
|
3
3
|
export default async function getOpt(token) {
|
|
4
|
+
const rclient = getRedis({ db: 0 });
|
|
5
|
+
|
|
4
6
|
const key = `opt:${token}`;
|
|
5
7
|
const data = await rclient.get(key);
|
|
6
8
|
if (!data) return null;
|
package/crud/funcs/setOpt.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createHash } from 'crypto';
|
|
2
|
-
import
|
|
2
|
+
import getRedis from '../../redis/funcs/getRedis.js';
|
|
3
3
|
|
|
4
4
|
function md5(string) {
|
|
5
5
|
return createHash('md5').update(string).digest('hex');
|
|
@@ -9,6 +9,8 @@ export default async function setOpt(params) {
|
|
|
9
9
|
const token = Buffer.from(md5(typeof params === 'object' ? JSON.stringify(params) : params), 'hex').toString('base64').replace(/[+-=]+/g, '');
|
|
10
10
|
// const token = md5(params);
|
|
11
11
|
const key = `opt:${token}`;
|
|
12
|
+
|
|
13
|
+
const rclient = getRedis({ db: 0 });
|
|
12
14
|
await rclient.set(key, JSON.stringify(params), 'EX', 60 * 60);
|
|
13
15
|
return token;
|
|
14
16
|
}
|
package/crud/index.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import fp from 'fastify-plugin';
|
|
2
|
-
|
|
3
1
|
import getOPt from './funcs/getOpt.js';
|
|
4
2
|
import setOpt from './funcs/setOpt.js';
|
|
5
3
|
import isFileExists from './funcs/isFileExists.js';
|
|
6
4
|
import dataUpdate from './funcs/dataUpdate.js';
|
|
5
|
+
import dataInsert from './funcs/dataInsert.js';
|
|
7
6
|
|
|
8
7
|
import update from './controllers/update.js';
|
|
9
8
|
import insert from './controllers/insert.js';
|
|
10
9
|
import deleteCrud from './controllers/deleteCrud.js';
|
|
11
10
|
|
|
12
|
-
import config from '../config.js';
|
|
13
|
-
|
|
14
|
-
const prefix = config.prefix || '/api';
|
|
11
|
+
// import config from '../config.js';
|
|
15
12
|
|
|
16
|
-
async function plugin(fastify) {
|
|
13
|
+
async function plugin(fastify, config = {}) {
|
|
14
|
+
const prefix = config.prefix || '/api';
|
|
17
15
|
// funcs
|
|
18
16
|
fastify.decorate('setOpt', setOpt);
|
|
19
17
|
fastify.decorate('getOpt', getOPt);
|
|
20
18
|
fastify.decorate('dataUpdate', dataUpdate);
|
|
19
|
+
fastify.decorate('dataInsert', dataInsert);
|
|
20
|
+
|
|
21
21
|
fastify.decorate('isFileExists', isFileExists);
|
|
22
22
|
|
|
23
23
|
// api
|
package/helper.js
CHANGED
package/package.json
CHANGED
package/pg/funcs/getMeta.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import pgClient from '../pgClients.js';
|
|
1
|
+
// import pgClient from '../pgClients.js';
|
|
2
|
+
import getPG from './getPG.js';
|
|
2
3
|
|
|
3
4
|
const data = {};
|
|
4
5
|
|
|
5
6
|
// decorator
|
|
6
7
|
export default async function getMeta(opt) {
|
|
7
|
-
const pg =
|
|
8
|
+
const pg = opt.pg || getPG({ name: 'client' });
|
|
8
9
|
const table = opt.table || opt;
|
|
9
10
|
|
|
10
11
|
if (data[table]) return data[table];
|
|
@@ -19,13 +20,8 @@ export default async function getMeta(opt) {
|
|
|
19
20
|
const pk = pks1[table];
|
|
20
21
|
|
|
21
22
|
const geomAttr = fields.find((el) => pg.pgType[el.dataTypeID] === 'geometry')?.name; // change geometry text to geometry code
|
|
22
|
-
const q = `select st_extent( ${geomAttr}) as bounds, count(*),
|
|
23
|
-
(select st_srid(${geomAttr}) from ${table} where ${geomAttr} is not null limit 1) as srid,
|
|
24
|
-
json_agg(distinct ST_GeometryType(${geomAttr}))::json as geometry from ${table}
|
|
25
|
-
where ${geomAttr} is not null `;
|
|
26
|
-
const geom = await pg.one(q, { cache: 0 });
|
|
27
23
|
|
|
28
|
-
const res = { pk, columns: fields, geom };
|
|
24
|
+
const res = { pk, columns: fields, geom: geomAttr };
|
|
29
25
|
data[table] = res;
|
|
30
26
|
return res;
|
|
31
27
|
}
|
package/pg/funcs/getPG.js
CHANGED
|
@@ -3,22 +3,23 @@ import config from '../../config.js';
|
|
|
3
3
|
import pgClients from '../pgClients.js';
|
|
4
4
|
import init from './init.js';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
user, password, host, port, db,
|
|
6
|
+
function getPG({
|
|
7
|
+
user, password, host, port, db, database, name: origin,
|
|
8
8
|
}) {
|
|
9
|
-
|
|
9
|
+
const name = origin || db || database;
|
|
10
|
+
if (pgClients[name]) return pgClients[name];
|
|
10
11
|
|
|
11
12
|
const dbConfig = {
|
|
12
13
|
user: user || config.pg?.user,
|
|
13
14
|
password: password || config.pg?.password,
|
|
14
15
|
host: host || config.pg?.host,
|
|
15
16
|
port: port || config.pg?.port,
|
|
16
|
-
database: db || config.pg?.db,
|
|
17
|
+
database: db || database || config.pg?.db || config.pg?.database,
|
|
17
18
|
};
|
|
18
19
|
|
|
19
|
-
pgClients[
|
|
20
|
-
init(pgClients[
|
|
21
|
-
return pgClients[
|
|
20
|
+
pgClients[name] = new pg.Pool(dbConfig);
|
|
21
|
+
init(pgClients[name]);
|
|
22
|
+
return pgClients[name];
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
export default getPG;
|
package/pg/funcs/init.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import crypto from 'crypto';
|
|
2
2
|
|
|
3
3
|
// import pg from 'pg';
|
|
4
|
-
import
|
|
4
|
+
import getRedis from '../../redis/funcs/getRedis.js';
|
|
5
5
|
// import config from '../config.js';
|
|
6
6
|
|
|
7
7
|
async function init(client) {
|
|
@@ -21,6 +21,7 @@ async function init(client) {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
async function queryCache(query) {
|
|
24
|
+
const rclient = getRedis({ db: 0 })
|
|
24
25
|
const hash = crypto.createHash('sha1').update(query).digest('base64');
|
|
25
26
|
const keyCache = `pg:${hash}`;
|
|
26
27
|
const cache = await rclient.get(keyCache);
|
package/pg/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// import fp from 'fastify-plugin';
|
|
2
|
-
import pg from 'pg';
|
|
2
|
+
//import pg from 'pg';
|
|
3
3
|
import pgClients from './funcs/pgClients.js';
|
|
4
4
|
import init from './funcs/init.js';
|
|
5
5
|
import autoIndex from './funcs/autoIndex.js';
|
|
@@ -17,19 +17,13 @@ function close() {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
async function plugin(fastify, config) {
|
|
20
|
-
const client =
|
|
21
|
-
host: config.pg?.host || '127.0.0.1',
|
|
22
|
-
port: config.pg?.port || 5432,
|
|
23
|
-
database: config.pg?.database || 'postgres',
|
|
24
|
-
user: config.pg?.user || 'postgres',
|
|
25
|
-
password: config.pg?.password || 'postgres',
|
|
26
|
-
});
|
|
20
|
+
const client = getPG({ ...config.pg || {}, name: 'client' })
|
|
27
21
|
await init(client);
|
|
28
22
|
fastify.addHook('onRequest', async (req) => {
|
|
29
23
|
// req.funcs = fastify;
|
|
30
24
|
req.pg = client;
|
|
31
25
|
});
|
|
32
|
-
pgClients.client = client;
|
|
26
|
+
//pgClients.client = client;
|
|
33
27
|
|
|
34
28
|
// fastify.decorate('pg', client);
|
|
35
29
|
fastify.decorate('autoIndex', autoIndex);
|
package/redis/client.js
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import redisClients from './funcs/redisClients.js';
|
|
2
|
+
import getRedis from './funcs/getRedis.js';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
if (!redisClients[0]) {
|
|
5
|
+
getRedis({ db: 0 });
|
|
6
|
+
}
|
|
5
7
|
|
|
6
|
-
|
|
7
|
-
keyPrefix: `${config?.db}:`,
|
|
8
|
-
host: config?.redis?.host || '127.0.0.1',
|
|
9
|
-
port: config?.redis?.port || 6379, // Redis port
|
|
10
|
-
family: 4, // 4 (IPv4) or 6 (IPv6)
|
|
11
|
-
closeClient: true,
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
export default rclient;
|
|
8
|
+
export default redisClients[0];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import Redis from 'ioredis';
|
|
2
|
+
import config from '../../config.js';
|
|
3
|
+
import redisClients from './redisClients.js';
|
|
4
|
+
|
|
5
|
+
function getRedis({ db }) {
|
|
6
|
+
if (!config.redis) return null;
|
|
7
|
+
if (redisClients[db]) return redisClients[db];
|
|
8
|
+
|
|
9
|
+
const redisConfig = {
|
|
10
|
+
db,
|
|
11
|
+
keyPrefix: `${config.db}:`,
|
|
12
|
+
host: config.redis?.host || '127.0.0.1',
|
|
13
|
+
port: config.redis?.port || 6379, // Redis port
|
|
14
|
+
family: 4, // 4 (IPv4) or 6 (IPv6)
|
|
15
|
+
closeClient: true,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
redisClients[db] = new Redis(redisConfig);
|
|
19
|
+
|
|
20
|
+
return redisClients[db];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default getRedis;
|
package/redis/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import client from './client.js';
|
|
1
|
+
// import client from './client.js';
|
|
2
|
+
import getRedis from './funcs/getRedis.js';
|
|
3
|
+
// import client from './funcs/redisClients.js';
|
|
2
4
|
|
|
3
5
|
function close(fastify) {
|
|
4
6
|
fastify.rclient.quit();
|
|
@@ -6,6 +8,7 @@ function close(fastify) {
|
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
async function plugin(fastify) {
|
|
11
|
+
const client = getRedis({ db: 0 });
|
|
9
12
|
client.getJSON = client.get;
|
|
10
13
|
fastify.decorate('rclient', client);
|
|
11
14
|
// fastify.decorate('rclient2', client2);
|
|
@@ -21,7 +21,8 @@ export default async function data(req) {
|
|
|
21
21
|
const cols = columns.map((el) => el.name || el).join(',');
|
|
22
22
|
const sqlTable = sql?.filter?.((el) => !el?.disabled && el?.sql?.replace).map((el, i) => ` left join lateral (${el.sql}) ${el.name || `t${i}`} on 1=1 `)?.join('') || '';
|
|
23
23
|
|
|
24
|
-
const fData = query.filter ? await getFilterSQL(
|
|
24
|
+
const fData = query.filter ? await getFilterSQL({
|
|
25
|
+
filter: query.filter,
|
|
25
26
|
table,
|
|
26
27
|
json: 1,
|
|
27
28
|
}) : {};
|
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import pgClients from '../../../pg/pgClients.js';
|
|
1
|
+
// import pgClients from '../../../pg/pgClients.js';
|
|
2
|
+
import getPG from '../../../pg/funcs/getPG.js';
|
|
2
3
|
|
|
3
4
|
import getSelect from './getSelect.js';
|
|
4
|
-
|
|
5
|
+
/*
|
|
5
6
|
function getTable(table) {
|
|
6
7
|
// eslint-disable-next-line class-methods-use-this
|
|
7
8
|
const result = table.toLowerCase().replace(/[\n\r]+/g, ' ').split(' from ').filter((el) => /^[a-z0-9_]+\.[a-z0-9_]+/.test(el))
|
|
8
9
|
.map((el) => el.split(/[ )]/)[0]);
|
|
9
10
|
return result?.pop();
|
|
10
|
-
}
|
|
11
|
+
} */
|
|
11
12
|
|
|
12
13
|
const selectMeta = {};
|
|
13
14
|
|
|
14
|
-
export default async function getSelectMeta({ name, pg
|
|
15
|
+
export default async function getSelectMeta({ name, pg: pg1 }) {
|
|
15
16
|
if (selectMeta[name]) return selectMeta[name];
|
|
17
|
+
const pg = pg1 || getPG({ db: 'client' });
|
|
16
18
|
|
|
17
19
|
const cls = await getSelect(name);
|
|
18
20
|
|
package/table/index.js
CHANGED
|
@@ -3,11 +3,8 @@ import data from './controllers/data.js';
|
|
|
3
3
|
import filter from './controllers/filter.js';
|
|
4
4
|
import form from './controllers/form.js';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const prefix = config.prefix || '/api';
|
|
9
|
-
|
|
10
|
-
async function plugin(fastify) {
|
|
6
|
+
async function plugin(fastify, config = {}) {
|
|
7
|
+
const prefix = config.prefix || '/api';
|
|
11
8
|
fastify.get(`${prefix}/suggest/:data`, {}, suggest);
|
|
12
9
|
fastify.get(`${prefix}/data/:table/:id?`, {}, data); // vs.crm.data.api с node
|
|
13
10
|
fastify.get(`${prefix}/filter/:table`, {}, filter);
|
package/test/funcs/crud.test.js
CHANGED
package/test/funcs/pg.test.js
CHANGED
package/test/funcs/redis.test.js
CHANGED
package/test/funcs/table.test.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { test } from 'node:test';
|
|
2
2
|
import assert from 'node:assert';
|
|
3
|
-
import '../
|
|
3
|
+
import '../config.js';
|
|
4
4
|
import pgClients from '../../pg/pgClients.js';
|
|
5
5
|
import rclient from '../../redis/client.js';
|
|
6
6
|
import getFilterSQL from '../../table/funcs/getFilterSQL/index.js';
|
|
File without changes
|