@btst/stack 2.1.0 → 2.2.0
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/dist/api/index.cjs +9 -1
- package/dist/api/index.d.cts +4 -4
- package/dist/api/index.d.mts +4 -4
- package/dist/api/index.d.ts +4 -4
- package/dist/api/index.mjs +9 -1
- package/dist/client/index.d.cts +2 -2
- package/dist/client/index.d.mts +2 -2
- package/dist/client/index.d.ts +2 -2
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/packages/stack/src/plugins/ai-chat/api/getters.cjs +42 -0
- package/dist/packages/stack/src/plugins/ai-chat/api/getters.mjs +39 -0
- package/dist/packages/stack/src/plugins/ai-chat/api/plugin.cjs +5 -0
- package/dist/packages/stack/src/plugins/ai-chat/api/plugin.mjs +5 -0
- package/dist/packages/stack/src/plugins/blog/api/getters.cjs +131 -0
- package/dist/packages/stack/src/plugins/blog/api/getters.mjs +127 -0
- package/dist/packages/stack/src/plugins/blog/api/plugin.cjs +9 -107
- package/dist/packages/stack/src/plugins/blog/api/plugin.mjs +9 -107
- package/dist/packages/stack/src/plugins/blog/client/plugin.cjs +1 -1
- package/dist/packages/stack/src/plugins/blog/client/plugin.mjs +1 -1
- package/dist/packages/stack/src/plugins/cms/api/getters.cjs +146 -0
- package/dist/packages/stack/src/plugins/cms/api/getters.mjs +138 -0
- package/dist/packages/stack/src/plugins/cms/api/plugin.cjs +560 -622
- package/dist/packages/stack/src/plugins/cms/api/plugin.mjs +559 -621
- package/dist/packages/stack/src/plugins/cms/client/components/pages/content-editor-page.internal.cjs +1 -1
- package/dist/packages/stack/src/plugins/cms/client/components/pages/content-editor-page.internal.mjs +1 -1
- package/dist/packages/stack/src/plugins/cms/client/hooks/cms-hooks.cjs +6 -3
- package/dist/packages/stack/src/plugins/cms/client/hooks/cms-hooks.mjs +6 -3
- package/dist/packages/stack/src/plugins/form-builder/api/getters.cjs +111 -0
- package/dist/packages/stack/src/plugins/form-builder/api/getters.mjs +104 -0
- package/dist/packages/stack/src/plugins/form-builder/api/plugin.cjs +16 -88
- package/dist/packages/stack/src/plugins/form-builder/api/plugin.mjs +12 -84
- package/dist/packages/stack/src/plugins/form-builder/client/components/pages/submissions-page.internal.cjs +1 -1
- package/dist/packages/stack/src/plugins/form-builder/client/components/pages/submissions-page.internal.mjs +1 -1
- package/dist/packages/stack/src/plugins/kanban/api/getters.cjs +84 -0
- package/dist/packages/stack/src/plugins/kanban/api/getters.mjs +81 -0
- package/dist/packages/stack/src/plugins/kanban/api/plugin.cjs +9 -123
- package/dist/packages/stack/src/plugins/kanban/api/plugin.mjs +9 -123
- package/dist/packages/stack/src/plugins/kanban/client/plugin.cjs +1 -1
- package/dist/packages/stack/src/plugins/kanban/client/plugin.mjs +1 -1
- package/dist/plugins/ai-chat/api/index.cjs +3 -0
- package/dist/plugins/ai-chat/api/index.d.cts +27 -4
- package/dist/plugins/ai-chat/api/index.d.mts +27 -4
- package/dist/plugins/ai-chat/api/index.d.ts +27 -4
- package/dist/plugins/ai-chat/api/index.mjs +1 -0
- package/dist/plugins/ai-chat/client/hooks/index.d.cts +2 -2
- package/dist/plugins/ai-chat/client/hooks/index.d.mts +2 -2
- package/dist/plugins/ai-chat/client/hooks/index.d.ts +2 -2
- package/dist/plugins/ai-chat/query-keys.d.cts +9 -284
- package/dist/plugins/ai-chat/query-keys.d.mts +9 -284
- package/dist/plugins/ai-chat/query-keys.d.ts +9 -284
- package/dist/plugins/api/index.d.cts +4 -3
- package/dist/plugins/api/index.d.mts +4 -3
- package/dist/plugins/api/index.d.ts +4 -3
- package/dist/plugins/blog/api/index.cjs +4 -0
- package/dist/plugins/blog/api/index.d.cts +3 -2
- package/dist/plugins/blog/api/index.d.mts +3 -2
- package/dist/plugins/blog/api/index.d.ts +3 -2
- package/dist/plugins/blog/api/index.mjs +1 -0
- package/dist/plugins/blog/client/hooks/index.d.cts +4 -4
- package/dist/plugins/blog/client/hooks/index.d.mts +4 -4
- package/dist/plugins/blog/client/hooks/index.d.ts +4 -4
- package/dist/plugins/blog/client/index.d.cts +1 -1
- package/dist/plugins/blog/client/index.d.mts +1 -1
- package/dist/plugins/blog/client/index.d.ts +1 -1
- package/dist/plugins/blog/query-keys.cjs +7 -4
- package/dist/plugins/blog/query-keys.d.cts +81 -27
- package/dist/plugins/blog/query-keys.d.mts +81 -27
- package/dist/plugins/blog/query-keys.d.ts +81 -27
- package/dist/plugins/blog/query-keys.mjs +7 -4
- package/dist/plugins/client/index.d.cts +2 -2
- package/dist/plugins/client/index.d.mts +2 -2
- package/dist/plugins/client/index.d.ts +2 -2
- package/dist/plugins/cms/api/index.cjs +4 -0
- package/dist/plugins/cms/api/index.d.cts +61 -5
- package/dist/plugins/cms/api/index.d.mts +61 -5
- package/dist/plugins/cms/api/index.d.ts +61 -5
- package/dist/plugins/cms/api/index.mjs +1 -0
- package/dist/plugins/cms/client/hooks/index.d.cts +1 -1
- package/dist/plugins/cms/client/hooks/index.d.mts +1 -1
- package/dist/plugins/cms/client/hooks/index.d.ts +1 -1
- package/dist/plugins/cms/query-keys.d.cts +2 -1
- package/dist/plugins/cms/query-keys.d.mts +2 -1
- package/dist/plugins/cms/query-keys.d.ts +2 -1
- package/dist/plugins/form-builder/api/index.cjs +4 -0
- package/dist/plugins/form-builder/api/index.d.cts +77 -7
- package/dist/plugins/form-builder/api/index.d.mts +77 -7
- package/dist/plugins/form-builder/api/index.d.ts +77 -7
- package/dist/plugins/form-builder/api/index.mjs +1 -0
- package/dist/plugins/form-builder/client/components/index.d.cts +1 -1
- package/dist/plugins/form-builder/client/components/index.d.mts +1 -1
- package/dist/plugins/form-builder/client/components/index.d.ts +1 -1
- package/dist/plugins/form-builder/client/hooks/index.d.cts +1 -1
- package/dist/plugins/form-builder/client/hooks/index.d.mts +1 -1
- package/dist/plugins/form-builder/client/hooks/index.d.ts +1 -1
- package/dist/plugins/form-builder/query-keys.d.cts +2 -1
- package/dist/plugins/form-builder/query-keys.d.mts +2 -1
- package/dist/plugins/form-builder/query-keys.d.ts +2 -1
- package/dist/plugins/kanban/api/index.cjs +3 -0
- package/dist/plugins/kanban/api/index.d.cts +40 -43
- package/dist/plugins/kanban/api/index.d.mts +40 -43
- package/dist/plugins/kanban/api/index.d.ts +40 -43
- package/dist/plugins/kanban/api/index.mjs +1 -0
- package/dist/plugins/kanban/client/components/index.d.cts +1 -1
- package/dist/plugins/kanban/client/components/index.d.mts +1 -1
- package/dist/plugins/kanban/client/components/index.d.ts +1 -1
- package/dist/plugins/kanban/client/hooks/index.d.cts +1 -1
- package/dist/plugins/kanban/client/hooks/index.d.mts +1 -1
- package/dist/plugins/kanban/client/hooks/index.d.ts +1 -1
- package/dist/plugins/kanban/client/index.d.cts +1 -1
- package/dist/plugins/kanban/client/index.d.mts +1 -1
- package/dist/plugins/kanban/client/index.d.ts +1 -1
- package/dist/plugins/kanban/query-keys.cjs +4 -3
- package/dist/plugins/kanban/query-keys.d.cts +2 -1
- package/dist/plugins/kanban/query-keys.d.mts +2 -1
- package/dist/plugins/kanban/query-keys.d.ts +2 -1
- package/dist/plugins/kanban/query-keys.mjs +4 -3
- package/dist/plugins/open-api/api/index.d.cts +2 -2
- package/dist/plugins/open-api/api/index.d.mts +2 -2
- package/dist/plugins/open-api/api/index.d.ts +2 -2
- package/dist/plugins/route-docs/client/index.d.cts +1 -1
- package/dist/plugins/route-docs/client/index.d.mts +1 -1
- package/dist/plugins/route-docs/client/index.d.ts +1 -1
- package/dist/plugins/ui-builder/index.d.cts +1 -1
- package/dist/plugins/ui-builder/index.d.mts +1 -1
- package/dist/plugins/ui-builder/index.d.ts +1 -1
- package/dist/shared/{stack.BoA0xkJv.d.cts → stack.7n9Y_u7N.d.cts} +33 -7
- package/dist/shared/{stack.BoA0xkJv.d.mts → stack.7n9Y_u7N.d.mts} +33 -7
- package/dist/shared/{stack.BoA0xkJv.d.ts → stack.7n9Y_u7N.d.ts} +33 -7
- package/dist/shared/stack.BeSm90va.d.ts +289 -0
- package/dist/shared/{stack.DzH_wcvr.d.mts → stack.CIrIsc-A.d.cts} +2 -2
- package/dist/shared/{stack.DzH_wcvr.d.ts → stack.CIrIsc-A.d.mts} +2 -2
- package/dist/shared/{stack.DzH_wcvr.d.cts → stack.CIrIsc-A.d.ts} +2 -2
- package/dist/shared/stack.CMh_EdxW.d.cts +289 -0
- package/dist/shared/{stack.BsXokfNh.d.mts → stack.CXjzTMsb.d.cts} +1 -1
- package/dist/shared/{stack.BsXokfNh.d.ts → stack.CXjzTMsb.d.mts} +1 -1
- package/dist/shared/{stack.BsXokfNh.d.cts → stack.CXjzTMsb.d.ts} +1 -1
- package/dist/shared/stack.Dg09R0oB.d.mts +289 -0
- package/dist/shared/{stack.DKDMI-QO.d.mts → stack.QD1y_7NY.d.cts} +7 -1
- package/dist/shared/{stack.DKDMI-QO.d.ts → stack.QD1y_7NY.d.mts} +7 -1
- package/dist/shared/{stack.DKDMI-QO.d.cts → stack.QD1y_7NY.d.ts} +7 -1
- package/package.json +1 -1
- package/src/__tests__/stack-api.test.ts +118 -0
- package/src/api/index.ts +15 -1
- package/src/plugins/ai-chat/__tests__/getters.test.ts +109 -0
- package/src/plugins/ai-chat/api/getters.ts +71 -0
- package/src/plugins/ai-chat/api/index.ts +1 -0
- package/src/plugins/ai-chat/api/plugin.ts +8 -0
- package/src/plugins/api/index.ts +3 -1
- package/src/plugins/blog/__tests__/getters.test.ts +540 -0
- package/src/plugins/blog/api/getters.ts +243 -0
- package/src/plugins/blog/api/index.ts +7 -0
- package/src/plugins/blog/api/plugin.ts +13 -141
- package/src/plugins/blog/client/plugin.tsx +2 -1
- package/src/plugins/blog/query-keys.ts +16 -13
- package/src/plugins/cms/__tests__/getters.test.ts +206 -0
- package/src/plugins/cms/api/getters.ts +244 -0
- package/src/plugins/cms/api/index.ts +5 -0
- package/src/plugins/cms/api/plugin.ts +50 -154
- package/src/plugins/cms/client/components/pages/content-editor-page.internal.tsx +1 -1
- package/src/plugins/cms/client/hooks/cms-hooks.tsx +3 -0
- package/src/plugins/cms/types.ts +1 -1
- package/src/plugins/form-builder/__tests__/getters.test.ts +159 -0
- package/src/plugins/form-builder/api/getters.ts +203 -0
- package/src/plugins/form-builder/api/index.ts +1 -0
- package/src/plugins/form-builder/api/plugin.ts +22 -115
- package/src/plugins/form-builder/client/components/pages/submissions-page.internal.tsx +1 -1
- package/src/plugins/form-builder/types.ts +2 -2
- package/src/plugins/kanban/__tests__/getters.test.ts +172 -0
- package/src/plugins/kanban/api/getters.ts +149 -0
- package/src/plugins/kanban/api/index.ts +1 -0
- package/src/plugins/kanban/api/plugin.ts +16 -146
- package/src/plugins/kanban/client/plugin.tsx +2 -1
- package/src/plugins/kanban/query-keys.ts +8 -5
- package/src/types.ts +44 -5
- package/dist/shared/{stack.CbuN2zVV.d.cts → stack.BkYlUT_8.d.cts} +6 -6
- package/dist/shared/{stack.CbuN2zVV.d.mts → stack.BkYlUT_8.d.mts} +6 -6
- package/dist/shared/{stack.CbuN2zVV.d.ts → stack.BkYlUT_8.d.ts} +6 -6
|
@@ -3,6 +3,7 @@ import { z } from 'zod';
|
|
|
3
3
|
import { blogSchema } from '../db.mjs';
|
|
4
4
|
import { slugify } from '../utils.mjs';
|
|
5
5
|
import { createPostSchema, updatePostSchema } from '../schemas.mjs';
|
|
6
|
+
import { getAllTags, getPostBySlug, getAllPosts } from './getters.mjs';
|
|
6
7
|
|
|
7
8
|
const PostListQuerySchema = z.object({
|
|
8
9
|
slug: z.string().optional(),
|
|
@@ -23,6 +24,11 @@ const NextPreviousPostsQuerySchema = z.object({
|
|
|
23
24
|
const blogBackendPlugin = (hooks) => defineBackendPlugin({
|
|
24
25
|
name: "blog",
|
|
25
26
|
dbPlugin: blogSchema,
|
|
27
|
+
api: (adapter) => ({
|
|
28
|
+
getAllPosts: (params) => getAllPosts(adapter, params),
|
|
29
|
+
getPostBySlug: (slug) => getPostBySlug(adapter, slug),
|
|
30
|
+
getAllTags: () => getAllTags(adapter)
|
|
31
|
+
}),
|
|
26
32
|
routes: (adapter) => {
|
|
27
33
|
const findOrCreateTags = async (tagInputs) => {
|
|
28
34
|
if (tagInputs.length === 0) return [];
|
|
@@ -103,111 +109,9 @@ const blogBackendPlugin = (hooks) => defineBackendPlugin({
|
|
|
103
109
|
});
|
|
104
110
|
}
|
|
105
111
|
}
|
|
106
|
-
|
|
107
|
-
if (query.tagSlug) {
|
|
108
|
-
const tag = await adapter.findOne({
|
|
109
|
-
model: "tag",
|
|
110
|
-
where: [
|
|
111
|
-
{
|
|
112
|
-
field: "slug",
|
|
113
|
-
value: query.tagSlug,
|
|
114
|
-
operator: "eq"
|
|
115
|
-
}
|
|
116
|
-
]
|
|
117
|
-
});
|
|
118
|
-
if (!tag) {
|
|
119
|
-
return [];
|
|
120
|
-
}
|
|
121
|
-
const postTags = await adapter.findMany({
|
|
122
|
-
model: "postTag",
|
|
123
|
-
where: [
|
|
124
|
-
{
|
|
125
|
-
field: "tagId",
|
|
126
|
-
value: tag.id,
|
|
127
|
-
operator: "eq"
|
|
128
|
-
}
|
|
129
|
-
]
|
|
130
|
-
});
|
|
131
|
-
tagFilterPostIds = new Set(postTags.map((pt) => pt.postId));
|
|
132
|
-
if (tagFilterPostIds.size === 0) {
|
|
133
|
-
return [];
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
const whereConditions = [];
|
|
137
|
-
if (query.published !== void 0) {
|
|
138
|
-
whereConditions.push({
|
|
139
|
-
field: "published",
|
|
140
|
-
value: query.published,
|
|
141
|
-
operator: "eq"
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
if (query.slug) {
|
|
145
|
-
whereConditions.push({
|
|
146
|
-
field: "slug",
|
|
147
|
-
value: query.slug,
|
|
148
|
-
operator: "eq"
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
const posts = await adapter.findMany({
|
|
152
|
-
model: "post",
|
|
153
|
-
limit: query.query || query.tagSlug ? void 0 : query.limit ?? 10,
|
|
154
|
-
offset: query.query || query.tagSlug ? void 0 : query.offset ?? 0,
|
|
155
|
-
where: whereConditions,
|
|
156
|
-
sortBy: {
|
|
157
|
-
field: "createdAt",
|
|
158
|
-
direction: "desc"
|
|
159
|
-
},
|
|
160
|
-
join: {
|
|
161
|
-
postTag: true
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
const tagIds = /* @__PURE__ */ new Set();
|
|
165
|
-
for (const post of posts) {
|
|
166
|
-
if (post.postTag) {
|
|
167
|
-
for (const pt of post.postTag) {
|
|
168
|
-
tagIds.add(pt.tagId);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
const tags = tagIds.size > 0 ? await adapter.findMany({
|
|
173
|
-
model: "tag"
|
|
174
|
-
}) : [];
|
|
175
|
-
const tagMap = /* @__PURE__ */ new Map();
|
|
176
|
-
for (const tag of tags) {
|
|
177
|
-
if (tagIds.has(tag.id)) {
|
|
178
|
-
tagMap.set(tag.id, tag);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
let result = posts.map((post) => {
|
|
182
|
-
const postTags = (post.postTag || []).map((pt) => {
|
|
183
|
-
const tag = tagMap.get(pt.tagId);
|
|
184
|
-
return tag ? { ...tag } : void 0;
|
|
185
|
-
}).filter((tag) => tag !== void 0);
|
|
186
|
-
const { postTag: _, ...postWithoutJoin } = post;
|
|
187
|
-
return {
|
|
188
|
-
...postWithoutJoin,
|
|
189
|
-
tags: postTags
|
|
190
|
-
};
|
|
191
|
-
});
|
|
192
|
-
if (tagFilterPostIds) {
|
|
193
|
-
result = result.filter((post) => tagFilterPostIds.has(post.id));
|
|
194
|
-
}
|
|
195
|
-
if (query.query) {
|
|
196
|
-
const searchLower = query.query.toLowerCase();
|
|
197
|
-
result = result.filter((post) => {
|
|
198
|
-
const titleMatch = post.title?.toLowerCase().includes(searchLower);
|
|
199
|
-
const contentMatch = post.content?.toLowerCase().includes(searchLower);
|
|
200
|
-
const excerptMatch = post.excerpt?.toLowerCase().includes(searchLower);
|
|
201
|
-
return titleMatch || contentMatch || excerptMatch;
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
if (query.tagSlug || query.query) {
|
|
205
|
-
const offset = query.offset ?? 0;
|
|
206
|
-
const limit = query.limit ?? 10;
|
|
207
|
-
result = result.slice(offset, offset + limit);
|
|
208
|
-
}
|
|
112
|
+
const result = await getAllPosts(adapter, query);
|
|
209
113
|
if (hooks?.onPostsRead) {
|
|
210
|
-
await hooks.onPostsRead(result, query, context);
|
|
114
|
+
await hooks.onPostsRead(result.items, query, context);
|
|
211
115
|
}
|
|
212
116
|
return result;
|
|
213
117
|
} catch (error) {
|
|
@@ -550,9 +454,7 @@ const blogBackendPlugin = (hooks) => defineBackendPlugin({
|
|
|
550
454
|
method: "GET"
|
|
551
455
|
},
|
|
552
456
|
async () => {
|
|
553
|
-
return await adapter
|
|
554
|
-
model: "tag"
|
|
555
|
-
});
|
|
457
|
+
return await getAllTags(adapter);
|
|
556
458
|
}
|
|
557
459
|
);
|
|
558
460
|
return {
|
|
@@ -472,7 +472,7 @@ const blogClientPlugin = (config) => client.defineClientPlugin({
|
|
|
472
472
|
published: "true"
|
|
473
473
|
}
|
|
474
474
|
});
|
|
475
|
-
const page = res.data ?? [];
|
|
475
|
+
const page = res.data?.items ?? [];
|
|
476
476
|
posts.push(...page);
|
|
477
477
|
if (page.length < limit) break;
|
|
478
478
|
offset += limit;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function serializeContentType(ct) {
|
|
4
|
+
const needsMigration = !ct.autoFormVersion || ct.autoFormVersion < 2;
|
|
5
|
+
const migratedJsonSchema = needsMigration ? migrateToUnifiedSchema(ct.jsonSchema, ct.fieldConfig) : ct.jsonSchema;
|
|
6
|
+
return {
|
|
7
|
+
id: ct.id,
|
|
8
|
+
name: ct.name,
|
|
9
|
+
slug: ct.slug,
|
|
10
|
+
description: ct.description,
|
|
11
|
+
jsonSchema: migratedJsonSchema,
|
|
12
|
+
createdAt: ct.createdAt.toISOString(),
|
|
13
|
+
updatedAt: ct.updatedAt.toISOString()
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function migrateToUnifiedSchema(jsonSchemaStr, fieldConfigStr) {
|
|
17
|
+
if (!fieldConfigStr) return jsonSchemaStr;
|
|
18
|
+
try {
|
|
19
|
+
const jsonSchema = JSON.parse(jsonSchemaStr);
|
|
20
|
+
const fieldConfig = JSON.parse(fieldConfigStr);
|
|
21
|
+
if (!jsonSchema.properties || typeof fieldConfig !== "object") {
|
|
22
|
+
return jsonSchemaStr;
|
|
23
|
+
}
|
|
24
|
+
for (const [key, config] of Object.entries(fieldConfig)) {
|
|
25
|
+
if (jsonSchema.properties[key] && typeof config === "object" && config !== null && "fieldType" in config) {
|
|
26
|
+
jsonSchema.properties[key].fieldType = config.fieldType;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return JSON.stringify(jsonSchema);
|
|
30
|
+
} catch {
|
|
31
|
+
return jsonSchemaStr;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function serializeContentItem(item) {
|
|
35
|
+
return {
|
|
36
|
+
...item,
|
|
37
|
+
createdAt: item.createdAt.toISOString(),
|
|
38
|
+
updatedAt: item.updatedAt.toISOString()
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function serializeContentItemWithType(item) {
|
|
42
|
+
const parsedData = JSON.parse(item.data);
|
|
43
|
+
return {
|
|
44
|
+
...serializeContentItem(item),
|
|
45
|
+
parsedData,
|
|
46
|
+
contentType: item.contentType ? serializeContentType(item.contentType) : void 0
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
async function getAllContentTypes(adapter) {
|
|
50
|
+
const contentTypes = await adapter.findMany({
|
|
51
|
+
model: "contentType",
|
|
52
|
+
sortBy: { field: "name", direction: "asc" }
|
|
53
|
+
});
|
|
54
|
+
return contentTypes.map(serializeContentType);
|
|
55
|
+
}
|
|
56
|
+
async function getAllContentItems(adapter, contentTypeSlug, params) {
|
|
57
|
+
const contentType = await adapter.findOne({
|
|
58
|
+
model: "contentType",
|
|
59
|
+
where: [
|
|
60
|
+
{
|
|
61
|
+
field: "slug",
|
|
62
|
+
value: contentTypeSlug,
|
|
63
|
+
operator: "eq"
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
});
|
|
67
|
+
if (!contentType) {
|
|
68
|
+
return {
|
|
69
|
+
items: [],
|
|
70
|
+
total: 0,
|
|
71
|
+
limit: params?.limit,
|
|
72
|
+
offset: params?.offset
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
const whereConditions = [
|
|
76
|
+
{
|
|
77
|
+
field: "contentTypeId",
|
|
78
|
+
value: contentType.id,
|
|
79
|
+
operator: "eq"
|
|
80
|
+
}
|
|
81
|
+
];
|
|
82
|
+
if (params?.slug) {
|
|
83
|
+
whereConditions.push({
|
|
84
|
+
field: "slug",
|
|
85
|
+
value: params.slug,
|
|
86
|
+
operator: "eq"
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
const total = await adapter.count({
|
|
90
|
+
model: "contentItem",
|
|
91
|
+
where: whereConditions
|
|
92
|
+
});
|
|
93
|
+
const items = await adapter.findMany({
|
|
94
|
+
model: "contentItem",
|
|
95
|
+
where: whereConditions,
|
|
96
|
+
limit: params?.limit,
|
|
97
|
+
offset: params?.offset,
|
|
98
|
+
sortBy: { field: "createdAt", direction: "desc" },
|
|
99
|
+
join: { contentType: true }
|
|
100
|
+
});
|
|
101
|
+
return {
|
|
102
|
+
items: items.map(serializeContentItemWithType),
|
|
103
|
+
total,
|
|
104
|
+
limit: params?.limit,
|
|
105
|
+
offset: params?.offset
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
async function getContentItemBySlug(adapter, contentTypeSlug, slug) {
|
|
109
|
+
const contentType = await adapter.findOne({
|
|
110
|
+
model: "contentType",
|
|
111
|
+
where: [
|
|
112
|
+
{
|
|
113
|
+
field: "slug",
|
|
114
|
+
value: contentTypeSlug,
|
|
115
|
+
operator: "eq"
|
|
116
|
+
}
|
|
117
|
+
]
|
|
118
|
+
});
|
|
119
|
+
if (!contentType) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
const item = await adapter.findOne({
|
|
123
|
+
model: "contentItem",
|
|
124
|
+
where: [
|
|
125
|
+
{
|
|
126
|
+
field: "contentTypeId",
|
|
127
|
+
value: contentType.id,
|
|
128
|
+
operator: "eq"
|
|
129
|
+
},
|
|
130
|
+
{ field: "slug", value: slug, operator: "eq" }
|
|
131
|
+
],
|
|
132
|
+
join: { contentType: true }
|
|
133
|
+
});
|
|
134
|
+
if (!item) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
return serializeContentItemWithType(item);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
exports.getAllContentItems = getAllContentItems;
|
|
141
|
+
exports.getAllContentTypes = getAllContentTypes;
|
|
142
|
+
exports.getContentItemBySlug = getContentItemBySlug;
|
|
143
|
+
exports.migrateToUnifiedSchema = migrateToUnifiedSchema;
|
|
144
|
+
exports.serializeContentItem = serializeContentItem;
|
|
145
|
+
exports.serializeContentItemWithType = serializeContentItemWithType;
|
|
146
|
+
exports.serializeContentType = serializeContentType;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
function serializeContentType(ct) {
|
|
2
|
+
const needsMigration = !ct.autoFormVersion || ct.autoFormVersion < 2;
|
|
3
|
+
const migratedJsonSchema = needsMigration ? migrateToUnifiedSchema(ct.jsonSchema, ct.fieldConfig) : ct.jsonSchema;
|
|
4
|
+
return {
|
|
5
|
+
id: ct.id,
|
|
6
|
+
name: ct.name,
|
|
7
|
+
slug: ct.slug,
|
|
8
|
+
description: ct.description,
|
|
9
|
+
jsonSchema: migratedJsonSchema,
|
|
10
|
+
createdAt: ct.createdAt.toISOString(),
|
|
11
|
+
updatedAt: ct.updatedAt.toISOString()
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function migrateToUnifiedSchema(jsonSchemaStr, fieldConfigStr) {
|
|
15
|
+
if (!fieldConfigStr) return jsonSchemaStr;
|
|
16
|
+
try {
|
|
17
|
+
const jsonSchema = JSON.parse(jsonSchemaStr);
|
|
18
|
+
const fieldConfig = JSON.parse(fieldConfigStr);
|
|
19
|
+
if (!jsonSchema.properties || typeof fieldConfig !== "object") {
|
|
20
|
+
return jsonSchemaStr;
|
|
21
|
+
}
|
|
22
|
+
for (const [key, config] of Object.entries(fieldConfig)) {
|
|
23
|
+
if (jsonSchema.properties[key] && typeof config === "object" && config !== null && "fieldType" in config) {
|
|
24
|
+
jsonSchema.properties[key].fieldType = config.fieldType;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return JSON.stringify(jsonSchema);
|
|
28
|
+
} catch {
|
|
29
|
+
return jsonSchemaStr;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function serializeContentItem(item) {
|
|
33
|
+
return {
|
|
34
|
+
...item,
|
|
35
|
+
createdAt: item.createdAt.toISOString(),
|
|
36
|
+
updatedAt: item.updatedAt.toISOString()
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function serializeContentItemWithType(item) {
|
|
40
|
+
const parsedData = JSON.parse(item.data);
|
|
41
|
+
return {
|
|
42
|
+
...serializeContentItem(item),
|
|
43
|
+
parsedData,
|
|
44
|
+
contentType: item.contentType ? serializeContentType(item.contentType) : void 0
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
async function getAllContentTypes(adapter) {
|
|
48
|
+
const contentTypes = await adapter.findMany({
|
|
49
|
+
model: "contentType",
|
|
50
|
+
sortBy: { field: "name", direction: "asc" }
|
|
51
|
+
});
|
|
52
|
+
return contentTypes.map(serializeContentType);
|
|
53
|
+
}
|
|
54
|
+
async function getAllContentItems(adapter, contentTypeSlug, params) {
|
|
55
|
+
const contentType = await adapter.findOne({
|
|
56
|
+
model: "contentType",
|
|
57
|
+
where: [
|
|
58
|
+
{
|
|
59
|
+
field: "slug",
|
|
60
|
+
value: contentTypeSlug,
|
|
61
|
+
operator: "eq"
|
|
62
|
+
}
|
|
63
|
+
]
|
|
64
|
+
});
|
|
65
|
+
if (!contentType) {
|
|
66
|
+
return {
|
|
67
|
+
items: [],
|
|
68
|
+
total: 0,
|
|
69
|
+
limit: params?.limit,
|
|
70
|
+
offset: params?.offset
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const whereConditions = [
|
|
74
|
+
{
|
|
75
|
+
field: "contentTypeId",
|
|
76
|
+
value: contentType.id,
|
|
77
|
+
operator: "eq"
|
|
78
|
+
}
|
|
79
|
+
];
|
|
80
|
+
if (params?.slug) {
|
|
81
|
+
whereConditions.push({
|
|
82
|
+
field: "slug",
|
|
83
|
+
value: params.slug,
|
|
84
|
+
operator: "eq"
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const total = await adapter.count({
|
|
88
|
+
model: "contentItem",
|
|
89
|
+
where: whereConditions
|
|
90
|
+
});
|
|
91
|
+
const items = await adapter.findMany({
|
|
92
|
+
model: "contentItem",
|
|
93
|
+
where: whereConditions,
|
|
94
|
+
limit: params?.limit,
|
|
95
|
+
offset: params?.offset,
|
|
96
|
+
sortBy: { field: "createdAt", direction: "desc" },
|
|
97
|
+
join: { contentType: true }
|
|
98
|
+
});
|
|
99
|
+
return {
|
|
100
|
+
items: items.map(serializeContentItemWithType),
|
|
101
|
+
total,
|
|
102
|
+
limit: params?.limit,
|
|
103
|
+
offset: params?.offset
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
async function getContentItemBySlug(adapter, contentTypeSlug, slug) {
|
|
107
|
+
const contentType = await adapter.findOne({
|
|
108
|
+
model: "contentType",
|
|
109
|
+
where: [
|
|
110
|
+
{
|
|
111
|
+
field: "slug",
|
|
112
|
+
value: contentTypeSlug,
|
|
113
|
+
operator: "eq"
|
|
114
|
+
}
|
|
115
|
+
]
|
|
116
|
+
});
|
|
117
|
+
if (!contentType) {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
const item = await adapter.findOne({
|
|
121
|
+
model: "contentItem",
|
|
122
|
+
where: [
|
|
123
|
+
{
|
|
124
|
+
field: "contentTypeId",
|
|
125
|
+
value: contentType.id,
|
|
126
|
+
operator: "eq"
|
|
127
|
+
},
|
|
128
|
+
{ field: "slug", value: slug, operator: "eq" }
|
|
129
|
+
],
|
|
130
|
+
join: { contentType: true }
|
|
131
|
+
});
|
|
132
|
+
if (!item) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
return serializeContentItemWithType(item);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export { getAllContentItems, getAllContentTypes, getContentItemBySlug, migrateToUnifiedSchema, serializeContentItem, serializeContentItemWithType, serializeContentType };
|