@kyro-cms/admin 0.1.6 → 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.
- package/README.md +149 -51
- package/package.json +54 -5
- package/src/collections/auth/index.ts +2 -2
- package/src/collections/portfolio/index.ts +343 -0
- package/src/components/ActionBar.tsx +153 -16
- package/src/components/Admin.tsx +137 -28
- package/src/components/ApiExplorer.tsx +325 -0
- package/src/components/ApiKeysManager.tsx +563 -0
- package/src/components/AuditLogsPage.tsx +664 -0
- package/src/components/AutoForm.tsx +2155 -770
- package/src/components/BrandingHub.tsx +267 -0
- package/src/components/BulkActionsBar.tsx +3 -3
- package/src/components/CreateView.tsx +4 -4
- package/src/components/Dashboard.tsx +393 -0
- package/src/components/DetailView.tsx +200 -58
- package/src/components/DeveloperCenter.tsx +403 -0
- package/src/components/EnhancedListView.tsx +890 -0
- package/src/components/GraphQLExplorer.tsx +675 -0
- package/src/components/GraphQLPlayground.tsx +627 -0
- package/src/components/ListView.tsx +192 -54
- package/src/components/MediaGallery.tsx +1569 -0
- package/src/components/Modal.tsx +206 -0
- package/src/components/RestPlayground.tsx +951 -0
- package/src/components/Sidebar.astro +237 -0
- package/src/components/ThemeProvider.tsx +8 -2
- package/src/components/UserManagement.tsx +204 -0
- package/src/components/VersionHistoryPanel.tsx +3 -3
- package/src/components/WebhookManager.tsx +608 -0
- package/src/components/blocks/AccordionBlock.tsx +65 -0
- package/src/components/blocks/ArrayBlock.tsx +84 -0
- package/src/components/blocks/BlockEditModal.tsx +363 -0
- package/src/components/blocks/ButtonBlock.tsx +64 -0
- package/src/components/blocks/ChildBlocksTree.tsx +551 -0
- package/src/components/blocks/CodeBlock.tsx +114 -0
- package/src/components/blocks/ColumnsBlock.tsx +93 -0
- package/src/components/blocks/DividerBlock.tsx +43 -0
- package/src/components/blocks/FileBlock.tsx +63 -0
- package/src/components/blocks/HeadingBlock.tsx +59 -0
- package/src/components/blocks/HeroBlock.tsx +99 -0
- package/src/components/blocks/ImageBlock.tsx +82 -0
- package/src/components/blocks/LinkBlock.tsx +65 -0
- package/src/components/blocks/ListBlock.tsx +60 -0
- package/src/components/blocks/ParagraphBlock.tsx +61 -0
- package/src/components/blocks/RelationshipBlock.tsx +72 -0
- package/src/components/blocks/RichTextBlock.tsx +66 -0
- package/src/components/blocks/VStackBlock.tsx +61 -0
- package/src/components/blocks/VideoBlock.tsx +65 -0
- package/src/components/blocks/index.ts +10 -0
- package/src/components/fields/AccordionField.tsx +213 -0
- package/src/components/fields/ArrayField.tsx +241 -0
- package/src/components/fields/BlocksField.tsx +323 -0
- package/src/components/fields/ButtonField.tsx +53 -0
- package/src/components/fields/CheckboxField.tsx +18 -8
- package/src/components/fields/ChildrenField.tsx +48 -0
- package/src/components/fields/CodeField.tsx +294 -0
- package/src/components/fields/ColumnsField.tsx +137 -0
- package/src/components/fields/DateField.tsx +24 -12
- package/src/components/fields/EditorClient.tsx +537 -0
- package/src/components/fields/HeadingField.tsx +31 -0
- package/src/components/fields/HeroField.tsx +101 -0
- package/src/components/fields/JSONField.tsx +341 -0
- package/src/components/fields/LinkField.tsx +81 -0
- package/src/components/fields/ListField.tsx +74 -0
- package/src/components/fields/MarkdownField.tsx +260 -0
- package/src/components/fields/NumberField.tsx +25 -13
- package/src/components/fields/PortableTextField.tsx +155 -0
- package/src/components/fields/PortableTextRenderer.tsx +68 -0
- package/src/components/fields/RelationshipBlockField.tsx +233 -0
- package/src/components/fields/RelationshipField.tsx +278 -60
- package/src/components/fields/SelectField.tsx +28 -16
- package/src/components/fields/TextField.tsx +31 -15
- package/src/components/fields/UploadField.tsx +613 -0
- package/src/components/fields/VideoField.tsx +73 -0
- package/src/components/fields/extensions/blockComponents.tsx +247 -0
- package/src/components/fields/extensions/blocksStore.ts +273 -0
- package/src/components/fields/index.ts +24 -0
- package/src/components/index.ts +1 -2
- package/src/components/layout/Header.tsx +2 -2
- package/src/components/layout/Layout.tsx +3 -3
- package/src/components/ui/Badge.tsx +9 -4
- package/src/components/ui/BlockDrawer.tsx +79 -0
- package/src/components/ui/Button.tsx +1 -1
- package/src/components/ui/CommandPalette.tsx +362 -0
- package/src/components/ui/CommandPaletteWrapper.tsx +97 -0
- package/src/components/ui/Dropdown.tsx +1 -1
- package/src/components/ui/Modal.tsx +37 -12
- package/src/components/ui/PromptModal.tsx +94 -0
- package/src/components/ui/SlidePanel.tsx +43 -16
- package/src/components/ui/Toast.tsx +80 -14
- package/src/env.d.ts +16 -0
- package/src/env.ts +20 -0
- package/src/index.ts +0 -1
- package/src/layouts/AdminLayout.astro +164 -170
- package/src/layouts/AuthLayout.astro +23 -6
- package/src/lib/MediaService.ts +541 -0
- package/src/lib/api.ts +163 -0
- package/src/lib/auth/sqlite-adapter.ts +319 -0
- package/src/lib/config.ts +23 -7
- package/src/lib/dataStore.ts +188 -73
- package/src/lib/date-utils.ts +69 -0
- package/src/lib/db/adapter.ts +54 -0
- package/src/lib/db/drizzle-mysql-adapter.ts +194 -0
- package/src/lib/db/drizzle-mysql-auth-adapter.ts +327 -0
- package/src/lib/db/drizzle-postgres-adapter.ts +202 -0
- package/src/lib/db/drizzle-postgres-auth-adapter.ts +304 -0
- package/src/lib/db/drizzle-sqlite-adapter.ts +227 -0
- package/src/lib/db/drizzle-sqlite-auth-adapter.ts +548 -0
- package/src/lib/db/index.ts +449 -0
- package/src/lib/db/mongodb-adapter.ts +207 -0
- package/src/lib/db/mongodb-auth-adapter.ts +305 -0
- package/src/lib/db/schema/mysql-auth.ts +113 -0
- package/src/lib/db/schema/mysql-content.ts +20 -0
- package/src/lib/db/schema/postgres-auth.ts +116 -0
- package/src/lib/db/schema/postgres-content.ts +35 -0
- package/src/lib/db/schema/postgres-media.ts +52 -0
- package/src/lib/db/schema/postgres-settings.ts +11 -0
- package/src/lib/db/schema/sqlite-auth.ts +112 -0
- package/src/lib/db/schema/sqlite-content.ts +20 -0
- package/src/lib/db/version-adapter.ts +248 -0
- package/src/lib/graphql/index.ts +1 -0
- package/src/lib/graphql/schema.ts +443 -0
- package/src/lib/i18n.tsx +353 -0
- package/src/lib/rate-limit.ts +267 -0
- package/src/lib/slugify.ts +15 -0
- package/src/lib/storage.ts +374 -0
- package/src/lib/store.ts +85 -0
- package/src/lib/validation.ts +250 -0
- package/src/middleware.ts +70 -11
- package/src/pages/[collection]/[id].astro +178 -122
- package/src/pages/[collection]/index.astro +24 -156
- package/src/pages/admin/api-explorer.astro +98 -0
- package/src/pages/admin/graphql-explorer.astro +40 -0
- package/src/pages/admin/graphql.astro +97 -0
- package/src/pages/admin/index.astro +200 -139
- package/src/pages/admin/keys.astro +8 -0
- package/src/pages/admin/rest-playground.astro +44 -0
- package/src/pages/admin/webhooks.astro +8 -0
- package/src/pages/api/[collection]/[id]/publish.ts +52 -0
- package/src/pages/api/[collection]/[id]/unpublish.ts +42 -0
- package/src/pages/api/[collection]/[id]/versions.ts +66 -0
- package/src/pages/api/[collection]/[id].ts +114 -159
- package/src/pages/api/[collection]/index.ts +150 -230
- package/src/pages/api/auth/[id].ts +48 -69
- package/src/pages/api/auth/audit-logs.ts +20 -43
- package/src/pages/api/auth/login.ts +159 -45
- package/src/pages/api/auth/logout.ts +42 -24
- package/src/pages/api/auth/refresh.ts +119 -0
- package/src/pages/api/auth/register.ts +110 -40
- package/src/pages/api/auth/users.ts +22 -97
- package/src/pages/api/collections.ts +59 -0
- package/src/pages/api/globals/[slug]/test.ts +172 -0
- package/src/pages/api/globals/[slug].ts +42 -0
- package/src/pages/api/graphql.ts +90 -0
- package/src/pages/api/health.ts +417 -40
- package/src/pages/api/keys/[id].ts +26 -0
- package/src/pages/api/keys/index.ts +75 -0
- package/src/pages/api/media/[id].ts +309 -0
- package/src/pages/api/media/folders.ts +609 -0
- package/src/pages/api/media/index.ts +146 -0
- package/src/pages/api/media/resize.ts +267 -0
- package/src/pages/api/search.ts +82 -0
- package/src/pages/api/slug-availability.ts +70 -0
- package/src/pages/api/storage-config.ts +20 -0
- package/src/pages/api/storage-status.ts +206 -0
- package/src/pages/api/upload.ts +334 -0
- package/src/pages/api/webhooks/index.ts +71 -0
- package/src/pages/audit/index.astro +2 -104
- package/src/pages/login.astro +11 -11
- package/src/pages/media.astro +10 -0
- package/src/pages/preview/[collection]/[id].astro +178 -0
- package/src/pages/register.astro +13 -13
- package/src/pages/roles/index.astro +21 -21
- package/src/pages/settings/[slug].astro +162 -0
- package/src/pages/settings/index.astro +9 -0
- package/src/pages/users/[id].astro +29 -21
- package/src/pages/users/index.astro +22 -17
- package/src/pages/users/new.astro +18 -17
- package/src/styles/main.css +563 -128
- package/src/components/layout/Sidebar.tsx +0 -497
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export type DateStyle = "full" | "date" | "time" | "short";
|
|
2
|
+
|
|
3
|
+
export function formatDate(
|
|
4
|
+
date: string | Date | null | undefined,
|
|
5
|
+
style: DateStyle = "date",
|
|
6
|
+
): string {
|
|
7
|
+
if (!date) return "—";
|
|
8
|
+
const d = new Date(date);
|
|
9
|
+
if (isNaN(d.getTime())) return "—";
|
|
10
|
+
|
|
11
|
+
switch (style) {
|
|
12
|
+
case "full":
|
|
13
|
+
return d.toLocaleString("en-US", {
|
|
14
|
+
dateStyle: "medium",
|
|
15
|
+
timeStyle: "short",
|
|
16
|
+
});
|
|
17
|
+
case "time":
|
|
18
|
+
return d.toLocaleTimeString("en-US", {
|
|
19
|
+
hour: "numeric",
|
|
20
|
+
minute: "2-digit",
|
|
21
|
+
});
|
|
22
|
+
case "short":
|
|
23
|
+
return d.toLocaleDateString("en-US", {
|
|
24
|
+
month: "short",
|
|
25
|
+
day: "numeric",
|
|
26
|
+
});
|
|
27
|
+
case "date":
|
|
28
|
+
default:
|
|
29
|
+
return d.toLocaleDateString("en-US", {
|
|
30
|
+
month: "short",
|
|
31
|
+
day: "numeric",
|
|
32
|
+
year: "numeric",
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function formatRelativeTime(
|
|
38
|
+
date: string | Date | null | undefined,
|
|
39
|
+
): string {
|
|
40
|
+
if (!date) return "—";
|
|
41
|
+
const d = new Date(date);
|
|
42
|
+
if (isNaN(d.getTime())) return "—";
|
|
43
|
+
|
|
44
|
+
const now = new Date();
|
|
45
|
+
const diffMs = now.getTime() - d.getTime();
|
|
46
|
+
const diffSec = Math.floor(diffMs / 1000);
|
|
47
|
+
const diffMin = Math.floor(diffSec / 60);
|
|
48
|
+
const diffHour = Math.floor(diffMin / 60);
|
|
49
|
+
const diffDay = Math.floor(diffHour / 24);
|
|
50
|
+
|
|
51
|
+
if (diffSec < 60) return "Just now";
|
|
52
|
+
if (diffMin < 60) return `${diffMin}m ago`;
|
|
53
|
+
if (diffHour < 24) return `${diffHour}h ago`;
|
|
54
|
+
if (diffDay < 7) return `${diffDay}d ago`;
|
|
55
|
+
return formatDate(d, "date");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function getTimestamp(): string {
|
|
59
|
+
return `?t=${Date.now()}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function generateId(length: number = 16): string {
|
|
63
|
+
const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
64
|
+
let result = "";
|
|
65
|
+
for (let i = 0; i < length; i++) {
|
|
66
|
+
result += chars[Math.floor(Math.random() * chars.length)];
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { CollectionConfig } from "@kyro-cms/core";
|
|
2
|
+
|
|
3
|
+
export interface DatabaseAdapter {
|
|
4
|
+
initialize(collections: Record<string, CollectionConfig>): void;
|
|
5
|
+
|
|
6
|
+
find<T = any>(
|
|
7
|
+
slug: string,
|
|
8
|
+
options?: { page?: number; limit?: number },
|
|
9
|
+
): Promise<{
|
|
10
|
+
docs: T[];
|
|
11
|
+
totalDocs: number;
|
|
12
|
+
totalPages: number;
|
|
13
|
+
page: number;
|
|
14
|
+
}>;
|
|
15
|
+
|
|
16
|
+
findById<T = any>(slug: string, id: string): Promise<T | null>;
|
|
17
|
+
|
|
18
|
+
create<T = any>(slug: string, data: Partial<T>): Promise<T>;
|
|
19
|
+
|
|
20
|
+
update<T = any>(
|
|
21
|
+
slug: string,
|
|
22
|
+
id: string,
|
|
23
|
+
data: Partial<T>,
|
|
24
|
+
): Promise<T | null>;
|
|
25
|
+
|
|
26
|
+
delete(slug: string, id: string): Promise<boolean>;
|
|
27
|
+
|
|
28
|
+
findGlobal<T = any>(slug: string): Promise<T>;
|
|
29
|
+
|
|
30
|
+
updateGlobal<T = any>(slug: string, data: Partial<T>): Promise<T>;
|
|
31
|
+
|
|
32
|
+
seedGlobal(slug: string, data: any): Promise<void>;
|
|
33
|
+
|
|
34
|
+
count(slug: string): Promise<number>;
|
|
35
|
+
|
|
36
|
+
seed(slug: string, docs: any[]): Promise<void>;
|
|
37
|
+
|
|
38
|
+
isSeeded(slug: string): Promise<boolean>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type DatabaseType = "sqlite" | "postgres" | "mysql" | "mongodb";
|
|
42
|
+
|
|
43
|
+
export interface DatabaseConfig {
|
|
44
|
+
type: DatabaseType;
|
|
45
|
+
connectionString?: string;
|
|
46
|
+
host?: string;
|
|
47
|
+
port?: number;
|
|
48
|
+
database?: string;
|
|
49
|
+
username?: string;
|
|
50
|
+
password?: string;
|
|
51
|
+
poolMin?: number;
|
|
52
|
+
poolMax?: number;
|
|
53
|
+
contentDbPath?: string;
|
|
54
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import type { CollectionConfig } from "@kyro-cms/core";
|
|
2
|
+
import type { DatabaseAdapter, DatabaseConfig } from "./adapter";
|
|
3
|
+
import mysql from "mysql2/promise";
|
|
4
|
+
import { drizzle } from "drizzle-orm/mysql2";
|
|
5
|
+
import { eq, desc, sql, and } from "drizzle-orm";
|
|
6
|
+
import * as contentSchema from "./schema/mysql-content";
|
|
7
|
+
|
|
8
|
+
export class MySQLAdapter implements DatabaseAdapter {
|
|
9
|
+
private pool: mysql.Pool;
|
|
10
|
+
private db = drizzle(this.pool);
|
|
11
|
+
private initialized = false;
|
|
12
|
+
|
|
13
|
+
constructor(private config: DatabaseConfig) {
|
|
14
|
+
this.pool = mysql.createPool({
|
|
15
|
+
connectionString: config.connectionString,
|
|
16
|
+
host: config.host,
|
|
17
|
+
port: config.port,
|
|
18
|
+
database: config.database,
|
|
19
|
+
user: config.username,
|
|
20
|
+
password: config.password,
|
|
21
|
+
waitForConnections: true,
|
|
22
|
+
connectionLimit: config.poolMax || 10,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
initialize(collections: Record<string, CollectionConfig>) {
|
|
27
|
+
if (this.initialized) return;
|
|
28
|
+
this.initialized = true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private getTimestamp(): Date {
|
|
32
|
+
return new Date();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
private async generateId(slug: string): Promise<string> {
|
|
36
|
+
const result = await this.db
|
|
37
|
+
.select({ count: sql<number>`count(*)` })
|
|
38
|
+
.from(contentSchema.documents)
|
|
39
|
+
.where(eq(contentSchema.documents.collection, slug));
|
|
40
|
+
return `${slug}-${(result[0]?.count || 0) + 1}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async find<T = any>(
|
|
44
|
+
slug: string,
|
|
45
|
+
options: { page?: number; limit?: number } = {},
|
|
46
|
+
): Promise<{
|
|
47
|
+
docs: T[];
|
|
48
|
+
totalDocs: number;
|
|
49
|
+
totalPages: number;
|
|
50
|
+
page: number;
|
|
51
|
+
}> {
|
|
52
|
+
const page = options.page || 1;
|
|
53
|
+
const limit = options.limit || 25;
|
|
54
|
+
const start = (page - 1) * limit;
|
|
55
|
+
|
|
56
|
+
const countResult = await this.db
|
|
57
|
+
.select({ count: sql<number>`count(*)` })
|
|
58
|
+
.from(contentSchema.documents)
|
|
59
|
+
.where(eq(contentSchema.documents.collection, slug));
|
|
60
|
+
const count = countResult[0]?.count || 0;
|
|
61
|
+
|
|
62
|
+
const docs = await this.db
|
|
63
|
+
.select({ data: contentSchema.documents.data })
|
|
64
|
+
.from(contentSchema.documents)
|
|
65
|
+
.where(eq(contentSchema.documents.collection, slug))
|
|
66
|
+
.orderBy(desc(contentSchema.documents.createdAt))
|
|
67
|
+
.limit(limit)
|
|
68
|
+
.offset(start);
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
docs: docs.map((d) => d.data as T),
|
|
72
|
+
totalDocs: count,
|
|
73
|
+
totalPages: Math.ceil(count / limit),
|
|
74
|
+
page,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async findById<T = any>(slug: string, id: string): Promise<T | null> {
|
|
79
|
+
const doc = await this.db
|
|
80
|
+
.select({ data: contentSchema.documents.data })
|
|
81
|
+
.from(contentSchema.documents)
|
|
82
|
+
.where(
|
|
83
|
+
and(
|
|
84
|
+
eq(contentSchema.documents.collection, slug),
|
|
85
|
+
eq(contentSchema.documents.id, id),
|
|
86
|
+
),
|
|
87
|
+
)
|
|
88
|
+
.get();
|
|
89
|
+
return (doc?.data as T) || null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async create<T = any>(slug: string, data: Partial<T>): Promise<T> {
|
|
93
|
+
const now = this.getTimestamp();
|
|
94
|
+
const id = await this.generateId(slug);
|
|
95
|
+
const newDoc = {
|
|
96
|
+
id,
|
|
97
|
+
collection: slug,
|
|
98
|
+
data: { id, ...data, createdAt: now, updatedAt: now },
|
|
99
|
+
createdAt: now,
|
|
100
|
+
updatedAt: now,
|
|
101
|
+
};
|
|
102
|
+
await this.db.insert(contentSchema.documents).values(newDoc as any);
|
|
103
|
+
return newDoc.data as T;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async update<T = any>(
|
|
107
|
+
slug: string,
|
|
108
|
+
id: string,
|
|
109
|
+
data: Partial<T>,
|
|
110
|
+
): Promise<T | null> {
|
|
111
|
+
const existing = await this.findById<T>(slug, id);
|
|
112
|
+
if (!existing) return null;
|
|
113
|
+
const now = this.getTimestamp();
|
|
114
|
+
const updated = { ...existing, ...data, id, updatedAt: now };
|
|
115
|
+
await this.db
|
|
116
|
+
.update(contentSchema.documents)
|
|
117
|
+
.set({ data: updated, updatedAt: now })
|
|
118
|
+
.where(
|
|
119
|
+
and(
|
|
120
|
+
eq(contentSchema.documents.collection, slug),
|
|
121
|
+
eq(contentSchema.documents.id, id),
|
|
122
|
+
),
|
|
123
|
+
);
|
|
124
|
+
return updated as T;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async delete(slug: string, id: string): Promise<boolean> {
|
|
128
|
+
await this.db
|
|
129
|
+
.delete(contentSchema.documents)
|
|
130
|
+
.where(
|
|
131
|
+
and(
|
|
132
|
+
eq(contentSchema.documents.collection, slug),
|
|
133
|
+
eq(contentSchema.documents.id, id),
|
|
134
|
+
),
|
|
135
|
+
);
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async findGlobal<T = any>(slug: string): Promise<T> {
|
|
140
|
+
const global = await this.db
|
|
141
|
+
.select({ data: contentSchema.globals.data })
|
|
142
|
+
.from(contentSchema.globals)
|
|
143
|
+
.where(eq(contentSchema.globals.slug, slug))
|
|
144
|
+
.get();
|
|
145
|
+
return (global?.data as T) || ({} as T);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async updateGlobal<T = any>(slug: string, data: Partial<T>): Promise<T> {
|
|
149
|
+
const now = this.getTimestamp();
|
|
150
|
+
const current = await this.findGlobal<T>(slug);
|
|
151
|
+
const updated = { ...current, ...data };
|
|
152
|
+
await this.db
|
|
153
|
+
.insert(contentSchema.globals)
|
|
154
|
+
.values({ slug, data: updated, updatedAt: now })
|
|
155
|
+
.onDuplicateKeyUpdate({ set: { data: updated, updatedAt: now } });
|
|
156
|
+
return updated as T;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async seedGlobal(slug: string, data: any): Promise<void> {
|
|
160
|
+
const now = this.getTimestamp();
|
|
161
|
+
await this.db
|
|
162
|
+
.insert(contentSchema.globals)
|
|
163
|
+
.values({ slug, data, updatedAt: now })
|
|
164
|
+
.onDuplicateKeyUpdate({ set: { data: data } });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async count(slug: string): Promise<number> {
|
|
168
|
+
const result = await this.db
|
|
169
|
+
.select({ count: sql<number>`count(*)` })
|
|
170
|
+
.from(contentSchema.documents)
|
|
171
|
+
.where(eq(contentSchema.documents.collection, slug));
|
|
172
|
+
return result[0]?.count || 0;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async seed(slug: string, docs: any[]): Promise<void> {
|
|
176
|
+
const now = this.getTimestamp();
|
|
177
|
+
const values = docs.map((doc, i) => ({
|
|
178
|
+
id: doc.id || `${slug}-${i + 1}`,
|
|
179
|
+
collection: slug,
|
|
180
|
+
data: { ...doc, createdAt: now, updatedAt: now },
|
|
181
|
+
createdAt: now,
|
|
182
|
+
updatedAt: now,
|
|
183
|
+
}));
|
|
184
|
+
await this.db.insert(contentSchema.documents).values(values as any);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async isSeeded(slug: string): Promise<boolean> {
|
|
188
|
+
return (await this.count(slug)) > 0;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async close(): Promise<void> {
|
|
192
|
+
await this.pool.end();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import type { AuthAdapter, AuthUser, Session, UserRole } from "@kyro-cms/core";
|
|
2
|
+
import type { AuditLog, AuditLogFilter } from "@kyro-cms/core";
|
|
3
|
+
import mysql from "mysql2/promise";
|
|
4
|
+
import { drizzle } from "drizzle-orm/mysql2";
|
|
5
|
+
import { eq, and, desc, sql } from "drizzle-orm";
|
|
6
|
+
import bcrypt from "bcryptjs";
|
|
7
|
+
import { randomBytes } from "crypto";
|
|
8
|
+
import * as authSchema from "./schema/mysql-auth";
|
|
9
|
+
|
|
10
|
+
export interface MySQLAuthAdapterOptions {
|
|
11
|
+
connectionString?: string;
|
|
12
|
+
host?: string;
|
|
13
|
+
port?: number;
|
|
14
|
+
username?: string;
|
|
15
|
+
password?: string;
|
|
16
|
+
database?: string;
|
|
17
|
+
sessionTTL?: number;
|
|
18
|
+
refreshTokenTTL?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const DEFAULT_SESSION_TTL = 86400;
|
|
22
|
+
const DEFAULT_REFRESH_TTL = 604800;
|
|
23
|
+
|
|
24
|
+
export class MySQLAuthAdapter implements AuthAdapter {
|
|
25
|
+
private pool: mysql.Pool;
|
|
26
|
+
private db;
|
|
27
|
+
private sessionTTL: number;
|
|
28
|
+
private refreshTTL: number;
|
|
29
|
+
|
|
30
|
+
constructor(private options: MySQLAuthAdapterOptions) {
|
|
31
|
+
this.pool = mysql.createPool({
|
|
32
|
+
uri: options.connectionString,
|
|
33
|
+
host: options.host,
|
|
34
|
+
port: options.port,
|
|
35
|
+
database: options.database,
|
|
36
|
+
user: options.username,
|
|
37
|
+
password: options.password,
|
|
38
|
+
waitForConnections: true,
|
|
39
|
+
connectionLimit: 10,
|
|
40
|
+
});
|
|
41
|
+
this.db = drizzle(this.pool);
|
|
42
|
+
this.sessionTTL = options.sessionTTL || DEFAULT_SESSION_TTL;
|
|
43
|
+
this.refreshTTL = options.refreshTokenTTL || DEFAULT_REFRESH_TTL;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private generateId(): string {
|
|
47
|
+
return crypto.randomUUID();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async createUser(data: {
|
|
51
|
+
email: string;
|
|
52
|
+
passwordHash: string;
|
|
53
|
+
role: UserRole;
|
|
54
|
+
tenantId?: string;
|
|
55
|
+
}): Promise<AuthUser> {
|
|
56
|
+
const now = new Date();
|
|
57
|
+
const id = this.generateId();
|
|
58
|
+
await this.db.insert(authSchema.users).values({
|
|
59
|
+
id,
|
|
60
|
+
email: data.email.toLowerCase(),
|
|
61
|
+
passwordHash: data.passwordHash,
|
|
62
|
+
name: null,
|
|
63
|
+
role: data.role,
|
|
64
|
+
tenantId: data.tenantId,
|
|
65
|
+
emailVerified: false,
|
|
66
|
+
locked: false,
|
|
67
|
+
lastLogin: null,
|
|
68
|
+
createdAt: now,
|
|
69
|
+
updatedAt: now,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const [user] = await this.db
|
|
73
|
+
.select()
|
|
74
|
+
.from(authSchema.users)
|
|
75
|
+
.where(eq(authSchema.users.id, id))
|
|
76
|
+
.limit(1);
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
id: user.id,
|
|
80
|
+
email: user.email,
|
|
81
|
+
role: user.role as UserRole,
|
|
82
|
+
tenantId: user.tenantId || undefined,
|
|
83
|
+
createdAt: user.createdAt.toISOString(),
|
|
84
|
+
updatedAt: user.updatedAt.toISOString(),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async findUserByEmail(email: string): Promise<AuthUser | null> {
|
|
89
|
+
const [user] = await this.db
|
|
90
|
+
.select()
|
|
91
|
+
.from(authSchema.users)
|
|
92
|
+
.where(eq(authSchema.users.email, email.toLowerCase()))
|
|
93
|
+
.limit(1);
|
|
94
|
+
if (!user) return null;
|
|
95
|
+
return {
|
|
96
|
+
id: user.id,
|
|
97
|
+
email: user.email,
|
|
98
|
+
role: user.role as UserRole,
|
|
99
|
+
tenantId: user.tenantId || undefined,
|
|
100
|
+
createdAt: user.createdAt.toISOString(),
|
|
101
|
+
updatedAt: user.updatedAt.toISOString(),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async findUserById(id: string): Promise<AuthUser | null> {
|
|
106
|
+
const [user] = await this.db
|
|
107
|
+
.select()
|
|
108
|
+
.from(authSchema.users)
|
|
109
|
+
.where(eq(authSchema.users.id, id))
|
|
110
|
+
.limit(1);
|
|
111
|
+
if (!user) return null;
|
|
112
|
+
return {
|
|
113
|
+
id: user.id,
|
|
114
|
+
email: user.email,
|
|
115
|
+
role: user.role as UserRole,
|
|
116
|
+
tenantId: user.tenantId || undefined,
|
|
117
|
+
createdAt: user.createdAt.toISOString(),
|
|
118
|
+
updatedAt: user.updatedAt.toISOString(),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async updateUser(
|
|
123
|
+
id: string,
|
|
124
|
+
data: Partial<AuthUser>,
|
|
125
|
+
): Promise<AuthUser | null> {
|
|
126
|
+
const updates: Record<string, unknown> = { updatedAt: new Date() };
|
|
127
|
+
if (data.email) updates.email = data.email.toLowerCase();
|
|
128
|
+
if (data.role) updates.role = data.role;
|
|
129
|
+
if (data.passwordHash) updates.passwordHash = data.passwordHash;
|
|
130
|
+
|
|
131
|
+
await this.db
|
|
132
|
+
.update(authSchema.users)
|
|
133
|
+
.set(updates)
|
|
134
|
+
.where(eq(authSchema.users.id, id));
|
|
135
|
+
|
|
136
|
+
const [user] = await this.db
|
|
137
|
+
.select()
|
|
138
|
+
.from(authSchema.users)
|
|
139
|
+
.where(eq(authSchema.users.id, id))
|
|
140
|
+
.limit(1);
|
|
141
|
+
if (!user) return null;
|
|
142
|
+
return {
|
|
143
|
+
id: user.id,
|
|
144
|
+
email: user.email,
|
|
145
|
+
role: user.role as UserRole,
|
|
146
|
+
tenantId: user.tenantId || undefined,
|
|
147
|
+
createdAt: user.createdAt.toISOString(),
|
|
148
|
+
updatedAt: user.updatedAt.toISOString(),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async deleteUser(id: string): Promise<boolean> {
|
|
153
|
+
await this.db.delete(authSchema.users).where(eq(authSchema.users.id, id));
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async verifyPassword(password: string, hash: string): Promise<boolean> {
|
|
158
|
+
return bcrypt.compare(password, hash);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async hashPassword(password: string): Promise<string> {
|
|
162
|
+
return bcrypt.hash(password, 10);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async createSession(
|
|
166
|
+
userId: string,
|
|
167
|
+
data?: { ipAddress?: string; userAgent?: string },
|
|
168
|
+
): Promise<Session> {
|
|
169
|
+
const now = new Date();
|
|
170
|
+
const expiresAt = new Date(now.getTime() + this.sessionTTL * 1000);
|
|
171
|
+
const token = randomBytes(32).toString("hex");
|
|
172
|
+
const refreshToken = randomBytes(32).toString("hex");
|
|
173
|
+
const id = this.generateId();
|
|
174
|
+
|
|
175
|
+
await this.db.insert(authSchema.sessions).values({
|
|
176
|
+
id,
|
|
177
|
+
token,
|
|
178
|
+
refreshToken,
|
|
179
|
+
userId,
|
|
180
|
+
expiresAt,
|
|
181
|
+
createdAt: now,
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
id,
|
|
186
|
+
token,
|
|
187
|
+
refreshToken: refreshToken || undefined,
|
|
188
|
+
userId,
|
|
189
|
+
expiresAt: expiresAt.toISOString(),
|
|
190
|
+
createdAt: now.toISOString(),
|
|
191
|
+
ipAddress: data?.ipAddress,
|
|
192
|
+
userAgent: data?.userAgent,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async findSessionByToken(token: string): Promise<Session | null> {
|
|
197
|
+
const [session] = await this.db
|
|
198
|
+
.select()
|
|
199
|
+
.from(authSchema.sessions)
|
|
200
|
+
.where(eq(authSchema.sessions.token, token))
|
|
201
|
+
.limit(1);
|
|
202
|
+
|
|
203
|
+
if (!session || new Date(session.expiresAt) < new Date()) return null;
|
|
204
|
+
|
|
205
|
+
return {
|
|
206
|
+
id: session.id,
|
|
207
|
+
token: session.token,
|
|
208
|
+
refreshToken: session.refreshToken || undefined,
|
|
209
|
+
userId: session.userId,
|
|
210
|
+
expiresAt: session.expiresAt.toISOString(),
|
|
211
|
+
createdAt: session.createdAt.toISOString(),
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async deleteSession(sessionId: string): Promise<boolean> {
|
|
216
|
+
await this.db
|
|
217
|
+
.delete(authSchema.sessions)
|
|
218
|
+
.where(eq(authSchema.sessions.token, sessionId));
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async deleteUserSessions(userId: string): Promise<number> {
|
|
223
|
+
await this.db
|
|
224
|
+
.delete(authSchema.sessions)
|
|
225
|
+
.where(eq(authSchema.sessions.userId, userId));
|
|
226
|
+
return 1;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async hasAnyUsers(): Promise<boolean> {
|
|
230
|
+
const [result] = await this.db
|
|
231
|
+
.select({ count: sql<number>`count(*)` })
|
|
232
|
+
.from(authSchema.users);
|
|
233
|
+
return Number(result?.count || 0) > 0;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async findAuditLogs(
|
|
237
|
+
filter: AuditLogFilter,
|
|
238
|
+
): Promise<{ logs: AuditLog[]; total: number }> {
|
|
239
|
+
const {
|
|
240
|
+
limit = 50,
|
|
241
|
+
offset = 0,
|
|
242
|
+
userId,
|
|
243
|
+
action,
|
|
244
|
+
success,
|
|
245
|
+
startDate,
|
|
246
|
+
endDate,
|
|
247
|
+
} = filter;
|
|
248
|
+
|
|
249
|
+
const conditions = [];
|
|
250
|
+
if (userId) conditions.push(eq(authSchema.auditLogs.userId, userId));
|
|
251
|
+
if (action) {
|
|
252
|
+
if (Array.isArray(action)) {
|
|
253
|
+
conditions.push(
|
|
254
|
+
sql`${authSchema.auditLogs.action} IN (${action.join(",")})`,
|
|
255
|
+
);
|
|
256
|
+
} else {
|
|
257
|
+
conditions.push(eq(authSchema.auditLogs.action, action));
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (success !== undefined)
|
|
261
|
+
conditions.push(eq(authSchema.auditLogs.success, success));
|
|
262
|
+
if (startDate)
|
|
263
|
+
conditions.push(sql`${authSchema.auditLogs.createdAt} >= ${startDate}`);
|
|
264
|
+
if (endDate)
|
|
265
|
+
conditions.push(sql`${authSchema.auditLogs.createdAt} <= ${endDate}`);
|
|
266
|
+
|
|
267
|
+
const whereClause = conditions.length > 0 ? and(...conditions) : undefined;
|
|
268
|
+
|
|
269
|
+
const countResult = await this.db
|
|
270
|
+
.select({ count: sql<number>`count(*)` })
|
|
271
|
+
.from(authSchema.auditLogs)
|
|
272
|
+
.where(whereClause);
|
|
273
|
+
|
|
274
|
+
const logs = await this.db
|
|
275
|
+
.select()
|
|
276
|
+
.from(authSchema.auditLogs)
|
|
277
|
+
.where(whereClause)
|
|
278
|
+
.orderBy(desc(authSchema.auditLogs.createdAt))
|
|
279
|
+
.limit(limit)
|
|
280
|
+
.offset(offset);
|
|
281
|
+
|
|
282
|
+
return {
|
|
283
|
+
logs: logs.map((log) => ({
|
|
284
|
+
id: log.id,
|
|
285
|
+
timestamp: log.createdAt,
|
|
286
|
+
action: log.action as AuditLog["action"],
|
|
287
|
+
userId: log.userId || undefined,
|
|
288
|
+
userEmail: log.userEmail || undefined,
|
|
289
|
+
role: log.role || undefined,
|
|
290
|
+
resource: log.resource,
|
|
291
|
+
ipAddress: log.ipAddress || undefined,
|
|
292
|
+
userAgent: log.userAgent || undefined,
|
|
293
|
+
success: log.success,
|
|
294
|
+
error: log.error || undefined,
|
|
295
|
+
metadata: log.metadata || undefined,
|
|
296
|
+
})),
|
|
297
|
+
total: Number(countResult[0]?.count || 0),
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
async createAuditLog(
|
|
302
|
+
data: Omit<AuditLog, "id" | "timestamp">,
|
|
303
|
+
): Promise<AuditLog> {
|
|
304
|
+
const id = this.generateId();
|
|
305
|
+
const timestamp = new Date();
|
|
306
|
+
|
|
307
|
+
await this.db.insert(authSchema.auditLogs).values({
|
|
308
|
+
id,
|
|
309
|
+
action: data.action,
|
|
310
|
+
userId: data.userId,
|
|
311
|
+
userEmail: data.userEmail,
|
|
312
|
+
role: data.role,
|
|
313
|
+
resource: data.resource,
|
|
314
|
+
ipAddress: data.ipAddress,
|
|
315
|
+
userAgent: data.userAgent,
|
|
316
|
+
success: data.success,
|
|
317
|
+
error: data.error,
|
|
318
|
+
metadata: data.metadata,
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
return {
|
|
322
|
+
...data,
|
|
323
|
+
id,
|
|
324
|
+
timestamp,
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
}
|