@opengis/fastify-table 1.0.46 → 1.0.47
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 +4 -0
- package/crud/controllers/properties.add.js +51 -0
- package/crud/controllers/properties.get.js +20 -0
- package/crud/index.js +6 -0
- package/package.json +1 -1
- package/server/migrations/crm.sql +31 -1
- package/test/api/crud.test.js +20 -0
package/Changelog.md
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import dataInsert from "../funcs/dataInsert.js";
|
|
2
|
+
|
|
3
|
+
const table = 'crm.properties';
|
|
4
|
+
|
|
5
|
+
function checkKeyType({ body, key }) {
|
|
6
|
+
if (typeof body[key] === 'number' && !body[key].toString().includes('.')) {
|
|
7
|
+
return { [key]: 'int' };
|
|
8
|
+
} else if (typeof body[key] === 'object') {
|
|
9
|
+
return { [key]: 'json' };
|
|
10
|
+
} else if (Date.parse(body[key], 'yyyy/MM/ddTHH:mm:ss.000Z')) {
|
|
11
|
+
return { [key]: 'date' };
|
|
12
|
+
}
|
|
13
|
+
return { [key]: 'text' };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default async function addExtraProperties({
|
|
17
|
+
pg, params = {}, body = {},
|
|
18
|
+
}) {
|
|
19
|
+
const { id } = params;
|
|
20
|
+
if (!id) {
|
|
21
|
+
return { message: 'not enougn params: 1', status: 400 };
|
|
22
|
+
}
|
|
23
|
+
const extraProperties = Object.keys(body);
|
|
24
|
+
if (!extraProperties.length) {
|
|
25
|
+
return { message: 'not enougn params: 2', status: 400 };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!pg.pk?.[table]) {
|
|
29
|
+
return { message: 'table not found: crm.properties', status: 400 };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
await pg.query(`delete from crm.properties where object_id=$1`, [id]);
|
|
34
|
+
const keyTypeMatch = extraProperties.filter((key) => body[key]).reduce((acc, curr) => Object.assign(acc, checkKeyType({ body, key: curr })), {});
|
|
35
|
+
const res = await Promise.all(Object.keys(keyTypeMatch).map(async (key) => {
|
|
36
|
+
const propertyType = keyTypeMatch[key];
|
|
37
|
+
const { rows = [] } = await dataInsert({
|
|
38
|
+
pg, table, data: {
|
|
39
|
+
property_type: propertyType,
|
|
40
|
+
property_key: key,
|
|
41
|
+
object_id: id,
|
|
42
|
+
[`property_${propertyType}`]: body[key],
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return { id: rows[0]?.property_id, type: propertyType, value: body[key] };
|
|
46
|
+
}));
|
|
47
|
+
return { message: { rows: res }, status: 200 };
|
|
48
|
+
} catch (err) {
|
|
49
|
+
return { error: err.toString(), status: 500 };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export default async function getExtraProperties({
|
|
2
|
+
pg, params = {},
|
|
3
|
+
}) {
|
|
4
|
+
const { id } = params;
|
|
5
|
+
if (!id) {
|
|
6
|
+
return { message: 'not enougn params', status: 400 };
|
|
7
|
+
}
|
|
8
|
+
try {
|
|
9
|
+
const { rows = [] } = pg.pk?.['crm.properties']
|
|
10
|
+
? await pg.query(`select property_key, property_type, property_text, property_int,
|
|
11
|
+
property_json, property_date from crm.properties where property_key is not null and object_id=$1`, [id])
|
|
12
|
+
: {};
|
|
13
|
+
if (!rows.length) return {};
|
|
14
|
+
|
|
15
|
+
const data = rows.reduce((acc, curr) => Object.assign(acc, { [curr.property_key]: curr[`property_${curr.property_type}`] }), {});
|
|
16
|
+
return { message: data, status: 200 };
|
|
17
|
+
} catch (err) {
|
|
18
|
+
return { error: err.toString(), status: 500 };
|
|
19
|
+
}
|
|
20
|
+
}
|
package/crud/index.js
CHANGED
|
@@ -8,6 +8,9 @@ import update from './controllers/update.js';
|
|
|
8
8
|
import insert from './controllers/insert.js';
|
|
9
9
|
import deleteCrud from './controllers/deleteCrud.js';
|
|
10
10
|
|
|
11
|
+
import getExtraProperties from './controllers/properties.get.js';
|
|
12
|
+
import addExtraProperties from './controllers/properties.add.js';
|
|
13
|
+
|
|
11
14
|
// import config from '../config.js';
|
|
12
15
|
|
|
13
16
|
async function plugin(fastify, config = {}) {
|
|
@@ -24,6 +27,9 @@ async function plugin(fastify, config = {}) {
|
|
|
24
27
|
fastify.put(`${prefix}/table/:table/:id`, {}, update);
|
|
25
28
|
fastify.delete(`${prefix}/table/:table/:id`, {}, deleteCrud);
|
|
26
29
|
fastify.post(`${prefix}/table/:table`, {}, insert);
|
|
30
|
+
|
|
31
|
+
fastify.get(`${prefix}/properties/:id`, {}, getExtraProperties);
|
|
32
|
+
fastify.post(`${prefix}/properties/:id`, {}, addExtraProperties);
|
|
27
33
|
}
|
|
28
34
|
|
|
29
35
|
export default plugin;
|
package/package.json
CHANGED
|
@@ -115,4 +115,34 @@ ALTER TABLE crm.cls ADD COLUMN IF NOT EXISTS cdate timestamp without time zone D
|
|
|
115
115
|
ALTER TABLE crm.cls ADD COLUMN IF NOT EXISTS editor_id text;
|
|
116
116
|
ALTER TABLE crm.cls ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
|
|
117
117
|
ALTER TABLE crm.cls ADD CONSTRAINT crm_cls_pkey PRIMARY KEY (cls_id);
|
|
118
|
-
ALTER TABLE crm.cls ADD CONSTRAINT crm_cls_unique UNIQUE (code, parent);
|
|
118
|
+
ALTER TABLE crm.cls ADD CONSTRAINT crm_cls_unique UNIQUE (code, parent);
|
|
119
|
+
|
|
120
|
+
-- DROP TABLE crm.properties;
|
|
121
|
+
CREATE TABLE IF NOT EXISTS crm.properties();
|
|
122
|
+
ALTER TABLE crm.properties DROP CONSTRAINT IF EXISTS crm_properties_pkey;
|
|
123
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS property_id text NOT NULL DEFAULT next_id();
|
|
124
|
+
|
|
125
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS table_name text;
|
|
126
|
+
COMMENT ON COLUMN crm.properties.table_name IS 'Таблиця';
|
|
127
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS object_id text;
|
|
128
|
+
COMMENT ON COLUMN crm.properties.object_id IS 'ID Об''єкту';
|
|
129
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS property_key text;
|
|
130
|
+
COMMENT ON COLUMN crm.properties.object_id IS 'Назва атрибуту';
|
|
131
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS property_text text;
|
|
132
|
+
COMMENT ON COLUMN crm.properties.property_text IS 'Текст';
|
|
133
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS property_int integer;
|
|
134
|
+
COMMENT ON COLUMN crm.properties.property_int IS 'Integer';
|
|
135
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS property_json json;
|
|
136
|
+
COMMENT ON COLUMN crm.properties.property_json IS 'JSON';
|
|
137
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS property_date date;
|
|
138
|
+
COMMENT ON COLUMN crm.properties.property_json IS 'Дата';
|
|
139
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS property_type text;
|
|
140
|
+
COMMENT ON COLUMN crm.properties.property_type IS 'Тип даних';
|
|
141
|
+
|
|
142
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS uid text NOT NULL DEFAULT '1'::text;
|
|
143
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS editor_id text;
|
|
144
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS editor_date timestamp without time zone;
|
|
145
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS cdate timestamp without time zone DEFAULT now();
|
|
146
|
+
ALTER TABLE crm.properties ADD COLUMN IF NOT EXISTS files json;
|
|
147
|
+
|
|
148
|
+
ALTER TABLE crm.properties ADD CONSTRAINT crm_properties_pkey PRIMARY KEY(property_id);
|
package/test/api/crud.test.js
CHANGED
|
@@ -26,6 +26,26 @@ test('api crud', async (t) => {
|
|
|
26
26
|
assert.ok(rep.rows ? rep.rows[0]?.map_id : rep.map_id);
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
+
await t.test('POST /properties/:id', async () => {
|
|
30
|
+
const res = await app.inject({
|
|
31
|
+
method: 'POST',
|
|
32
|
+
url: `${prefix}/properties/5400000`,
|
|
33
|
+
body: {
|
|
34
|
+
custom_alias: 'testMap',
|
|
35
|
+
custom_ord: 5,
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
assert.ok(res.json().message.rows.length, 'not ok');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
await t.test('GET /properties/:id', async () => {
|
|
42
|
+
const res = await app.inject({
|
|
43
|
+
method: 'GET',
|
|
44
|
+
url: `${prefix}/properties/5400000`,
|
|
45
|
+
});
|
|
46
|
+
assert.ok(res.json().message.custom_alias, 'not ok');
|
|
47
|
+
});
|
|
48
|
+
|
|
29
49
|
await t.test('PUT /update', async () => {
|
|
30
50
|
const res = await app.inject({
|
|
31
51
|
method: 'PUT',
|