sandcms 0.6.1 → 0.6.2

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.
Files changed (41) hide show
  1. package/README.md +1 -1
  2. package/dist/cli/doctor.js +2 -2
  3. package/dist/cli/files.js +11 -6
  4. package/dist/cli/index.js +1 -1
  5. package/dist/cli/upgrade.js +2 -1
  6. package/dist/env.d.ts +80 -0
  7. package/dist/env.js +103 -0
  8. package/dist/server/admin-load.d.ts +46 -76
  9. package/dist/server/admin-load.js +98 -24
  10. package/dist/server/api-pagination.d.ts +2 -0
  11. package/dist/server/api-pagination.js +51 -7
  12. package/dist/server/cms.d.ts +17 -2
  13. package/dist/server/cms.js +3 -1
  14. package/dist/server/content.d.ts +15 -2
  15. package/dist/server/content.js +72 -30
  16. package/dist/server/db/migration-data.js +8 -0
  17. package/dist/server/db/migrations/0007_content_and_job_indexes.sql +2 -0
  18. package/dist/server/db/migrations/0008_search_text_cache.sql +45 -0
  19. package/dist/server/db/migrations/meta/0007_snapshot.json +1643 -0
  20. package/dist/server/db/migrations/meta/0008_snapshot.json +1711 -0
  21. package/dist/server/db/migrations/meta/_journal.json +14 -0
  22. package/dist/server/db/schema.d.ts +83 -0
  23. package/dist/server/db/schema.js +23 -5
  24. package/dist/server/email/index.d.ts +5 -1
  25. package/dist/server/email/index.js +12 -11
  26. package/dist/server/media/runner.js +217 -191
  27. package/dist/server/media/service.d.ts +6 -0
  28. package/dist/server/media/service.js +36 -3
  29. package/dist/server/search.d.ts +2 -0
  30. package/dist/server/search.js +7 -0
  31. package/dist/worker/index.js +129 -76
  32. package/docs/cloudflare.md +1 -1
  33. package/docs/deployment.md +15 -1
  34. package/docs/email.md +5 -5
  35. package/docs/media.md +2 -2
  36. package/docs/package.md +4 -4
  37. package/docs/releases.md +10 -0
  38. package/docs/upgrades.md +31 -1
  39. package/package.json +5 -1
  40. package/templates/env.ts +8 -0
  41. package/templates/integration.ts +5 -5
@@ -1,7 +1,46 @@
1
1
  import { error } from '@sveltejs/kit';
2
- import { and, desc, eq, inArray, lt, or } from 'drizzle-orm';
2
+ import { and, desc, eq, inArray, isNull, lt, or, sql } from 'drizzle-orm';
3
3
  import { documents, media } from './db/schema.js';
4
- import { searchQuery, documentText, assetText, matchScore } from './search.js';
4
+ import { searchQuery, documentText, assetText, matchScore, documentSearchText, assetSearchText } from './search.js';
5
+ // Rows with a NULL cache are kept and checked in JS, which also fills the cache.
6
+ function searchFilter(column, query) {
7
+ const terms = query.split(' ');
8
+ return sql `(${column} IS NULL OR (${sql.join(terms.map((term) => sql `instr(${column}, ${term}) > 0`), sql ` AND `)}))`;
9
+ }
10
+ // One immediate transaction per fetched batch. Each row is re-read inside it, so a concurrent
11
+ // change cannot be overwritten by text computed from older data.
12
+ function fillDocumentSearchText(cms, batch) {
13
+ if (!batch.some((row) => row.searchText === null))
14
+ return;
15
+ cms.store.sqlite.transaction(() => {
16
+ for (const { id } of batch) {
17
+ const row = cms.store.db.select().from(documents).where(eq(documents.id, id)).get();
18
+ if (!row || row.searchText !== null)
19
+ continue;
20
+ cms.store.db
21
+ .update(documents)
22
+ .set({ searchText: documentSearchText(cms, row) })
23
+ .where(and(eq(documents.id, id), isNull(documents.searchText)))
24
+ .run();
25
+ }
26
+ }).immediate();
27
+ }
28
+ function fillAssetSearchText(cms, batch, labels) {
29
+ if (!batch.some((row) => row.searchText === null))
30
+ return;
31
+ cms.store.sqlite.transaction(() => {
32
+ for (const { id } of batch) {
33
+ const row = cms.store.db.select().from(media).where(eq(media.id, id)).get();
34
+ if (!row || row.searchText !== null)
35
+ continue;
36
+ cms.store.db
37
+ .update(media)
38
+ .set({ searchText: assetSearchText(cms, row, labels) })
39
+ .where(and(eq(media.id, id), isNull(media.searchText)))
40
+ .run();
41
+ }
42
+ }).immediate();
43
+ }
5
44
  function pagination(params, scope) {
6
45
  const rawLimit = params.get('limit') ?? '100';
7
46
  if (!/^\d+$/.test(rawLimit) || Number(rawLimit) < 1 || Number(rawLimit) > 500)
@@ -38,12 +77,13 @@ function page(rows, limit, scope, time) {
38
77
  };
39
78
  }
