opacacms 0.3.16 → 0.3.18
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/dist/admin/{stores → features/collections}/admin-queries.d.ts +1 -1
- package/dist/admin/{ui/views → features/collections}/collection-list-view.d.ts +1 -1
- package/dist/admin/{ui/views → features/collections}/document-edit-view.d.ts +2 -1
- package/dist/admin/{ui/views → features/collections}/global-edit-view.d.ts +1 -1
- package/dist/admin/{ui/components → features/collections}/versions-sheet.d.ts +3 -3
- package/dist/admin/{ui/views → features/dashboard}/dashboard-view.d.ts +1 -1
- package/dist/admin/{ui/components/media/AssetManagerModal.d.ts → features/media/components/asset-manager-modal.d.ts} +1 -2
- package/dist/admin/features/media/media-registry-view.d.ts +7 -0
- package/dist/admin/{ui/components/PluginSettingsForm.d.ts → features/settings/plugin-settings-form.d.ts} +3 -3
- package/dist/admin/{ui/views → features/settings}/settings-view.d.ts +2 -2
- package/dist/admin/{stores → features/ui-shell}/config.d.ts +1 -1
- package/dist/admin/{stores → features/ui-shell}/ui.d.ts +1 -1
- package/dist/admin/index.d.ts +8 -8
- package/dist/admin/index.js +4 -4
- package/dist/admin/ui/{components/ui → core}/blocks.d.ts +1 -1
- package/dist/admin/ui/core/breadcrumbs.d.ts +21 -0
- package/dist/admin/ui/{components/ui → core}/button.d.ts +4 -4
- package/dist/admin/ui/{components/ui → core}/collapsible.d.ts +2 -2
- package/dist/admin/ui/core/context-menu.d.ts +29 -0
- package/dist/admin/ui/core/dialog.d.ts +20 -0
- package/dist/admin/ui/{components/ui → core}/group.d.ts +2 -2
- package/dist/admin/ui/core/index.d.ts +17 -0
- package/dist/admin/ui/{components/ui → core}/input.d.ts +2 -2
- package/dist/admin/ui/{components/ui → core}/join.d.ts +1 -1
- package/dist/admin/ui/core/label.d.ts +4 -0
- package/dist/admin/ui/core/popover.d.ts +10 -0
- package/dist/admin/ui/{components/ui → core}/radio-group.d.ts +2 -2
- package/dist/admin/ui/{components/ui → core}/relationship.d.ts +1 -1
- package/dist/admin/ui/{components/ui → core}/scroll-area.d.ts +1 -1
- package/dist/admin/ui/core/select.d.ts +15 -0
- package/dist/admin/ui/{components/ui → core}/sheet.d.ts +6 -6
- package/dist/admin/ui/core/sidebar.d.ts +69 -0
- package/dist/admin/ui/core/skeleton.d.ts +2 -0
- package/dist/admin/ui/core/switch.d.ts +6 -0
- package/dist/admin/ui/core/table.d.ts +10 -0
- package/dist/admin/ui/core/tabs.d.ts +11 -0
- package/dist/admin/ui/hooks/use-mobile.d.ts +1 -0
- package/dist/admin/ui/{admin-client.d.ts → layout/admin-client.d.ts} +1 -1
- package/dist/admin/ui/{admin-layout.d.ts → layout/admin-layout.d.ts} +2 -2
- package/dist/admin/ui/{components → shared}/column-visibility-toggle.d.ts +1 -1
- package/dist/admin/ui/{components → shared}/detail-sheet.d.ts +2 -2
- package/dist/admin/ui/{components/fields/ArrayField.d.ts → shared/fields/array-field.d.ts} +1 -1
- package/dist/admin/ui/shared/fields/blocks-field.d.ts +25 -0
- package/dist/admin/ui/{components/fields/BooleanField.d.ts → shared/fields/boolean-field.d.ts} +1 -1
- package/dist/admin/ui/{components/fields/CollapsibleField.d.ts → shared/fields/collapsible-field.d.ts} +1 -1
- package/dist/admin/ui/{components/fields/FileField.d.ts → shared/fields/file-field.d.ts} +13 -3
- package/dist/admin/ui/{components/fields/GroupField.d.ts → shared/fields/group-field.d.ts} +1 -1
- package/dist/admin/ui/shared/fields/index.d.ts +28 -0
- package/dist/admin/ui/{components/fields/JoinField.d.ts → shared/fields/join-field.d.ts} +1 -1
- package/dist/admin/ui/{components/fields/RelationshipField.d.ts → shared/fields/relationship-field.d.ts} +1 -1
- package/dist/admin/ui/{components → shared}/fields/richtext-editor/index.d.ts +2 -2
- package/dist/admin/ui/{components/fields/richtext-editor/nodes/ImageNode.d.ts → shared/fields/richtext-editor/nodes/image-node.d.ts} +3 -3
- package/dist/admin/ui/{components/fields/TabsField.d.ts → shared/fields/tabs-field.d.ts} +1 -1
- package/dist/admin/ui/{components/fields/VirtualField.d.ts → shared/fields/virtual-field.d.ts} +1 -1
- package/dist/admin/ui/{components → shared}/toast.d.ts +1 -1
- package/dist/admin/webcomponent.js +34 -34
- package/dist/admin.css +1 -1
- package/dist/{chunk-5y0mkt7x.js → chunk-2abqb0h6.js} +2 -2
- package/dist/{chunk-m83ybzf8.js → chunk-hrxq1x98.js} +1 -1
- package/dist/{chunk-nch158fe.js → chunk-y3ehzvp4.js} +1 -1
- package/dist/{chunk-vmz9ncf1.js → chunk-yw9w63kr.js} +3 -3
- package/dist/cli/commands/init.d.ts +1 -0
- package/dist/cli/core/seeding/auto-seed.d.ts +1 -1
- package/dist/cli/index.js +28 -3
- package/dist/config.d.ts +2 -6
- package/dist/db/adapter.d.ts +1 -1
- package/dist/db/better-sqlite.d.ts +1 -1
- package/dist/db/better-sqlite.js +587 -9
- package/dist/db/bun-sqlite.js +590 -9
- package/dist/db/d1.d.ts +1 -1
- package/dist/db/d1.js +605 -9
- package/dist/db/index.d.ts +0 -5
- package/dist/db/index.js +0 -35
- package/dist/db/kysely/plugins/draft-swapper.d.ts +2 -2
- package/dist/db/kysely/plugins/i18n-fallback.d.ts +2 -2
- package/dist/db/kysely/plugins/json-flattener.d.ts +2 -2
- package/dist/db/kysely/plugins/virtual-field-resolver.d.ts +2 -2
- package/dist/db/kysely/schema-builder.d.ts +1 -1
- package/dist/db/kysely/snapshot/snapshot-manager.d.ts +1 -1
- package/dist/db/postgres.js +613 -9
- package/dist/db/sqlite.d.ts +1 -1
- package/dist/db/sqlite.js +602 -9
- package/dist/runtimes/bun.js +1 -1
- package/dist/runtimes/cloudflare-workers.js +1 -1
- package/dist/runtimes/next.js +1 -1
- package/dist/runtimes/node.js +1 -1
- package/dist/server.js +4 -4
- package/dist/types.d.ts +3 -3
- package/package.json +25 -7
- package/dist/admin/ui/components/Table.d.ts +0 -10
- package/dist/admin/ui/components/fields/BlocksField.d.ts +0 -17
- package/dist/admin/ui/components/fields/index.d.ts +0 -28
- package/dist/admin/ui/components/ui/breadcrumbs.d.ts +0 -7
- package/dist/admin/ui/components/ui/dialog.d.ts +0 -27
- package/dist/admin/ui/components/ui/index.d.ts +0 -17
- package/dist/admin/ui/components/ui/label.d.ts +0 -3
- package/dist/admin/ui/components/ui/select.d.ts +0 -37
- package/dist/admin/ui/components/ui/tabs.d.ts +0 -17
- package/dist/admin/ui/views/media-registry-view.d.ts +0 -7
- package/dist/chunk-5b9eqr34.js +0 -608
- package/dist/chunk-dz5bh1bd.js +0 -586
- package/dist/chunk-nz6xhtja.js +0 -601
- package/dist/chunk-qsefknd3.js +0 -593
- package/dist/chunk-tsmhn78f.js +0 -616
- /package/dist/admin/{stores → features/auth}/auth.d.ts +0 -0
- /package/dist/admin/{ui/views → features/auth}/login-view.d.ts +0 -0
- /package/dist/admin/{ui/components → features/collections}/data-detail-view.d.ts +0 -0
- /package/dist/admin/{ui/views → features/init}/init-view.d.ts +0 -0
- /package/dist/admin/{stores → features/media}/media.d.ts +0 -0
- /package/dist/admin/{stores → features/ui-shell}/column-visibility.d.ts +0 -0
- /package/dist/admin/{stores → features/ui-shell}/query.d.ts +0 -0
- /package/dist/admin/ui/{components/ui → core}/accordion.d.ts +0 -0
- /package/dist/admin/ui/{components/ui → core}/alert-dialog.d.ts +0 -0
- /package/dist/admin/ui/{components/ui → core}/separator.d.ts +0 -0
- /package/dist/admin/ui/{components/ui → core}/tooltip.d.ts +0 -0
- /package/dist/admin/ui/{components/ui → core}/utils.d.ts +0 -0
- /package/dist/admin/ui/{components → shared}/custom-alert.d.ts +0 -0
- /package/dist/admin/ui/{components/fields/DateField.d.ts → shared/fields/date-field.d.ts} +0 -0
- /package/dist/admin/ui/{components/fields/FieldLabel.d.ts → shared/fields/field-label.d.ts} +0 -0
- /package/dist/admin/ui/{components/fields/NumberField.d.ts → shared/fields/number-field.d.ts} +0 -0
- /package/dist/admin/ui/{components/fields/RadioField.d.ts → shared/fields/radio-field.d.ts} +0 -0
- /package/dist/admin/ui/{components/fields/richtext-editor/nodes/ImageComponent.d.ts → shared/fields/richtext-editor/nodes/image-component.d.ts} +0 -0
- /package/dist/admin/ui/{components/fields/richtext-editor/plugins/ComponentPickerPlugin.d.ts → shared/fields/richtext-editor/plugins/component-picker-plugin.d.ts} +0 -0
- /package/dist/admin/ui/{components/fields/richtext-editor/plugins/EditableSyncPlugin.d.ts → shared/fields/richtext-editor/plugins/editable-sync-plugin.d.ts} +0 -0
- /package/dist/admin/ui/{components/fields/richtext-editor/plugins/NotionToolbarPlugin.d.ts → shared/fields/richtext-editor/plugins/notion-toolbar-plugin.d.ts} +0 -0
- /package/dist/admin/ui/{components/fields/richtext-editor/plugins/SimpleToolbarPlugin.d.ts → shared/fields/richtext-editor/plugins/simple-toolbar-plugin.d.ts} +0 -0
- /package/dist/admin/ui/{components/fields/richtext-editor/plugins/ValueSyncPlugin.d.ts → shared/fields/richtext-editor/plugins/value-sync-plugin.d.ts} +0 -0
- /package/dist/admin/ui/{components/fields/RowField.d.ts → shared/fields/row-field.d.ts} +0 -0
- /package/dist/admin/ui/{components/fields/SelectField.d.ts → shared/fields/select-field.d.ts} +0 -0
- /package/dist/admin/ui/{components/fields/TextAreaField.d.ts → shared/fields/text-area-field.d.ts} +0 -0
- /package/dist/admin/ui/{components/fields/TextField.d.ts → shared/fields/text-field.d.ts} +0 -0
- /package/dist/admin/ui/{components → shared}/fields/utils.d.ts +0 -0
- /package/dist/admin/ui/{components → shared}/link.d.ts +0 -0
- /package/dist/admin/ui/{components → shared}/plugin-iframe.d.ts +0 -0
package/dist/chunk-qsefknd3.js
DELETED
|
@@ -1,593 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
buildKyselyWhere,
|
|
3
|
-
flattenPayload,
|
|
4
|
-
pushSchema,
|
|
5
|
-
unflattenRow
|
|
6
|
-
} from "./chunk-re459gm9.js";
|
|
7
|
-
import {
|
|
8
|
-
BaseDatabaseAdapter
|
|
9
|
-
} from "./chunk-s8mqwnm1.js";
|
|
10
|
-
import {
|
|
11
|
-
requestContext
|
|
12
|
-
} from "./chunk-q5sb5dcr.js";
|
|
13
|
-
import {
|
|
14
|
-
flattenFields,
|
|
15
|
-
getRelationalFields,
|
|
16
|
-
toSnakeCase
|
|
17
|
-
} from "./chunk-5xpf5jxd.js";
|
|
18
|
-
import {
|
|
19
|
-
logger
|
|
20
|
-
} from "./chunk-jq1drsen.js";
|
|
21
|
-
import {
|
|
22
|
-
__require
|
|
23
|
-
} from "./chunk-8sqjbsgt.js";
|
|
24
|
-
|
|
25
|
-
// src/db/bun-sqlite.ts
|
|
26
|
-
import fs from "node:fs/promises";
|
|
27
|
-
import path from "node:path";
|
|
28
|
-
import { pathToFileURL } from "node:url";
|
|
29
|
-
import { FileMigrationProvider, Migrator, sql } from "kysely";
|
|
30
|
-
class BunSQLiteAdapter extends BaseDatabaseAdapter {
|
|
31
|
-
path;
|
|
32
|
-
name = "bun-sqlite";
|
|
33
|
-
_rawDb;
|
|
34
|
-
_db = null;
|
|
35
|
-
_collections = [];
|
|
36
|
-
_globals = [];
|
|
37
|
-
push;
|
|
38
|
-
migrationDir;
|
|
39
|
-
pushDestructive;
|
|
40
|
-
get raw() {
|
|
41
|
-
return this._rawDb;
|
|
42
|
-
}
|
|
43
|
-
get db() {
|
|
44
|
-
if (!this._db)
|
|
45
|
-
throw new Error("Database not connected. Call connect() first.");
|
|
46
|
-
return this._db;
|
|
47
|
-
}
|
|
48
|
-
constructor(path2, options) {
|
|
49
|
-
super();
|
|
50
|
-
this.path = path2;
|
|
51
|
-
this.push = options?.push ?? true;
|
|
52
|
-
this.pushDestructive = options?.pushDestructive ?? false;
|
|
53
|
-
this.migrationDir = options?.migrationDir ?? "./migrations";
|
|
54
|
-
}
|
|
55
|
-
async connect() {
|
|
56
|
-
if (this._db)
|
|
57
|
-
return;
|
|
58
|
-
const { Database } = await import("bun:sqlite");
|
|
59
|
-
const { BunSqliteDialect } = await import("kysely-bun-sqlite");
|
|
60
|
-
this._rawDb = new Database(this.path);
|
|
61
|
-
const { createOpacaKysely } = await import("./chunk-gzbz5jwy.js");
|
|
62
|
-
this._db = createOpacaKysely({
|
|
63
|
-
dialect: new BunSqliteDialect({
|
|
64
|
-
database: this._rawDb
|
|
65
|
-
}),
|
|
66
|
-
config: {
|
|
67
|
-
collections: this._collections,
|
|
68
|
-
globals: this._globals,
|
|
69
|
-
db: { name: "bun-sqlite" }
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
async disconnect() {
|
|
74
|
-
if (this._db)
|
|
75
|
-
await this.db.destroy();
|
|
76
|
-
}
|
|
77
|
-
async unsafe(query, params) {
|
|
78
|
-
const compiled = sql.raw(query);
|
|
79
|
-
const result = await compiled.execute(this.db);
|
|
80
|
-
return result.rows;
|
|
81
|
-
}
|
|
82
|
-
async count(collection, query) {
|
|
83
|
-
const tableName = toSnakeCase(collection);
|
|
84
|
-
let qb = this.db.selectFrom(tableName).select((eb) => eb.fn.count("id").as("count"));
|
|
85
|
-
if (query && Object.keys(query).length > 0) {
|
|
86
|
-
qb = qb.where((eb) => buildKyselyWhere(eb, query) || eb.val(true));
|
|
87
|
-
}
|
|
88
|
-
const result = await qb.executeTakeFirst();
|
|
89
|
-
return Number(result?.count || 0);
|
|
90
|
-
}
|
|
91
|
-
async create(collection, data) {
|
|
92
|
-
const tableName = toSnakeCase(collection);
|
|
93
|
-
const colDef = this._collections.find((c) => c.slug === collection);
|
|
94
|
-
const flatData = flattenPayload(data);
|
|
95
|
-
const relationalFields = getRelationalFields(colDef?.fields || []);
|
|
96
|
-
const hasManyData = {};
|
|
97
|
-
const blocksData = {};
|
|
98
|
-
for (const field of relationalFields) {
|
|
99
|
-
const key = field.name;
|
|
100
|
-
if (Array.isArray(flatData[key])) {
|
|
101
|
-
if (field.type === "relationship" && "hasMany" in field && field.hasMany) {
|
|
102
|
-
hasManyData[key] = flatData[key];
|
|
103
|
-
} else if (field.type === "blocks") {
|
|
104
|
-
blocksData[key] = flatData[key];
|
|
105
|
-
}
|
|
106
|
-
delete flatData[key];
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
const dbFields = flattenFields(colDef?.fields || []);
|
|
110
|
-
const validCols = new Set(dbFields.map((f) => toSnakeCase(f.name)));
|
|
111
|
-
validCols.add("id");
|
|
112
|
-
validCols.add(toSnakeCase("createdAt"));
|
|
113
|
-
validCols.add(toSnakeCase("updatedAt"));
|
|
114
|
-
const filteredData = {};
|
|
115
|
-
for (const col of Object.keys(flatData)) {
|
|
116
|
-
if (validCols.has(col)) {
|
|
117
|
-
filteredData[col] = flatData[col];
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
if (!filteredData.id) {
|
|
121
|
-
filteredData.id = crypto.randomUUID();
|
|
122
|
-
flatData.id = filteredData.id;
|
|
123
|
-
}
|
|
124
|
-
await this.db.insertInto(tableName).values(filteredData).execute();
|
|
125
|
-
for (const [key, values] of Object.entries(hasManyData)) {
|
|
126
|
-
const joinTableName = `${tableName}_${toSnakeCase(key)}_relations`.toLowerCase();
|
|
127
|
-
if (values.length > 0) {
|
|
128
|
-
const joinData = values.map((val, idx) => {
|
|
129
|
-
const tId = typeof val === "object" ? val.id : val;
|
|
130
|
-
return { id: crypto.randomUUID(), source_id: flatData.id, target_id: tId, order: idx };
|
|
131
|
-
});
|
|
132
|
-
await this.db.insertInto(joinTableName).values(joinData).execute();
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
for (const [key, blocks] of Object.entries(blocksData)) {
|
|
136
|
-
for (let i = 0;i < blocks.length; i++) {
|
|
137
|
-
const block = blocks[i];
|
|
138
|
-
if (!block.blockType)
|
|
139
|
-
continue;
|
|
140
|
-
const blockTableName = `${tableName}_${toSnakeCase(key)}_${toSnakeCase(block.blockType)}`.toLowerCase();
|
|
141
|
-
const bId = block.id || crypto.randomUUID();
|
|
142
|
-
const blockDef = relationalFields.find((f) => f.name === key && f.type === "blocks");
|
|
143
|
-
const blockConfig = blockDef?.blocks?.find((b) => b.slug === block.blockType);
|
|
144
|
-
const blockJsonFields = blockConfig?.fields.filter((f) => ["richtext", "json", "file"].includes(f.type)).map((f) => f.name) || [];
|
|
145
|
-
const blockFlatData = flattenPayload({ ...block, id: bId }, "", blockJsonFields);
|
|
146
|
-
for (const f of blockJsonFields) {
|
|
147
|
-
if (blockFlatData[f] && typeof blockFlatData[f] === "object") {
|
|
148
|
-
blockFlatData[f] = JSON.stringify(blockFlatData[f]);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
delete blockFlatData.blockType;
|
|
152
|
-
const blockData = {
|
|
153
|
-
...blockFlatData,
|
|
154
|
-
_parent_id: flatData.id,
|
|
155
|
-
_order: i,
|
|
156
|
-
block_type: block.blockType
|
|
157
|
-
};
|
|
158
|
-
await this.db.insertInto(blockTableName).values(blockData).execute();
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return this.findOne(collection, { id: flatData.id }, this.db);
|
|
162
|
-
}
|
|
163
|
-
async findOne(collection, query, tx) {
|
|
164
|
-
const tableName = toSnakeCase(collection);
|
|
165
|
-
const executor = tx || this.db;
|
|
166
|
-
let qb = executor.selectFrom(tableName).selectAll();
|
|
167
|
-
if (query && Object.keys(query).length > 0) {
|
|
168
|
-
qb = qb.where((eb) => buildKyselyWhere(eb, query) || eb.val(true));
|
|
169
|
-
}
|
|
170
|
-
const row = await qb.executeTakeFirst();
|
|
171
|
-
if (!row)
|
|
172
|
-
return null;
|
|
173
|
-
const unflattened = unflattenRow(row);
|
|
174
|
-
const colDef = this._collections.find((c) => c.slug === collection);
|
|
175
|
-
if (colDef) {
|
|
176
|
-
const relationalFields = getRelationalFields(colDef.fields);
|
|
177
|
-
for (const field of relationalFields) {
|
|
178
|
-
if (!field.name)
|
|
179
|
-
continue;
|
|
180
|
-
const snakeName = toSnakeCase(field.name);
|
|
181
|
-
if (field.type === "relationship" && "hasMany" in field && field.hasMany) {
|
|
182
|
-
const joinTableName = `${toSnakeCase(collection)}_${snakeName}_relations`.toLowerCase();
|
|
183
|
-
try {
|
|
184
|
-
const relations = await executor.selectFrom(joinTableName).selectAll().where("source_id", "=", row.id).orderBy("order", "asc").execute();
|
|
185
|
-
const parts = field.name.split("__");
|
|
186
|
-
let current = unflattened;
|
|
187
|
-
for (let i = 0;i < parts.length - 1; i++) {
|
|
188
|
-
if (!current[parts[i]])
|
|
189
|
-
current[parts[i]] = {};
|
|
190
|
-
current = current[parts[i]];
|
|
191
|
-
}
|
|
192
|
-
current[parts[parts.length - 1]] = relations.map((r) => r.target_id);
|
|
193
|
-
} catch (e) {}
|
|
194
|
-
} else if (field.type === "blocks" && field.blocks) {
|
|
195
|
-
const blockData = [];
|
|
196
|
-
for (const b of field.blocks) {
|
|
197
|
-
const blockTableName = `${collection}_${snakeName}_${toSnakeCase(b.slug)}`.toLowerCase();
|
|
198
|
-
try {
|
|
199
|
-
const blocks = await executor.selectFrom(blockTableName).selectAll().where("_parent_id", "=", row.id).execute();
|
|
200
|
-
for (const blk of blocks) {
|
|
201
|
-
const uf = unflattenRow(blk);
|
|
202
|
-
uf.blockType = blk.block_type;
|
|
203
|
-
blockData.push(uf);
|
|
204
|
-
}
|
|
205
|
-
} catch (e) {}
|
|
206
|
-
}
|
|
207
|
-
blockData.sort((a, b) => a._order - b._order);
|
|
208
|
-
blockData.forEach((b) => {
|
|
209
|
-
delete b._order;
|
|
210
|
-
delete b._parentId;
|
|
211
|
-
});
|
|
212
|
-
const parts = field.name.split("__");
|
|
213
|
-
let current = unflattened;
|
|
214
|
-
for (let i = 0;i < parts.length - 1; i++) {
|
|
215
|
-
if (!current[parts[i]])
|
|
216
|
-
current[parts[i]] = {};
|
|
217
|
-
current = current[parts[i]];
|
|
218
|
-
}
|
|
219
|
-
current[parts[parts.length - 1]] = blockData;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
return unflattened;
|
|
224
|
-
}
|
|
225
|
-
async find(collection, query, options) {
|
|
226
|
-
const page = options?.page || 1;
|
|
227
|
-
const limit = options?.limit || 10;
|
|
228
|
-
const offset = (page - 1) * limit;
|
|
229
|
-
const cursorColumn = options?.cursorColumn || "id";
|
|
230
|
-
const total = await this.count(collection, query);
|
|
231
|
-
const tableName = toSnakeCase(collection);
|
|
232
|
-
let qb = this.db.selectFrom(tableName).selectAll().limit(limit);
|
|
233
|
-
if (options?.after) {
|
|
234
|
-
qb = qb.where(cursorColumn, ">", options.after);
|
|
235
|
-
} else if (options?.before) {
|
|
236
|
-
qb = qb.where(cursorColumn, "<", options.before);
|
|
237
|
-
} else {
|
|
238
|
-
qb = qb.offset(offset);
|
|
239
|
-
}
|
|
240
|
-
if (query && Object.keys(query).length > 0) {
|
|
241
|
-
qb = qb.where((eb) => buildKyselyWhere(eb, query) || eb.val(true));
|
|
242
|
-
}
|
|
243
|
-
if (options?.sort) {
|
|
244
|
-
const [col, dir] = (options.sort ?? "").split(":");
|
|
245
|
-
if (col) {
|
|
246
|
-
qb = qb.orderBy(col, dir === "desc" ? "desc" : "asc");
|
|
247
|
-
}
|
|
248
|
-
} else {
|
|
249
|
-
qb = qb.orderBy(cursorColumn, "desc");
|
|
250
|
-
}
|
|
251
|
-
const rows = await qb.execute();
|
|
252
|
-
if (rows.length === 0) {
|
|
253
|
-
const totalPages2 = Math.ceil(total / limit);
|
|
254
|
-
const afterAnchor = options?.after;
|
|
255
|
-
const beforeAnchor = options?.before;
|
|
256
|
-
return {
|
|
257
|
-
docs: [],
|
|
258
|
-
totalDocs: total,
|
|
259
|
-
limit,
|
|
260
|
-
totalPages: totalPages2,
|
|
261
|
-
page,
|
|
262
|
-
pagingCounter: offset + 1,
|
|
263
|
-
hasNextPage: false,
|
|
264
|
-
hasPrevPage: !!afterAnchor || page > 1,
|
|
265
|
-
prevPage: null,
|
|
266
|
-
nextPage: null,
|
|
267
|
-
nextCursor: null,
|
|
268
|
-
prevCursor: afterAnchor ? afterAnchor : null
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
const rowIds = rows.map((r) => r.id);
|
|
272
|
-
const docs = rows.map((r) => unflattenRow(r));
|
|
273
|
-
const colDef = this._collections.find((c) => c.slug === collection);
|
|
274
|
-
if (colDef) {
|
|
275
|
-
const relationalFields = getRelationalFields(colDef.fields);
|
|
276
|
-
for (const field of relationalFields) {
|
|
277
|
-
if (!field.name)
|
|
278
|
-
continue;
|
|
279
|
-
const snakeName = toSnakeCase(field.name);
|
|
280
|
-
if (field.type === "relationship" && "hasMany" in field && field.hasMany) {
|
|
281
|
-
const joinTableName = `${toSnakeCase(collection)}_${snakeName}_relations`.toLowerCase();
|
|
282
|
-
try {
|
|
283
|
-
const allRelations = await this.db.selectFrom(joinTableName).selectAll().where("source_id", "in", rowIds).orderBy("order", "asc").execute();
|
|
284
|
-
const relationsBySource = allRelations.reduce((acc, r) => {
|
|
285
|
-
if (!acc[r.source_id])
|
|
286
|
-
acc[r.source_id] = [];
|
|
287
|
-
acc[r.source_id].push(r.target_id);
|
|
288
|
-
return acc;
|
|
289
|
-
}, {});
|
|
290
|
-
const parts = field.name.split("__");
|
|
291
|
-
for (let docIdx = 0;docIdx < docs.length; docIdx++) {
|
|
292
|
-
const rowId = rowIds[docIdx];
|
|
293
|
-
let current = docs[docIdx];
|
|
294
|
-
for (let i = 0;i < parts.length - 1; i++) {
|
|
295
|
-
if (!current[parts[i]])
|
|
296
|
-
current[parts[i]] = {};
|
|
297
|
-
current = current[parts[i]];
|
|
298
|
-
}
|
|
299
|
-
current[parts[parts.length - 1]] = relationsBySource[rowId] || [];
|
|
300
|
-
}
|
|
301
|
-
} catch (e) {}
|
|
302
|
-
} else if (field.type === "blocks" && field.blocks) {
|
|
303
|
-
const blocksBySource = {};
|
|
304
|
-
for (const b of field.blocks) {
|
|
305
|
-
const blockTableName = `${collection}_${snakeName}_${toSnakeCase(b.slug)}`.toLowerCase();
|
|
306
|
-
try {
|
|
307
|
-
const allBlocks = await this.db.selectFrom(blockTableName).selectAll().where("_parent_id", "in", rowIds).execute();
|
|
308
|
-
for (const blk of allBlocks) {
|
|
309
|
-
const uf = unflattenRow(blk);
|
|
310
|
-
uf.blockType = blk.block_type;
|
|
311
|
-
if (!blocksBySource[blk._parent_id])
|
|
312
|
-
blocksBySource[blk._parent_id] = [];
|
|
313
|
-
blocksBySource[blk._parent_id].push(uf);
|
|
314
|
-
}
|
|
315
|
-
} catch (e) {}
|
|
316
|
-
}
|
|
317
|
-
const parts = field.name.split("__");
|
|
318
|
-
for (let docIdx = 0;docIdx < docs.length; docIdx++) {
|
|
319
|
-
const rowId = rowIds[docIdx];
|
|
320
|
-
const blockData = blocksBySource[rowId] || [];
|
|
321
|
-
blockData.sort((a, b) => a._order - b._order);
|
|
322
|
-
blockData.forEach((b) => {
|
|
323
|
-
delete b._order;
|
|
324
|
-
delete b._parentId;
|
|
325
|
-
});
|
|
326
|
-
let current = docs[docIdx];
|
|
327
|
-
for (let i = 0;i < parts.length - 1; i++) {
|
|
328
|
-
if (!current[parts[i]])
|
|
329
|
-
current[parts[i]] = {};
|
|
330
|
-
current = current[parts[i]];
|
|
331
|
-
}
|
|
332
|
-
current[parts[parts.length - 1]] = blockData;
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
const totalPages = Math.ceil(total / limit);
|
|
338
|
-
const hasNextPage = page * limit < total;
|
|
339
|
-
return {
|
|
340
|
-
docs: docs.filter(Boolean),
|
|
341
|
-
totalDocs: total,
|
|
342
|
-
limit,
|
|
343
|
-
totalPages,
|
|
344
|
-
page,
|
|
345
|
-
pagingCounter: offset + 1,
|
|
346
|
-
hasNextPage,
|
|
347
|
-
hasPrevPage: page > 1 || !!options?.after || !!options?.before,
|
|
348
|
-
prevPage: page > 1 ? page - 1 : null,
|
|
349
|
-
nextPage: page < totalPages ? page + 1 : null,
|
|
350
|
-
nextCursor: hasNextPage && docs.length > 0 ? docs[docs.length - 1][cursorColumn] : null,
|
|
351
|
-
prevCursor: docs.length > 0 && (page > 1 || !!options?.after || !!options?.before) ? docs[0][cursorColumn] : null
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
async update(collection, query, data) {
|
|
355
|
-
return this.db.transaction().execute(async (tx) => {
|
|
356
|
-
let normalizedQuery = query;
|
|
357
|
-
if (typeof query !== "object" || query === null) {
|
|
358
|
-
normalizedQuery = { id: query };
|
|
359
|
-
}
|
|
360
|
-
const current = await this.findOne(collection, normalizedQuery, tx);
|
|
361
|
-
if (!current)
|
|
362
|
-
throw new Error("Document not found");
|
|
363
|
-
const store = requestContext.getStore();
|
|
364
|
-
if (store)
|
|
365
|
-
store.previousData = current;
|
|
366
|
-
const colDef = this._collections.find((c) => c.slug === collection);
|
|
367
|
-
const jsonFields = colDef?.fields.filter((f) => ["richtext", "json", "file"].includes(f.type)).map((f) => f.name) || [];
|
|
368
|
-
const flatData = flattenPayload(data, "", jsonFields);
|
|
369
|
-
for (const field of jsonFields) {
|
|
370
|
-
if (flatData[field] && typeof flatData[field] === "object") {
|
|
371
|
-
flatData[field] = JSON.stringify(flatData[field]);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
const relationalFields = getRelationalFields(colDef?.fields || []);
|
|
375
|
-
const hasManyData = {};
|
|
376
|
-
const blocksData = {};
|
|
377
|
-
for (const field of relationalFields) {
|
|
378
|
-
const key = field.name;
|
|
379
|
-
if (Array.isArray(flatData[key])) {
|
|
380
|
-
if (field.type === "relationship" && "hasMany" in field && field.hasMany) {
|
|
381
|
-
hasManyData[key] = flatData[key];
|
|
382
|
-
} else if (field.type === "blocks") {
|
|
383
|
-
blocksData[key] = flatData[key];
|
|
384
|
-
}
|
|
385
|
-
delete flatData[key];
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
if (Object.keys(flatData).length > 0) {
|
|
389
|
-
await tx.updateTable(toSnakeCase(collection)).set(flatData).where("id", "=", current.id).execute();
|
|
390
|
-
}
|
|
391
|
-
for (const [key, values] of Object.entries(hasManyData)) {
|
|
392
|
-
const joinTableName = `${toSnakeCase(collection)}_${toSnakeCase(key)}_relations`.toLowerCase();
|
|
393
|
-
await tx.deleteFrom(joinTableName).where("source_id", "=", current.id).execute();
|
|
394
|
-
if (values.length > 0) {
|
|
395
|
-
const joinData = values.map((val, idx) => {
|
|
396
|
-
const tId = typeof val === "object" ? val.id : val;
|
|
397
|
-
return { id: crypto.randomUUID(), source_id: current.id, target_id: tId, order: idx };
|
|
398
|
-
});
|
|
399
|
-
await tx.insertInto(joinTableName).values(joinData).execute();
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
for (const [key, blocks] of Object.entries(blocksData)) {
|
|
403
|
-
const blockDef = relationalFields.find((f) => f.name === key && f.type === "blocks");
|
|
404
|
-
if (blockDef && "blocks" in blockDef && blockDef.blocks) {
|
|
405
|
-
for (const b of blockDef.blocks) {
|
|
406
|
-
const blockTableName = `${toSnakeCase(collection)}_${toSnakeCase(key)}_${toSnakeCase(b.slug)}`.toLowerCase();
|
|
407
|
-
try {
|
|
408
|
-
await tx.deleteFrom(blockTableName).where("_parent_id", "=", current.id).execute();
|
|
409
|
-
} catch (e) {}
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
for (let i = 0;i < blocks.length; i++) {
|
|
413
|
-
const block = blocks[i];
|
|
414
|
-
if (!block.blockType)
|
|
415
|
-
continue;
|
|
416
|
-
const blockTableName = `${toSnakeCase(collection)}_${toSnakeCase(key)}_${toSnakeCase(block.blockType)}`.toLowerCase();
|
|
417
|
-
const bId = block.id || crypto.randomUUID();
|
|
418
|
-
const blockConfig = blockDef?.blocks?.find((b) => b.slug === block.blockType);
|
|
419
|
-
const blockJsonFields = blockConfig?.fields.filter((f) => ["richtext", "json", "file"].includes(f.type)).map((f) => f.name) || [];
|
|
420
|
-
const blockFlatData = flattenPayload({ ...block, id: bId }, "", blockJsonFields);
|
|
421
|
-
for (const f of blockJsonFields) {
|
|
422
|
-
if (blockFlatData[f] && typeof blockFlatData[f] === "object") {
|
|
423
|
-
blockFlatData[f] = JSON.stringify(blockFlatData[f]);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
delete blockFlatData.blockType;
|
|
427
|
-
const blockData = {
|
|
428
|
-
...blockFlatData,
|
|
429
|
-
_parent_id: current.id,
|
|
430
|
-
_order: i,
|
|
431
|
-
block_type: block.blockType
|
|
432
|
-
};
|
|
433
|
-
await this.db.insertInto(blockTableName).values(blockData).execute();
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
return this.findOne(collection, { id: current.id }, tx);
|
|
437
|
-
});
|
|
438
|
-
}
|
|
439
|
-
async updateMany(collection, query, data) {
|
|
440
|
-
return this.db.transaction().execute(async (tx) => {
|
|
441
|
-
let qb = tx.updateTable(toSnakeCase(collection));
|
|
442
|
-
if (query && Object.keys(query).length > 0) {
|
|
443
|
-
qb = qb.where((eb) => buildKyselyWhere(eb, query) || eb.val(true));
|
|
444
|
-
}
|
|
445
|
-
const colDef = this._collections.find((c) => c.slug === collection);
|
|
446
|
-
const jsonFields = colDef?.fields.filter((f) => ["richtext", "json", "file"].includes(f.type)).map((f) => f.name) || [];
|
|
447
|
-
const flatData = flattenPayload(data, "", jsonFields);
|
|
448
|
-
for (const field of jsonFields) {
|
|
449
|
-
if (flatData[field] && typeof flatData[field] === "object") {
|
|
450
|
-
flatData[field] = JSON.stringify(flatData[field]);
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
for (const key in flatData) {
|
|
454
|
-
if (Array.isArray(flatData[key])) {
|
|
455
|
-
delete flatData[key];
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
if (Object.keys(flatData).length > 0) {
|
|
459
|
-
const result = await qb.set(flatData).executeTakeFirst();
|
|
460
|
-
return Number(result.numUpdatedRows || 0);
|
|
461
|
-
}
|
|
462
|
-
return 0;
|
|
463
|
-
});
|
|
464
|
-
}
|
|
465
|
-
async delete(collection, query) {
|
|
466
|
-
let normalizedQuery = query;
|
|
467
|
-
if (typeof query !== "object" || query === null) {
|
|
468
|
-
normalizedQuery = { id: query };
|
|
469
|
-
}
|
|
470
|
-
const current = await this.findOne(collection, normalizedQuery);
|
|
471
|
-
if (!current)
|
|
472
|
-
return false;
|
|
473
|
-
await this.db.transaction().execute(async (tx) => {
|
|
474
|
-
const colDef = this._collections.find((c) => c.slug === collection);
|
|
475
|
-
if (colDef) {
|
|
476
|
-
const relationalFields = getRelationalFields(colDef.fields);
|
|
477
|
-
for (const field of relationalFields) {
|
|
478
|
-
if (!field.name)
|
|
479
|
-
continue;
|
|
480
|
-
const snakeName = toSnakeCase(field.name);
|
|
481
|
-
if (field.type === "relationship" && "hasMany" in field && field.hasMany) {
|
|
482
|
-
const joinTableName = `${toSnakeCase(collection)}_${snakeName}_relations`.toLowerCase();
|
|
483
|
-
try {
|
|
484
|
-
await tx.deleteFrom(joinTableName).where("source_id", "=", current.id).execute();
|
|
485
|
-
} catch (e) {}
|
|
486
|
-
} else if (field.type === "blocks" && field.blocks) {
|
|
487
|
-
for (const b of field.blocks) {
|
|
488
|
-
const blockTableName = `${toSnakeCase(collection)}_${snakeName}_${toSnakeCase(b.slug)}`.toLowerCase();
|
|
489
|
-
try {
|
|
490
|
-
await tx.deleteFrom(blockTableName).where("_parent_id", "=", current.id).execute();
|
|
491
|
-
} catch (e) {}
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
await tx.deleteFrom(toSnakeCase(collection)).where("id", "=", current.id).execute();
|
|
497
|
-
});
|
|
498
|
-
return true;
|
|
499
|
-
}
|
|
500
|
-
async deleteMany(collection, query) {
|
|
501
|
-
return this.db.transaction().execute(async (tx) => {
|
|
502
|
-
let selectQb = tx.selectFrom(toSnakeCase(collection)).select("id");
|
|
503
|
-
if (query && Object.keys(query).length > 0) {
|
|
504
|
-
selectQb = selectQb.where((eb) => buildKyselyWhere(eb, query) || eb.val(true));
|
|
505
|
-
}
|
|
506
|
-
const docs = await selectQb.execute();
|
|
507
|
-
if (docs.length === 0)
|
|
508
|
-
return 0;
|
|
509
|
-
const ids = docs.map((d) => d.id);
|
|
510
|
-
const colDef = this._collections.find((c) => c.slug === collection);
|
|
511
|
-
if (colDef) {
|
|
512
|
-
const relationalFields = getRelationalFields(colDef.fields);
|
|
513
|
-
for (const field of relationalFields) {
|
|
514
|
-
if (!field.name)
|
|
515
|
-
continue;
|
|
516
|
-
const snakeName = toSnakeCase(field.name);
|
|
517
|
-
if (field.type === "relationship" && "hasMany" in field && field.hasMany) {
|
|
518
|
-
const joinTableName = `${toSnakeCase(collection)}_${snakeName}_relations`.toLowerCase();
|
|
519
|
-
try {
|
|
520
|
-
await tx.deleteFrom(joinTableName).where("source_id", "in", ids).execute();
|
|
521
|
-
} catch (e) {}
|
|
522
|
-
} else if (field.type === "blocks" && field.blocks) {
|
|
523
|
-
for (const b of field.blocks) {
|
|
524
|
-
const blockTableName = `${toSnakeCase(collection)}_${snakeName}_${toSnakeCase(b.slug)}`.toLowerCase();
|
|
525
|
-
try {
|
|
526
|
-
await tx.deleteFrom(blockTableName).where("_parent_id", "in", ids).execute();
|
|
527
|
-
} catch (e) {}
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
const result = await tx.deleteFrom(toSnakeCase(collection)).where("id", "in", ids).executeTakeFirst();
|
|
533
|
-
return Number(result.numDeletedRows || 0);
|
|
534
|
-
});
|
|
535
|
-
}
|
|
536
|
-
async findGlobal(slug) {
|
|
537
|
-
const tableName = toSnakeCase(slug);
|
|
538
|
-
const row = await this.db.selectFrom(tableName).selectAll().limit(1).executeTakeFirst();
|
|
539
|
-
return row ? unflattenRow(row) : null;
|
|
540
|
-
}
|
|
541
|
-
async updateGlobal(slug, data) {
|
|
542
|
-
const tableName = toSnakeCase(slug);
|
|
543
|
-
const existing = await this.findGlobal(slug);
|
|
544
|
-
const flatData = flattenPayload(data);
|
|
545
|
-
const globalDef = this._globals.find((g) => g.slug === slug);
|
|
546
|
-
const now = new Date().toISOString();
|
|
547
|
-
flatData[toSnakeCase("updatedAt")] = now;
|
|
548
|
-
if (!existing) {
|
|
549
|
-
if (!flatData.id)
|
|
550
|
-
flatData.id = "global";
|
|
551
|
-
flatData[toSnakeCase("createdAt")] = now;
|
|
552
|
-
await this.db.insertInto(tableName).values(flatData).execute();
|
|
553
|
-
} else {
|
|
554
|
-
await this.db.updateTable(tableName).set(flatData).where("id", "=", existing.id).execute();
|
|
555
|
-
}
|
|
556
|
-
return this.findGlobal(slug);
|
|
557
|
-
}
|
|
558
|
-
async runMigrations() {
|
|
559
|
-
const migrator = new Migrator({
|
|
560
|
-
db: this.db,
|
|
561
|
-
provider: new FileMigrationProvider({
|
|
562
|
-
fs,
|
|
563
|
-
path,
|
|
564
|
-
migrationFolder: pathToFileURL(path.resolve(process.cwd(), this.migrationDir)).href
|
|
565
|
-
})
|
|
566
|
-
});
|
|
567
|
-
const { error, results } = await migrator.migrateToLatest();
|
|
568
|
-
results?.forEach((it) => {
|
|
569
|
-
if (it.status === "Success") {
|
|
570
|
-
logger.success(`\uD83D\uDE80 Migration "${it.migrationName}" was executed successfully`);
|
|
571
|
-
} else if (it.status === "Error") {
|
|
572
|
-
logger.error(`❌ Migration "${it.migrationName}" failed`);
|
|
573
|
-
}
|
|
574
|
-
});
|
|
575
|
-
if (error) {
|
|
576
|
-
throw error;
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
async migrate(collections, globals = []) {
|
|
580
|
-
this._collections = collections;
|
|
581
|
-
this._globals = globals;
|
|
582
|
-
if (this.push) {
|
|
583
|
-
await pushSchema(this.db, "sqlite", collections, globals, {
|
|
584
|
-
pushDestructive: this.pushDestructive
|
|
585
|
-
});
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
function createBunSQLiteAdapter(path2, options) {
|
|
590
|
-
return new BunSQLiteAdapter(path2, options);
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
export { BunSQLiteAdapter, createBunSQLiteAdapter };
|