@nextblock-cms/db 0.14.3 → 0.14.5

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.
@@ -1083,6 +1083,7 @@ export declare const getServiceRoleSupabaseClient: () => import('@supabase/supab
1083
1083
  language_id: number;
1084
1084
  meta_description: string | null;
1085
1085
  meta_title: string | null;
1086
+ published_at: string | null;
1086
1087
  slug: string;
1087
1088
  status: Database["public"]["Enums"]["page_status"];
1088
1089
  title: string;
@@ -1099,6 +1100,7 @@ export declare const getServiceRoleSupabaseClient: () => import('@supabase/supab
1099
1100
  language_id: number;
1100
1101
  meta_description?: string | null;
1101
1102
  meta_title?: string | null;
1103
+ published_at?: string | null;
1102
1104
  slug: string;
1103
1105
  status?: Database["public"]["Enums"]["page_status"];
1104
1106
  title: string;
@@ -1115,6 +1117,7 @@ export declare const getServiceRoleSupabaseClient: () => import('@supabase/supab
1115
1117
  language_id?: number;
1116
1118
  meta_description?: string | null;
1117
1119
  meta_title?: string | null;
1120
+ published_at?: string | null;
1118
1121
  slug?: string;
1119
1122
  status?: Database["public"]["Enums"]["page_status"];
1120
1123
  title?: string;
@@ -1613,6 +1616,7 @@ export declare const getServiceRoleSupabaseClient: () => import('@supabase/supab
1613
1616
  price: number;
1614
1617
  prices: import('./types').Json;
1615
1618
  product_type: string;
1619
+ published_at: string | null;
1616
1620
  sale_end_at: string | null;
1617
1621
  sale_price: number | null;
1618
1622
  sale_prices: import('./types').Json | null;
@@ -1650,6 +1654,7 @@ export declare const getServiceRoleSupabaseClient: () => import('@supabase/supab
1650
1654
  price: number;
1651
1655
  prices?: import('./types').Json;
1652
1656
  product_type: string;
1657
+ published_at?: string | null;
1653
1658
  sale_end_at?: string | null;
1654
1659
  sale_price?: number | null;
1655
1660
  sale_prices?: import('./types').Json | null;
@@ -1687,6 +1692,7 @@ export declare const getServiceRoleSupabaseClient: () => import('@supabase/supab
1687
1692
  price?: number;
1688
1693
  prices?: import('./types').Json;
1689
1694
  product_type?: string;
1695
+ published_at?: string | null;
1690
1696
  sale_end_at?: string | null;
1691
1697
  sale_price?: number | null;
1692
1698
  sale_prices?: import('./types').Json | null;
@@ -1122,6 +1122,7 @@ export type Database = {
1122
1122
  language_id: number;
1123
1123
  meta_description: string | null;
1124
1124
  meta_title: string | null;
1125
+ published_at: string | null;
1125
1126
  slug: string;
1126
1127
  status: Database["public"]["Enums"]["page_status"];
1127
1128
  title: string;
@@ -1138,6 +1139,7 @@ export type Database = {
1138
1139
  language_id: number;
1139
1140
  meta_description?: string | null;
1140
1141
  meta_title?: string | null;
1142
+ published_at?: string | null;
1141
1143
  slug: string;
1142
1144
  status?: Database["public"]["Enums"]["page_status"];
1143
1145
  title: string;
@@ -1154,6 +1156,7 @@ export type Database = {
1154
1156
  language_id?: number;
1155
1157
  meta_description?: string | null;
1156
1158
  meta_title?: string | null;
1159
+ published_at?: string | null;
1157
1160
  slug?: string;
1158
1161
  status?: Database["public"]["Enums"]["page_status"];
1159
1162
  title?: string;
@@ -1679,6 +1682,7 @@ export type Database = {
1679
1682
  price: number;
1680
1683
  prices: Json;
1681
1684
  product_type: string;
1685
+ published_at: string | null;
1682
1686
  sale_end_at: string | null;
1683
1687
  sale_price: number | null;
1684
1688
  sale_prices: Json | null;
@@ -1716,6 +1720,7 @@ export type Database = {
1716
1720
  price: number;
1717
1721
  prices?: Json;
1718
1722
  product_type: string;
1723
+ published_at?: string | null;
1719
1724
  sale_end_at?: string | null;
1720
1725
  sale_price?: number | null;
1721
1726
  sale_prices?: Json | null;
@@ -1753,6 +1758,7 @@ export type Database = {
1753
1758
  price?: number;
1754
1759
  prices?: Json;
1755
1760
  product_type?: string;
1761
+ published_at?: string | null;
1756
1762
  sale_end_at?: string | null;
1757
1763
  sale_price?: number | null;
1758
1764
  sale_prices?: Json | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nextblock-cms/db",
3
- "version": "0.14.3",
3
+ "version": "0.14.5",
4
4
  "main": "index.cjs.js",
5
5
  "module": "index.es.js",
6
6
  "types": "index.d.ts",
@@ -0,0 +1,43 @@
1
+ -- Scheduled publishing for pages and products.
2
+ --
3
+ -- Posts already support scheduling: `posts.published_at` exists and every public
4
+ -- read gates on `published_at IS NULL OR published_at <= now()`, so a row with
5
+ -- status='published' and a future date is withheld until the date passes. Pages
6
+ -- and products had no equivalent column, so "go live on Tuesday" was impossible
7
+ -- for them. This adds the same column with the same semantics.
8
+ --
9
+ -- Visibility is derived from the (status, published_at) PAIR — no new enum value:
10
+ --
11
+ -- status = draft/archived -> not public, whatever the date
12
+ -- status = published|active, published_at NULL -> public now
13
+ -- status = published|active, date <= now() -> public now
14
+ -- status = published|active, date > now() -> SCHEDULED (withheld)
15
+ --
16
+ -- Deriving "scheduled" instead of storing it keeps `page_status` unchanged (adding
17
+ -- an enum value can't be done inside a transaction with other DDL in Postgres) and
18
+ -- matches what posts have always done, so one code path covers all three types.
19
+ --
20
+ -- NULL is the safe default: every existing published row keeps rendering exactly as
21
+ -- before, so this migration needs no backfill and changes no current behavior.
22
+ --
23
+ -- Forward-only and idempotent.
24
+
25
+ ALTER TABLE public.pages
26
+ ADD COLUMN IF NOT EXISTS published_at timestamp with time zone;
27
+
28
+ COMMENT ON COLUMN public.pages.published_at IS
29
+ 'Optional go-live moment. NULL = live as soon as status is published. A future value withholds the page from public reads until it passes (status stays "published"; the CMS renders that pair as "Scheduled").';
30
+
31
+ ALTER TABLE public.products
32
+ ADD COLUMN IF NOT EXISTS published_at timestamp with time zone;
33
+
34
+ COMMENT ON COLUMN public.products.published_at IS
35
+ 'Optional go-live moment. NULL = live as soon as status is active. A future value withholds the product from public reads until it passes (status stays "active"; the CMS renders that pair as "Scheduled").';
36
+
37
+ -- Public listing/index queries filter on the (status, published_at) pair together
38
+ -- (catalog, sitemap, page lookups), so a composite index serves them in one pass.
39
+ CREATE INDEX IF NOT EXISTS pages_status_published_at_idx
40
+ ON public.pages (status, published_at);
41
+
42
+ CREATE INDEX IF NOT EXISTS products_status_published_at_idx
43
+ ON public.products (status, published_at);
@@ -0,0 +1,310 @@
1
+ -- 00000000000016_product_revisions_and_revision_baseline.sql
2
+ --
3
+ -- Revision History, part 1 of 2 (schema). The application-side rewrite lives in
4
+ -- apps/nextblock/app/cms/revisions/**.
5
+ --
6
+ -- Three things happen here:
7
+ --
8
+ -- 1. products.version — the monotonic counter the hybrid revision engine drives,
9
+ -- mirroring pages.version / posts.version.
10
+ --
11
+ -- 2. product_revisions — a structural mirror of page_revisions / post_revisions.
12
+ -- product_id is uuid (products.id is uuid, not bigint), and
13
+ -- writes are gated on is_admin() to match products_*_policy
14
+ -- rather than the ADMIN|WRITER pattern the page/post revision
15
+ -- tables use. A WRITER who could insert a revision but not
16
+ -- apply a restore would get a silent no-op restore, because
17
+ -- PostgREST returns no error for an UPDATE matching zero rows.
18
+ --
19
+ -- 3. Revision baseline — every page, post and product gets a real `snapshot` row to
20
+ -- restore to. Until now the CMS synthesised a fake "Initial
21
+ -- Version" entry in the UI whose Restore button resolved to
22
+ -- "current metadata + zero blocks" and wiped the content.
23
+ -- There is now an actual stored baseline instead.
24
+ --
25
+ -- Case A (version = 1, no revisions at all): the live row IS
26
+ -- version 1. This covers seeded content — 00000000000003
27
+ -- inserts every page and post at version 1 and writes no
28
+ -- revision rows — and everything authored since the CMS save
29
+ -- path stopped recording revisions. Snapshotting it at
30
+ -- version 1 is what makes "restore the original seeded page"
31
+ -- real for the first time.
32
+ --
33
+ -- Case B (version > 1 but no snapshot at or below it): the
34
+ -- true v1 is unrecoverable and is NOT fabricated. A snapshot
35
+ -- of the current state is stored at the current version so the
36
+ -- diff chain has a valid base and future restores resolve.
37
+ --
38
+ -- Forward-only, idempotent, and it modifies no existing row: every backfill is an
39
+ -- INSERT ... WHERE NOT EXISTS ... ON CONFLICT DO NOTHING.
40
+
41
+ -- ---------------------------------------------------------------------------
42
+ -- 1. products.version
43
+ -- ---------------------------------------------------------------------------
44
+
45
+ ALTER TABLE public.products
46
+ ADD COLUMN IF NOT EXISTS version integer DEFAULT 1 NOT NULL;
47
+
48
+ COMMENT ON COLUMN public.products.version IS 'Monotonic version number for hybrid revisions.';
49
+
50
+ -- ---------------------------------------------------------------------------
51
+ -- 2. product_revisions
52
+ -- ---------------------------------------------------------------------------
53
+
54
+ CREATE TABLE IF NOT EXISTS public.product_revisions (
55
+ id bigint NOT NULL,
56
+ product_id uuid NOT NULL,
57
+ author_id uuid,
58
+ version integer NOT NULL,
59
+ revision_type public.revision_type NOT NULL,
60
+ content jsonb NOT NULL,
61
+ created_at timestamp with time zone DEFAULT now() NOT NULL
62
+ );
63
+
64
+ COMMENT ON TABLE public.product_revisions IS 'Hybrid (snapshot/diff) revisions for products.';
65
+ COMMENT ON COLUMN public.product_revisions.content IS 'If snapshot: full content; if diff: JSON Patch array.';
66
+
67
+ DO $rb$
68
+ BEGIN
69
+ IF NOT EXISTS (
70
+ SELECT 1 FROM pg_attribute
71
+ WHERE attrelid = 'public.product_revisions'::regclass
72
+ AND attname = 'id'
73
+ AND attidentity <> ''
74
+ ) THEN
75
+ ALTER TABLE public.product_revisions
76
+ ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
77
+ SEQUENCE NAME public.product_revisions_id_seq
78
+ START WITH 1
79
+ INCREMENT BY 1
80
+ NO MINVALUE
81
+ NO MAXVALUE
82
+ CACHE 1
83
+ );
84
+ END IF;
85
+ END $rb$;
86
+
87
+ DO $rb$
88
+ BEGIN
89
+ IF NOT EXISTS (SELECT 1 FROM pg_constraint
90
+ WHERE conname = 'product_revisions_pkey'
91
+ AND conrelid = 'public.product_revisions'::regclass) THEN
92
+ ALTER TABLE ONLY public.product_revisions
93
+ ADD CONSTRAINT product_revisions_pkey PRIMARY KEY (id);
94
+ END IF;
95
+ END $rb$;
96
+
97
+ DO $rb$
98
+ BEGIN
99
+ IF NOT EXISTS (SELECT 1 FROM pg_constraint
100
+ WHERE conname = 'product_revisions_product_version_key'
101
+ AND conrelid = 'public.product_revisions'::regclass) THEN
102
+ ALTER TABLE ONLY public.product_revisions
103
+ ADD CONSTRAINT product_revisions_product_version_key UNIQUE (product_id, version);
104
+ END IF;
105
+ END $rb$;
106
+
107
+ DO $rb$
108
+ BEGIN
109
+ IF NOT EXISTS (SELECT 1 FROM pg_constraint
110
+ WHERE conname = 'product_revisions_author_id_fkey'
111
+ AND conrelid = 'public.product_revisions'::regclass) THEN
112
+ ALTER TABLE ONLY public.product_revisions
113
+ ADD CONSTRAINT product_revisions_author_id_fkey
114
+ FOREIGN KEY (author_id) REFERENCES public.profiles(id) ON DELETE SET NULL;
115
+ END IF;
116
+ END $rb$;
117
+
118
+ DO $rb$
119
+ BEGIN
120
+ IF NOT EXISTS (SELECT 1 FROM pg_constraint
121
+ WHERE conname = 'product_revisions_product_id_fkey'
122
+ AND conrelid = 'public.product_revisions'::regclass) THEN
123
+ ALTER TABLE ONLY public.product_revisions
124
+ ADD CONSTRAINT product_revisions_product_id_fkey
125
+ FOREIGN KEY (product_id) REFERENCES public.products(id) ON DELETE CASCADE;
126
+ END IF;
127
+ END $rb$;
128
+
129
+ CREATE INDEX IF NOT EXISTS idx_product_revisions_author_id
130
+ ON public.product_revisions USING btree (author_id);
131
+
132
+ CREATE INDEX IF NOT EXISTS idx_product_revisions_product_id_version
133
+ ON public.product_revisions USING btree (product_id, version);
134
+
135
+ ALTER TABLE public.product_revisions ENABLE ROW LEVEL SECURITY;
136
+
137
+ DROP POLICY IF EXISTS product_revisions_read_policy ON public.product_revisions;
138
+ CREATE POLICY product_revisions_read_policy ON public.product_revisions
139
+ FOR SELECT TO authenticated USING (true);
140
+
141
+ DROP POLICY IF EXISTS product_revisions_insert_policy ON public.product_revisions;
142
+ CREATE POLICY product_revisions_insert_policy ON public.product_revisions
143
+ FOR INSERT TO authenticated
144
+ WITH CHECK (((SELECT public.is_admin() AS is_admin) IS TRUE));
145
+
146
+ DROP POLICY IF EXISTS product_revisions_update_policy ON public.product_revisions;
147
+ CREATE POLICY product_revisions_update_policy ON public.product_revisions
148
+ FOR UPDATE TO authenticated
149
+ USING (((SELECT public.is_admin() AS is_admin) IS TRUE))
150
+ WITH CHECK (((SELECT public.is_admin() AS is_admin) IS TRUE));
151
+
152
+ DROP POLICY IF EXISTS product_revisions_delete_policy ON public.product_revisions;
153
+ CREATE POLICY product_revisions_delete_policy ON public.product_revisions
154
+ FOR DELETE TO authenticated
155
+ USING (((SELECT public.is_admin() AS is_admin) IS TRUE));
156
+
157
+ GRANT ALL ON TABLE public.product_revisions TO anon;
158
+ GRANT ALL ON TABLE public.product_revisions TO authenticated;
159
+ GRANT ALL ON TABLE public.product_revisions TO service_role;
160
+ GRANT ALL ON SEQUENCE public.product_revisions_id_seq TO anon;
161
+ GRANT ALL ON SEQUENCE public.product_revisions_id_seq TO authenticated;
162
+ GRANT ALL ON SEQUENCE public.product_revisions_id_seq TO service_role;
163
+
164
+ -- ---------------------------------------------------------------------------
165
+ -- 3. Revision baseline backfill
166
+ --
167
+ -- The JSON shape must match FullPageContent / FullPostContent / FullProductContent
168
+ -- in apps/nextblock/app/cms/revisions/utils.ts exactly, or the first diff taken
169
+ -- against a baseline row will be full of phantom operations.
170
+ --
171
+ -- Timestamps are rendered with an explicit millisecond-precision UTC format so they
172
+ -- match JavaScript's Date#toISOString() ("2026-07-03T17:52:15.643Z"). Postgres'
173
+ -- default jsonb rendering of timestamptz ("2026-07-03T17:52:15.643901+00:00") would
174
+ -- differ from the value the application writes and produce a spurious diff on the
175
+ -- very next save.
176
+ -- ---------------------------------------------------------------------------
177
+
178
+ -- 3a. Pages
179
+ INSERT INTO public.page_revisions (page_id, author_id, version, revision_type, content)
180
+ SELECT
181
+ p.id,
182
+ NULL::uuid,
183
+ p.version,
184
+ 'snapshot'::public.revision_type,
185
+ jsonb_build_object(
186
+ 'meta', jsonb_build_object(
187
+ 'title', p.title,
188
+ 'slug', p.slug,
189
+ 'language_id', p.language_id,
190
+ 'status', p.status,
191
+ 'meta_title', p.meta_title,
192
+ 'meta_description', p.meta_description,
193
+ 'custom_canonical', p.custom_canonical,
194
+ 'published_at', to_char(p.published_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"'),
195
+ 'feature_image_id', p.feature_image_id
196
+ ),
197
+ 'blocks', COALESCE((
198
+ SELECT jsonb_agg(
199
+ jsonb_build_object(
200
+ 'language_id', b.language_id,
201
+ 'block_type', b.block_type,
202
+ 'content', b.content,
203
+ 'order', b."order"
204
+ ) ORDER BY b."order" ASC, b.id ASC
205
+ )
206
+ FROM public.blocks b
207
+ WHERE b.page_id = p.id
208
+ ), '[]'::jsonb)
209
+ )
210
+ FROM public.pages p
211
+ WHERE NOT EXISTS (
212
+ SELECT 1 FROM public.page_revisions r
213
+ WHERE r.page_id = p.id
214
+ AND r.revision_type = 'snapshot'
215
+ AND r.version <= p.version
216
+ )
217
+ ON CONFLICT (page_id, version) DO NOTHING;
218
+
219
+ -- 3b. Posts
220
+ INSERT INTO public.post_revisions (post_id, author_id, version, revision_type, content)
221
+ SELECT
222
+ po.id,
223
+ NULL::uuid,
224
+ po.version,
225
+ 'snapshot'::public.revision_type,
226
+ jsonb_build_object(
227
+ 'meta', jsonb_build_object(
228
+ 'title', po.title,
229
+ 'slug', po.slug,
230
+ 'language_id', po.language_id,
231
+ 'status', po.status,
232
+ 'meta_title', po.meta_title,
233
+ 'meta_description', po.meta_description,
234
+ 'custom_canonical', po.custom_canonical,
235
+ 'published_at', to_char(po.published_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"'),
236
+ 'feature_image_id', po.feature_image_id,
237
+ 'label', po.label,
238
+ 'excerpt', po.excerpt,
239
+ 'subtitle', po.subtitle
240
+ ),
241
+ 'blocks', COALESCE((
242
+ SELECT jsonb_agg(
243
+ jsonb_build_object(
244
+ 'language_id', b.language_id,
245
+ 'block_type', b.block_type,
246
+ 'content', b.content,
247
+ 'order', b."order"
248
+ ) ORDER BY b."order" ASC, b.id ASC
249
+ )
250
+ FROM public.blocks b
251
+ WHERE b.post_id = po.id
252
+ ), '[]'::jsonb)
253
+ )
254
+ FROM public.posts po
255
+ WHERE NOT EXISTS (
256
+ SELECT 1 FROM public.post_revisions r
257
+ WHERE r.post_id = po.id
258
+ AND r.revision_type = 'snapshot'
259
+ AND r.version <= po.version
260
+ )
261
+ ON CONFLICT (post_id, version) DO NOTHING;
262
+
263
+ -- 3c. Products.
264
+ --
265
+ -- Content only. price/prices/sale_*/scheduled_*/stock/sku/average_rating/total_reviews
266
+ -- are deliberately excluded from the snapshot: pricing and inventory are mutated from
267
+ -- outside the editor (promotions, Freemius sync, order fulfilment), ratings are derived
268
+ -- aggregates, and inventory_items is keyed by bare SKU text with no FK to products — so
269
+ -- replaying commerce state on restore would reach rows the editor never touched.
270
+ -- Restoring a product restores its content, not its commerce state.
271
+ INSERT INTO public.product_revisions (product_id, author_id, version, revision_type, content)
272
+ SELECT
273
+ pr.id,
274
+ NULL::uuid,
275
+ pr.version,
276
+ 'snapshot'::public.revision_type,
277
+ jsonb_build_object(
278
+ 'meta', jsonb_build_object(
279
+ 'title', pr.title,
280
+ 'slug', pr.slug,
281
+ 'language_id', pr.language_id,
282
+ 'status', pr.status,
283
+ 'meta_title', pr.meta_title,
284
+ 'meta_description', pr.meta_description,
285
+ 'custom_canonical', pr.custom_canonical,
286
+ 'published_at', to_char(pr.published_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"'),
287
+ 'short_description', pr.short_description,
288
+ 'description_json', pr.description_json
289
+ ),
290
+ 'blocks', COALESCE((
291
+ SELECT jsonb_agg(
292
+ jsonb_build_object(
293
+ 'language_id', b.language_id,
294
+ 'block_type', b.block_type,
295
+ 'content', b.content,
296
+ 'order', b."order"
297
+ ) ORDER BY b."order" ASC, b.id ASC
298
+ )
299
+ FROM public.blocks b
300
+ WHERE b.product_id = pr.id
301
+ ), '[]'::jsonb)
302
+ )
303
+ FROM public.products pr
304
+ WHERE NOT EXISTS (
305
+ SELECT 1 FROM public.product_revisions r
306
+ WHERE r.product_id = pr.id
307
+ AND r.revision_type = 'snapshot'
308
+ AND r.version <= pr.version
309
+ )
310
+ ON CONFLICT (product_id, version) DO NOTHING;