sandcms 0.6.0 → 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.
- package/README.md +1 -1
- package/dist/cli/cloudflare.js +6 -6
- package/dist/cli/doctor.js +2 -2
- package/dist/cli/files.js +11 -6
- package/dist/cli/index.js +1 -1
- package/dist/cli/upgrade.js +2 -1
- package/dist/env.d.ts +80 -0
- package/dist/env.js +103 -0
- package/dist/server/admin-load.d.ts +46 -76
- package/dist/server/admin-load.js +98 -24
- package/dist/server/api-pagination.d.ts +2 -0
- package/dist/server/api-pagination.js +51 -7
- package/dist/server/cms.d.ts +19 -2
- package/dist/server/cms.js +13 -1
- package/dist/server/content.d.ts +15 -2
- package/dist/server/content.js +72 -30
- package/dist/server/db/auto-migrate.d.ts +12 -0
- package/dist/server/db/auto-migrate.js +23 -0
- package/dist/server/db/migration-data.js +8 -0
- package/dist/server/db/migrations/0007_content_and_job_indexes.sql +2 -0
- package/dist/server/db/migrations/0008_search_text_cache.sql +45 -0
- package/dist/server/db/migrations/meta/0007_snapshot.json +1643 -0
- package/dist/server/db/migrations/meta/0008_snapshot.json +1711 -0
- package/dist/server/db/migrations/meta/_journal.json +14 -0
- package/dist/server/db/schema.d.ts +83 -0
- package/dist/server/db/schema.js +23 -5
- package/dist/server/db/store.js +24 -9
- package/dist/server/email/index.d.ts +5 -1
- package/dist/server/email/index.js +12 -11
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +1 -0
- package/dist/server/media/runner.js +217 -191
- package/dist/server/media/service.d.ts +6 -0
- package/dist/server/media/service.js +36 -3
- package/dist/server/search.d.ts +2 -0
- package/dist/server/search.js +7 -0
- package/dist/worker/index.d.ts +1 -1
- package/dist/worker/index.js +134 -81
- package/dist/worker/local.js +5 -5
- package/dist/worker/uploads.d.ts +5 -5
- package/dist/worker/uploads.js +11 -9
- package/docs/cloudflare.md +7 -7
- package/docs/deployment.md +18 -0
- package/docs/email.md +5 -5
- package/docs/media.md +2 -2
- package/docs/package.md +4 -4
- package/docs/releases.md +18 -0
- package/docs/upgrades.md +66 -0
- package/package.json +5 -1
- package/templates/env.ts +8 -0
- package/templates/integration.ts +6 -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
|
|
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
|
|
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
|
}
|
package/dist/server/cms.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export interface CmsOptions {
|
|
|
12
12
|
workerUrl?: string;
|
|
13
13
|
trustedProxyHeader?: string;
|
|
14
14
|
migrate?: boolean;
|
|
15
|
+
/** Back up an existing database, then apply pending migrations on startup. */
|
|
16
|
+
autoMigrate?: boolean;
|
|
15
17
|
email?: EmailOptions;
|
|
16
18
|
seo?: SeoOptions;
|
|
17
19
|
}
|
|
@@ -1903,6 +1905,7 @@ export declare function createCMS(options: CmsOptions): {
|
|
|
1903
1905
|
publishedAt: number | null;
|
|
1904
1906
|
updatedAt: number;
|
|
1905
1907
|
createdAt: number;
|
|
1908
|
+
searchText: string | null;
|
|
1906
1909
|
} | undefined;
|
|
1907
1910
|
list: (id: string) => {
|
|
1908
1911
|
id: string;
|
|
@@ -1916,6 +1919,7 @@ export declare function createCMS(options: CmsOptions): {
|
|
|
1916
1919
|
publishedAt: number | null;
|
|
1917
1920
|
updatedAt: number;
|
|
1918
1921
|
createdAt: number;
|
|
1922
|
+
searchText: string | null;
|
|
1919
1923
|
}[];
|
|
1920
1924
|
ensureSingleton: (id: string) => {
|
|
1921
1925
|
id: string;
|
|
@@ -1929,6 +1933,7 @@ export declare function createCMS(options: CmsOptions): {
|
|
|
1929
1933
|
publishedAt: number | null;
|
|
1930
1934
|
updatedAt: number;
|
|
1931
1935
|
createdAt: number;
|
|
1936
|
+
searchText: string | null;
|
|
1932
1937
|
};
|
|
1933
1938
|
save: (blueprintId: string, input: unknown, id?: string, version?: number) => {
|
|
1934
1939
|
id: string;
|
|
@@ -1942,6 +1947,7 @@ export declare function createCMS(options: CmsOptions): {
|
|
|
1942
1947
|
publishedAt: number | null;
|
|
1943
1948
|
updatedAt: number;
|
|
1944
1949
|
createdAt: number;
|
|
1950
|
+
searchText: string | null;
|
|
1945
1951
|
};
|
|
1946
1952
|
duplicate: (id: string, version: number) => {
|
|
1947
1953
|
id: string;
|
|
@@ -1955,6 +1961,7 @@ export declare function createCMS(options: CmsOptions): {
|
|
|
1955
1961
|
publishedAt: number | null;
|
|
1956
1962
|
updatedAt: number;
|
|
1957
1963
|
createdAt: number;
|
|
1964
|
+
searchText: string | null;
|
|
1958
1965
|
};
|
|
1959
1966
|
publish: (id: string, version: number) => {
|
|
1960
1967
|
id: string;
|
|
@@ -1968,10 +1975,13 @@ export declare function createCMS(options: CmsOptions): {
|
|
|
1968
1975
|
publishedAt: number | null;
|
|
1969
1976
|
updatedAt: number;
|
|
1970
1977
|
createdAt: number;
|
|
1978
|
+
searchText: string | null;
|
|
1971
1979
|
};
|
|
1972
1980
|
remove: (id: string) => void;
|
|
1973
1981
|
clearReferences: (targetId: string, kind: "entry" | "media") => void;
|
|
1974
|
-
published: (id: string, slug?: string
|
|
1982
|
+
published: (id: string, slug?: string, options?: {
|
|
1983
|
+
limit?: number;
|
|
1984
|
+
}) => {
|
|
1975
1985
|
id: string;
|
|
1976
1986
|
blueprint: string;
|
|
1977
1987
|
data: import("../blueprints/types.js").Payload;
|
|
@@ -1981,7 +1991,7 @@ export declare function createCMS(options: CmsOptions): {
|
|
|
1981
1991
|
id: string;
|
|
1982
1992
|
data: import("../blueprints/types.js").Payload;
|
|
1983
1993
|
} | null;
|
|
1984
|
-
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;
|
|
1985
1995
|
unpublish: (id: string, version: number) => {
|
|
1986
1996
|
id: string;
|
|
1987
1997
|
blueprint: string;
|
|
@@ -1994,6 +2004,7 @@ export declare function createCMS(options: CmsOptions): {
|
|
|
1994
2004
|
publishedAt: number | null;
|
|
1995
2005
|
updatedAt: number;
|
|
1996
2006
|
createdAt: number;
|
|
2007
|
+
searchText: string | null;
|
|
1997
2008
|
};
|
|
1998
2009
|
usages: (id: string) => {
|
|
1999
2010
|
documentId: string;
|
|
@@ -2074,6 +2085,7 @@ export declare function createCMS(options: CmsOptions): {
|
|
|
2074
2085
|
variants: import("./db/schema.ts").Variant[];
|
|
2075
2086
|
error: string | null;
|
|
2076
2087
|
createdAt: number;
|
|
2088
|
+
searchText: string | null;
|
|
2077
2089
|
} | undefined;
|
|
2078
2090
|
upload: (file: File, blueprintId?: string) => Promise<{
|
|
2079
2091
|
blueprint: string;
|
|
@@ -2088,6 +2100,7 @@ export declare function createCMS(options: CmsOptions): {
|
|
|
2088
2100
|
variants: import("./db/schema.ts").Variant[];
|
|
2089
2101
|
error: string | null;
|
|
2090
2102
|
createdAt: number;
|
|
2103
|
+
searchText: string | null;
|
|
2091
2104
|
}>;
|
|
2092
2105
|
initiateUpload: (input: {
|
|
2093
2106
|
name: string;
|
|
@@ -2112,8 +2125,10 @@ export declare function createCMS(options: CmsOptions): {
|
|
|
2112
2125
|
variants: import("./db/schema.ts").Variant[];
|
|
2113
2126
|
error: string | null;
|
|
2114
2127
|
createdAt: number;
|
|
2128
|
+
searchText: string | null;
|
|
2115
2129
|
};
|
|
2116
2130
|
tick: () => Promise<boolean>;
|
|
2131
|
+
drain: () => Promise<void>;
|
|
2117
2132
|
retry: (id: string) => void;
|
|
2118
2133
|
version: string;
|
|
2119
2134
|
versionForAsset: (asset: NonNullable<ReturnType<(id: string) => {
|
|
@@ -2129,6 +2144,7 @@ export declare function createCMS(options: CmsOptions): {
|
|
|
2129
2144
|
variants: import("./db/schema.ts").Variant[];
|
|
2130
2145
|
error: string | null;
|
|
2131
2146
|
createdAt: number;
|
|
2147
|
+
searchText: string | null;
|
|
2132
2148
|
} | undefined>>) => string;
|
|
2133
2149
|
workerUrl: string;
|
|
2134
2150
|
list: () => {
|
|
@@ -2144,6 +2160,7 @@ export declare function createCMS(options: CmsOptions): {
|
|
|
2144
2160
|
variants: import("./db/schema.ts").Variant[];
|
|
2145
2161
|
error: string | null;
|
|
2146
2162
|
createdAt: number;
|
|
2163
|
+
searchText: string | null;
|
|
2147
2164
|
}[];
|
|
2148
2165
|
updateMetadata: (id: string, input: unknown) => void;
|
|
2149
2166
|
health(): Promise<{
|
package/dist/server/cms.js
CHANGED
|
@@ -2,6 +2,7 @@ import { createLinkResolver } from './links.js';
|
|
|
2
2
|
import { configureFileFields } from '../blueprints/files.js';
|
|
3
3
|
import { loadDefinition } from '../blueprints/load.js';
|
|
4
4
|
import { openStore, assertSchema } from './db/store.js';
|
|
5
|
+
import { autoMigrate } from './db/auto-migrate.js';
|
|
5
6
|
import { createAuth, requireUser } from './auth.js';
|
|
6
7
|
import { createContent } from './content.js';
|
|
7
8
|
import { createSEO } from './seo.js';
|
|
@@ -13,9 +14,20 @@ import { createMedia } from './media/service.js';
|
|
|
13
14
|
import { error } from '@sveltejs/kit';
|
|
14
15
|
export function createCMS(options) {
|
|
15
16
|
options = { ...options, origin: new URL(options.origin).origin };
|
|
17
|
+
// migrate would apply pending migrations before autoMigrate could take its snapshot.
|
|
18
|
+
if (options.migrate && options.autoMigrate)
|
|
19
|
+
throw new Error('Use either migrate or autoMigrate, not both.');
|
|
16
20
|
const definition = configureFileFields(options.definition ?? loadDefinition(options.root)), store = openStore(options.database, { migrate: options.migrate });
|
|
17
21
|
try {
|
|
22
|
+
if (options.autoMigrate) {
|
|
23
|
+
const result = autoMigrate(store);
|
|
24
|
+
if (result.applied.length)
|
|
25
|
+
console.info(`SandCMS applied migrations: ${result.applied.join(', ')}` +
|
|
26
|
+
(result.backup ? `. Backup: ${result.backup}` : ''));
|
|
27
|
+
}
|
|
18
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;');
|
|
19
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);
|
|
20
32
|
return {
|
|
21
33
|
definition,
|
|
@@ -46,7 +58,7 @@ export function createCMS(options) {
|
|
|
46
58
|
const doc = content.get(previewId);
|
|
47
59
|
return doc?.blueprint === blueprint ? { id: doc.id, data: doc.draft } : null;
|
|
48
60
|
}
|
|
49
|
-
return content.published(blueprint, slug)[0] ?? null;
|
|
61
|
+
return content.published(blueprint, slug, { limit: 1 })[0] ?? null;
|
|
50
62
|
},
|
|
51
63
|
close() {
|
|
52
64
|
store.close();
|
package/dist/server/content.d.ts
CHANGED
|
@@ -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 {};
|
package/dist/server/content.js
CHANGED
|
@@ -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 =
|
|
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 =
|
|
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 =
|
|
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
|
-
//
|
|
250
|
-
|
|
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
|
-
|
|
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) =>
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
.map((r) => ({
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
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;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Store } from './store.js';
|
|
2
|
+
/**
|
|
3
|
+
* Apply pending migrations when the website starts. An existing database is first copied to a
|
|
4
|
+
* verified `<database>.before-migrate-<timestamp>-<id>.sqlite` snapshot next to it.
|
|
5
|
+
*/
|
|
6
|
+
export declare function autoMigrate(store: Store): {
|
|
7
|
+
applied: string[];
|
|
8
|
+
backup?: undefined;
|
|
9
|
+
} | {
|
|
10
|
+
applied: string[];
|
|
11
|
+
backup: string | undefined;
|
|
12
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { backup } from '../../cli/backup.js';
|
|
2
|
+
import { applyMigrations, pendingMigrations } from './store.js';
|
|
3
|
+
/**
|
|
4
|
+
* Apply pending migrations when the website starts. An existing database is first copied to a
|
|
5
|
+
* verified `<database>.before-migrate-<timestamp>-<id>.sqlite` snapshot next to it.
|
|
6
|
+
*/
|
|
7
|
+
export function autoMigrate(store) {
|
|
8
|
+
if (!pendingMigrations(store).length)
|
|
9
|
+
return { applied: [] };
|
|
10
|
+
const version = store.sqlite.query('PRAGMA user_version').get();
|
|
11
|
+
if (version.user_version === 0) {
|
|
12
|
+
// Without history, only an empty database is fresh; anything else cannot be snapshotted safely.
|
|
13
|
+
const tables = store.sqlite
|
|
14
|
+
.query("SELECT 1 FROM sqlite_master WHERE name NOT LIKE 'sqlite_%' AND name != 'schema_migrations' LIMIT 1")
|
|
15
|
+
.get();
|
|
16
|
+
if (tables)
|
|
17
|
+
throw new Error('Database has no migration history but is not empty; automatic migration refused. Use the matching package version or an explicitly initialized fresh database. See docs/upgrades.md.');
|
|
18
|
+
}
|
|
19
|
+
const snapshot = store.path !== ':memory:' && version.user_version > 0
|
|
20
|
+
? backup(store.path, `${store.path}.before-migrate-${Date.now()}-${crypto.randomUUID().slice(0, 8)}.sqlite`)
|
|
21
|
+
: undefined;
|
|
22
|
+
return { applied: applyMigrations(store), backup: snapshot };
|
|
23
|
+
}
|
|
@@ -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,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;
|