40
79
  const scanBatch = 200;
41
- function scan(limit, cursor, time, fetch, match) {
80
+ function scan(limit, cursor, time, fetch, match, prepare) {
42
81
  if (!match)
43
82
  return fetch(cursor, limit + 1);
44
83
  const rows = [];
45
84
  for (let after = cursor;;) {
46
85
  const batch = fetch(after, scanBatch);
86
+ prepare?.(batch);
47
87
  for (const row of batch)
48
88
  if (match(row) && rows.push(row) > limit)
49
89
  return rows;
@@ -64,10 +104,12 @@ export function contentPage(cms, params) {
64
104
  .from(documents)
65
105
  .where(and(eq(documents.blueprint, blueprint), after
66
106
  ? or(lt(documents.updatedAt, after.time), and(eq(documents.updatedAt, after.time), lt(documents.id, after.id)))
67
- : undefined))
107
+ : undefined, query ? searchFilter(documents.searchText, query) : undefined))
68
108
  .orderBy(desc(documents.updatedAt), desc(documents.id))
69
109
  .limit(count)
70
- .all(), query ? (row) => matchScore(query, String(row.draft.title ?? ''), documentText(cms, row)) >= 0 : undefined);
110
+ .all(), query
111
+ ? (row) => matchScore(query, String(row.draft.title ?? ''), documentText(cms, row)) >= 0
112
+ : undefined, query ? (batch) => fillDocumentSearchText(cms, batch) : undefined);
71
113
  return page(rows, limit, scope, (row) => row.updatedAt);
72
114
  }
73
115
  export function mediaPage(cms, params) {
@@ -79,9 +121,11 @@ export function mediaPage(cms, params) {
79
121
  .from(media)
80
122
  .where(and(blueprint ? eq(media.blueprint, blueprint) : undefined, mime.length ? inArray(media.mime, mime) : undefined, after
81
123
  ? or(lt(media.createdAt, after.time), and(eq(media.createdAt, after.time), lt(media.id, after.id)))
82
- : undefined))
124
+ : undefined, query ? searchFilter(media.searchText, query) : undefined))
83
125
  .orderBy(desc(media.createdAt), desc(media.id))
84
126
  .limit(count)
85
- .all(), query ? (row) => matchScore(query, row.filename, assetText(cms, row, labels)) >= 0 : undefined);
127
+ .all(), query
128
+ ? (row) => matchScore(query, row.filename, assetText(cms, row, labels)) >= 0
129
+ : undefined, query ? (batch) => fillAssetSearchText(cms, batch, labels) : undefined);
86
130
  return page(rows, limit, scope, (row) => row.createdAt);
87
131
  }
@@ -1905,6 +1905,7 @@ export declare function createCMS(options: CmsOptions): {
1905
1905
  publishedAt: number | null;
1906
1906
  updatedAt: number;
1907
1907
  createdAt: number;
1908
+ searchText: string | null;
1908
1909
  } | undefined;
