@respectify/astro 0.1.9 → 1.0.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/README.md +213 -69
- package/client/comments.ts +2 -0
- package/default-respectify.config.json +1 -0
- package/drizzle/postgres.config.ts +8 -0
- package/drizzle/sqlite.config.ts +8 -0
- package/migrations/postgres/0000_respectify_comment.sql +14 -0
- package/migrations/postgres/meta/0000_snapshot.json +144 -0
- package/migrations/postgres/meta/_journal.json +13 -0
- package/migrations/sqlite/0000_respectify_comment.sql +13 -0
- package/migrations/sqlite/meta/0000_snapshot.json +101 -0
- package/migrations/sqlite/meta/_journal.json +13 -0
- package/package.json +35 -13
- package/respectify.config.sample.jsonc +47 -5
- package/src/actions.ts +133 -75
- package/src/adapters/postgres/index.ts +7 -0
- package/src/adapters/postgres/store.ts +83 -0
- package/src/adapters/postgres/table.ts +37 -0
- package/src/adapters/sqlite/index.ts +8 -0
- package/src/adapters/sqlite/store.ts +91 -0
- package/src/adapters/sqlite/table.ts +41 -0
- package/src/adapters/sqlite/text-date.ts +37 -0
- package/src/env.d.ts +11 -12
- package/src/index.ts +91 -7
- package/src/lib/config.ts +60 -0
- package/src/lib/service.ts +74 -23
- package/src/options.ts +135 -0
- package/src/routes/comments.ts +31 -33
- package/src/runtime-config.ts +9 -33
- package/src/runtime.ts +28 -0
- package/src/store/limits.ts +11 -0
- package/src/store/schemas.ts +41 -0
- package/src/store/types.ts +52 -0
- package/src/testing/comment-store-contract.ts +197 -0
- package/src/testing/index.ts +1 -0
- package/src/types.ts +87 -17
- package/src/schema.ts +0 -25
package/README.md
CHANGED
|
@@ -16,49 +16,102 @@ Built for production — powers [nickhodges.com](https://nickhodges.com) as a li
|
|
|
16
16
|
- **Works with prerendered pages** — comments load at runtime via API; no rebuild needed
|
|
17
17
|
- **One component** — `<CommentSection postSlug="..." />`
|
|
18
18
|
- **Powered by Respectify branding** — optional callout and badge for demos
|
|
19
|
-
- **
|
|
19
|
+
- **Your database** — Postgres or SQLite/libSQL (Turso) through your own [Drizzle](https://orm.drizzle.team) connection, or any store you write against `ICommentStore`
|
|
20
|
+
- **Never touches your schema** — the package ships table definitions and drizzle-kit migrations; you decide when they run
|
|
20
21
|
- **Self-contained CSS** — no Tailwind required; customize via CSS variables
|
|
21
22
|
- **Fail-closed** — rejects comments when Respectify is unavailable
|
|
22
23
|
|
|
23
24
|
## Requirements
|
|
24
25
|
|
|
25
|
-
- Astro 5 or
|
|
26
|
-
-
|
|
26
|
+
- Astro 5, 6 or 7 with **`output: 'server'`** (or hybrid) and a server adapter (Vercel, Node, etc.)
|
|
27
|
+
- A database you reach through Drizzle ORM (`drizzle-orm` 0.45): Postgres (node-postgres, postgres.js, Neon, PGlite, …) or SQLite (libSQL/Turso, better-sqlite3, Cloudflare D1, …)
|
|
27
28
|
- A [Respectify](https://respectify.ai) account (`RESPECTIFY_EMAIL` + `RESPECTIFY_API_KEY`)
|
|
28
29
|
|
|
29
|
-
>
|
|
30
|
+
> Upgrading from 0.x, which stored comments through Astro DB? See [Migrating from 0.x (Astro DB)](#migrating-from-0x-astro-db).
|
|
31
|
+
>
|
|
32
|
+
> Prefer API-only moderation without a comment UI or database? Use [`@respectify/astro-moderation`](https://www.npmjs.com/package/@respectify/astro-moderation) or [`@respectify/client`](https://www.npmjs.com/package/@respectify/client).
|
|
30
33
|
|
|
31
34
|
## Quick start
|
|
32
35
|
|
|
33
36
|
### 1. Install
|
|
34
37
|
|
|
38
|
+
Install the package, Drizzle, and the driver for your database. For Postgres with node-postgres:
|
|
39
|
+
|
|
35
40
|
```bash
|
|
36
|
-
npm install @respectify/astro @respectify/client
|
|
41
|
+
npm install @respectify/astro @respectify/client drizzle-orm pg
|
|
42
|
+
npm install -D drizzle-kit @types/pg
|
|
37
43
|
```
|
|
38
44
|
|
|
39
|
-
|
|
45
|
+
For libSQL/Turso, install `@libsql/client` instead of `pg`. The package never installs a driver itself: `pg` and `@libsql/client` are optional peer dependencies, so you only get the one you use.
|
|
46
|
+
|
|
47
|
+
### 2. Add the comment table and migrate
|
|
48
|
+
|
|
49
|
+
Add the table to your Drizzle schema:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
// src/db/schema.ts
|
|
53
|
+
export { respectifyComments } from '@respectify/astro/postgres'; // or '@respectify/astro/sqlite'
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Then generate and run the migration with drizzle-kit, the way you migrate the rest of your schema:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npx drizzle-kit generate
|
|
60
|
+
npx drizzle-kit migrate
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The tables are named `respectify_comment` (Postgres) and `Comment` (SQLite). For another name, use `createPostgresCommentTable('my_comments')` or `createSqliteCommentTable('my_comments')` and pass the table to the store. If you manage your schema some other way, the SQL drizzle-kit generates for the default tables is in [`migrations/postgres`](./migrations/postgres) and [`migrations/sqlite`](./migrations/sqlite).
|
|
64
|
+
|
|
65
|
+
The integration never creates, alters or migrates a table.
|
|
66
|
+
|
|
67
|
+
### 3. Write the runtime module
|
|
68
|
+
|
|
69
|
+
The store, and any functions, live in a module that runs inside your server bundle, so they use the connection your site already has:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
// src/respectify.runtime.ts
|
|
73
|
+
import { defineRespectifyRuntime } from '@respectify/astro';
|
|
74
|
+
import { createPostgresCommentStore } from '@respectify/astro/postgres';
|
|
75
|
+
import { drizzle } from 'drizzle-orm/node-postgres';
|
|
76
|
+
import { Pool } from 'pg';
|
|
77
|
+
import { DATABASE_URL } from 'astro:env/server';
|
|
78
|
+
|
|
79
|
+
const db = drizzle({ client: new Pool({ connectionString: DATABASE_URL }) });
|
|
80
|
+
|
|
81
|
+
export default defineRespectifyRuntime({
|
|
82
|
+
store: createPostgresCommentStore(db),
|
|
83
|
+
// Optional: the canonical URL Respectify reads a post from for topic context
|
|
84
|
+
getPostUrl: (slug, site) => `${site}/blog/${slug}/`,
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
For SQLite, use `createSqliteCommentStore(db)` from `@respectify/astro/sqlite` with your Drizzle SQLite database (for libSQL/Turso, `drizzle({ client: createClient({ url, authToken }) })` from `drizzle-orm/libsql`).
|
|
89
|
+
|
|
90
|
+
### 4. Add the integration
|
|
40
91
|
|
|
41
92
|
```ts
|
|
42
93
|
// astro.config.ts
|
|
43
|
-
import { defineConfig } from 'astro/config';
|
|
44
|
-
import db from '@astrojs/db';
|
|
94
|
+
import { defineConfig, envField } from 'astro/config';
|
|
45
95
|
import respectify from '@respectify/astro';
|
|
46
96
|
|
|
47
97
|
export default defineConfig({
|
|
48
98
|
site: 'https://yoursite.com',
|
|
49
99
|
output: 'server',
|
|
50
100
|
adapter: vercel(), // or node, netlify, etc.
|
|
101
|
+
env: {
|
|
102
|
+
schema: {
|
|
103
|
+
DATABASE_URL: envField.string({ context: 'server', access: 'secret' }),
|
|
104
|
+
},
|
|
105
|
+
},
|
|
51
106
|
integrations: [
|
|
52
|
-
db(),
|
|
53
107
|
respectify({
|
|
54
|
-
|
|
55
|
-
getPostUrl: (slug, site) => `${site}/blog/${slug}/`,
|
|
108
|
+
runtime: './src/respectify.runtime.ts',
|
|
56
109
|
}),
|
|
57
110
|
],
|
|
58
111
|
});
|
|
59
112
|
```
|
|
60
113
|
|
|
61
|
-
###
|
|
114
|
+
### 5. Configure Respectify
|
|
62
115
|
|
|
63
116
|
Copy a starter config to your project root:
|
|
64
117
|
|
|
@@ -77,53 +130,10 @@ Add environment variables:
|
|
|
77
130
|
```env
|
|
78
131
|
RESPECTIFY_EMAIL=you@example.com
|
|
79
132
|
RESPECTIFY_API_KEY=your-api-key
|
|
80
|
-
|
|
81
|
-
ASTRO_DB_APP_TOKEN=...
|
|
133
|
+
DATABASE_URL=postgres://...
|
|
82
134
|
```
|
|
83
135
|
|
|
84
|
-
###
|
|
85
|
-
|
|
86
|
-
Copy this into `db/config.ts` (or import from `@respectify/astro/schema` if your setup supports it):
|
|
87
|
-
|
|
88
|
-
```ts
|
|
89
|
-
import { defineDb, defineTable, column, NOW } from 'astro:db';
|
|
90
|
-
|
|
91
|
-
const Comment = defineTable({
|
|
92
|
-
columns: {
|
|
93
|
-
id: column.number({ primaryKey: true }),
|
|
94
|
-
postSlug: column.text(),
|
|
95
|
-
author: column.text(),
|
|
96
|
-
email: column.text({ optional: true }),
|
|
97
|
-
content: column.text(),
|
|
98
|
-
createdAt: column.date({ default: NOW }),
|
|
99
|
-
approved: column.boolean({ default: false }),
|
|
100
|
-
parentId: column.number({ optional: true }),
|
|
101
|
-
},
|
|
102
|
-
indexes: [
|
|
103
|
-
{ on: ['postSlug'], unique: false },
|
|
104
|
-
{ on: ['approved'], unique: false },
|
|
105
|
-
],
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
export default defineDb({ tables: { Comment } });
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
Or use the exported helper:
|
|
112
|
-
|
|
113
|
-
```ts
|
|
114
|
-
import { defineDb } from 'astro:db';
|
|
115
|
-
import { Comment } from '@respectify/astro/schema';
|
|
116
|
-
|
|
117
|
-
export default defineDb({ tables: { Comment } });
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
Run migrations:
|
|
121
|
-
|
|
122
|
-
```bash
|
|
123
|
-
npx astro db push
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
### 5. Wire up Astro Actions
|
|
136
|
+
### 6. Wire up Astro Actions
|
|
127
137
|
|
|
128
138
|
```ts
|
|
129
139
|
// src/actions/index.ts
|
|
@@ -134,7 +144,7 @@ export const server = {
|
|
|
134
144
|
};
|
|
135
145
|
```
|
|
136
146
|
|
|
137
|
-
###
|
|
147
|
+
### 7. Add comments to your layout
|
|
138
148
|
|
|
139
149
|
```astro
|
|
140
150
|
---
|
|
@@ -146,6 +156,42 @@ import CommentSection from '@respectify/astro/components/CommentSection.astro';
|
|
|
146
156
|
|
|
147
157
|
That's it. Comments are moderated by Respectify before they appear.
|
|
148
158
|
|
|
159
|
+
## Storage
|
|
160
|
+
|
|
161
|
+
The actions, the comments API route and the moderation service depend only on the `ICommentStore` interface:
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
interface ICommentStore {
|
|
165
|
+
findById(id: number): Promise<CommentRecord | undefined>;
|
|
166
|
+
listApproved(postSlug: string): Promise<PublicComment[]>; // oldest first, ties by id
|
|
167
|
+
create(input: NewComment): Promise<CommentRecord>; // validates with newCommentSchema
|
|
168
|
+
delete(id: number): Promise<boolean>; // replies are kept, with parentId null
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
| Store | Import | Works with |
|
|
173
|
+
| --- | --- | --- |
|
|
174
|
+
| `createPostgresCommentStore(db, { table? })` | `@respectify/astro/postgres` | Any Drizzle Postgres database: node-postgres, postgres.js, Neon, PGlite, … |
|
|
175
|
+
| `createSqliteCommentStore(db, { table? })` | `@respectify/astro/sqlite` | Any Drizzle SQLite database: libSQL/Turso, better-sqlite3, Cloudflare D1, … |
|
|
176
|
+
|
|
177
|
+
Both take your Drizzle database instance and never open a connection of their own.
|
|
178
|
+
|
|
179
|
+
### Writing your own store
|
|
180
|
+
|
|
181
|
+
Implement `ICommentStore` (types from `@respectify/astro`) and run the shared contract suite, which every built-in store passes, against it with Vitest:
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
// test/my-comment-store.test.ts
|
|
185
|
+
import { describeCommentStoreContract } from '@respectify/astro/testing';
|
|
186
|
+
|
|
187
|
+
describeCommentStoreContract('my store', async () => ({
|
|
188
|
+
store: createMyCommentStore(await createEmptyTestDatabase()),
|
|
189
|
+
dispose: () => closeTestDatabase(),
|
|
190
|
+
}));
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
The factory must return a store on a new, empty table each time.
|
|
194
|
+
|
|
149
195
|
## Component props
|
|
150
196
|
|
|
151
197
|
| Prop | Default | Description |
|
|
@@ -172,25 +218,23 @@ Override on `.rf-comments`:
|
|
|
172
218
|
|
|
173
219
|
### Integration options
|
|
174
220
|
|
|
175
|
-
Any `@respectify/client` option can be set via `client` and `megacall`. Values in `astro.config` override the same keys in `respectify.config.json`.
|
|
221
|
+
`respectify()` takes only values that can be serialized, because Astro runs `astro.config` apart from your server bundle. Functions go in the runtime module. Any `@respectify/client` option can be set via `client` and `megacall`. Values in `astro.config` override the same keys in `respectify.config.json`.
|
|
176
222
|
|
|
177
223
|
```ts
|
|
178
224
|
respectify({
|
|
225
|
+
// Required: the module whose default export is defineRespectifyRuntime({ store, ... })
|
|
226
|
+
runtime: './src/respectify.runtime.ts',
|
|
227
|
+
|
|
179
228
|
configPath: './respectify.config.json',
|
|
180
229
|
commentsApiPath: '/api/respectify/comments',
|
|
181
230
|
showBranding: true,
|
|
182
|
-
getPostUrl: (slug, site) => `${site}/posts/${slug}/`,
|
|
183
|
-
|
|
184
|
-
// Alternative to getPostUrl: hand Respectify the post body directly instead of
|
|
185
|
-
// having it fetch getPostUrl. Useful for staged/preview/auth-gated content, or
|
|
186
|
-
// when the URL isn't live yet right after deploy. Takes priority over getPostUrl.
|
|
187
|
-
// getPostContent: async (slug) => loadPostBody(slug),
|
|
188
231
|
|
|
189
232
|
// Per-IP submission limit, enforced in the `comments.submit` action.
|
|
190
233
|
// In-memory per server process — not shared across multiple instances/regions.
|
|
191
234
|
rateLimit: { windowMs: 300_000, maxRequests: 10 },
|
|
192
235
|
|
|
193
|
-
// Forwarded to RespectifyClient
|
|
236
|
+
// Forwarded to RespectifyClient. Credentials are not accepted here, since these options are
|
|
237
|
+
// written into the server bundle: use RESPECTIFY_EMAIL / RESPECTIFY_API_KEY
|
|
194
238
|
client: {
|
|
195
239
|
baseUrl: 'https://app.respectify.ai',
|
|
196
240
|
timeout: 30_000,
|
|
@@ -208,6 +252,24 @@ respectify({
|
|
|
208
252
|
sensitiveTopics: [],
|
|
209
253
|
dogwhistleExamples: [],
|
|
210
254
|
},
|
|
255
|
+
|
|
256
|
+
// Comment-health tolerances sent with every megacall, merged per item over the
|
|
257
|
+
// JSON config's `tolerances` (these win). See "Comment health and tolerances" below.
|
|
258
|
+
tolerances: { insult: 'relaxed', threat: 'zero_tolerance', quality: 'strict' },
|
|
259
|
+
});
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
The runtime module takes the store and the two optional post functions:
|
|
263
|
+
|
|
264
|
+
```ts
|
|
265
|
+
export default defineRespectifyRuntime({
|
|
266
|
+
store,
|
|
267
|
+
// The canonical URL for a post, used to initialize Respectify topics (default: `${site}/posts/${slug}/`)
|
|
268
|
+
getPostUrl: (slug, site) => `${site}/posts/${slug}/`,
|
|
269
|
+
// Alternative to getPostUrl: hand Respectify the post body directly instead of having it
|
|
270
|
+
// fetch getPostUrl. Useful for staged/preview/auth-gated content, or when the URL isn't
|
|
271
|
+
// live yet right after deploy. Takes priority over getPostUrl.
|
|
272
|
+
getPostContent: async (slug) => loadPostBody(slug),
|
|
211
273
|
});
|
|
212
274
|
```
|
|
213
275
|
|
|
@@ -221,7 +283,7 @@ import { verifyRespectifyCredentials } from '@respectify/astro/credentials';
|
|
|
221
283
|
const isConnected = await verifyRespectifyCredentials();
|
|
222
284
|
```
|
|
223
285
|
|
|
224
|
-
Useful for an admin health-check page — returns `true` when `RESPECTIFY_EMAIL`/`RESPECTIFY_API_KEY` (or `client.email`/`client.apiKey`) are valid and the subscription is active.
|
|
286
|
+
Useful for an admin health-check page — returns `true` when `RESPECTIFY_EMAIL`/`RESPECTIFY_API_KEY` (or `client.email`/`client.apiKey` in `respectify.config.json`) are valid and the subscription is active.
|
|
225
287
|
|
|
226
288
|
### `respectify.config.json`
|
|
227
289
|
|
|
@@ -231,6 +293,36 @@ For a field-by-field walkthrough with comments, see [`respectify.config.sample.j
|
|
|
231
293
|
|
|
232
294
|
Replies automatically pass the parent comment text as `replyToComment` to Respectify.
|
|
233
295
|
|
|
296
|
+
### Comment health and tolerances
|
|
297
|
+
|
|
298
|
+
Comment health has ten items: the nine problem categories and `quality`. `tolerances` (in `respectify.config.json` or the integration options) sets a level per item; items not named are `standard`. Nothing is stored on the API key.
|
|
299
|
+
|
|
300
|
+
```json
|
|
301
|
+
{
|
|
302
|
+
"tolerances": { "insult": "relaxed", "profanity": "off", "threat": "zero_tolerance", "quality": "strict" }
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
- Items: `threat`, `identity_attack`, `sexual_content`, `harassment`, `insult`, `profanity`, `dismissive_tone`, `logical_fallacy`, `low_effort`, `quality`
|
|
307
|
+
- Levels, loosest to strictest: `off`, `permissive`, `relaxed`, `standard` (default), `strict`, `zero_tolerance`
|
|
308
|
+
- A problem category fails on its worst finding (severity 1 minor, 2 clear, 3 severe, 4 extreme): `permissive` filters only the very worst, failing extreme findings alone; `relaxed` fails severe and extreme findings; `standard` fails clear, severe and extreme ones; `strict` and `zero_tolerance` fail any. `quality` sets the lowest 1-5 quality score accepted: `permissive` and `relaxed` 2, `standard` 3, `strict` 4, `zero_tolerance` 5. `off` leaves the item out of comment health.
|
|
309
|
+
- `threat`, `identity_attack`, `sexual_content` and `harassment` are floored: they can be made stricter but never `permissive`, `relaxed` or `off`. The config fails to load if you try (and the integration throws at setup for the `tolerances` option).
|
|
310
|
+
|
|
311
|
+
Tolerances shape the comment score, so they are only sent when a comment score runs (which needs topic context). When the API returns them, the analysis result carries `healthScore` (1.0-5.0) and `healthItems` (per item: `score`, `minimum`, `is_acceptable`).
|
|
312
|
+
|
|
313
|
+
### How comments are approved, warned or blocked
|
|
314
|
+
|
|
315
|
+
Spam, off-topic (`blockOffTopic`) and dogwhistle (`blockDogwhistles`) rejections come first. Suspected spam is not spam: the comment is sent back to the commenter (never approved, even with `allowOverride`) with the server's suggestion as `feedback`, such as "This looks like spam. How about removing the link?", while any comment-health coaching stays in `suggestion`. The analysis result carries `suspectedSpam` and `spamSuggestion` (and `isSpam: false`) whenever a spam check ran; `suspectedSpam` is `false` when an older API omits it, and the `comments.submit` action returns `suspectedSpam` for a comment it did not publish. Then, on content:
|
|
316
|
+
|
|
317
|
+
| Comment score says | Respectfulness (`1 - toxicity_score`) | Result |
|
|
318
|
+
| --- | --- | --- |
|
|
319
|
+
| `exceeds_tolerance: false` | any | Approved (`feedback.approved`) |
|
|
320
|
+
| `exceeds_tolerance: true` | `< thresholds.block` and `blockDisrespectful` | Blocked (`feedback.blocked`) |
|
|
321
|
+
| `exceeds_tolerance: true` | otherwise | Warning (`feedback.warning`), approved only when `allowOverride` |
|
|
322
|
+
| no comment score | — | Approved |
|
|
323
|
+
|
|
324
|
+
`exceeds_tolerance` decides whether a comment has a problem at all, because it reflects your site's comment-health tolerances (any item below its minimum, or severe toxicity); `toxicity_score` does not, so it only chooses between warning and blocking. `thresholds.autoApprove` applies only when the API returns a comment score without `exceeds_tolerance` (an API older than tolerances), where the previous bands are used. The returned `exceedsTolerance` field carries the verdict.
|
|
325
|
+
|
|
234
326
|
### Admin delete (optional)
|
|
235
327
|
|
|
236
328
|
Set `enableDelete` on `CommentSection` and implement auth so `context.locals.isAuthenticated` is true. See the [Nick-Blog](https://github.com/NickHodges/Nick-Blog) repo for a full admin auth example.
|
|
@@ -241,12 +333,64 @@ Set `enableDelete` on `CommentSection` and implement auth so `context.locals.isA
|
|
|
241
333
|
Visitor submits comment
|
|
242
334
|
→ Astro Action (comments.submit)
|
|
243
335
|
→ Respectify megacall (configured checks: spam, score, relevance, dogwhistle, …)
|
|
244
|
-
→ If approved:
|
|
245
|
-
→ Client refreshes comment list via GET /api/respectify/comments
|
|
336
|
+
→ If approved: store.create() saves it through your ICommentStore
|
|
337
|
+
→ Client refreshes comment list via GET /api/respectify/comments (store.listApproved())
|
|
246
338
|
```
|
|
247
339
|
|
|
248
340
|
Post pages can stay `prerender = true`. Only the API route and actions need server runtime.
|
|
249
341
|
|
|
342
|
+
## Migrating from 0.x (Astro DB)
|
|
343
|
+
|
|
344
|
+
1.0 removes Astro DB, which Astro deprecated in 6.4 and removed in 7.0, so it is a breaking release. What changes:
|
|
345
|
+
|
|
346
|
+
| 0.x | 1.0 |
|
|
347
|
+
| --- | --- |
|
|
348
|
+
| `@astrojs/db` peer dependency, `db()` integration, `db/config.ts` | Your own Drizzle database, passed to a store in a runtime module |
|
|
349
|
+
| `@respectify/astro/schema` | `@respectify/astro/sqlite` or `@respectify/astro/postgres` (`respectifyComments`) |
|
|
350
|
+
| `getPostUrl` / `getPostContent` in `respectify({...})` | In `defineRespectifyRuntime({...})`; passing them to `respectify()` now fails at startup |
|
|
351
|
+
| `astro db push`, `astro build --remote` | drizzle-kit (or your own migrations); plain `astro build` |
|
|
352
|
+
| `ASTRO_DB_REMOTE_URL`, `ASTRO_DB_APP_TOKEN` | Your own variables, declared with `astro:env` |
|
|
353
|
+
| `client.email` / `client.apiKey` in `respectify({...})` | `RESPECTIFY_EMAIL` / `RESPECTIFY_API_KEY` (or `respectify.config.json`); in `astro.config` they now fail at startup |
|
|
354
|
+
| `comments.delete` of a missing comment succeeded | Fails with `NOT_FOUND`; a reply to a missing comment, or one on another post, fails with `BAD_REQUEST` |
|
|
355
|
+
| Comments API returns `id, author, content, createdAt` | Adds `parentId` |
|
|
356
|
+
|
|
357
|
+
0.x also lost every `respectify()` option at runtime in production builds (they were kept in module state that never reached the server bundle), so `getPostUrl`, `tolerances`, `rateLimit`, `client` and `megacall` silently fell back to their defaults. 1.0 passes them to the server bundle through a virtual module. If you had set them, review them: they now take effect.
|
|
358
|
+
|
|
359
|
+
### Keep your Turso database (no data to move)
|
|
360
|
+
|
|
361
|
+
The SQLite store's default table is exactly the `Comment` table Astro DB created (same columns, defaults, index names and date format), so your existing comments are used as they are.
|
|
362
|
+
|
|
363
|
+
1. `npm uninstall @astrojs/db` and `npm install drizzle-orm @libsql/client`.
|
|
364
|
+
2. Remove `db()` from `astro.config` and delete `db/config.ts`. Drop `--remote` from your build script.
|
|
365
|
+
3. Declare the Turso URL and token in `astro.config`'s `env.schema` (they can keep their names), and write the runtime module:
|
|
366
|
+
|
|
367
|
+
```ts
|
|
368
|
+
// src/respectify.runtime.ts
|
|
369
|
+
import { defineRespectifyRuntime } from '@respectify/astro';
|
|
370
|
+
import { createSqliteCommentStore } from '@respectify/astro/sqlite';
|
|
371
|
+
import { createClient } from '@libsql/client';
|
|
372
|
+
import { drizzle } from 'drizzle-orm/libsql';
|
|
373
|
+
import { ASTRO_DB_REMOTE_URL, ASTRO_DB_APP_TOKEN } from 'astro:env/server';
|
|
374
|
+
|
|
375
|
+
const db = drizzle({ client: createClient({ url: ASTRO_DB_REMOTE_URL, authToken: ASTRO_DB_APP_TOKEN }) });
|
|
376
|
+
|
|
377
|
+
export default defineRespectifyRuntime({ store: createSqliteCommentStore(db) });
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
4. Change `respectify({ ... })` to `respectify({ runtime: './src/respectify.runtime.ts', ... })`, moving any `getPostUrl` / `getPostContent` into the runtime module.
|
|
381
|
+
|
|
382
|
+
Do **not** run the reference migration in `migrations/sqlite` against this database: the table already exists. If you manage the rest of the database with drizzle-kit, take the table in with `drizzle-kit pull` first, or mark the migration as applied. drizzle-kit marks the `id` column `NOT NULL` where Astro DB did not; for an `INTEGER PRIMARY KEY` in SQLite that makes no difference, so there is nothing to change.
|
|
383
|
+
|
|
384
|
+
### Move your comments to Postgres
|
|
385
|
+
|
|
386
|
+
1. Create the table in Postgres: add `respectifyComments` from `@respectify/astro/postgres` to your Drizzle schema and run drizzle-kit (or apply [`migrations/postgres`](./migrations/postgres)).
|
|
387
|
+
2. Copy the rows from Turso with Drizzle, reading through `respectifyComments` from `@respectify/astro/sqlite` (which reads Astro DB's text dates as UTC) and inserting through the Postgres table, keeping `id`, `parentId`, `approved` and `createdAt`. Insert in one transaction, parents before replies (order by `id`), and set any `parentId` whose parent no longer exists to null, since the Postgres table has a foreign key.
|
|
388
|
+
3. Explicit ids leave the identity sequence at 1, so set it after the highest id copied: `select setval(pg_get_serial_sequence('respectify_comment', 'id'), <max id>, true)`.
|
|
389
|
+
4. Read the rows back and compare them field by field before switching the site over.
|
|
390
|
+
5. Point the runtime module at `createPostgresCommentStore(db)`, then remove `@astrojs/db` as above.
|
|
391
|
+
|
|
392
|
+
Run the copy as a dry run first, and have it check the target host and database before it writes.
|
|
393
|
+
|
|
250
394
|
## License
|
|
251
395
|
|
|
252
396
|
MIT
|
package/client/comments.ts
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { defineConfig } from 'drizzle-kit';
|
|
2
|
+
|
|
3
|
+
/** Generates the reference Postgres migration in migrations/postgres from the adapter's table. */
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
dialect: 'postgresql',
|
|
6
|
+
schema: './src/adapters/postgres/table.ts',
|
|
7
|
+
out: './migrations/postgres',
|
|
8
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { defineConfig } from 'drizzle-kit';
|
|
2
|
+
|
|
3
|
+
/** Generates the reference SQLite migration in migrations/sqlite from the adapter's table. */
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
dialect: 'sqlite',
|
|
6
|
+
schema: './src/adapters/sqlite/table.ts',
|
|
7
|
+
out: './migrations/sqlite',
|
|
8
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
CREATE TABLE "respectify_comment" (
|
|
2
|
+
"id" integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "respectify_comment_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
|
|
3
|
+
"post_slug" text NOT NULL,
|
|
4
|
+
"author" text NOT NULL,
|
|
5
|
+
"email" text,
|
|
6
|
+
"content" text NOT NULL,
|
|
7
|
+
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
|
8
|
+
"approved" boolean DEFAULT false NOT NULL,
|
|
9
|
+
"parent_id" integer
|
|
10
|
+
);
|
|
11
|
+
--> statement-breakpoint
|
|
12
|
+
ALTER TABLE "respectify_comment" ADD CONSTRAINT "respectify_comment_parent_id_respectify_comment_id_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."respectify_comment"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
|
|
13
|
+
CREATE INDEX "respectify_comment_post_slug_created_at_idx" ON "respectify_comment" USING btree ("post_slug","created_at");--> statement-breakpoint
|
|
14
|
+
CREATE INDEX "respectify_comment_parent_id_idx" ON "respectify_comment" USING btree ("parent_id");
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "79148351-cfed-4a49-abf8-72f584610262",
|
|
3
|
+
"prevId": "00000000-0000-0000-0000-000000000000",
|
|
4
|
+
"version": "7",
|
|
5
|
+
"dialect": "postgresql",
|
|
6
|
+
"tables": {
|
|
7
|
+
"public.respectify_comment": {
|
|
8
|
+
"name": "respectify_comment",
|
|
9
|
+
"schema": "",
|
|
10
|
+
"columns": {
|
|
11
|
+
"id": {
|
|
12
|
+
"name": "id",
|
|
13
|
+
"type": "integer",
|
|
14
|
+
"primaryKey": true,
|
|
15
|
+
"notNull": true,
|
|
16
|
+
"identity": {
|
|
17
|
+
"type": "byDefault",
|
|
18
|
+
"name": "respectify_comment_id_seq",
|
|
19
|
+
"schema": "public",
|
|
20
|
+
"increment": "1",
|
|
21
|
+
"startWith": "1",
|
|
22
|
+
"minValue": "1",
|
|
23
|
+
"maxValue": "2147483647",
|
|
24
|
+
"cache": "1",
|
|
25
|
+
"cycle": false
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"post_slug": {
|
|
29
|
+
"name": "post_slug",
|
|
30
|
+
"type": "text",
|
|
31
|
+
"primaryKey": false,
|
|
32
|
+
"notNull": true
|
|
33
|
+
},
|
|
34
|
+
"author": {
|
|
35
|
+
"name": "author",
|
|
36
|
+
"type": "text",
|
|
37
|
+
"primaryKey": false,
|
|
38
|
+
"notNull": true
|
|
39
|
+
},
|
|
40
|
+
"email": {
|
|
41
|
+
"name": "email",
|
|
42
|
+
"type": "text",
|
|
43
|
+
"primaryKey": false,
|
|
44
|
+
"notNull": false
|
|
45
|
+
},
|
|
46
|
+
"content": {
|
|
47
|
+
"name": "content",
|
|
48
|
+
"type": "text",
|
|
49
|
+
"primaryKey": false,
|
|
50
|
+
"notNull": true
|
|
51
|
+
},
|
|
52
|
+
"created_at": {
|
|
53
|
+
"name": "created_at",
|
|
54
|
+
"type": "timestamp with time zone",
|
|
55
|
+
"primaryKey": false,
|
|
56
|
+
"notNull": true,
|
|
57
|
+
"default": "now()"
|
|
58
|
+
},
|
|
59
|
+
"approved": {
|
|
60
|
+
"name": "approved",
|
|
61
|
+
"type": "boolean",
|
|
62
|
+
"primaryKey": false,
|
|
63
|
+
"notNull": true,
|
|
64
|
+
"default": false
|
|
65
|
+
},
|
|
66
|
+
"parent_id": {
|
|
67
|
+
"name": "parent_id",
|
|
68
|
+
"type": "integer",
|
|
69
|
+
"primaryKey": false,
|
|
70
|
+
"notNull": false
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
"indexes": {
|
|
74
|
+
"respectify_comment_post_slug_created_at_idx": {
|
|
75
|
+
"name": "respectify_comment_post_slug_created_at_idx",
|
|
76
|
+
"columns": [
|
|
77
|
+
{
|
|
78
|
+
"expression": "post_slug",
|
|
79
|
+
"isExpression": false,
|
|
80
|
+
"asc": true,
|
|
81
|
+
"nulls": "last"
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
"expression": "created_at",
|
|
85
|
+
"isExpression": false,
|
|
86
|
+
"asc": true,
|
|
87
|
+
"nulls": "last"
|
|
88
|
+
}
|
|
89
|
+
],
|
|
90
|
+
"isUnique": false,
|
|
91
|
+
"concurrently": false,
|
|
92
|
+
"method": "btree",
|
|
93
|
+
"with": {}
|
|
94
|
+
},
|
|
95
|
+
"respectify_comment_parent_id_idx": {
|
|
96
|
+
"name": "respectify_comment_parent_id_idx",
|
|
97
|
+
"columns": [
|
|
98
|
+
{
|
|
99
|
+
"expression": "parent_id",
|
|
100
|
+
"isExpression": false,
|
|
101
|
+
"asc": true,
|
|
102
|
+
"nulls": "last"
|
|
103
|
+
}
|
|
104
|
+
],
|
|
105
|
+
"isUnique": false,
|
|
106
|
+
"concurrently": false,
|
|
107
|
+
"method": "btree",
|
|
108
|
+
"with": {}
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
"foreignKeys": {
|
|
112
|
+
"respectify_comment_parent_id_respectify_comment_id_fk": {
|
|
113
|
+
"name": "respectify_comment_parent_id_respectify_comment_id_fk",
|
|
114
|
+
"tableFrom": "respectify_comment",
|
|
115
|
+
"tableTo": "respectify_comment",
|
|
116
|
+
"columnsFrom": [
|
|
117
|
+
"parent_id"
|
|
118
|
+
],
|
|
119
|
+
"columnsTo": [
|
|
120
|
+
"id"
|
|
121
|
+
],
|
|
122
|
+
"onDelete": "set null",
|
|
123
|
+
"onUpdate": "no action"
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
"compositePrimaryKeys": {},
|
|
127
|
+
"uniqueConstraints": {},
|
|
128
|
+
"policies": {},
|
|
129
|
+
"checkConstraints": {},
|
|
130
|
+
"isRLSEnabled": false
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
"enums": {},
|
|
134
|
+
"schemas": {},
|
|
135
|
+
"sequences": {},
|
|
136
|
+
"roles": {},
|
|
137
|
+
"policies": {},
|
|
138
|
+
"views": {},
|
|
139
|
+
"_meta": {
|
|
140
|
+
"columns": {},
|
|
141
|
+
"schemas": {},
|
|
142
|
+
"tables": {}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
CREATE TABLE `Comment` (
|
|
2
|
+
`id` integer PRIMARY KEY NOT NULL,
|
|
3
|
+
`postSlug` text NOT NULL,
|
|
4
|
+
`author` text NOT NULL,
|
|
5
|
+
`email` text,
|
|
6
|
+
`content` text NOT NULL,
|
|
7
|
+
`createdAt` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
|
8
|
+
`approved` integer DEFAULT false NOT NULL,
|
|
9
|
+
`parentId` integer
|
|
10
|
+
);
|
|
11
|
+
--> statement-breakpoint
|
|
12
|
+
CREATE INDEX `Comment_postSlug_idx` ON `Comment` (`postSlug`);--> statement-breakpoint
|
|
13
|
+
CREATE INDEX `Comment_approved_idx` ON `Comment` (`approved`);
|