@opengis/cms 0.0.26 → 0.0.28
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/README.md +98 -98
- package/dist/index-W-qQIppj-DunG40EG.js +2452 -0
- package/dist/index.html +28 -28
- package/dist/index.js +12683 -6702
- package/dist/index.umd.cjs +87 -41
- package/dist/style.css +1 -0
- package/package.json +68 -68
- package/plugin.js +76 -76
- package/server/app.js +35 -35
- package/server/config.js +4 -4
- package/server/functions/getDraftKey.js +22 -22
- package/server/index.js +22 -22
- package/server/migrations/fixes.sql +123 -123
- package/server/migrations/site.sql +545 -545
- package/server/plugins/adminHook.js +78 -78
- package/server/plugins/hook.js +59 -59
- package/server/plugins/vite.js +84 -84
- package/server/routes/category/controllers/cms.category.delete.js +21 -21
- package/server/routes/category/controllers/cms.category.get.js +17 -17
- package/server/routes/category/controllers/cms.category.list.js +16 -16
- package/server/routes/category/controllers/cms.category.post.js +21 -21
- package/server/routes/category/controllers/cms.category.put.js +23 -23
- package/server/routes/category/index.mjs +22 -22
- package/server/routes/cms/controllers/cmsStat.js +55 -55
- package/server/routes/cms/controllers/cmsSuggest.js +57 -57
- package/server/routes/cms/controllers/deleteContent.js +114 -114
- package/server/routes/cms/controllers/deleteMedia.js +75 -75
- package/server/routes/cms/controllers/downloadMedia.js +48 -48
- package/server/routes/cms/controllers/getContent.js +110 -110
- package/server/routes/cms/controllers/getContentBySlug.js +95 -95
- package/server/routes/cms/controllers/getPermissions.js +15 -15
- package/server/routes/cms/controllers/insertContent.js +218 -218
- package/server/routes/cms/controllers/listMedia.js +93 -93
- package/server/routes/cms/controllers/metadataMedia.js +38 -38
- package/server/routes/cms/controllers/properties.get.js +53 -53
- package/server/routes/cms/controllers/properties.post.js +99 -99
- package/server/routes/cms/controllers/searchContent.js +205 -205
- package/server/routes/cms/controllers/setPermissions.js +49 -49
- package/server/routes/cms/controllers/translate.js +89 -89
- package/server/routes/cms/controllers/updateContent.js +238 -238
- package/server/routes/cms/controllers/uploadMedia.js +78 -78
- package/server/routes/cms/index.mjs +114 -114
- package/server/routes/cms/utils/additionalData.js +35 -35
- package/server/routes/cms/utils/getCollection.js +81 -81
- package/server/routes/cms/utils/getSingle.js +187 -187
- package/server/routes/cms/utils/insertContentLocalization.js +86 -86
- package/server/routes/cms/utils/requestTranslation.js +85 -85
- package/server/routes/cms/utils/updateLocalization.js +47 -47
- package/server/routes/cmsSpace/controllers/deleteSpace.js +25 -25
- package/server/routes/cmsSpace/controllers/getSpaces.js +27 -27
- package/server/routes/cmsSpace/controllers/insertSpace.js +21 -21
- package/server/routes/cmsSpace/controllers/updateSpace.js +23 -23
- package/server/routes/cmsSpace/index.mjs +20 -20
- package/server/routes/contentType/controllers/addContentType.js +162 -162
- package/server/routes/contentType/controllers/contentTypeList.js +54 -54
- package/server/routes/contentType/controllers/delContentType.js +75 -75
- package/server/routes/contentType/controllers/editContentType.js +61 -61
- package/server/routes/contentType/controllers/getContentType.js +37 -37
- package/server/routes/contentType/index.mjs +35 -35
- package/server/routes/contentType/utils/updateContents.js +28 -28
- package/server/routes/contentType/utils/updateCustomContentTable.js +55 -55
- package/server/routes/feedback/controllers/email.list.js +24 -24
- package/server/routes/feedback/controllers/feedback.js +48 -48
- package/server/routes/feedback/controllers/feedback.list.js +37 -37
- package/server/routes/feedback/controllers/news.subscriptions.js +44 -44
- package/server/routes/feedback/index.mjs +71 -71
- package/server/routes/logs/controllers/export.user.logs.js +77 -77
- package/server/routes/logs/controllers/user.logs.js +44 -44
- package/server/routes/logs/index.mjs +9 -9
- package/server/routes/menu/controllers/addMenu.js +37 -37
- package/server/routes/menu/controllers/delMenu.js +31 -31
- package/server/routes/menu/controllers/editMenu.js +41 -41
- package/server/routes/menu/controllers/getMenu.js +42 -42
- package/server/routes/menu/index.mjs +13 -13
- package/server/routes/migration/controllers/collectionToCustom.js +137 -137
- package/server/routes/migration/index.mjs +8 -8
- package/server/routes/root.mjs +8 -8
- package/server/routes/tags/controllers/add.tags.js +24 -24
- package/server/routes/tags/controllers/del.tags.js +19 -19
- package/server/routes/tags/controllers/edit.tags.js +25 -25
- package/server/routes/tags/controllers/get.tags.js +15 -15
- package/server/routes/tags/index.mjs +14 -14
- package/server/templates/cls/cms.category_type.json +9 -9
- package/server/templates/cls/cms.content_review_status.json +9 -9
- package/server/templates/cls/cms.content_status.json +9 -9
- package/server/templates/cls/cms.content_type.json +9 -9
- package/server/templates/cls/cms.lang.json +9 -9
- package/server/templates/page/login.html +126 -126
- package/server/templates/select/core.user_mentioned.sql +1 -1
- package/dist/index-W-qQIppj-DRzFSjU1.js +0 -2452
|
@@ -1,162 +1,162 @@
|
|
|
1
|
-
import { config, logger, dataInsert, pgClients } from '@opengis/fastify-table/utils.js';
|
|
2
|
-
|
|
3
|
-
import updateCustomContentTable from '../utils/updateCustomContentTable.js'; // custom table
|
|
4
|
-
import updateContents from '../utils/updateContents.js'; // site.contents + site.content_data
|
|
5
|
-
|
|
6
|
-
const columns = [
|
|
7
|
-
{
|
|
8
|
-
name: 'slug',
|
|
9
|
-
label: 'Slug',
|
|
10
|
-
type: 'slug',
|
|
11
|
-
parent: 'title',
|
|
12
|
-
required: true
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
name: 'title',
|
|
16
|
-
label: 'Заголовок',
|
|
17
|
-
type: 'text',
|
|
18
|
-
required: true
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
name: 'status',
|
|
22
|
-
label: 'Статус',
|
|
23
|
-
type: 'select',
|
|
24
|
-
data: "content.status",
|
|
25
|
-
required: true
|
|
26
|
-
},
|
|
27
|
-
{
|
|
28
|
-
name: 'published_at',
|
|
29
|
-
label: 'Дата публікації',
|
|
30
|
-
type: 'date',
|
|
31
|
-
required: true
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
name: 'is_pin',
|
|
35
|
-
label: 'Закріпити контент',
|
|
36
|
-
type: 'boolean',
|
|
37
|
-
required: false,
|
|
38
|
-
default: 'false',
|
|
39
|
-
hidden: true,
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
name: 'meta',
|
|
43
|
-
label: 'Meta',
|
|
44
|
-
type: 'datalist',
|
|
45
|
-
hidden: true,
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
name: 'created_at',
|
|
49
|
-
label: 'Дата та час створення',
|
|
50
|
-
type: 'datetime',
|
|
51
|
-
default: 'now()',
|
|
52
|
-
hidden: true,
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
name: 'updated_at',
|
|
56
|
-
label: 'Дата та час редагування',
|
|
57
|
-
type: 'datetime',
|
|
58
|
-
default: 'now()',
|
|
59
|
-
hidden: true,
|
|
60
|
-
},
|
|
61
|
-
{
|
|
62
|
-
name: 'created_by',
|
|
63
|
-
type: 'text',
|
|
64
|
-
label: 'Автор',
|
|
65
|
-
hidden: true
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
name: 'updated_by',
|
|
69
|
-
type: 'text',
|
|
70
|
-
label: 'Редактор',
|
|
71
|
-
default: 'now()',
|
|
72
|
-
hidden: true
|
|
73
|
-
}
|
|
74
|
-
];
|
|
75
|
-
|
|
76
|
-
export default async function builderPost(req, reply) {
|
|
77
|
-
const { pg = pgClients.client, user = {}, body = {} } = req;
|
|
78
|
-
|
|
79
|
-
if (!pg?.pk?.['site.content_types']) {
|
|
80
|
-
return reply.status(404).send('table not found');
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const { customTable = true } = body;
|
|
84
|
-
|
|
85
|
-
if (!body.name) {
|
|
86
|
-
return reply.status(400).send('not enough body params: name');
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (typeof body.name !== 'string') {
|
|
90
|
-
return reply.status(400).send('invalid body params: name not as string');
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const rowCount = await pg.query('select content_type_id as id from site.content_types where name=$1', [body.name]).then(el => el.rowCount);
|
|
94
|
-
|
|
95
|
-
if (rowCount > 0) {
|
|
96
|
-
return reply.status(400).send('Тип контенту з даною назвою вже існує');
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const tablename = body.type === 'collection' && true ? body.name.toLowerCase().replace(/ /g, '').replace(/'/g, "''").replace(/'/g, '').replace(/-/g, '_') : undefined; // customTable
|
|
100
|
-
|
|
101
|
-
const client = await pg.connect();
|
|
102
|
-
|
|
103
|
-
try {
|
|
104
|
-
await client.query('BEGIN');
|
|
105
|
-
|
|
106
|
-
const columns1 = (body.fields || body.columns || columns.filter(el => !el.hidden)).filter(el => el.name).map(el => Object.assign(el, { name: el.name.toLowerCase() })); // for rest crud
|
|
107
|
-
|
|
108
|
-
if (body.type === 'single') {
|
|
109
|
-
const singleColumns = [
|
|
110
|
-
{
|
|
111
|
-
name: 'main_image',
|
|
112
|
-
label: 'Головне зображення',
|
|
113
|
-
type: 'image',
|
|
114
|
-
required: false
|
|
115
|
-
},
|
|
116
|
-
{
|
|
117
|
-
name: 'single_body',
|
|
118
|
-
label: 'Контент',
|
|
119
|
-
type: 'richtext',
|
|
120
|
-
required: false
|
|
121
|
-
},
|
|
122
|
-
{
|
|
123
|
-
name: 'single_sections',
|
|
124
|
-
label: 'Секції',
|
|
125
|
-
type: 'reference',
|
|
126
|
-
required: false
|
|
127
|
-
},
|
|
128
|
-
]
|
|
129
|
-
columns1.push(...singleColumns);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const { rows = [] } = await dataInsert({
|
|
133
|
-
pg: client,
|
|
134
|
-
table: 'site.content_types',
|
|
135
|
-
data: {
|
|
136
|
-
...body,
|
|
137
|
-
table_name: tablename,
|
|
138
|
-
columns: columns1,
|
|
139
|
-
},
|
|
140
|
-
uid: user?.uid,
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
Object.assign(body, { slug: body.slug || body.name });
|
|
144
|
-
|
|
145
|
-
if (tablename) {
|
|
146
|
-
await updateCustomContentTable({ pg: client, id: rows?.[0]?.content_type_id, columns });
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const res = await updateContents(client, rows?.[0]?.content_type_id, body, user?.uid);
|
|
150
|
-
Object.assign(rows?.[0], res || {}); // update rows with contents
|
|
151
|
-
|
|
152
|
-
await client.query('COMMIT');
|
|
153
|
-
|
|
154
|
-
return reply.status(200).send({ id: rows?.[0]?.content_type_id, rows });
|
|
155
|
-
} catch (err) {
|
|
156
|
-
await client.query('ROLLBACK');
|
|
157
|
-
logger.file('cms/contentType', { error: err.toString(), stack: err.stack });
|
|
158
|
-
return reply.status(500).send('Помилка при додаванні типу контенту');
|
|
159
|
-
} finally {
|
|
160
|
-
client.release();
|
|
161
|
-
}
|
|
162
|
-
}
|
|
1
|
+
import { config, logger, dataInsert, pgClients } from '@opengis/fastify-table/utils.js';
|
|
2
|
+
|
|
3
|
+
import updateCustomContentTable from '../utils/updateCustomContentTable.js'; // custom table
|
|
4
|
+
import updateContents from '../utils/updateContents.js'; // site.contents + site.content_data
|
|
5
|
+
|
|
6
|
+
const columns = [
|
|
7
|
+
{
|
|
8
|
+
name: 'slug',
|
|
9
|
+
label: 'Slug',
|
|
10
|
+
type: 'slug',
|
|
11
|
+
parent: 'title',
|
|
12
|
+
required: true
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
name: 'title',
|
|
16
|
+
label: 'Заголовок',
|
|
17
|
+
type: 'text',
|
|
18
|
+
required: true
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: 'status',
|
|
22
|
+
label: 'Статус',
|
|
23
|
+
type: 'select',
|
|
24
|
+
data: "content.status",
|
|
25
|
+
required: true
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: 'published_at',
|
|
29
|
+
label: 'Дата публікації',
|
|
30
|
+
type: 'date',
|
|
31
|
+
required: true
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: 'is_pin',
|
|
35
|
+
label: 'Закріпити контент',
|
|
36
|
+
type: 'boolean',
|
|
37
|
+
required: false,
|
|
38
|
+
default: 'false',
|
|
39
|
+
hidden: true,
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: 'meta',
|
|
43
|
+
label: 'Meta',
|
|
44
|
+
type: 'datalist',
|
|
45
|
+
hidden: true,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: 'created_at',
|
|
49
|
+
label: 'Дата та час створення',
|
|
50
|
+
type: 'datetime',
|
|
51
|
+
default: 'now()',
|
|
52
|
+
hidden: true,
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: 'updated_at',
|
|
56
|
+
label: 'Дата та час редагування',
|
|
57
|
+
type: 'datetime',
|
|
58
|
+
default: 'now()',
|
|
59
|
+
hidden: true,
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: 'created_by',
|
|
63
|
+
type: 'text',
|
|
64
|
+
label: 'Автор',
|
|
65
|
+
hidden: true
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: 'updated_by',
|
|
69
|
+
type: 'text',
|
|
70
|
+
label: 'Редактор',
|
|
71
|
+
default: 'now()',
|
|
72
|
+
hidden: true
|
|
73
|
+
}
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
export default async function builderPost(req, reply) {
|
|
77
|
+
const { pg = pgClients.client, user = {}, body = {} } = req;
|
|
78
|
+
|
|
79
|
+
if (!pg?.pk?.['site.content_types']) {
|
|
80
|
+
return reply.status(404).send('table not found');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const { customTable = true } = body;
|
|
84
|
+
|
|
85
|
+
if (!body.name) {
|
|
86
|
+
return reply.status(400).send('not enough body params: name');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (typeof body.name !== 'string') {
|
|
90
|
+
return reply.status(400).send('invalid body params: name not as string');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const rowCount = await pg.query('select content_type_id as id from site.content_types where name=$1', [body.name]).then(el => el.rowCount);
|
|
94
|
+
|
|
95
|
+
if (rowCount > 0) {
|
|
96
|
+
return reply.status(400).send('Тип контенту з даною назвою вже існує');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const tablename = body.type === 'collection' && true ? body.name.toLowerCase().replace(/ /g, '').replace(/'/g, "''").replace(/'/g, '').replace(/-/g, '_') : undefined; // customTable
|
|
100
|
+
|
|
101
|
+
const client = await pg.connect();
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
await client.query('BEGIN');
|
|
105
|
+
|
|
106
|
+
const columns1 = (body.fields || body.columns || columns.filter(el => !el.hidden)).filter(el => el.name).map(el => Object.assign(el, { name: el.name.toLowerCase() })); // for rest crud
|
|
107
|
+
|
|
108
|
+
if (body.type === 'single') {
|
|
109
|
+
const singleColumns = [
|
|
110
|
+
{
|
|
111
|
+
name: 'main_image',
|
|
112
|
+
label: 'Головне зображення',
|
|
113
|
+
type: 'image',
|
|
114
|
+
required: false
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: 'single_body',
|
|
118
|
+
label: 'Контент',
|
|
119
|
+
type: 'richtext',
|
|
120
|
+
required: false
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
name: 'single_sections',
|
|
124
|
+
label: 'Секції',
|
|
125
|
+
type: 'reference',
|
|
126
|
+
required: false
|
|
127
|
+
},
|
|
128
|
+
]
|
|
129
|
+
columns1.push(...singleColumns);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const { rows = [] } = await dataInsert({
|
|
133
|
+
pg: client,
|
|
134
|
+
table: 'site.content_types',
|
|
135
|
+
data: {
|
|
136
|
+
...body,
|
|
137
|
+
table_name: tablename,
|
|
138
|
+
columns: columns1,
|
|
139
|
+
},
|
|
140
|
+
uid: user?.uid,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
Object.assign(body, { slug: body.slug || body.name });
|
|
144
|
+
|
|
145
|
+
if (tablename) {
|
|
146
|
+
await updateCustomContentTable({ pg: client, id: rows?.[0]?.content_type_id, columns });
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const res = await updateContents(client, rows?.[0]?.content_type_id, body, user?.uid);
|
|
150
|
+
Object.assign(rows?.[0], res || {}); // update rows with contents
|
|
151
|
+
|
|
152
|
+
await client.query('COMMIT');
|
|
153
|
+
|
|
154
|
+
return reply.status(200).send({ id: rows?.[0]?.content_type_id, rows });
|
|
155
|
+
} catch (err) {
|
|
156
|
+
await client.query('ROLLBACK');
|
|
157
|
+
logger.file('cms/contentType', { error: err.toString(), stack: err.stack });
|
|
158
|
+
return reply.status(500).send('Помилка при додаванні типу контенту');
|
|
159
|
+
} finally {
|
|
160
|
+
client.release();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
import { config, pgClients } from '@opengis/fastify-table/utils.js';
|
|
2
|
-
|
|
3
|
-
const maxLimit = 100;
|
|
4
|
-
|
|
5
|
-
export default async function builderList({ pg = pgClients.client, query = {} }, reply) {
|
|
6
|
-
if (!pg) {
|
|
7
|
-
return reply.status(200).send({ total: 0, msg: 'empty pg', rows: [] });
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const limit = Math.min(maxLimit, query.limit || 16);
|
|
11
|
-
const offset = query.page && query.page > 0 ? (query.page - 1) * limit : 0;
|
|
12
|
-
|
|
13
|
-
const q = `select a.content_type_id as id, a.name, a.title, a.description, a.table_name as table, type, columns, a.include_search, json_build_object('locales', locales, 'default_locale', default_locale, 'name', space_name) as space FROM site.content_types a
|
|
14
|
-
left join lateral ( select locales, default_locale, name as space_name from site.spaces where space_id= 'default' limit 1) b on 1=1
|
|
15
|
-
where ${query.type === 'collection' ? `type='collection'` : (query.type === 'single' ? `type='single'` : '1=1')}`;
|
|
16
|
-
|
|
17
|
-
const where = query.search?.replace ? `(name ilike '%${query.search.replace(/'/, "''")}%' or title ilike '%${query.search.replace(/'/, "''")}%')` : '1=1';
|
|
18
|
-
const q1 = q + `and ${where} order by case when status='archived' then true else false end limit ${limit} offset ${offset}`;
|
|
19
|
-
|
|
20
|
-
if (query.sql) return q1;
|
|
21
|
-
|
|
22
|
-
const { rows = [] } = await pg.query(q1);
|
|
23
|
-
|
|
24
|
-
const { total = 0, filtered = 0 } = await pg.query(`select
|
|
25
|
-
count(*)::int as total,
|
|
26
|
-
count(*) filter (where ${where})::int as filtered
|
|
27
|
-
from (${q})q`
|
|
28
|
-
).then(el => el.rows?.[0] || {});
|
|
29
|
-
|
|
30
|
-
await Promise.all(rows.map(async row => {
|
|
31
|
-
if (row.table && false) {
|
|
32
|
-
await pg.query('alter table data.' + row.table + ' add column if not exists created_at timestamp with time zone not null default now()');
|
|
33
|
-
await pg.query('alter table data.' + row.table + ' add column if not exists updated_at timestamp with time zone not null default now()');
|
|
34
|
-
await pg.query('alter table data.' + row.table + ' add column if not exists created_by text');
|
|
35
|
-
await pg.query('alter table data.' + row.table + ' add column if not exists updated_by text');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const q2 = row.table
|
|
39
|
-
? `select count(*) from data."${row.table}" where $1=$1`
|
|
40
|
-
: `select count(*) from site.contents where $1=$1 and ${row.id === 'pages' ? `slug <> 'pages' and content_type_id in (select content_type_id from site.content_types where type = 'single' or content_type_id='pages')` : 'content_type_id=$1'}`;
|
|
41
|
-
|
|
42
|
-
const q1 = row.table
|
|
43
|
-
? `select updated_at from data."${row.table}" where $1=$1 order by updated_at desc limit 1`
|
|
44
|
-
: `select updated_at from site.content_data where content_id in ( select content_id from site.contents where content_type_id=$1 limit 1) order by updated_at desc limit 1`;
|
|
45
|
-
|
|
46
|
-
const count = await pg.queryCache(q2, { table: row.table ? `data.${row.table}` : 'site.content_data', args: [row.id] }).then(el => el.rows?.[0]?.count || '0');
|
|
47
|
-
|
|
48
|
-
const lastEdit = await pg.queryCache(q1, { table: row.table ? `data.${row.table}` : 'site.content_data', args: [row.id] }).then(el => el.rows?.[0]?.updated_at);
|
|
49
|
-
|
|
50
|
-
Object.assign(row, { columns: undefined, fields: row.columns?.length || '0', entries: +count, last_edit: lastEdit });
|
|
51
|
-
}));
|
|
52
|
-
|
|
53
|
-
return reply.status(200).send({ total, filtered, count: rows.length, provider: 'pg', rows });
|
|
54
|
-
}
|
|
1
|
+
import { config, pgClients } from '@opengis/fastify-table/utils.js';
|
|
2
|
+
|
|
3
|
+
const maxLimit = 100;
|
|
4
|
+
|
|
5
|
+
export default async function builderList({ pg = pgClients.client, query = {} }, reply) {
|
|
6
|
+
if (!pg) {
|
|
7
|
+
return reply.status(200).send({ total: 0, msg: 'empty pg', rows: [] });
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const limit = Math.min(maxLimit, query.limit || 16);
|
|
11
|
+
const offset = query.page && query.page > 0 ? (query.page - 1) * limit : 0;
|
|
12
|
+
|
|
13
|
+
const q = `select a.content_type_id as id, a.name, a.title, a.description, a.table_name as table, type, columns, a.include_search, json_build_object('locales', locales, 'default_locale', default_locale, 'name', space_name) as space FROM site.content_types a
|
|
14
|
+
left join lateral ( select locales, default_locale, name as space_name from site.spaces where space_id= 'default' limit 1) b on 1=1
|
|
15
|
+
where ${query.type === 'collection' ? `type='collection'` : (query.type === 'single' ? `type='single'` : '1=1')}`;
|
|
16
|
+
|
|
17
|
+
const where = query.search?.replace ? `(name ilike '%${query.search.replace(/'/, "''")}%' or title ilike '%${query.search.replace(/'/, "''")}%')` : '1=1';
|
|
18
|
+
const q1 = q + `and ${where} order by case when status='archived' then true else false end limit ${limit} offset ${offset}`;
|
|
19
|
+
|
|
20
|
+
if (query.sql) return q1;
|
|
21
|
+
|
|
22
|
+
const { rows = [] } = await pg.query(q1);
|
|
23
|
+
|
|
24
|
+
const { total = 0, filtered = 0 } = await pg.query(`select
|
|
25
|
+
count(*)::int as total,
|
|
26
|
+
count(*) filter (where ${where})::int as filtered
|
|
27
|
+
from (${q})q`
|
|
28
|
+
).then(el => el.rows?.[0] || {});
|
|
29
|
+
|
|
30
|
+
await Promise.all(rows.map(async row => {
|
|
31
|
+
if (row.table && false) {
|
|
32
|
+
await pg.query('alter table data.' + row.table + ' add column if not exists created_at timestamp with time zone not null default now()');
|
|
33
|
+
await pg.query('alter table data.' + row.table + ' add column if not exists updated_at timestamp with time zone not null default now()');
|
|
34
|
+
await pg.query('alter table data.' + row.table + ' add column if not exists created_by text');
|
|
35
|
+
await pg.query('alter table data.' + row.table + ' add column if not exists updated_by text');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const q2 = row.table
|
|
39
|
+
? `select count(*) from data."${row.table}" where $1=$1`
|
|
40
|
+
: `select count(*) from site.contents where $1=$1 and ${row.id === 'pages' ? `slug <> 'pages' and content_type_id in (select content_type_id from site.content_types where type = 'single' or content_type_id='pages')` : 'content_type_id=$1'}`;
|
|
41
|
+
|
|
42
|
+
const q1 = row.table
|
|
43
|
+
? `select updated_at from data."${row.table}" where $1=$1 order by updated_at desc limit 1`
|
|
44
|
+
: `select updated_at from site.content_data where content_id in ( select content_id from site.contents where content_type_id=$1 limit 1) order by updated_at desc limit 1`;
|
|
45
|
+
|
|
46
|
+
const count = await pg.queryCache(q2, { table: row.table ? `data.${row.table}` : 'site.content_data', args: [row.id] }).then(el => el.rows?.[0]?.count || '0');
|
|
47
|
+
|
|
48
|
+
const lastEdit = await pg.queryCache(q1, { table: row.table ? `data.${row.table}` : 'site.content_data', args: [row.id] }).then(el => el.rows?.[0]?.updated_at);
|
|
49
|
+
|
|
50
|
+
Object.assign(row, { columns: undefined, fields: row.columns?.length || '0', entries: +count, last_edit: lastEdit });
|
|
51
|
+
}));
|
|
52
|
+
|
|
53
|
+
return reply.status(200).send({ total, filtered, count: rows.length, provider: 'pg', rows });
|
|
54
|
+
}
|
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
import { dataUpdate, dataDelete, pgClients } from '@opengis/fastify-table/utils.js';
|
|
2
|
-
|
|
3
|
-
export default async function builderDelete({ pg = pgClients.client, user = {}, params = {} }, reply) {
|
|
4
|
-
if (!params.id) {
|
|
5
|
-
return reply.status(400).send('not enough params: id');
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
if (!pg?.pk?.['site.content_types']) {
|
|
9
|
-
return reply.status(404).send('table not found');
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const { cid, table, title, type } = await pg.query(
|
|
13
|
-
`select content_type_id as cid, table_name as table, title, type from site.content_types where $1 in (content_type_id, name)`,
|
|
14
|
-
[params.id],
|
|
15
|
-
).then(el => el.rows?.[0] || {});
|
|
16
|
-
|
|
17
|
-
if (!cid) {
|
|
18
|
-
return reply.status(404).send('content type not found');
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (cid === 'pages') {
|
|
22
|
-
return reply.status(403).send('access restricted: pages content type cannot be deleted');
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// await pg.query('delete from site.content_data where content_id in (select content_id from site.contents where content_type_id=$1)', [cid]);
|
|
26
|
-
// await pg.query('delete from site.contents where content_type_id=$1', [cid]);
|
|
27
|
-
|
|
28
|
-
if (table) {
|
|
29
|
-
// await pg.query(`drop table if exists data."${table}"`);
|
|
30
|
-
if (pg?.pk && false) {
|
|
31
|
-
delete pg.pk[`data."${table}"`];
|
|
32
|
-
pg.tlist = pg.tlist.filter(el => el !== `data."${table}"`);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// await pg.query('delete from site.content_data where content_id in (select content_id from site.contents where content_type_id=$1)', [cid])
|
|
36
|
-
|
|
37
|
-
// await pg.query('delete from site.contents where content_type_id=$1', [cid]);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// cascade
|
|
41
|
-
const contentIds = await pg.query('select array_agg(content_id) from site.contents where content_type_id=$1 and title<>$2', [cid, title])
|
|
42
|
-
.then(el => el.rows?.[0]?.array_agg || []);
|
|
43
|
-
|
|
44
|
-
if (contentIds.length) {
|
|
45
|
-
// await pg.query('delete from site.content_data where content_id=any($1)', [contentIds]);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (type === 'collection' && table) {
|
|
49
|
-
const count = await pg.query(`select count(*)::int from data."${table}"`).then(el => el.rows?.[0]?.count || 0);
|
|
50
|
-
if (count === 0) {
|
|
51
|
-
await pg.query('delete from site.contents where content_type_id=$1', [cid]);
|
|
52
|
-
const res = await dataDelete({
|
|
53
|
-
pg,
|
|
54
|
-
table: 'site.content_types',
|
|
55
|
-
id: cid,
|
|
56
|
-
uid: user?.uid
|
|
57
|
-
});
|
|
58
|
-
// await pg.query(`drop table if exists data."${table}"`);
|
|
59
|
-
return { id: cid, rows: [res].filter(Boolean) };
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// console.log('archived', cid, contentIds);
|
|
64
|
-
const res = await dataUpdate({
|
|
65
|
-
pg,
|
|
66
|
-
table: 'site.content_types',
|
|
67
|
-
id: cid,
|
|
68
|
-
data: {
|
|
69
|
-
status: 'archived',
|
|
70
|
-
},
|
|
71
|
-
uid: user?.uid
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
return { id: cid, rows: [res].filter(Boolean) };
|
|
75
|
-
}
|
|
1
|
+
import { dataUpdate, dataDelete, pgClients } from '@opengis/fastify-table/utils.js';
|
|
2
|
+
|
|
3
|
+
export default async function builderDelete({ pg = pgClients.client, user = {}, params = {} }, reply) {
|
|
4
|
+
if (!params.id) {
|
|
5
|
+
return reply.status(400).send('not enough params: id');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (!pg?.pk?.['site.content_types']) {
|
|
9
|
+
return reply.status(404).send('table not found');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const { cid, table, title, type } = await pg.query(
|
|
13
|
+
`select content_type_id as cid, table_name as table, title, type from site.content_types where $1 in (content_type_id, name)`,
|
|
14
|
+
[params.id],
|
|
15
|
+
).then(el => el.rows?.[0] || {});
|
|
16
|
+
|
|
17
|
+
if (!cid) {
|
|
18
|
+
return reply.status(404).send('content type not found');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (cid === 'pages') {
|
|
22
|
+
return reply.status(403).send('access restricted: pages content type cannot be deleted');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// await pg.query('delete from site.content_data where content_id in (select content_id from site.contents where content_type_id=$1)', [cid]);
|
|
26
|
+
// await pg.query('delete from site.contents where content_type_id=$1', [cid]);
|
|
27
|
+
|
|
28
|
+
if (table) {
|
|
29
|
+
// await pg.query(`drop table if exists data."${table}"`);
|
|
30
|
+
if (pg?.pk && false) {
|
|
31
|
+
delete pg.pk[`data."${table}"`];
|
|
32
|
+
pg.tlist = pg.tlist.filter(el => el !== `data."${table}"`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// await pg.query('delete from site.content_data where content_id in (select content_id from site.contents where content_type_id=$1)', [cid])
|
|
36
|
+
|
|
37
|
+
// await pg.query('delete from site.contents where content_type_id=$1', [cid]);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// cascade
|
|
41
|
+
const contentIds = await pg.query('select array_agg(content_id) from site.contents where content_type_id=$1 and title<>$2', [cid, title])
|
|
42
|
+
.then(el => el.rows?.[0]?.array_agg || []);
|
|
43
|
+
|
|
44
|
+
if (contentIds.length) {
|
|
45
|
+
// await pg.query('delete from site.content_data where content_id=any($1)', [contentIds]);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (type === 'collection' && table) {
|
|
49
|
+
const count = await pg.query(`select count(*)::int from data."${table}"`).then(el => el.rows?.[0]?.count || 0);
|
|
50
|
+
if (count === 0) {
|
|
51
|
+
await pg.query('delete from site.contents where content_type_id=$1', [cid]);
|
|
52
|
+
const res = await dataDelete({
|
|
53
|
+
pg,
|
|
54
|
+
table: 'site.content_types',
|
|
55
|
+
id: cid,
|
|
56
|
+
uid: user?.uid
|
|
57
|
+
});
|
|
58
|
+
// await pg.query(`drop table if exists data."${table}"`);
|
|
59
|
+
return { id: cid, rows: [res].filter(Boolean) };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// console.log('archived', cid, contentIds);
|
|
64
|
+
const res = await dataUpdate({
|
|
65
|
+
pg,
|
|
66
|
+
table: 'site.content_types',
|
|
67
|
+
id: cid,
|
|
68
|
+
data: {
|
|
69
|
+
status: 'archived',
|
|
70
|
+
},
|
|
71
|
+
uid: user?.uid
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
return { id: cid, rows: [res].filter(Boolean) };
|
|
75
|
+
}
|