1909
1910
  list: (id: string) => {
1910
1911
  id: string;
@@ -1918,6 +1919,7 @@ export declare function createCMS(options: CmsOptions): {
1918
1919
  publishedAt: number | null;
1919
1920
  updatedAt: number;
1920
1921
  createdAt: number;
1922
+ searchText: string | null;
1921
1923
  }[];
1922
1924
  ensureSingleton: (id: string) => {
1923
1925
  id: string;
@@ -1931,6 +1933,7 @@ export declare function createCMS(options: CmsOptions): {
1931
1933
  publishedAt: number | null;
1932
1934
  updatedAt: number;
1933
1935
  createdAt: number;
1936
+ searchText: string | null;
1934
1937
  };
1935
1938
  save: (blueprintId: string, input: unknown, id?: string, version?: number) => {
1936
1939
  id: string;
@@ -1944,6 +1947,7 @@ export declare function createCMS(options: CmsOptions): {
1944
1947
  publishedAt: number | null;
1945
1948
  updatedAt: number;
1946
1949
  createdAt: number;
1950
+ searchText: string | null;
1947
1951
  };
1948
1952
  duplicate: (id: string, version: number) => {
1949
1953
  id: string;
@@ -1957,6 +1961,7 @@ export declare function createCMS(options: CmsOptions): {
1957
1961
  publishedAt: number | null;
1958
1962
  updatedAt: number;
1959
1963
  createdAt: number;
1964
+ searchText: string | null;
1960
1965
  };
1961
1966
  publish: (id: string, version: number) => {
1962
1967
  id: string;
@@ -1970,10 +1975,13 @@ export declare function createCMS(options: CmsOptions): {
1970
1975
  publishedAt: number | null;
1971
1976
  updatedAt: number;
1972
1977
  createdAt: number;
1978
+ searchText: string | null;
1973
1979
  };
1974
1980
  remove: (id: string) => void;
1975
1981
  clearReferences: (targetId: string, kind: "entry" | "media") => void;
1976
- published: (id: string, slug?: string) => {
1982
+ published: (id: string, slug?: string, options?: {
1983
+ limit?: number;
1984
+ }) => {
1977
1985
  id: string;
1978
1986
  blueprint: string;
1979
1987
  data: import("../blueprints/types.js").Payload;
@@ -1983,7 +1991,7 @@ export declare function createCMS(options: CmsOptions): {
1983
1991
  id: string;
1984
1992
  data: import("../blueprints/types.js").Payload;
1985
1993
  } | null;
1986
- validateLinkTarget: (field: import("../blueprints/types.js").Field, link: import("../link.ts").LinkValue, publish?: boolean) => void;
1994
+ validateLinkTarget: (field: import("../blueprints/types.js").Field, link: import("../link.ts").LinkValue, publish?: boolean, assets?: import("./content.js").AssetMap) => void;
1987
1995
  unpublish: (id: string, version: number) => {
1988
1996
  id: string;
1989
1997
  blueprint: string;
@@ -1996,6 +2004,7 @@ export declare function createCMS(options: CmsOptions): {
1996
2004
  publishedAt: number | null;
1997
2005
  updatedAt: number;
1998
2006
  createdAt: number;
2007
+ searchText: string | null;
1999
2008
  };
2000
2009
  usages: (id: string) => {
2001
2010
  documentId: string;
@@ -2076,6 +2085,7 @@ export declare function createCMS(options: CmsOptions): {
2076
2085
  variants: import("./db/schema.ts").Variant[];
2077
2086
  error: string | null;
2078
2087
  createdAt: number;
2088
+ searchText: string | null;
2079
2089
  } | undefined;
2080
2090
  upload: (file: File, blueprintId?: string) => Promise<{
2081
2091
  blueprint: string;
@@ -2090,6 +2100,7 @@ export declare function createCMS(options: CmsOptions): {
2090
2100
  variants: import("./db/schema.ts").Variant[];
2091
2101
  error: string | null;
2092
2102
  createdAt: number;
2103
+ searchText: string | null;
2093
2104
  }>;
2094
2105
  initiateUpload: (input: {
2095
2106
  name: string;
@@ -2114,8 +2125,10 @@ export declare function createCMS(options: CmsOptions): {
2114
2125
  variants: import("./db/schema.ts").Variant[];
2115
2126
  error: string | null;
2116
2127
  createdAt: number;
2128
+ searchText: string | null;
2117
2129
  };
2118
2130
  tick: () => Promise<boolean>;
2131
+ drain: () => Promise<void>;
2119
2132
  retry: (id: string) => void;
2120
2133
  version: string;
2121
2134
  versionForAsset: (asset: NonNullable<ReturnType<(id: string) => {
@@ -2131,6 +2144,7 @@ export declare function createCMS(options: CmsOptions): {
2131
2144
  variants: import("./db/schema.ts").Variant[];
2132
2145
  error: string | null;
2133
2146
  createdAt: number;
2147
+ searchText: string | null;
2134
2148
  } | undefined>>) => string;
2135
2149
  workerUrl: string;
2136
2150
  list: () => {
@@ -2146,6 +2160,7 @@ export declare function createCMS(options: CmsOptions): {
2146
2160
  variants: import("./db/schema.ts").Variant[];
2147
2161
  error: string | null;
2148
2162
  createdAt: number;
2163
+ searchText: string | null;
2149
2164
  }[];
2150
2165
  updateMetadata: (id: string, input: unknown) => void;
2151
2166
  health(): Promise<{
@@ -26,6 +26,8 @@ export function createCMS(options) {
26
26
  (result.backup ? `. Backup: ${result.backup}` : ''));
27
27
  }
28
28
  assertSchema(store);
29
+ // Blueprint labels and fields can change between deploys, so cached search text is rebuilt lazily.
30
+ store.sqlite.exec('UPDATE documents SET searchText = NULL WHERE searchText IS NOT NULL; UPDATE media SET searchText = NULL WHERE searchText IS NOT NULL;');
29
31
  const email = createEmail(options.email), auth = createAuth(store, options), content = createContent(store, definition), notifications = createNotifications(store, definition, email, options.origin), forms = createForms(store, definition, notifications.send), media = createMedia(store, definition, content, options.workerToken ?? '', options.workerUrl);
30
32
  return {
31
33
  definition,
@@ -56,7 +58,7 @@ export function createCMS(options) {
56
58
  const doc = content.get(previewId);
57
59
  return doc?.blueprint === blueprint ? { id: doc.id, data: doc.draft } : null;
58
60
  }
59
- return content.published(blueprint, slug)[0] ?? null;
61
+ return content.published(blueprint, slug, { limit: 1 })[0] ?? null;
60
62
  },
61
63
  close() {
62
64
  store.close();
@@ -1,6 +1,9 @@
1
1
  import { type LinkValue } from '../link.js';
2
2
  import type { Blueprint, Field, Payload, SiteDefinition } from '../blueprints/types.js';
3
3
  import type { Store } from './db/store.js';
4
+ import { media } from './db/schema.js';
5
+ type MediaRow = typeof media.$inferSelect;
6
+ export type AssetMap = Map<string, MediaRow>;
4
7
  export declare function createContent(store: Store, definition: SiteDefinition): {
5
8
  blueprint: (id: string) => Blueprint;
6
9
  get: (id: string) => {
@@ -15,6 +18,7 @@ export declare function createContent(store: Store, definition: SiteDefinition):
15
18
  publishedAt: number | null;
16
19
  updatedAt: number;
17
20
  createdAt: number;
21
+ searchText: string | null;
18
22
  } | undefined;
19
23
  list: (id: string) => {
20
24
  id: string;
@@ -28,6 +32,7 @@ export declare function createContent(store: Store, definition: SiteDefinition):
28
32
  publishedAt: number | null;
29
33
  updatedAt: number;
30
34
  createdAt: number;
35
+ searchText: string | null;
31
36
  }[];
32
37
  ensureSingleton: (id: string) => {
33
38
  id: string;
@@ -41,6 +46,7 @@ export declare function createContent(store: Store, definition: SiteDefinition):
41
46
  publishedAt: number | null;
42
47
  updatedAt: number;
43
48
  createdAt: number;
49
+ searchText: string | null;
44
50
  };
45
51
  save: (blueprintId: string, input: unknown, id?: string, version?: number) => {
46
52
  id: string;
@@ -54,6 +60,7 @@ export declare function createContent(store: Store, definition: SiteDefinition):
54
60
  publishedAt: number | null;
55
61
  updatedAt: number;
56
62
  createdAt: number;
63
+ searchText: string | null;
57
64
  };
58
65
  duplicate: (id: string, version: number) => {
59
66
  id: string;
@@ -67,6 +74,7 @@ export declare function createContent(store: Store, definition: SiteDefinition):
67
74
  publishedAt: number | null;
68
75
  updatedAt: number;
69
76
  createdAt: number;
77
+ searchText: string | null;
70
78
  };
71
79
  publish: (id: string, version: number) => {
72
80
  id: string;
@@ -80,10 +88,13 @@ export declare function createContent(store: Store, definition: SiteDefinition):
80
88
  publishedAt: number | null;
81
89
  updatedAt: number;
82
90
  createdAt: number;
91
+ searchText: string | null;
83
92
  };
84
93
  remove: (id: string) => void;
85
94
  clearReferences: (targetId: string, kind: "entry" | "media") => void;
86
- published: (id: string, slug?: string) => {
95
+ published: (id: string, slug?: string, options?: {
96
+ limit?: number;
97
+ }) => {
87
98
  id: string;
88
99
  blueprint: string;
89
100
  data: Payload;
@@ -93,7 +104,7 @@ export declare function createContent(store: Store, definition: SiteDefinition):
93
104
  id: string;
94
105
  data: Payload;
95
106
  } | null;
96
- validateLinkTarget: (field: Field, link: LinkValue, publish?: boolean) => void;
107
+ validateLinkTarget: (field: Field, link: LinkValue, publish?: boolean, assets?: AssetMap) => void;
97
108
  unpublish: (id: string, version: number) => {
98
109
  id: string;
99
110
  blueprint: string;
@@ -106,6 +117,7 @@ export declare function createContent(store: Store, definition: SiteDefinition):
106
117
  publishedAt: number | null;
107
118
  updatedAt: number;
108
119
  createdAt: number;
120
+ searchText: string | null;
109
121
  };
110
122
  usages: (id: string) => {
111
123
  documentId: string;
@@ -114,3 +126,4 @@ export declare function createContent(store: Store, definition: SiteDefinition):
114
126
  }[];
115
127
  };
116
128
  export type Content = ReturnType<typeof createContent>;
129
+ export {};
@@ -1,10 +1,17 @@
1
1
  import { isLinkValue } from '../link.js';
2
2
  import { effectiveMetadata, getFileBlueprint } from '../blueprints/files.js';
3
- import { and, eq, desc, isNotNull } from 'drizzle-orm';
3
+ import { and, asc, eq, desc, inArray, isNotNull, or, sql } from 'drizzle-orm';
4
4
  import { validatePayload, walkValues, jsonPayload } from '../blueprints/validate.js';
5
5
  import { mergeDraft } from '../blueprints/merge.js';
6
6
  import { assetPresetVersion } from './media/presets.js';
7
7
  import { documents, references, media } from './db/schema.js';
8
+ // Keeps IN (...) lists well under SQLite's bound-variable limit.
9
+ const chunks = (items, size = 500) => {
10
+ const out = [];
11
+ for (let i = 0; i < items.length; i += size)
12
+ out.push(items.slice(i, i + size));
13
+ return out;
14
+ };
8
15
  export function createContent(store, definition) {
9
16
  const { db } = store;
10
17
  const blueprint = (id) => {
@@ -14,6 +21,26 @@ export function createContent(store, definition) {
14
21
  return b;
15
22
  };
16
23
  const get = (id) => db.select().from(documents).where(eq(documents.id, id)).get();
24
+ const loadAssets = (ids) => {
25
+ const assets = new Map();
26
+ for (const chunk of chunks([...new Set(ids)]))
27
+ for (const row of db.select().from(media).where(inArray(media.id, chunk)).all())
28
+ assets.set(row.id, row);
29
+ return assets;
30
+ };
31
+ // Every media id a payload can reference, including gallery rows and nested fields.
32
+ const assetIds = (b, data) => {
33
+ const ids = new Set();
34
+ walkValues(b.fields, data, (f, v) => {
35
+ if (f.type === 'link' && isLinkValue(v) && v.type === 'file' && v.value)
36
+ ids.add(v.value);
37
+ if (f.type === 'media' || f.type === 'media-list')
38
+ for (const id of Array.isArray(v) ? v : [v])
39
+ if (id)
40
+ ids.add(String(id));
41
+ });
42
+ return ids;
43
+ };
17
44
  const index = (id, b, data, snapshot) => {
18
45
  db.delete(references)
19
46
  .where(and(eq(references.documentId, id), eq(references.snapshot, snapshot)))
@@ -34,7 +61,7 @@ export function createContent(store, definition) {
34
61
  .values({ id: crypto.randomUUID(), documentId: id, targetId, kind, snapshot })
35
62
  .run();
36
63
  };
37
- const validateLinkTarget = (field, link, publish = false) => {
64
+ const validateLinkTarget = (field, link, publish = false, assets) => {
38
65
  if (!link.value)
39
66
  return;
40
67
  if (link.type === 'entry') {
@@ -47,7 +74,9 @@ export function createContent(store, definition) {
47
74
  throw new Error('Choose a valid link entry');
48
75
  }
49
76
  if (link.type === 'file') {
50
- const asset = db.select().from(media).where(eq(media.id, link.value)).get();
77
+ const asset = assets
78
+ ? assets.get(link.value)
79
+ : db.select().from(media).where(eq(media.id, link.value)).get();
51
80
  if (!asset)
52
81
  throw new Error('Selected link file no longer exists');
53
82
  const file = getFileBlueprint(definition, asset.blueprint);
@@ -63,9 +92,10 @@ export function createContent(store, definition) {
63
92
  }
64
93
  };
65
94
  const validateReferences = (b, data, publish) => {
95
+ const assets = loadAssets(assetIds(b, data));
66
96
  walkValues(b.fields, data, (f, v) => {
67
97
  if (f.type === 'link' && isLinkValue(v))
68
- validateLinkTarget(f, v, publish);
98
+ validateLinkTarget(f, v, publish, assets);
69
99
  if (f.type === 'relation' && v) {
70
100
  const target = get(String(v));
71
101
  if (!target || target.blueprint !== f.collection)
@@ -74,11 +104,7 @@ export function createContent(store, definition) {
74
104
  if (f.type === 'media' || f.type === 'media-list')
75
105
  for (const id of Array.isArray(v) ? v : [v])
76
106
  if (id) {
77
- const asset = db
78
- .select()
79
- .from(media)
80
- .where(eq(media.id, String(id)))
81
- .get();
107
+ const asset = assets.get(String(id));
82
108
  if (!asset)
83
109
  throw new Error('Selected image no longer exists');
84
110
  if (asset.status === 'deleting')
@@ -109,11 +135,7 @@ export function createContent(store, definition) {
109
135
  ? value
110
136
  : (Array.isArray(value) ? value : [value]).map((image) => ({ image }));
111
137
  for (const row of rows) {
112
- const asset = db
113
- .select()
114
- .from(media)
115
- .where(eq(media.id, String(row.image)))
116
- .get();
138
+ const asset = assets.get(String(row.image));
117
139
  if (!asset)
118
140
  continue;
119
141
  const file = getFileBlueprint(definition, asset.blueprint);
@@ -132,7 +154,7 @@ export function createContent(store, definition) {
132
154
  .select()
133
155
  .from(documents)
134
156
  .where(eq(documents.blueprint, id))
135
- .orderBy(desc(documents.updatedAt))
157
+ .orderBy(desc(documents.updatedAt), asc(documents.id))
136
158
  .all();
137
159
  };
138
160
  const ensureSingleton = (id) => {
@@ -246,8 +268,22 @@ export function createContent(store, definition) {
246
268
  return save(b.id, data);
247
269
  });
248
270
  const clearReferences = (targetId, kind) => {
249
- // Scan known documents so stale indexes cannot leave a public reference behind.
250
- for (const row of db.select().from(documents).all()) {
271
+ // Candidates come from the reference index plus a raw-text match, so a stale or missing
272
+ // index cannot leave a public reference behind. UUID ids make substring matches precise.
273
+ const candidates = new Set(db
274
+ .selectDistinct({ documentId: references.documentId })
275
+ .from(references)
276
+ .where(eq(references.targetId, targetId))
277
+ .all()
278
+ .map((r) => r.documentId));
279
+ for (const r of db
280
+ .select({ id: documents.id })
281
+ .from(documents)
282
+ .where(or(sql `instr(${documents.draft}, ${targetId}) > 0`, sql `instr(${documents.published}, ${targetId}) > 0`))
283
+ .all())
284
+ candidates.add(r.id);
285
+ const docs = chunks([...candidates]).flatMap((ids) => db.select().from(documents).where(inArray(documents.id, ids)).all());
286
+ for (const row of docs) {
251
287
  const b = definition.blueprints.find((b) => b.id === row.blueprint);
252
288
  if (!b)
253
289
  continue;
@@ -276,7 +312,12 @@ export function createContent(store, definition) {
276
312
  index(row.id, b, published, 'published');
277
313
  }
278
314
  }
279
- for (const asset of db.select().from(media).all()) {
315
+ const assetRows = db
316
+ .select()
317
+ .from(media)
318
+ .where(sql `instr(${media.metadata}, ${targetId}) > 0`)
319
+ .all();
320
+ for (const asset of assetRows) {
280
321
  const file = getFileBlueprint(definition, asset.blueprint);
281
322
  const before = asset.metadata;
282
323
  const metadata = walkValues(file.fields, before, (field, value) => {
@@ -297,18 +338,19 @@ export function createContent(store, definition) {
297
338
  clearReferences(id, 'entry');
298
339
  db.delete(documents).where(eq(documents.id, id)).run();
299
340
  });
300
- const published = (id, slug) => db
301
- .select()
302
- .from(documents)
303
- .where(and(eq(documents.blueprint, id), isNotNull(documents.published), slug === undefined ? undefined : eq(documents.publishedSlug, slug)))
304
- .orderBy(desc(documents.publishedAt))
305
- .all()
306
- .map((r) => ({
307
- id: r.id,
308
- blueprint: r.blueprint,
309
- data: r.published,
310
- publishedAt: r.publishedAt
311
- }));
341
+ const published = (id, slug, options = {}) => {
342
+ const query = db
343
+ .select()
344
+ .from(documents)
345
+ .where(and(eq(documents.blueprint, id), isNotNull(documents.published), slug === undefined ? undefined : eq(documents.publishedSlug, slug)))
346
+ .orderBy(desc(documents.publishedAt));
347
+ return (options.limit === undefined ? query.all() : query.limit(options.limit).all()).map((r) => ({
348
+ id: r.id,
349
+ blueprint: r.blueprint,
350
+ data: r.published,
351
+ publishedAt: r.publishedAt
352
+ }));
353
+ };
312
354
  const resolveRelation = (id) => {
313
355
  const row = get(id);
314
356
  return row?.published ? { id: row.id, data: row.published } : null;
@@ -27,5 +27,13 @@ export const migrationData = [
27
27
  {
28
28
  "name": "0006_durable_media_deletion.sql",
29
29
  "sql": "CREATE TABLE `media_deletions` (\n\t`mediaId` text PRIMARY KEY NOT NULL,\n\t`attempts` integer DEFAULT 0 NOT NULL,\n\t`nextAttempt` integer DEFAULT 0 NOT NULL,\n\t`leaseUntil` integer DEFAULT 0 NOT NULL,\n\tFOREIGN KEY (`mediaId`) REFERENCES `media`(`id`) ON UPDATE no action ON DELETE cascade\n);\n"
30
+ },
31
+ {
32
+ "name": "0007_content_and_job_indexes.sql",
33
+ "sql": "CREATE INDEX `job_media` ON `jobs` (`mediaId`,`version`);--> statement-breakpoint\nCREATE INDEX `reference_document` ON `content_references` (`documentId`,`snapshot`);"
34
+ },
35
+ {
36
+ "name": "0008_search_text_cache.sql",
37
+ "sql": "CREATE TABLE `media_document_links` (\n\t`mediaId` text NOT NULL,\n\t`documentId` text NOT NULL,\n\tPRIMARY KEY(`mediaId`, `documentId`),\n\tFOREIGN KEY (`mediaId`) REFERENCES `media`(`id`) ON UPDATE no action ON DELETE cascade\n);\n--> statement-breakpoint\nCREATE INDEX `media_document_link_document` ON `media_document_links` (`documentId`);--> statement-breakpoint\nALTER TABLE `documents` ADD `searchText` text;--> statement-breakpoint\nALTER TABLE `media` ADD `searchText` text;--> statement-breakpoint\nINSERT OR IGNORE INTO `media_document_links` (`mediaId`, `documentId`)\nSELECT `media`.`id`, `documents`.`id` FROM `media`, json_tree(`media`.`metadata`) AS `node`\nJOIN `documents` ON `documents`.`id` = `node`.`value` WHERE `node`.`type` = 'text';--> statement-breakpoint\nCREATE TRIGGER `media_document_links_inserted` AFTER INSERT ON `media` BEGIN\n INSERT OR IGNORE INTO `media_document_links` (`mediaId`, `documentId`)\n SELECT NEW.`id`, `documents`.`id` FROM json_tree(NEW.`metadata`) AS `node`\n JOIN `documents` ON `documents`.`id` = `node`.`value` WHERE `node`.`type` = 'text';\nEND;--> statement-breakpoint\nCREATE TRIGGER `media_document_links_updated` AFTER UPDATE OF `metadata` ON `media` BEGIN\n DELETE FROM `media_document_links` WHERE `mediaId` = NEW.`id`;\n INSERT OR IGNORE INTO `media_document_links` (`mediaId`, `documentId`)\n SELECT NEW.`id`, `documents`.`id` FROM json_tree(NEW.`metadata`) AS `node`\n JOIN `documents` ON `documents`.`id` = `node`.`value` WHERE `node`.`type` = 'text';\nEND;--> statement-breakpoint\nCREATE TRIGGER `documents_search_text_stale` AFTER UPDATE OF `draft`, `blueprint` ON `documents` BEGIN\n UPDATE `documents` SET `searchText` = NULL WHERE `id` = NEW.`id`;\nEND;--> statement-breakpoint\nCREATE TRIGGER `documents_search_text_label` AFTER UPDATE OF `draft`, `blueprint` ON `documents`\nWHEN json_extract(OLD.`draft`, '$.title') IS NOT json_extract(NEW.`draft`, '$.title')\n OR json_extract(OLD.`draft`, '$.name') IS NOT json_extract(NEW.`draft`, '$.name')\n OR OLD.`blueprint` IS NOT NEW.`blueprint`\nBEGIN\n UPDATE `media` SET `searchText` = NULL WHERE `id` IN (\n SELECT `mediaId` FROM `media_document_links` WHERE `documentId` = NEW.`id`\n );\nEND;--> statement-breakpoint\nCREATE TRIGGER `documents_search_text_deleted` AFTER DELETE ON `documents` BEGIN\n UPDATE `media` SET `searchText` = NULL WHERE `id` IN (\n SELECT `mediaId` FROM `media_document_links` WHERE `documentId` = OLD.`id`\n );\n DELETE FROM `media_document_links` WHERE `documentId` = OLD.`id`;\nEND;--> statement-breakpoint\nCREATE TRIGGER `media_search_text_stale` AFTER UPDATE OF `metadata`, `filename`, `blueprint` ON `media` BEGIN\n UPDATE `media` SET `searchText` = NULL WHERE `id` = NEW.`id`;\nEND;\n"
30
38
  }
31
39
  ];
@@ -0,0 +1,2 @@
1
+ CREATE INDEX `job_media` ON `jobs` (`mediaId`,`version`);--> statement-breakpoint
2
+ CREATE INDEX `reference_document` ON `content_references` (`documentId`,`snapshot`);
@@ -0,0 +1,45 @@
1
+ CREATE TABLE `media_document_links` (
2
+ `mediaId` text NOT NULL,
3
+ `documentId` text NOT NULL,
4
+ PRIMARY KEY(`mediaId`, `documentId`),
5
+ FOREIGN KEY (`mediaId`) REFERENCES `media`(`id`) ON UPDATE no action ON DELETE cascade
6
+ );
7
+ --> statement-breakpoint
8
+ CREATE INDEX `media_document_link_document` ON `media_document_links` (`documentId`);--> statement-breakpoint
9
+ ALTER TABLE `documents` ADD `searchText` text;--> statement-breakpoint
10
+ ALTER TABLE `media` ADD `searchText` text;--> statement-breakpoint
11
+ INSERT OR IGNORE INTO `media_document_links` (`mediaId`, `documentId`)
12
+ SELECT `media`.`id`, `documents`.`id` FROM `media`, json_tree(`media`.`metadata`) AS `node`
13
+ JOIN `documents` ON `documents`.`id` = `node`.`value` WHERE `node`.`type` = 'text';--> statement-breakpoint
14
+ CREATE TRIGGER `media_document_links_inserted` AFTER INSERT ON `media` BEGIN
15
+ INSERT OR IGNORE INTO `media_document_links` (`mediaId`, `documentId`)
16
+ SELECT NEW.`id`, `documents`.`id` FROM json_tree(NEW.`metadata`) AS `node`
17
+ JOIN `documents` ON `documents`.`id` = `node`.`value` WHERE `node`.`type` = 'text';
18
+ END;--> statement-breakpoint
19
+ CREATE TRIGGER `media_document_links_updated` AFTER UPDATE OF `metadata` ON `media` BEGIN
20
+ DELETE FROM `media_document_links` WHERE `mediaId` = NEW.`id`;
21
+ INSERT OR IGNORE INTO `media_document_links` (`mediaId`, `documentId`)
22
+ SELECT NEW.`id`, `documents`.`id` FROM json_tree(NEW.`metadata`) AS `node`
23
+ JOIN `documents` ON `documents`.`id` = `node`.`value` WHERE `node`.`type` = 'text';
24
+ END;--> statement-breakpoint
25
+ CREATE TRIGGER `documents_search_text_stale` AFTER UPDATE OF `draft`, `blueprint` ON `documents` BEGIN
26
+ UPDATE `documents` SET `searchText` = NULL WHERE `id` = NEW.`id`;
27
+ END;--> statement-breakpoint
28
+ CREATE TRIGGER `documents_search_text_label` AFTER UPDATE OF `draft`, `blueprint` ON `documents`
29
+ WHEN json_extract(OLD.`draft`, '$.title') IS NOT json_extract(NEW.`draft`, '$.title')
30
+ OR json_extract(OLD.`draft`, '$.name') IS NOT json_extract(NEW.`draft`, '$.name')
31
+ OR OLD.`blueprint` IS NOT NEW.`blueprint`
32
+ BEGIN
33
+ UPDATE `media` SET `searchText` = NULL WHERE `id` IN (
34
+ SELECT `mediaId` FROM `media_document_links` WHERE `documentId` = NEW.`id`
35
+ );
36
+ END;--> statement-breakpoint
37
+ CREATE TRIGGER `documents_search_text_deleted` AFTER DELETE ON `documents` BEGIN
38
+ UPDATE `media` SET `searchText` = NULL WHERE `id` IN (
39
+ SELECT `mediaId` FROM `media_document_links` WHERE `documentId` = OLD.`id`
40
+ );
41
+ DELETE FROM `media_document_links` WHERE `documentId` = OLD.`id`;
42
+ END;--> statement-breakpoint
43
+ CREATE TRIGGER `media_search_text_stale` AFTER UPDATE OF `metadata`, `filename`, `blueprint` ON `media` BEGIN
44
+ UPDATE `media` SET `searchText` = NULL WHERE `id` = NEW.`id`;
45
+ END;