opacacms 0.1.7 → 0.1.8

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 (46) hide show
  1. package/dist/admin/index.js +255 -20
  2. package/dist/admin/webcomponent.js +291 -46
  3. package/dist/cli/index.js +3638 -30
  4. package/dist/client.js +126 -5
  5. package/dist/db/bun-sqlite.js +790 -21
  6. package/dist/db/d1.js +788 -19
  7. package/dist/db/index.js +53 -4
  8. package/dist/db/postgres.js +792 -23
  9. package/dist/db/sqlite.js +788 -19
  10. package/dist/index.js +456 -8
  11. package/dist/runtimes/bun.js +1909 -8
  12. package/dist/runtimes/cloudflare-workers.js +1910 -9
  13. package/dist/runtimes/next.js +1908 -7
  14. package/dist/runtimes/node.js +1909 -8
  15. package/dist/schema/collection.d.ts +3 -7
  16. package/dist/schema/fields/index.d.ts +24 -25
  17. package/dist/schema/global.d.ts +9 -9
  18. package/dist/schema/index.d.ts +30 -4
  19. package/dist/schema/index.js +546 -1
  20. package/dist/server.js +2246 -17
  21. package/dist/storage/index.js +40 -1
  22. package/package.json +1 -1
  23. package/dist/chunk-16vgcf3k.js +0 -88
  24. package/dist/chunk-2yz1nsxs.js +0 -126
  25. package/dist/chunk-5gvbp2qa.js +0 -167
  26. package/dist/chunk-62ev8gnc.js +0 -41
  27. package/dist/chunk-6ew02s0c.js +0 -472
  28. package/dist/chunk-7a9kn0np.js +0 -116
  29. package/dist/chunk-8sqjbsgt.js +0 -42
  30. package/dist/chunk-9kxpbcb1.js +0 -85
  31. package/dist/chunk-cvdd4eqh.js +0 -110
  32. package/dist/chunk-d3ffeqp9.js +0 -87
  33. package/dist/chunk-fa5mg0hr.js +0 -96
  34. package/dist/chunk-j4d50hrx.js +0 -20
  35. package/dist/chunk-jwjk85ze.js +0 -15
  36. package/dist/chunk-m09hahe2.js +0 -250
  37. package/dist/chunk-s8mqwnm1.js +0 -14
  38. package/dist/chunk-srsac177.js +0 -85
  39. package/dist/chunk-v521d72w.js +0 -10
  40. package/dist/chunk-vtvqfhgy.js +0 -2442
  41. package/dist/chunk-xa7rjsn2.js +0 -20
  42. package/dist/chunk-xg35h5a3.js +0 -15
  43. package/dist/chunk-y8hc6nm4.js +0 -17
  44. package/dist/chunk-ybbbqj63.js +0 -130
  45. package/dist/chunk-yr32cp7h.js +0 -1603
  46. package/dist/chunk-zvwb67nd.js +0 -332
