@basicbenframework/cms 0.8.0 → 0.9.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/lib/client/pages/Authentication.js +1 -1
- package/lib/client/pages/Database.js +15 -10
- package/lib/client/pages/Testing.js +1 -1
- package/lib/client/styles.css +9 -0
- package/lib/features/members/Member.js +3 -2
- package/lib/helpers/insert-row.d.ts +18 -0
- package/lib/helpers/insert-row.js +18 -0
- package/lib/hooks.js +21 -0
- package/lib/models/Category.js +3 -2
- package/lib/models/Comment.js +3 -2
- package/lib/models/ContentType.js +3 -2
- package/lib/models/Credential.js +5 -1
- package/lib/models/Media.js +3 -2
- package/lib/models/Menu.js +3 -2
- package/lib/models/Page.js +3 -2
- package/lib/models/Post.js +3 -2
- package/lib/models/PostRevision.js +4 -3
- package/lib/models/Snippet.js +3 -2
- package/lib/models/Tag.js +3 -2
- package/lib/models/Taxonomy.js +5 -2
- package/lib/models/User.js +5 -1
- package/package.json +3 -3
|
@@ -66,7 +66,7 @@ export const AuthController = {
|
|
|
66
66
|
const isFirstUser = (await (await query('users')).count()) === 0
|
|
67
67
|
const role = isFirstUser ? ROLES.ADMIN : DEFAULT_ROLE
|
|
68
68
|
|
|
69
|
-
const { lastInsertRowid } = await (await query('users')).insert({
|
|
69
|
+
const { lastInsertRowid } = await (await query('users')).only('name', 'email', 'password', 'role').insert({
|
|
70
70
|
name,
|
|
71
71
|
email,
|
|
72
72
|
password: await hashPassword(password),
|
|
@@ -45,7 +45,7 @@ import { ROLES } from '@basicbenframework/core/auth/permissions'
|
|
|
45
45
|
export async function seed() {
|
|
46
46
|
const password = await hashPassword('password123')
|
|
47
47
|
|
|
48
|
-
await (await db.table('users')).insert({
|
|
48
|
+
await (await db.table('users')).only('name', 'email', 'password', 'role').insert({
|
|
49
49
|
name: 'Admin User',
|
|
50
50
|
email: 'admin@example.com',
|
|
51
51
|
password,
|
|
@@ -58,7 +58,7 @@ export async function seed() {
|
|
|
58
58
|
export async function seed() {
|
|
59
59
|
const user = await (await db.table('users')).first()
|
|
60
60
|
|
|
61
|
-
await (await db.table('posts')).insert({
|
|
61
|
+
await (await db.table('posts')).only('user_id', 'title', 'content', 'published').insert({
|
|
62
62
|
user_id: user.id,
|
|
63
63
|
title: 'Welcome to BasicBen',
|
|
64
64
|
content: 'Your first post.',
|
|
@@ -91,7 +91,8 @@ const latest = await (await query('posts'))
|
|
|
91
91
|
.first()
|
|
92
92
|
|
|
93
93
|
// db.table() is the same builder under a different name
|
|
94
|
-
const drafts = await (await db.table('posts')).where('published', 0).get()` }), _jsx(CodeBlock, { title: "Writing", children:
|
|
94
|
+
const drafts = await (await db.table('posts')).where('published', 0).get()` }), _jsx(CodeBlock, { title: "Writing", children: `// Every write names the columns it may touch, or it throws.
|
|
95
|
+
const result = await (await query('posts')).only('user_id', 'title', 'content').insert({
|
|
95
96
|
user_id: 1,
|
|
96
97
|
title: 'My Post',
|
|
97
98
|
content: 'Hello world'
|
|
@@ -101,6 +102,7 @@ result.lastInsertRowid // id of the new row
|
|
|
101
102
|
result.changes // 1
|
|
102
103
|
|
|
103
104
|
await (await query('posts'))
|
|
105
|
+
.only('title')
|
|
104
106
|
.where('id', postId)
|
|
105
107
|
.update({ title: 'Updated Title' })
|
|
106
108
|
|
|
@@ -135,24 +137,27 @@ const page = await (await query('posts'))
|
|
|
135
137
|
{ name: 'only(...) / except(...)', desc: 'Mass assignment rules, below' },
|
|
136
138
|
{ name: 'toSql()', desc: 'The SELECT this builder would run' },
|
|
137
139
|
].map(({ name, desc }) => (_jsxs("div", { className: `rounded-lg p-3 ${t.card} border ${t.border}`, children: [_jsx("code", { className: "text-sm font-semibold", children: name }), _jsx("p", { className: `text-xs mt-1 ${t.muted}`, children: desc })] }, name))) }), _jsxs("p", { className: `text-sm ${t.muted} mt-4`, children: ["Multiple ", _jsx("code", { children: "where" }), " calls are joined with ", _jsx("code", { children: "AND" }), ". Table and column names are validated against ", _jsx("code", { children: "[a-zA-Z_][a-zA-Z0-9_]*" }), " and quoted, and every value goes in as a bound parameter, so user input cannot become SQL."] })] }), _jsxs(Card, { children: [_jsx("h2", { className: "text-lg font-semibold mb-2", children: "Booleans on SQLite" }), _jsxs("p", { className: `text-sm ${t.muted} mb-4`, children: [_jsx("code", { children: "node:sqlite" }), " refuses to bind JavaScript booleans. Passing", _jsx("code", { children: "true" }), " or ", _jsx("code", { children: "false" }), " as a value throws", _jsx("code", { children: "Provided value cannot be bound to SQLite parameter" }), ", whether it came from an insert or a where clause. Store flags as integers and convert at the edge."] }), _jsx(CodeBlock, { title: "Use 1 and 0", children: `// Throws on SQLite
|
|
138
|
-
await (await query('posts')).insert({ title: 'Draft', published: false })
|
|
140
|
+
await (await query('posts')).only('title', 'published').insert({ title: 'Draft', published: false })
|
|
139
141
|
await (await query('posts')).where('published', true).get()
|
|
140
142
|
|
|
141
143
|
// Works
|
|
142
|
-
await (await query('posts')).insert({ title: 'Draft', published: 0 })
|
|
144
|
+
await (await query('posts')).only('title', 'published').insert({ title: 'Draft', published: 0 })
|
|
143
145
|
await (await query('posts')).where('published', 1).get()
|
|
144
146
|
|
|
145
147
|
// Convert where the value arrives, not everywhere it is used
|
|
146
|
-
await (await query('posts')).insert({
|
|
148
|
+
await (await query('posts')).only('title', 'published').insert({
|
|
147
149
|
title: req.body.title,
|
|
148
150
|
published: req.body.published ? 1 : 0
|
|
149
|
-
})` })] }), _jsxs(Card, { children: [_jsx("h2", { className: "text-lg font-semibold mb-2", children: "Mass Assignment Protection" }), _jsxs("p", { className: `text-sm ${t.muted} mb-4`, children: [_jsx("code", { children: "
|
|
151
|
+
})` })] }), _jsxs(Card, { children: [_jsx("h2", { className: "text-lg font-semibold mb-2", children: "Mass Assignment Protection" }), _jsxs("p", { className: `text-sm ${t.muted} mb-4`, children: ["Every ", _jsx("code", { children: "insert()" }), " and ", _jsx("code", { children: "update()" }), " names the columns it may touch with ", _jsx("code", { children: "only()" }), ", or it throws (", _jsx("code", { children: "MASS_ASSIGNMENT" }), "). Anything not listed is dropped silently rather than raising, so a request body with extra fields still succeeds \u2014 minus the fields it should not have set. ", _jsx("code", { children: "except()" }), " then removes columns from that list; on its own it no longer permits a write.", _jsx("code", { children: "unguarded()" }), " writes every key, for the rare row whose keys you built yourself and have a reason not to list."] }), _jsx(CodeBlock, { title: "Whitelist with only()", children: `await (await query('users'))
|
|
150
152
|
.only('name', 'email', 'bio')
|
|
151
153
|
.insert(req.body)
|
|
152
154
|
|
|
153
155
|
// Given { name: 'Bob', email: 'bob@test.com', is_admin: 1 }
|
|
154
|
-
// the row is written without is_admin` }), _jsx(CodeBlock, { title: "
|
|
155
|
-
|
|
156
|
+
// the row is written without is_admin` }), _jsx(CodeBlock, { title: "Narrow a list with except()", children: `const PROFILE = ['name', 'email', 'bio', 'avatar_url']
|
|
157
|
+
|
|
158
|
+
await (await query('users'))
|
|
159
|
+
.only(...PROFILE)
|
|
160
|
+
.except('email') // this form cannot change the address
|
|
156
161
|
.where('id', userId)
|
|
157
162
|
.update(req.body)` }), _jsxs("p", { className: `text-sm ${t.muted} mt-4`, children: ["Prefer ", _jsx("code", { children: "only()" }), ". A whitelist stays correct when someone adds a column later; a blacklist quietly stops covering everything."] })] }), _jsxs(Card, { children: [_jsx("h2", { className: "text-lg font-semibold mb-2", children: "What the Builder Does Not Do" }), _jsxs("p", { className: `text-sm ${t.muted} mb-4`, children: ["The list above is all of it. There is no ", _jsx("code", { children: "join" }), ", no", _jsx("code", { children: "whereIn" }), ", no ", _jsx("code", { children: "orWhere" }), ", no ", _jsx("code", { children: "groupBy" }), ", and no aggregate beyond ", _jsx("code", { children: "count()" }), ". This is deliberate: a builder that covers every query ends up as large as the SQL it hides. When you need more, write SQL."] }), _jsx(CodeBlock, { title: "A join is a raw query", children: `import { db } from '@basicbenframework/core/db'
|
|
158
163
|
|
|
@@ -229,7 +234,7 @@ export const Post = {
|
|
|
229
234
|
// Through the builder so Postgres gets RETURNING id and the new
|
|
230
235
|
// id comes back.
|
|
231
236
|
const posts = await query('posts')
|
|
232
|
-
const result = await posts.insert({
|
|
237
|
+
const result = await posts.only('user_id', 'title', 'content', 'published').insert({
|
|
233
238
|
user_id: data.user_id,
|
|
234
239
|
title: data.title,
|
|
235
240
|
content: data.content,
|
|
@@ -79,7 +79,7 @@ afterAll(async () => {
|
|
|
79
79
|
|
|
80
80
|
describe('posts', () => {
|
|
81
81
|
it('inserts a row and reads it back', async () => {
|
|
82
|
-
const result = await (await query('posts')).insert({ title: 'Hello', published: 1 })
|
|
82
|
+
const result = await (await query('posts')).only('title', 'published').insert({ title: 'Hello', published: 1 })
|
|
83
83
|
const post = await (await query('posts')).find(result.lastInsertRowid)
|
|
84
84
|
|
|
85
85
|
expect(post.title).toBe('Hello')
|
package/lib/client/styles.css
CHANGED
|
@@ -13,6 +13,15 @@
|
|
|
13
13
|
|
|
14
14
|
@import 'tailwindcss';
|
|
15
15
|
|
|
16
|
+
/*
|
|
17
|
+
* Tailwind finds class names by scanning files, and it skips node_modules. A
|
|
18
|
+
* site that installs the CMS builds this file from
|
|
19
|
+
* node_modules/@basicbenframework/cms/lib/client, so without this line none of
|
|
20
|
+
* the admin's classes were scanned and it shipped unstyled. '../' is lib/ in
|
|
21
|
+
* the package and src/ here.
|
|
22
|
+
*/
|
|
23
|
+
@source '../';
|
|
24
|
+
|
|
16
25
|
@theme {
|
|
17
26
|
/* Used by the page shell in index.html. */
|
|
18
27
|
--color-dark-900: #0f0f0f;
|
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { hooks } from '@basicbenframework/core/hooks';
|
|
9
9
|
import { MEMBER_HOOKS } from "./events.js";
|
|
10
|
-
import { getDb
|
|
10
|
+
import { getDb } from '@basicbenframework/core/db';
|
|
11
|
+
import { insertRow } from "../../helpers/insert-row.js";
|
|
11
12
|
import { normalizeEmail } from "./session.js";
|
|
12
13
|
import { parseStoredTimestamp } from "../../models/visibility.js";
|
|
13
14
|
export const MEMBER_STATUSES = ['active', 'unsubscribed'];
|
|
@@ -48,7 +49,7 @@ export const Member = {
|
|
|
48
49
|
const now = new Date().toISOString();
|
|
49
50
|
const status = data.status ?? 'active';
|
|
50
51
|
// Through the query builder for RETURNING id on Postgres — see Post.create.
|
|
51
|
-
const result = await (
|
|
52
|
+
const result = await insertRow('members', {
|
|
52
53
|
email: normalizeEmail(data.email),
|
|
53
54
|
name: data.name ?? null,
|
|
54
55
|
status,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Insert a row the model built field by field. Returns the builder's result,
|
|
3
|
+
* whose `lastInsertRowid` is the new id.
|
|
4
|
+
*
|
|
5
|
+
* Core refuses a write that does not name its columns (strict mass
|
|
6
|
+
* assignment, core 0.9). A row written here is an object literal in the
|
|
7
|
+
* model's own code — every key is one the model chose — so its keys are the
|
|
8
|
+
* allowlist. A write that spreads in data from outside (a request body, a
|
|
9
|
+
* caller's record) must not come through here: it names its columns with
|
|
10
|
+
* `only()` itself, so a new key in that data can never reach the table.
|
|
11
|
+
*
|
|
12
|
+
* Inserts go through the query builder rather than raw SQL so Postgres gets a
|
|
13
|
+
* `RETURNING id` and the id comes back; a raw INSERT yields null there.
|
|
14
|
+
*/
|
|
15
|
+
export declare function insertRow(table: string, row: Record<string, unknown>): Promise<{
|
|
16
|
+
lastInsertRowid: number | bigint;
|
|
17
|
+
changes: number;
|
|
18
|
+
}>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { query } from '@basicbenframework/core/db';
|
|
2
|
+
/**
|
|
3
|
+
* Insert a row the model built field by field. Returns the builder's result,
|
|
4
|
+
* whose `lastInsertRowid` is the new id.
|
|
5
|
+
*
|
|
6
|
+
* Core refuses a write that does not name its columns (strict mass
|
|
7
|
+
* assignment, core 0.9). A row written here is an object literal in the
|
|
8
|
+
* model's own code — every key is one the model chose — so its keys are the
|
|
9
|
+
* allowlist. A write that spreads in data from outside (a request body, a
|
|
10
|
+
* caller's record) must not come through here: it names its columns with
|
|
11
|
+
* `only()` itself, so a new key in that data can never reach the table.
|
|
12
|
+
*
|
|
13
|
+
* Inserts go through the query builder rather than raw SQL so Postgres gets a
|
|
14
|
+
* `RETURNING id` and the id comes back; a raw INSERT yields null there.
|
|
15
|
+
*/
|
|
16
|
+
export async function insertRow(table, row) {
|
|
17
|
+
return (await query(table)).only(...Object.keys(row)).insert(row);
|
|
18
|
+
}
|
package/lib/hooks.js
CHANGED
|
@@ -99,3 +99,24 @@ for (const [event, kind] of CONTENT_EVENTS) {
|
|
|
99
99
|
* is no process to keep a timer, so a platform cron calls GET /api/cron/tick.
|
|
100
100
|
*/
|
|
101
101
|
hooks.on(HOOKS.SERVER_STARTED, async () => startCronInterval(), { name: 'cron:interval' });
|
|
102
|
+
/**
|
|
103
|
+
* One value per query key, as the CMS has always read them.
|
|
104
|
+
*
|
|
105
|
+
* Core 0.9 hands a repeated key over as an array (`?tag=a&tag=b` gives
|
|
106
|
+
* `['a', 'b']`). Every reader in the CMS takes a key as one string, many of
|
|
107
|
+
* them through an `as string` that the compiler cannot check, and an array
|
|
108
|
+
* there reaches a model, a `.trim()` or a SQL parameter it was never meant
|
|
109
|
+
* for. So the last value wins, which is what core did before 0.9, and nothing
|
|
110
|
+
* past this point ever sees an array. It runs first (priority 0), ahead of
|
|
111
|
+
* the theme's home page and every route.
|
|
112
|
+
*/
|
|
113
|
+
hooks.on(HOOKS.REQUEST_BEFORE, async (context) => {
|
|
114
|
+
const query = context.req.query;
|
|
115
|
+
if (query) {
|
|
116
|
+
for (const [key, value] of Object.entries(query)) {
|
|
117
|
+
if (Array.isArray(value))
|
|
118
|
+
query[key] = value[value.length - 1];
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return context;
|
|
122
|
+
}, { name: 'query:single-values', priority: 0 });
|
package/lib/models/Category.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { getDb
|
|
1
|
+
import { getDb } from '@basicbenframework/core/db';
|
|
2
|
+
import { insertRow } from "../helpers/insert-row.js";
|
|
2
3
|
import { livePost } from "./visibility.js";
|
|
3
4
|
function slugify(text) {
|
|
4
5
|
return text
|
|
@@ -48,7 +49,7 @@ export const Category = {
|
|
|
48
49
|
// INSERT there reports lastInsertRowid as null, so the row is written and
|
|
49
50
|
// the caller is handed an object with no id — the failure looks like a bug
|
|
50
51
|
// in whatever used the id next.
|
|
51
|
-
const result = await (
|
|
52
|
+
const result = await insertRow('categories', {
|
|
52
53
|
name: data.name,
|
|
53
54
|
slug,
|
|
54
55
|
description: data.description || null,
|
package/lib/models/Comment.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { getDb
|
|
1
|
+
import { getDb } from '@basicbenframework/core/db';
|
|
2
|
+
import { insertRow } from "../helpers/insert-row.js";
|
|
2
3
|
import { MAX_DEPTH } from "../helpers/comment-guard.js";
|
|
3
4
|
/**
|
|
4
5
|
* The listing a moderator works from.
|
|
@@ -141,7 +142,7 @@ export const Comment = {
|
|
|
141
142
|
// Through the query builder, which appends RETURNING id on Postgres. A raw
|
|
142
143
|
// INSERT there reports lastInsertRowid as null, so the row is written and
|
|
143
144
|
// the caller is handed an object with no id.
|
|
144
|
-
const result = await (
|
|
145
|
+
const result = await insertRow('comments', {
|
|
145
146
|
post_id: data.post_id,
|
|
146
147
|
user_id: data.user_id || null,
|
|
147
148
|
parent_id: data.parent_id || null,
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
* a database. Imports carry `.ts` extensions so the module also loads under
|
|
13
13
|
* plain Node, which is how tests/phase3-content-model.test.js exercises it.
|
|
14
14
|
*/
|
|
15
|
-
import { getDb
|
|
15
|
+
import { getDb } from '@basicbenframework/core/db';
|
|
16
|
+
import { insertRow } from "../helpers/insert-row.js";
|
|
16
17
|
import { isHttpUrl, isRelativePath, MAX_URL_LENGTH, normalizeRouteBase, POST_ROUTE_BASE } from "../helpers/content-paths.js";
|
|
17
18
|
/** The kinds of custom field an editor can declare. */
|
|
18
19
|
export const FIELD_TYPES = ['text', 'textarea', 'number', 'url', 'date', 'boolean', 'select'];
|
|
@@ -408,7 +409,7 @@ export const ContentType = {
|
|
|
408
409
|
},
|
|
409
410
|
async create(values) {
|
|
410
411
|
// Through the query builder, which appends RETURNING id on Postgres.
|
|
411
|
-
const result = await (
|
|
412
|
+
const result = await insertRow('content_types', { ...columnsFor(values), builtin: 0 });
|
|
412
413
|
const id = Number(result.lastInsertRowid);
|
|
413
414
|
if (values.fields)
|
|
414
415
|
await this.replaceFields(id, values.fields);
|
package/lib/models/Credential.js
CHANGED
|
@@ -9,8 +9,12 @@ export const Credential = {
|
|
|
9
9
|
return db.get('SELECT * FROM user_credentials WHERE credential_id = ?', [credentialId]);
|
|
10
10
|
},
|
|
11
11
|
async create(data) {
|
|
12
|
+
// The columns a new passkey may set. `id`, `created_at` and `last_used_at`
|
|
13
|
+
// are the database's and the sign-in path's.
|
|
12
14
|
const credentials = await query('user_credentials');
|
|
13
|
-
await credentials
|
|
15
|
+
await credentials
|
|
16
|
+
.only('user_id', 'credential_id', 'public_key', 'algorithm', 'sign_count', 'transports', 'label', 'backed_up')
|
|
17
|
+
.insert(data);
|
|
14
18
|
return this.findByCredentialId(data.credential_id);
|
|
15
19
|
},
|
|
16
20
|
async recordUse(id, signCount) {
|
package/lib/models/Media.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { getDb
|
|
1
|
+
import { getDb } from '@basicbenframework/core/db';
|
|
2
|
+
import { insertRow } from "../helpers/insert-row.js";
|
|
2
3
|
export const Media = {
|
|
3
4
|
/**
|
|
4
5
|
* A page of media, optionally filtered.
|
|
@@ -83,7 +84,7 @@ export const Media = {
|
|
|
83
84
|
// Through the query builder, which appends RETURNING id on Postgres. A raw
|
|
84
85
|
// INSERT there reports lastInsertRowid as null, so the row is written and
|
|
85
86
|
// the caller is handed an object with no id.
|
|
86
|
-
const result = await (
|
|
87
|
+
const result = await insertRow('media', {
|
|
87
88
|
user_id: data.user_id || null,
|
|
88
89
|
filename: data.filename,
|
|
89
90
|
original_name: data.original_name,
|
package/lib/models/Menu.js
CHANGED
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
* leaves out anything a visitor could not open — so a menu never links to a
|
|
7
7
|
* draft, a trashed post, or a page that was deleted out from under it.
|
|
8
8
|
*/
|
|
9
|
-
import { getDb
|
|
9
|
+
import { getDb } from '@basicbenframework/core/db';
|
|
10
|
+
import { insertRow } from "../helpers/insert-row.js";
|
|
10
11
|
import { livePost, livePage } from "./visibility.js";
|
|
11
12
|
import { postPath, pagePath, categoryPath, isSafeLinkUrl } from "../helpers/content-paths.js";
|
|
12
13
|
export const TARGET_TYPES = ['post', 'page', 'category'];
|
|
@@ -218,7 +219,7 @@ export const Menu = {
|
|
|
218
219
|
const now = new Date().toISOString();
|
|
219
220
|
// There is no natural unique key to read an item back by, so this one
|
|
220
221
|
// goes through the query builder, which appends RETURNING id on Postgres.
|
|
221
|
-
const result = await (
|
|
222
|
+
const result = await insertRow('menu_items', {
|
|
222
223
|
menu_id: menuId,
|
|
223
224
|
parent_id: data.parent_id,
|
|
224
225
|
label: data.label,
|
package/lib/models/Page.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { SEO_COLUMNS } from "../helpers/seo-fields.js";
|
|
2
|
-
import { getDb
|
|
2
|
+
import { getDb } from '@basicbenframework/core/db';
|
|
3
|
+
import { insertRow } from "../helpers/insert-row.js";
|
|
3
4
|
import { hooks, HOOKS } from '@basicbenframework/core/hooks';
|
|
4
5
|
import { renderContent } from '@basicbenframework/core/content';
|
|
5
6
|
import { getStorage } from '@basicbenframework/core/storage';
|
|
@@ -80,7 +81,7 @@ export const Page = {
|
|
|
80
81
|
// Through the query builder, which appends RETURNING id on Postgres. A raw
|
|
81
82
|
// INSERT there reports lastInsertRowid as null, so the row is written and
|
|
82
83
|
// the caller is handed an object with no id.
|
|
83
|
-
const result = await (
|
|
84
|
+
const result = await insertRow('pages', {
|
|
84
85
|
title: data.title,
|
|
85
86
|
slug,
|
|
86
87
|
content: data.content || null,
|
package/lib/models/Post.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { getDb
|
|
1
|
+
import { getDb } from '@basicbenframework/core/db';
|
|
2
|
+
import { insertRow } from "../helpers/insert-row.js";
|
|
2
3
|
import { hooks, HOOKS } from '@basicbenframework/core/hooks';
|
|
3
4
|
import { renderContent } from '@basicbenframework/core/content';
|
|
4
5
|
import { getStorage } from '@basicbenframework/core/storage';
|
|
@@ -246,7 +247,7 @@ export const Post = {
|
|
|
246
247
|
// INSERT there reports lastInsertRowid as null, so the row is written and
|
|
247
248
|
// the caller is handed an object with no id — the failure looks like a bug
|
|
248
249
|
// in whatever used the id next.
|
|
249
|
-
const result = await (
|
|
250
|
+
const result = await insertRow('posts', {
|
|
250
251
|
user_id: data.user_id,
|
|
251
252
|
title: data.title,
|
|
252
253
|
content: data.content,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { getDb
|
|
1
|
+
import { getDb } from '@basicbenframework/core/db';
|
|
2
|
+
import { insertRow } from "../helpers/insert-row.js";
|
|
2
3
|
import { parseStoredTimestamp } from "./visibility.js";
|
|
3
4
|
/** How many saved revisions a post keeps. The oldest beyond this are pruned. */
|
|
4
5
|
const KEEP = 50;
|
|
@@ -18,7 +19,7 @@ export const PostRevision = {
|
|
|
18
19
|
(after.excerpt !== undefined && (after.excerpt ?? null) !== (before.excerpt ?? null));
|
|
19
20
|
if (!changed)
|
|
20
21
|
return;
|
|
21
|
-
await (
|
|
22
|
+
await insertRow('post_revisions', {
|
|
22
23
|
post_id: before.id,
|
|
23
24
|
user_id: userId,
|
|
24
25
|
title: before.title,
|
|
@@ -48,7 +49,7 @@ export const PostRevision = {
|
|
|
48
49
|
async autosave(postId, userId, data) {
|
|
49
50
|
const db = await getDb();
|
|
50
51
|
await db.run('DELETE FROM post_revisions WHERE post_id = ? AND user_id = ? AND autosave = 1', [postId, userId]);
|
|
51
|
-
const result = await (
|
|
52
|
+
const result = await insertRow('post_revisions', {
|
|
52
53
|
post_id: postId,
|
|
53
54
|
user_id: userId,
|
|
54
55
|
title: data.title,
|
package/lib/models/Snippet.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { getDb
|
|
1
|
+
import { getDb } from '@basicbenframework/core/db';
|
|
2
|
+
import { insertRow } from "../helpers/insert-row.js";
|
|
2
3
|
const SELECT = `
|
|
3
4
|
SELECT snippets.id, snippets.name, snippets.content, snippets.created_by,
|
|
4
5
|
snippets.created_at, snippets.updated_at, users.name AS author_name
|
|
@@ -31,7 +32,7 @@ export const Snippet = {
|
|
|
31
32
|
const now = new Date().toISOString();
|
|
32
33
|
// Through the query builder, which appends RETURNING id on Postgres; a raw
|
|
33
34
|
// INSERT there reports no id (see Tag.create).
|
|
34
|
-
const result = await (
|
|
35
|
+
const result = await insertRow('snippets', {
|
|
35
36
|
name: data.name,
|
|
36
37
|
content: data.content,
|
|
37
38
|
created_by: data.created_by,
|
package/lib/models/Tag.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { getDb
|
|
1
|
+
import { getDb } from '@basicbenframework/core/db';
|
|
2
|
+
import { insertRow } from "../helpers/insert-row.js";
|
|
2
3
|
import { livePost } from "./visibility.js";
|
|
3
4
|
function slugify(text) {
|
|
4
5
|
return text
|
|
@@ -64,7 +65,7 @@ export const Tag = {
|
|
|
64
65
|
// INSERT there reports lastInsertRowid as null, so the row is written and
|
|
65
66
|
// the caller is handed an object with no id — the failure looks like a bug
|
|
66
67
|
// in whatever used the id next.
|
|
67
|
-
const result = await (
|
|
68
|
+
const result = await insertRow('tags', { name: data.name, slug });
|
|
68
69
|
return {
|
|
69
70
|
id: result.lastInsertRowid,
|
|
70
71
|
name: data.name,
|
package/lib/models/Taxonomy.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* so this loads under plain Node too.
|
|
12
12
|
*/
|
|
13
13
|
import { getDb, query } from '@basicbenframework/core/db';
|
|
14
|
+
import { insertRow } from "../helpers/insert-row.js";
|
|
14
15
|
import { slugify } from '@basicbenframework/core/content';
|
|
15
16
|
import { livePost } from "./visibility.js";
|
|
16
17
|
/** Keys the built-in taxonomies answer to, and words the API already uses. */
|
|
@@ -197,7 +198,7 @@ export const Taxonomy = {
|
|
|
197
198
|
return keys.filter((key) => !known.has(key));
|
|
198
199
|
},
|
|
199
200
|
async create(values) {
|
|
200
|
-
const result = await (
|
|
201
|
+
const result = await insertRow('taxonomies', {
|
|
201
202
|
key: values.key,
|
|
202
203
|
singular_label: values.singular_label,
|
|
203
204
|
plural_label: values.plural_label,
|
|
@@ -270,7 +271,9 @@ export const Taxonomy = {
|
|
|
270
271
|
return Boolean(row);
|
|
271
272
|
},
|
|
272
273
|
async createTerm(taxonomyId, values) {
|
|
273
|
-
const result = await (await query('terms'))
|
|
274
|
+
const result = await (await query('terms'))
|
|
275
|
+
.only('taxonomy_id', 'name', 'slug', 'description', 'parent_id')
|
|
276
|
+
.insert({ taxonomy_id: taxonomyId, ...values });
|
|
274
277
|
return (await this.findTerm(Number(result.lastInsertRowid)));
|
|
275
278
|
},
|
|
276
279
|
async updateTerm(id, values) {
|
package/lib/models/User.js
CHANGED
|
@@ -43,8 +43,12 @@ export const User = {
|
|
|
43
43
|
const slug = await uniqueSlug('users', data.slug || data.name, { fallback: 'author' });
|
|
44
44
|
// Goes through the query builder so Postgres gets a RETURNING clause and
|
|
45
45
|
// the new id comes back — a raw INSERT yields a null id there.
|
|
46
|
+
// The caller's data is spread in, so the columns are named: a key added to
|
|
47
|
+
// CreateUserData, or smuggled in by a caller, never reaches the table.
|
|
46
48
|
const users = await query('users');
|
|
47
|
-
const result = await users
|
|
49
|
+
const result = await users
|
|
50
|
+
.only('name', 'email', 'password', 'role', 'slug', 'email_verified', 'email_verified_at')
|
|
51
|
+
.insert({ ...data, role, slug });
|
|
48
52
|
return {
|
|
49
53
|
id: result.lastInsertRowid,
|
|
50
54
|
...data,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basicbenframework/cms",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "The BasicBen CMS. Posts, pages, media, an admin UI, and a headless content API.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"restore": "npm run restore --workspace site --"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
|
-
"@basicbenframework/core": "^0.
|
|
58
|
+
"@basicbenframework/core": "^0.9.0",
|
|
59
59
|
"react": "^19.2.0",
|
|
60
60
|
"react-dom": "^19.2.0"
|
|
61
61
|
},
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"tailwindcss": "^4.3.3"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
|
-
"@basicbenframework/core": "^0.
|
|
68
|
+
"@basicbenframework/core": "^0.9.0",
|
|
69
69
|
"@types/node": "^24",
|
|
70
70
|
"@types/react": "^19",
|
|
71
71
|
"@types/react-dom": "^19",
|