@opengis/fastify-table 1.0.0 → 1.0.1

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 ADDED
@@ -0,0 +1,12 @@
1
+ # fastify-table
2
+
3
+ ## 1.0.1 - 14.04.2024
4
+
5
+ - fix redis
6
+
7
+ ## 1.0.0 - 14.04.2024
8
+
9
+ - crud
10
+ - pg
11
+ - redis
12
+ - table
package/README.md CHANGED
@@ -1,26 +1,26 @@
1
- # fastify-table
2
-
3
- [![NPM version](https://img.shields.io/npm/v/@opengis/fastify-table)](https://www.npmjs.com/package/@opengis/fastify-table)
4
- [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](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
+ [![NPM version](https://img.shields.io/npm/v/@opengis/fastify-table)](https://www.npmjs.com/package/@opengis/fastify-table)
4
+ [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](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,7 +1,8 @@
1
- import pgClients from '../../pg/pgClients.js';
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, pg = pgClients.client }) {
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;
@@ -1,9 +1,11 @@
1
- import pgClients from '../../pg/pgClients.js';
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, pg = pgClients.client,
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);
@@ -1,6 +1,8 @@
1
- import rclient from '../../redis/client.js';
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;
@@ -1,5 +1,5 @@
1
1
  import { createHash } from 'crypto';
2
- import rclient from '../../redis/client.js';
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
@@ -1,7 +1,7 @@
1
1
  // This file contains code that we reuse
2
2
  // between our tests.
3
3
  import Fastify from 'fastify';
4
- import config from './test/confg.js';
4
+ import config from './test/config.js';
5
5
  import appService from './index.js';
6
6
 
7
7
  import rclient from './redis/client.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "main": "index.js",
@@ -1,10 +1,12 @@
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 = pgClient.client;
8
+
9
+ const pg = opt.pg || getPG({ db: 'client' });
8
10
  const table = opt.table || opt;
9
11
 
10
12
  if (data[table]) return data[table];
package/pg/funcs/getPG.js CHANGED
@@ -3,10 +3,11 @@ import config from '../../config.js';
3
3
  import pgClients from '../pgClients.js';
4
4
  import init from './init.js';
5
5
 
6
- async function getPG({
7
- user, password, host, port, db,
6
+ function getPG({
7
+ user, password, host, port, db, name: origin
8
8
  }) {
9
- if (pgClients[db]) return pgClients[db];
9
+ const name = origin || db
10
+ if (pgClients[name]) return pgClients[name];
10
11
 
11
12
  const dbConfig = {
12
13
  user: user || config.pg?.user,
@@ -16,9 +17,9 @@ async function getPG({
16
17
  database: db || config.pg?.db,
17
18
  };
18
19
 
19
- pgClients[db] = new pg.Pool(dbConfig);
20
- init(pgClients[db]);
21
- return pgClients[db];
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 rclient from '../../redis/client.js';
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 = new pg.Pool({
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 Redis from 'ioredis';
1
+ import redisClients from './funcs/redisClients.js';
2
+ import getRedis from './funcs/getRedis.js';
2
3
 
3
- // const config = {};
4
- import config from '../config.js';
4
+ if (!redisClients[0]) {
5
+ getRedis({ db: 0 });
6
+ }
5
7
 
6
- const rclient = new Redis({
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;
@@ -0,0 +1,2 @@
1
+ const redisClients = {};
2
+ export default redisClients;
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);
@@ -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 = pgClients.client }) {
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
- import config from '../config.js';
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);
@@ -1,6 +1,6 @@
1
1
  import { test } from 'node:test';
2
2
  import assert from 'node:assert';
3
- import '../confg.js';
3
+ import '../config.js';
4
4
 
5
5
  import pgClients from '../../pg/pgClients.js';
6
6
  import rclient from '../../redis/client.js';
@@ -1,7 +1,7 @@
1
1
  import { test } from 'node:test';
2
2
  import assert from 'node:assert';
3
3
 
4
- import '../confg.js';
4
+ import '../config.js';
5
5
 
6
6
  import getMeta from '../../pg/funcs/getMeta.js';
7
7
  import autoIndex from '../../pg/funcs/autoIndex.js';
@@ -1,7 +1,7 @@
1
1
  import { test } from 'node:test';
2
2
  import assert from 'node:assert';
3
3
 
4
- import '../confg.js';
4
+ import '../config.js';
5
5
 
6
6
  import rclient from '../../redis/client.js';
7
7
 
@@ -1,6 +1,6 @@
1
1
  import { test } from 'node:test';
2
2
  import assert from 'node:assert';
3
- import '../confg.js';
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