@@ -1,85 +0,0 @@
1
- import"./chunk-8sqjbsgt.js";
2
-
3
- // src/cli/d1-mock.ts
4
- import { Database } from "bun:sqlite";
5
- import fs from "node:fs";
6
- import path from "node:path";
7
- function createD1Mock(dbPath = ".opaca/local-d1.db") {
8
- const wranglerD1Dir = path.resolve(process.cwd(), ".wrangler/state/v3/d1/miniflare-D1DatabaseObject");
9
- if (fs.existsSync(wranglerD1Dir)) {
10
- const files = fs.readdirSync(wranglerD1Dir);
11
- const sqliteFile = files.find((f) => f.endsWith(".sqlite"));
12
- if (sqliteFile) {
13
- const wranglerPath = path.join(wranglerD1Dir, sqliteFile);
14
- try {
15
- const sqlite = new Database(wranglerPath);
16
- console.log(`[OpacaCMS] Using Wrangler D1 local state: ${sqliteFile}`);
17
- return wrapSqlite(sqlite);
18
- } catch (e) {
19
- console.warn(`[OpacaCMS] Failed to open Wrangler D1 state at ${wranglerPath}. Falling back...`);
20
- }
21
- }
22
- }
23
- const absolutePath = path.isAbsolute(dbPath) ? dbPath : path.resolve(process.cwd(), dbPath);
24
- const dir = path.dirname(absolutePath);
25
- try {
26
- if (!fs.existsSync(dir)) {
27
- fs.mkdirSync(dir, { recursive: true });
28
- }
29
- const sqlite = new Database(absolutePath);
30
- console.log(`[OpacaCMS] Using local CLI database: ${absolutePath}`);
31
- return wrapSqlite(sqlite);
32
- } catch (err) {
33
- console.warn(`[OpacaCMS] Warning: Could not open database at ${absolutePath}. Using memory database.`);
34
- return wrapSqlite(new Database(":memory:"));
35
- }
36
- }
37
- function wrapSqlite(sqlite) {
38
- return {
39
- prepare(query) {
40
- const stmt = sqlite.prepare(query);
41
- const bridge = {
42
- bind(...params) {
43
- bridge._params = params;
44
- return bridge;
45
- },
46
- async first() {
47
- const params = bridge._params || [];
48
- return stmt.get(...params);
49
- },
50
- async all() {
51
- const params = bridge._params || [];
52
- const results = stmt.all(...params);
53
- return { results, success: true, meta: {} };
54
- },
55
- async run() {
56
- const params = bridge._params || [];
57
- const result = stmt.run(...params);
58
- return {
59
- success: true,
60
- meta: { changes: result.changes, last_row_id: result.lastInsertRowid }
61
- };
62
- },
63
- async raw() {
64
- const params = bridge._params || [];
65
- return stmt.all(...params);
66
- }
67
- };
68
- return bridge;
69
- },
70
- async batch(statements) {
71
- const results = [];
72
- for (const stmt of statements) {
73
- results.push(await stmt.all());
74
- }
75
- return results;
76
- },
77
- async exec(query) {
78
- sqlite.exec(query);
79
- return { count: 0, duration: 0 };
80
- }
81
- };
82
- }
83
- export {
84
- createD1Mock
85
- };
@@ -1,110 +0,0 @@
1
- // src/db/kysely/field-mapper.ts
2
- function toSnakeCase(str) {
3
- return str.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/^_/, "");
4
- }
5
- function mapFieldToPostgresType(field) {
6
- switch (field.type) {
7
- case "text":
8
- case "richtext":
9
- case "select":
10
- case "radio":
11
- case "relationship":
12
- return "text";
13
- case "number":
14
- return "double precision";
15
- case "boolean":
16
- return "boolean";
17
- case "date":
18
- return "timestamp with time zone";
19
- case "json":
20
- case "file":
21
- return "jsonb";
22
- default:
23
- return "text";
24
- }
25
- }
26
- function mapFieldToSQLiteType(field) {
27
- switch (field.type) {
28
- case "text":
29
- case "richtext":
30
- case "select":
31
- case "radio":
32
- case "relationship":
33
- case "date":
34
- case "json":
35
- case "file":
36
- return "text";
37
- case "number":
38
- return "numeric";
39
- case "boolean":
40
- return "integer";
41
- default:
42
- return "text";
43
- }
44
- }
45
- function flattenFields(fields, prefix = "") {
46
- const result = [];
47
- for (const field of fields) {
48
- if (field.type === "join" || field.type === "virtual")
49
- continue;
50
- const currentName = field.name ? `${prefix}${field.name}` : undefined;
51
- if (field.type === "group") {
52
- if (field.fields && Array.isArray(field.fields)) {
53
- const nextPrefix = currentName ? `${currentName}__` : "";
54
- result.push(...flattenFields(field.fields, nextPrefix));
55
- }
56
- continue;
57
- }
58
- if (field.type === "blocks") {
59
- continue;
60
- }
61
- if (field.type === "relationship" && "hasMany" in field && field.hasMany) {
62
- continue;
63
- }
64
- if (currentName) {
65
- result.push({ ...field, name: currentName });
66
- }
67
- if (field.type === "row" || field.type === "collapsible") {
68
- if (field.fields && Array.isArray(field.fields)) {
69
- result.push(...flattenFields(field.fields, prefix));
70
- }
71
- }
72
- if (field.type === "tabs" && field.tabs && Array.isArray(field.tabs)) {
73
- for (const tab of field.tabs) {
74
- if (tab.fields && Array.isArray(tab.fields)) {
75
- result.push(...flattenFields(tab.fields, prefix));
76
- }
77
- }
78
- }
79
- }
80
- return result;
81
- }
82
- function getRelationalFields(fields, prefix = "") {
83
- const result = [];
84
- for (const field of fields) {
85
- const currentName = field.name ? `${prefix}${field.name}` : undefined;
86
- if (field.type === "relationship" && "hasMany" in field && field.hasMany || field.type === "blocks") {
87
- if (currentName) {
88
- result.push({ ...field, name: currentName });
89
- }
90
- continue;
91
- }
92
- if (field.type === "group" || field.type === "row" || field.type === "collapsible") {
93
- if (field.fields && Array.isArray(field.fields)) {
94
- const nextPrefix = field.type === "group" && field.name ? `${currentName}__` : prefix;
95
- result.push(...getRelationalFields(field.fields, nextPrefix));
96
- }
97
- continue;
98
- }
99
- if (field.type === "tabs" && field.tabs && Array.isArray(field.tabs)) {
100
- for (const tab of field.tabs) {
101
- if (tab.fields && Array.isArray(tab.fields)) {
102
- result.push(...getRelationalFields(tab.fields, prefix));
103
- }
104
- }
105
- }
106
- }
107
- return result;
108
- }
109
-
110
- export { toSnakeCase, mapFieldToPostgresType, mapFieldToSQLiteType, flattenFields, getRelationalFields };
@@ -1,87 +0,0 @@
1
- // src/client.ts
2
- class OpacaError extends Error {
3
- status;
4
- data;
5
- constructor(message, status, data = {}) {
6
- super(message);
7
- this.name = "OpacaError";
8
- this.status = status;
9
- this.data = data;
10
- }
11
- }
12
- function createClient(configOrOptions) {
13
- const options = configOrOptions.serverURL ? { baseURL: configOrOptions.serverURL } : configOrOptions;
14
- const baseURL = options.baseURL.replace(/\/$/, "");
15
- const getHeaders = () => {
16
- const headers = {
17
- "Content-Type": "application/json"
18
- };
19
- if (options.token) {
20
- headers.Authorization = `Bearer ${options.token}`;
21
- }
22
- return headers;
23
- };
24
- const fetcher = async (path, init) => {
25
- const res = await fetch(`${baseURL}${path}`, {
26
- ...init,
27
- credentials: "include",
28
- headers: {
29
- ...getHeaders(),
30
- ...init?.headers
31
- }
32
- });
33
- if (!res.ok) {
34
- const errorData = await res.json().catch(() => ({}));
35
- throw new OpacaError(errorData.message || `API Error: ${res.status}`, res.status, errorData);
36
- }
37
- return res.json();
38
- };
39
- const client = {
40
- getMetadata: () => fetcher("/api/__admin/metadata"),
41
- getCollections: () => fetcher("/api/__admin/collections"),
42
- getSetupStatus: () => fetcher("/api/__admin/setup"),
43
- collections: new Proxy({}, {
44
- get(_, collectionSlug) {
45
- return {
46
- find: (query) => {
47
- const searchParams = new URLSearchParams;
48
- if (query) {
49
- Object.entries(query).forEach(([key, value]) => {
50
- searchParams.append(key, typeof value === "object" ? JSON.stringify(value) : String(value));
51
- });
52
- }
53
- const queryString = searchParams.toString();
54
- return fetcher(`/api/${String(collectionSlug)}${queryString ? `?${queryString}` : ""}`);
55
- },
56
- findOne: (id) => fetcher(`/api/${String(collectionSlug)}/${id}`),
57
- list: () => fetcher(`/api/${String(collectionSlug)}`),
58
- create: (data) => fetcher(`/api/${String(collectionSlug)}`, {
59
- method: "POST",
60
- body: JSON.stringify(data)
61
- }),
62
- update: (id, data) => fetcher(`/api/${String(collectionSlug)}/${id}`, {
63
- method: "PATCH",
64
- body: JSON.stringify(data)
65
- }),
66
- delete: (id) => fetcher(`/api/${String(collectionSlug)}/${id}`, {
67
- method: "DELETE"
68
- })
69
- };
70
- }
71
- }),
72
- globals: new Proxy({}, {
73
- get(_, globalSlug) {
74
- return {
75
- get: () => fetcher(`/api/globals/${String(globalSlug)}`),
76
- update: (data) => fetcher(`/api/globals/${String(globalSlug)}`, {
77
- method: "PATCH",
78
- body: JSON.stringify(data)
79
- })
80
- };
81
- }
82
- })
83
- };
84
- return client;
85
- }
86
-
87
- export { OpacaError, createClient };
@@ -1,96 +0,0 @@
1
- import {
2
- __require
3
- } from "./chunk-8sqjbsgt.js";
4
-
5
- // src/admin/router.ts
6
- import { createRouter } from "@nanostores/router";
7
- var $router = createRouter({
8
- dashboard: "/admin",
9
- collections: "/admin/collections/:slug",
10
- document: "/admin/collections/:slug/:id",
11
- globals: "/admin/globals/:slug",
12
- settings: "/admin/settings"
13
- });
14
-
15
- // src/admin/stores/config.ts
16
- import { atom } from "nanostores";
17
- var $config = atom(null);
18
- var $needsInit = atom(false);
19
- var $isFetchingConfig = atom(false);
20
- var $hasMetadataError = atom(false);
21
- function setConfig(config) {
22
- $config.set(config);
23
- }
24
- function setNeedsInit(needs) {
25
- $needsInit.set(needs);
26
- }
27
-
28
- // src/admin/stores/query.ts
29
- import { nanoquery } from "@nanostores/query";
30
-
31
- // src/admin/api-client.ts
32
- import ky from "ky";
33
- var client = null;
34
- var currentBaseURL = null;
35
- var configureAPI = (serverUrl) => {
36
- if (!serverUrl || !serverUrl.startsWith("http"))
37
- return;
38
- const url = serverUrl;
39
- const baseURL = url.replace(/\/$/, "");
40
- if (client && currentBaseURL === baseURL)
41
- return;
42
- currentBaseURL = baseURL;
43
- client = ky.create({
44
- prefixUrl: baseURL,
45
- credentials: "include",
46
- retry: 0,
47
- hooks: {
48
- afterResponse: [
49
- async (_request, _options, response) => {
50
- if (response.status === 401 || response.status === 403) {
51
- try {
52
- const { $session } = await import("./chunk-xa7rjsn2.js");
53
- const { notify } = await import("./chunk-jwjk85ze.js");
54
- $session.set(null);
55
- notify("Session expired. Please log in again.", "error");
56
- } catch (e) {}
57
- }
58
- }
59
- ]
60
- }
61
- });
62
- };
63
- var getCurrentBaseURL = () => {
64
- if (!currentBaseURL) {
65
- throw new Error("API client not configured. Did you provide a valid serverUrl?");
66
- }
67
- return currentBaseURL;
68
- };
69
- var api = new Proxy({}, {
70
- get(_, prop) {
71
- if (!client) {
72
- throw new Error("API client not configured. Did you provide a valid serverUrl?");
73
- }
74
- if (!client)
75
- throw new Error("API client not configured");
76
- const c = client;
77
- if (c) {
78
- return c[prop];
79
- }
80
- return;
81
- }
82
- });
83
-
84
- // src/admin/stores/query.ts
85
- var [
86
- createFetcherStore,
87
- createMutatorStore,
88
- { invalidateKeys, revalidateKeys, mutateCache }
89
- ] = nanoquery({
90
- fetcher: (...keys) => {
91
- const url = keys.join("");
92
- return api.get(url).json();
93
- }
94
- });
95
-
96
- export { $router, $config, $needsInit, $isFetchingConfig, $hasMetadataError, setConfig, setNeedsInit, configureAPI, getCurrentBaseURL, api, createFetcherStore, createMutatorStore, invalidateKeys, revalidateKeys, mutateCache };
@@ -1,20 +0,0 @@
1
- // src/admin/stores/ui.ts
2
- import { persistentAtom } from "@nanostores/persistent";
3
- import { atom } from "nanostores";
4
- var $toasts = atom([]);
5
- var $isSidebarCollapsed = persistentAtom("opaca-sidebar-collapsed", false, {
6
- encode: JSON.stringify,
7
- decode: JSON.parse
8
- });
9
- function toggleSidebar() {
10
- $isSidebarCollapsed.set(!$isSidebarCollapsed.get());
11
- }
12
- function notify(message, type = "success") {
13
- const id = Math.random().toString(36).substring(2, 9);
14
- $toasts.set([...$toasts.get(), { id, message, type }]);
15
- }
16
- function clearToast(id) {
17
- $toasts.set($toasts.get().filter((t) => t.id !== id));
18
- }
19
-
20
- export { $toasts, $isSidebarCollapsed, toggleSidebar, notify, clearToast };
@@ -1,15 +0,0 @@
1
- import {
2
- $isSidebarCollapsed,
3
- $toasts,
4
- clearToast,
5
- notify,
6
- toggleSidebar
7
- } from "./chunk-j4d50hrx.js";
8
- import"./chunk-8sqjbsgt.js";
9
- export {
10
- toggleSidebar,
11
- notify,
12
- clearToast,
13
- $toasts,
14
- $isSidebarCollapsed
15
- };
@@ -1,250 +0,0 @@
1
- import {
2
- flattenFields,
3
- mapFieldToPostgresType,
4
- mapFieldToSQLiteType,
5
- toSnakeCase
6
- } from "./chunk-cvdd4eqh.js";
7
- import"./chunk-8sqjbsgt.js";
8
-
9
- // src/db/kysely/migration-generator.ts
10
- function generateMigrationCode(collections, globals = [], dialect) {
11
- const allSchemas = [...collections, ...globals];
12
- const mapType = dialect === "postgres" ? mapFieldToPostgresType : mapFieldToSQLiteType;
13
- const tsType = dialect === "postgres" ? "timestamp with time zone" : "text";
14
- let upCode = "";
15
- let downCode = "";
16
- for (const collection of allSchemas) {
17
- const slug = collection.slug;
18
- const flattenedFields = flattenFields(collection.fields);
19
- upCode += ` await db.schema.createTable('${slug}')
20
- `;
21
- upCode += ` .addColumn('id', 'text', (col) => col.primaryKey())
22
- `;
23
- for (const field of flattenedFields) {
24
- if (field.type === "relationship" && "hasMany" in field && field.hasMany)
25
- continue;
26
- const colName = toSnakeCase(field.name);
27
- if (colName === "id")
28
- continue;
29
- const colType = mapType(field);
30
- const constraints = [];
31
- if (field.required)
32
- constraints.push("notNull()");
33
- if (field.unique)
34
- constraints.push("unique()");
35
- if (field.defaultValue !== undefined) {
36
- let val = field.defaultValue;
37
- if (typeof val === "string") {
38
- val = `'${val}'`;
39
- } else if (typeof val === "boolean") {
40
- val = dialect === "sqlite" || dialect === "d1" ? val ? 1 : 0 : val;
41
- }
42
- constraints.push(`defaultTo(${val})`);
43
- }
44
- const constraintChain = constraints.length > 0 ? `.${constraints.join(".")}` : "";
45
- upCode += ` .addColumn('${colName}', '${colType}', (col) => col${constraintChain})
46
- `;
47
- }
48
- const ts = collection.timestamps !== false;
49
- if (ts) {
50
- const config = typeof collection.timestamps === "object" ? collection.timestamps : {};
51
- const createdField = toSnakeCase(config.createdAt || "createdAt");
52
- const updatedField = toSnakeCase(config.updatedAt || "updatedAt");
53
- upCode += ` .addColumn('${createdField}', '${tsType}', (col) => col.defaultTo('CURRENT_TIMESTAMP'))
54
- `;
55
- upCode += ` .addColumn('${updatedField}', '${tsType}', (col) => col.defaultTo('CURRENT_TIMESTAMP'))
56
- `;
57
- }
58
- upCode += ` .execute();
59
-
60
- `;
61
- downCode = ` await db.schema.dropTable('${slug}').execute();
62
- ` + downCode;
63
- for (const field of flattenedFields) {
64
- if (field.type === "relationship" && "hasMany" in field && field.hasMany) {
65
- const colName = toSnakeCase(field.name);
66
- const joinTableName = `${slug}_${colName}_relations`.toLowerCase();
67
- upCode += ` await db.schema.createTable('${joinTableName}')
68
- `;
69
- upCode += ` .addColumn('id', 'text', (col) => col.primaryKey())
70
- `;
71
- upCode += ` .addColumn('source_id', 'text', (col) => col.notNull())
72
- `;
73
- upCode += ` .addColumn('target_id', 'text', (col) => col.notNull())
74
- `;
75
- upCode += ` .addColumn('order', 'integer', (col) => col.defaultTo(0))
76
- `;
77
- upCode += ` .execute();
78
-
79
- `;
80
- downCode = ` await db.schema.dropTable('${joinTableName}').execute();
81
- ` + downCode;
82
- }
83
- }
84
- for (const field of collection.fields) {
85
- if (field.type === "blocks" && field.blocks && Array.isArray(field.blocks)) {
86
- for (const block of field.blocks) {
87
- const blockName = toSnakeCase(field.name);
88
- const blockTableName = `${slug}_${blockName}_${block.slug}`.toLowerCase();
89
- upCode += ` await db.schema.createTable('${blockTableName}')
90
- `;
91
- upCode += ` .addColumn('id', 'text', (col) => col.primaryKey())
92
- `;
93
- upCode += ` .addColumn('_parent_id', 'text', (col) => col.notNull())
94
- `;
95
- upCode += ` .addColumn('_order', 'integer', (col) => col.defaultTo(0))
96
- `;
97
- upCode += ` .addColumn('block_type', 'text', (col) => col.notNull())
98
- `;
99
- const blockFlattened = flattenFields(block.fields);
100
- for (const bField of blockFlattened) {
101
- const bColName = toSnakeCase(bField.name);
102
- if (bColName === "id")
103
- continue;
104
- const bColType = mapType(bField);
105
- const bConstraints = [];
106
- if (bField.required)
107
- bConstraints.push("notNull()");
108
- if (bField.unique)
109
- bConstraints.push("unique()");
110
- if (bField.defaultValue !== undefined) {
111
- let bVal = bField.defaultValue;
112
- if (typeof bVal === "string")
113
- bVal = `'${bVal}'`;
114
- else if (typeof bVal === "boolean")
115
- bVal = dialect === "sqlite" || dialect === "d1" ? bVal ? 1 : 0 : bVal;
116
- bConstraints.push(`defaultTo(${bVal})`);
117
- }
118
- const bConstraintChain = bConstraints.length > 0 ? `.${bConstraints.join(".")}` : "";
119
- upCode += ` .addColumn('${bColName}', '${bColType}', (col) => col${bConstraintChain})
120
- `;
121
- }
122
- upCode += ` .addColumn('created_at', '${tsType}', (col) => col.defaultTo('CURRENT_TIMESTAMP'))
123
- `;
124
- upCode += ` .addColumn('updated_at', '${tsType}', (col) => col.defaultTo('CURRENT_TIMESTAMP'))
125
- `;
126
- upCode += ` .execute();
127
-
128
- `;
129
- downCode = ` await db.schema.dropTable('${blockTableName}').execute();
130
- ` + downCode;
131
- }
132
- }
133
- }
134
- }
135
- return `import type { OpacaMigrationDb } from 'opacacms/db';
136
-
137
- export async function up(db: OpacaMigrationDb): Promise<void> {
138
- ${upCode}}
139
-
140
- export async function down(db: OpacaMigrationDb): Promise<void> {
141
- ${downCode}}
142
- `;
143
- }
144
- function generateSQLCode(collections, globals = []) {
145
- const allSchemas = [...collections, ...globals];
146
- let sql = `-- OpacaCMS Auto-generated SQL Migration
147
-
148
- `;
149
- for (const collection of allSchemas) {
150
- const slug = collection.slug;
151
- const flattenedFields = flattenFields(collection.fields);
152
- sql += `CREATE TABLE IF NOT EXISTS "${slug}" (
153
- `;
154
- sql += ` "id" TEXT PRIMARY KEY`;
155
- for (const field of flattenedFields) {
156
- if (field.type === "relationship" && "hasMany" in field && field.hasMany)
157
- continue;
158
- const colName = toSnakeCase(field.name);
159
- if (colName === "id")
160
- continue;
161
- const colType = mapFieldToSQLiteType(field);
162
- sql += `,
163
- "${colName}" ${colType.toUpperCase()}`;
164
- if (field.required)
165
- sql += " NOT NULL";
166
- if (field.unique)
167
- sql += " UNIQUE";
168
- if (field.defaultValue !== undefined) {
169
- let val = field.defaultValue;
170
- if (typeof val === "string")
171
- val = `'${val}'`;
172
- else if (typeof val === "boolean")
173
- val = val ? 1 : 0;
174
- sql += ` DEFAULT ${val}`;
175
- }
176
- }
177
- const ts = collection.timestamps !== false;
178
- if (ts) {
179
- const config = typeof collection.timestamps === "object" ? collection.timestamps : {};
180
- const createdField = toSnakeCase(config.createdAt || "createdAt");
181
- const updatedField = toSnakeCase(config.updatedAt || "updatedAt");
182
- sql += `,
183
- "${createdField}" TEXT DEFAULT CURRENT_TIMESTAMP`;
184
- sql += `,
185
- "${updatedField}" TEXT DEFAULT CURRENT_TIMESTAMP`;
186
- }
187
- sql += `
188
- );
189
-
190
- `;
191
- for (const field of flattenedFields) {
192
- if (field.type === "relationship" && "hasMany" in field && field.hasMany) {
193
- const colName = toSnakeCase(field.name);
194
- const joinTableName = `${slug}_${colName}_relations`.toLowerCase();
195
- sql += `CREATE TABLE IF NOT EXISTS "${joinTableName}" (
196
- `;
197
- sql += ` "id" TEXT PRIMARY KEY,
198
- `;
199
- sql += ` "source_id" TEXT NOT NULL,
200
- `;
201
- sql += ` "target_id" TEXT NOT NULL,
202
- `;
203
- sql += ` "order" INTEGER DEFAULT 0
204
- `;
205
- sql += `);
206
-
207
- `;
208
- }
209
- }
210
- for (const field of collection.fields) {
211
- if (field.type === "blocks" && field.blocks && Array.isArray(field.blocks)) {
212
- for (const block of field.blocks) {
213
- const blockName = toSnakeCase(field.name);
214
- const blockTableName = `${slug}_${blockName}_${block.slug}`.toLowerCase();
215
- sql += `CREATE TABLE IF NOT EXISTS "${blockTableName}" (
216
- `;
217
- sql += ` "id" TEXT PRIMARY KEY,
218
- `;
219
- sql += ` "_parent_id" TEXT NOT NULL,
220
- `;
221
- sql += ` "_order" INTEGER DEFAULT 0,
222
- `;
223
- sql += ` "block_type" TEXT NOT NULL`;
224
- const blockFlattened = flattenFields(block.fields);
225
- for (const bField of blockFlattened) {
226
- const bColName = toSnakeCase(bField.name);
227
- if (bColName === "id")
228
- continue;
229
- const bColType = mapFieldToSQLiteType(bField);
230
- sql += `,
231
- "${bColName}" ${bColType.toUpperCase()}`;
232
- }
233
- sql += `,
234
- "created_at" TEXT DEFAULT CURRENT_TIMESTAMP,
235
- `;
236
- sql += ` "updated_at" TEXT DEFAULT CURRENT_TIMESTAMP
237
- `;
238
- sql += `);
239
-
240
- `;
241
- }
242
- }
243
- }
244
- }
245
- return sql;
246
- }
247
- export {
248
- generateSQLCode,
249
- generateMigrationCode
250
- };
@@ -1,14 +0,0 @@
1
- // src/db/adapter.ts
2
- class BaseDatabaseAdapter {
3
- get raw() {
4
- return;
5
- }
6
- get db() {
7
- return;
8
- }
9
- push;
10
- pushDestructive;
11
- migrationDir;
12
- }
13
-
14
- export { BaseDatabaseAdapter };