openadminjs 0.1.24 → 0.3.0-rc.1
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 +117 -81
- package/dist/adapt-package-manager.d.ts +3 -0
- package/dist/adapt-package-manager.js +118 -0
- package/dist/create-project.d.ts +77 -2
- package/dist/create-project.js +685 -224
- package/dist/db-commands.d.ts +20 -0
- package/dist/db-commands.js +75 -0
- package/dist/detect-package-manager.d.ts +8 -0
- package/dist/detect-package-manager.js +37 -0
- package/dist/doctor.d.ts +26 -0
- package/dist/doctor.js +232 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +476 -166
- package/dist/render-schema.d.ts +3 -0
- package/dist/render-schema.js +33 -0
- package/dist/resource-slug.d.ts +1 -0
- package/dist/resource-slug.js +7 -0
- package/dist/runtime-mode.d.ts +16 -0
- package/dist/runtime-mode.js +138 -0
- package/dist/secrets.d.ts +24 -0
- package/dist/secrets.js +93 -0
- package/dist/security.d.ts +13 -0
- package/dist/security.js +168 -0
- package/dist/upgrade.d.ts +10 -0
- package/dist/upgrade.js +133 -0
- package/dist/validate-name.d.ts +33 -0
- package/dist/validate-name.js +206 -0
- package/package.json +39 -11
- package/template/.dockerignore +15 -0
- package/template/.env.production.example +5 -0
- package/template/Dockerfile +12 -0
- package/template/README.md +23 -3
- package/template/apps/admin/app/api/auth/login/route.ts +9 -9
- package/template/apps/admin/app/api/auth/logout/route.ts +11 -3
- package/template/apps/admin/app/api/auth/oidc/callback/route.ts +19 -0
- package/template/apps/admin/app/api/auth/oidc/start/route.ts +11 -0
- package/template/apps/admin/app/api/auth/oidc/status/route.ts +6 -0
- package/template/apps/admin/app/api/auth/refresh/route.ts +18 -11
- package/template/apps/admin/app/api/backend/[...path]/route.ts +48 -0
- package/template/apps/admin/app/api-tokens/page.tsx +39 -39
- package/template/apps/admin/app/assistant/page.tsx +11 -0
- package/template/apps/admin/app/dashboard/page.tsx +45 -462
- package/template/apps/admin/app/globals.css +52 -16
- package/template/apps/admin/app/layout.tsx +1 -1
- package/template/apps/admin/app/login/page.tsx +13 -145
- package/template/apps/admin/app/plugins/page.tsx +16 -402
- package/template/apps/admin/app/profile/page.tsx +5 -2
- package/template/apps/admin/app/settings/page.tsx +14 -265
- package/template/apps/admin/components/ai-assistant-widget.tsx +3 -6
- package/template/apps/admin/components/app-shell.tsx +25 -373
- package/template/apps/admin/components/auth-guard.tsx +8 -28
- package/template/apps/admin/components/logo.tsx +1 -30
- package/template/apps/admin/components/resource-form.tsx +3 -3
- package/template/apps/admin/components/resource-table.tsx +12 -8
- package/template/apps/admin/lib/api.ts +45 -107
- package/template/apps/admin/lib/resource-table-prefs.ts +1 -57
- package/template/apps/admin/lib/server-api.ts +7 -0
- package/template/apps/admin/lib/tokens.ts +9 -81
- package/template/apps/admin/middleware.ts +9 -50
- package/template/apps/admin/next.config.mjs +4 -0
- package/template/apps/admin/package.json +2 -1
- package/template/apps/admin/public/brand/favicon.svg +4 -1
- package/template/apps/admin/tailwind.config.ts +9 -8
- package/template/apps/api/package.json +13 -6
- package/template/apps/api/src/access/catalog.ts +4 -0
- package/template/apps/api/src/admin/admin-ai.controller.ts +15 -125
- package/template/apps/api/src/admin/admin-analytics.service.ts +16 -148
- package/template/apps/api/src/admin/admin-logs.controller.ts +4 -1
- package/template/apps/api/src/admin/admin-plugins.controller.ts +8 -8
- package/template/apps/api/src/admin/admin-plugins.service.ts +15 -259
- package/template/apps/api/src/admin/admin-resource.service.ts +105 -254
- package/template/apps/api/src/admin/admin.controller.ts +17 -4
- package/template/apps/api/src/admin/admin.module.ts +2 -2
- package/template/apps/api/src/admin/resource-policy.ts +1 -0
- package/template/apps/api/src/admin/resource-query.ts +1 -0
- package/template/apps/api/src/admin/safe-ai.service.ts +43 -0
- package/template/apps/api/src/admin/validate-resource-payload.ts +2 -0
- package/template/apps/api/src/auth/auth.controller.ts +4 -2
- package/template/apps/api/src/auth/auth.service.ts +68 -83
- package/template/apps/api/src/auth/sso-oidc.controller.ts +12 -74
- package/template/apps/api/src/auth/sso-oidc.service.ts +51 -268
- package/template/apps/api/src/common/api-exception.filter.ts +1 -0
- package/template/apps/api/src/common/auth.guard.ts +66 -39
- package/template/apps/api/src/common/json-field-codec.ts +138 -0
- package/template/apps/api/src/common/prisma.service.ts +12 -7
- package/template/apps/api/src/common/request-logging.interceptor.ts +8 -3
- package/template/apps/api/src/graphql/graphql.module.ts +14 -1
- package/template/apps/api/src/health/health.controller.ts +6 -0
- package/template/apps/api/src/main.ts +11 -2
- package/template/apps/api/src/plugins/plugin-loader.ts +5 -3
- package/template/apps/api/src/resources/commerce.resource.ts +3 -34
- package/template/apps/api/src/resources/operations.resource.ts +1 -0
- package/template/apps/api/src/store/payment/payment.controller.ts +15 -92
- package/template/apps/api/src/store/payment/payment.module.ts +2 -3
- package/template/apps/api/src/store/payment/payment.service.ts +121 -227
- package/template/apps/api/webpack.config.cjs +2 -1
- package/template/apps/web/next.config.mjs +4 -0
- package/template/apps/web/public/brand/favicon.svg +4 -1
- package/template/compose.yaml +56 -0
- package/template/examples/crm-admin/README.md +31 -0
- package/template/examples/crm-admin/package.json +17 -0
- package/template/examples/crm-admin/prisma/models.fragment.prisma +50 -0
- package/template/examples/crm-admin/src/index.ts +7 -0
- package/template/examples/crm-admin/src/resources/activities.resource.ts +29 -0
- package/template/examples/crm-admin/src/resources/companies.resource.ts +21 -0
- package/template/examples/crm-admin/src/resources/contacts.resource.ts +24 -0
- package/template/examples/crm-admin/src/resources/deals.resource.ts +30 -0
- package/template/examples/crm-admin/tsconfig.json +7 -0
- package/template/package.json +8 -1
- package/template/packages/admin-client/package.json +18 -0
- package/template/packages/admin-client/src/contracts.ts +29 -0
- package/template/packages/admin-client/src/index.ts +2 -0
- package/template/packages/admin-client/src/preferences.ts +57 -0
- package/template/packages/admin-client/tsconfig.json +12 -0
- package/template/packages/auth/package.json +1 -1
- package/template/packages/brand/site/assets/brand/favicon.svg +4 -1
- package/template/packages/brand/src/generate-assets.mjs +17 -186
- package/template/packages/core/package.json +1 -1
- package/template/packages/mail/package.json +1 -1
- package/template/packages/nest/package.json +22 -0
- package/template/packages/nest/src/api-exception.filter.ts +23 -0
- package/template/packages/nest/src/index.ts +3 -0
- package/template/packages/nest/src/resource-policy.ts +64 -0
- package/template/packages/nest/src/resource-query.ts +103 -0
- package/template/packages/nest/tsconfig.json +12 -0
- package/template/packages/notifications/package.json +1 -1
- package/template/packages/payments/package.json +1 -1
- package/template/packages/permissions/package.json +1 -1
- package/template/packages/permissions/src/index.ts +2 -2
- package/template/packages/plugin-sdk/package.json +1 -1
- package/template/packages/plugin-sdk/src/index.ts +2 -2
- package/template/packages/prisma/package.json +21 -0
- package/template/packages/prisma/schema/ai.prisma +36 -0
- package/template/packages/prisma/schema/audit.prisma +13 -0
- package/template/packages/prisma/schema/commerce.prisma +79 -0
- package/template/packages/prisma/schema/content.prisma +27 -0
- package/template/packages/prisma/schema/core.prisma +106 -0
- package/template/packages/prisma/schema/header.prisma +8 -0
- package/template/packages/prisma/schema/jobs.prisma +9 -0
- package/template/packages/prisma/schema/media.prisma +9 -0
- package/template/packages/prisma/schema/modules.json +47 -0
- package/template/packages/prisma/schema/notifications.prisma +9 -0
- package/template/packages/prisma/schema/settings.prisma +8 -0
- package/template/packages/prisma/schema/tokens.prisma +10 -0
- package/template/packages/prisma/src/index.d.ts +12 -0
- package/template/packages/prisma/src/index.mjs +138 -0
- package/template/packages/queue/package.json +1 -1
- package/template/packages/resource/package.json +1 -1
- package/template/packages/resource/src/index.ts +1 -0
- package/template/packages/storage/package.json +1 -1
- package/template/packages/storage/src/index.ts +37 -0
- package/template/prisma/migrations/20260713120000_add_user_oidc_key_and_category_order/migration.sql +11 -0
- package/template/prisma/migrations/20261126130000_add_sessions/migration.sql +26 -0
- package/template/prisma/migrations/20261126140000_api_token_hash/migration.sql +3 -0
- package/template/prisma/migrations/20261126150000_payment_idempotency/migration.sql +6 -0
- package/template/prisma/schema.prisma +82 -54
- package/template/prisma/seed.ts +14 -38
- package/template/runtime-versions.json +15 -0
- package/template/scripts/db-init.mjs +33 -0
- package/template/scripts/install-crm.mjs +50 -0
- package/template/scripts/migrate-existing.mjs +65 -0
- package/template/scripts/schema.mjs +48 -0
- package/template/tsconfig.base.json +23 -5
- package/LICENSE +0 -21
- package/dist/create-project.test.d.ts +0 -1
- package/dist/create-project.test.js +0 -99
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -7
- package/dist/resource-slug.test.d.ts +0 -1
- package/dist/resource-slug.test.js +0 -9
- package/template/apps/admin/.storybook/main.ts +0 -12
- package/template/apps/admin/.storybook/preview.ts +0 -10
- package/template/apps/admin/app/login/page.test.tsx +0 -18
- package/template/apps/admin/components/auth-guard.test.tsx +0 -55
- package/template/apps/admin/components/logo.stories.tsx +0 -24
- package/template/apps/admin/lib/api.test.ts +0 -118
- package/template/apps/admin/lib/auth-flow.e2e.test.ts +0 -79
- package/template/apps/admin/vitest.config.ts +0 -7
- package/template/apps/api/src/access/derive-permissions.test.ts +0 -26
- package/template/apps/api/src/admin/admin-resource.service.test.ts +0 -118
- package/template/apps/api/src/admin/validate-resource-payload.test.ts +0 -44
- package/template/apps/api/src/app.module.test.ts +0 -12
- package/template/apps/api/src/auth/auth.service.test.ts +0 -78
- package/template/apps/api/src/auth/sso-oidc.service.test.ts +0 -84
- package/template/apps/api/src/common/auth.guard.test.ts +0 -62
- package/template/apps/api/src/common/casl-ability.factory.test.ts +0 -33
- package/template/apps/api/src/common/request-logging.interceptor.test.ts +0 -39
- package/template/apps/api/src/graphql/admin-resources.resolver.test.ts +0 -45
- package/template/apps/api/src/graphql/ensure-gql-paramtypes.test.ts +0 -44
- package/template/apps/api/src/graphql/graphql-schema.test.ts +0 -26
- package/template/apps/api/src/plugins/manifest.schema.test.ts +0 -36
- package/template/apps/api/src/plugins/plugin-runtime.test.ts +0 -24
- package/template/apps/api/src/queue/queue.service.test.ts +0 -33
- package/template/apps/api/src/resources/resource-hooks.registry.test.ts +0 -72
- package/template/apps/api/vitest.config.ts +0 -21
- package/template/apps/web/lib/seo.test.ts +0 -41
- package/template/apps/web/vitest.config.ts +0 -19
- package/template/packages/auth/src/index.test.ts +0 -10
- package/template/packages/brand/src/index.test.ts +0 -9
- package/template/packages/core/src/index.test.ts +0 -27
- package/template/packages/core/vitest.config.ts +0 -9
- package/template/packages/mail/src/index.test.ts +0 -23
- package/template/packages/notifications/src/index.test.ts +0 -9
- package/template/packages/permissions/src/blueprints.test.ts +0 -62
- package/template/packages/permissions/src/index.test.ts +0 -34
- package/template/packages/plugin-sdk/src/sdk.test.ts +0 -13
- package/template/packages/plugin-sdk/vitest.config.ts +0 -5
- package/template/packages/queue/src/index.test.ts +0 -12
- package/template/packages/resource/src/index.test.ts +0 -196
- package/template/packages/storage/src/index.test.ts +0 -10
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
export type Permission = `${string}.${string}`;
|
|
2
2
|
|
|
3
|
-
export type { LocalizedRoleLabel, RoleBlueprint } from "./blueprints";
|
|
3
|
+
export type { LocalizedRoleLabel, RoleBlueprint } from "./blueprints.js";
|
|
4
4
|
export {
|
|
5
5
|
DEFAULT_AUTH_REALM,
|
|
6
6
|
expandRolePermissions,
|
|
7
7
|
materializeAllRoles,
|
|
8
8
|
realmOf,
|
|
9
9
|
roleMatrixKey
|
|
10
|
-
} from "./blueprints";
|
|
10
|
+
} from "./blueprints.js";
|
|
11
11
|
|
|
12
12
|
export function can(permissions: readonly string[], permission: string): boolean {
|
|
13
13
|
return permissions.includes(permission) || permissions.includes("*");
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { PluginCapability, PluginSurfaceRegistry, ResourceLifecycleHooks } from "./hooks";
|
|
1
|
+
import type { PluginCapability, PluginSurfaceRegistry, ResourceLifecycleHooks } from "./hooks.js";
|
|
2
2
|
|
|
3
|
-
export type * from "./hooks";
|
|
3
|
+
export type * from "./hooks.js";
|
|
4
4
|
|
|
5
5
|
export type PluginRegistrationContext<Config extends Record<string, unknown> = Record<string, unknown>> = {
|
|
6
6
|
readonly pluginId: string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openadminjs/prisma",
|
|
3
|
+
"version": "0.2.0-rc.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "./src/index.mjs",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.d.ts",
|
|
11
|
+
"import": "./src/index.mjs",
|
|
12
|
+
"default": "./src/index.mjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "node --check src/index.mjs",
|
|
17
|
+
"lint": "node --check src/index.mjs",
|
|
18
|
+
"typecheck": "node --check src/index.mjs",
|
|
19
|
+
"test": "node --test test/*.test.mjs"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
model AiConversation {
|
|
2
|
+
id String @id @default(cuid())
|
|
3
|
+
userId String
|
|
4
|
+
title String?
|
|
5
|
+
createdAt DateTime @default(now())
|
|
6
|
+
updatedAt DateTime @updatedAt
|
|
7
|
+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
|
8
|
+
messages AiMessage[]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
model AiMessage {
|
|
12
|
+
id String @id @default(cuid())
|
|
13
|
+
conversationId String
|
|
14
|
+
role String
|
|
15
|
+
content String
|
|
16
|
+
meta Json?
|
|
17
|
+
createdAt DateTime @default(now())
|
|
18
|
+
conversation AiConversation @relation(fields: [conversationId], references: [id], onDelete: Cascade)
|
|
19
|
+
|
|
20
|
+
@@index([conversationId, createdAt])
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
model AiArtifact {
|
|
24
|
+
id String @id @default(cuid())
|
|
25
|
+
userId String
|
|
26
|
+
conversationId String?
|
|
27
|
+
filename String
|
|
28
|
+
mimeType String
|
|
29
|
+
size Int
|
|
30
|
+
contentBase64 String
|
|
31
|
+
meta Json?
|
|
32
|
+
createdAt DateTime @default(now())
|
|
33
|
+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
|
34
|
+
|
|
35
|
+
@@index([userId, createdAt])
|
|
36
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
model AuditLog {
|
|
2
|
+
id String @id @default(cuid())
|
|
3
|
+
userId String?
|
|
4
|
+
resource String?
|
|
5
|
+
resourceId String?
|
|
6
|
+
action String
|
|
7
|
+
before Json?
|
|
8
|
+
after Json?
|
|
9
|
+
ipAddress String?
|
|
10
|
+
userAgent String?
|
|
11
|
+
createdAt DateTime @default(now())
|
|
12
|
+
user User? @relation(fields: [userId], references: [id])
|
|
13
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
model Product {
|
|
2
|
+
id String @id @default(cuid())
|
|
3
|
+
name String
|
|
4
|
+
slug String @unique
|
|
5
|
+
description String?
|
|
6
|
+
/** Price in smallest currency unit (e.g. cents for USD) */
|
|
7
|
+
price Int
|
|
8
|
+
currency String @default("USD")
|
|
9
|
+
active Boolean @default(true)
|
|
10
|
+
imageUrl String?
|
|
11
|
+
metadata Json?
|
|
12
|
+
createdAt DateTime @default(now())
|
|
13
|
+
updatedAt DateTime @updatedAt
|
|
14
|
+
orderItems OrderItem[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
model Order {
|
|
18
|
+
id String @id @default(cuid())
|
|
19
|
+
/** Null for guest checkouts */
|
|
20
|
+
userId String?
|
|
21
|
+
email String?
|
|
22
|
+
/** pending | paid | failed | refunded | cancelled */
|
|
23
|
+
status String @default("pending")
|
|
24
|
+
currency String @default("USD")
|
|
25
|
+
/** Total in smallest currency unit */
|
|
26
|
+
total Int
|
|
27
|
+
notes String?
|
|
28
|
+
checkoutKey String? @unique
|
|
29
|
+
checkoutRequestHash String?
|
|
30
|
+
checkoutSessionId String? @unique
|
|
31
|
+
paymentIntentId String? @unique
|
|
32
|
+
metadata Json?
|
|
33
|
+
createdAt DateTime @default(now())
|
|
34
|
+
updatedAt DateTime @updatedAt
|
|
35
|
+
user User? @relation(fields: [userId], references: [id])
|
|
36
|
+
items OrderItem[]
|
|
37
|
+
transactions Transaction[]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
model OrderItem {
|
|
41
|
+
id String @id @default(cuid())
|
|
42
|
+
orderId String
|
|
43
|
+
productId String?
|
|
44
|
+
name String
|
|
45
|
+
/** Unit price at time of purchase */
|
|
46
|
+
price Int
|
|
47
|
+
quantity Int @default(1)
|
|
48
|
+
order Order @relation(fields: [orderId], references: [id], onDelete: Cascade)
|
|
49
|
+
product Product? @relation(fields: [productId], references: [id], onDelete: SetNull)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
model Transaction {
|
|
53
|
+
id String @id @default(cuid())
|
|
54
|
+
orderId String
|
|
55
|
+
/** Payment provider slug: stripe | paypal | mock */
|
|
56
|
+
provider String
|
|
57
|
+
/** Provider-side ID (Stripe PaymentIntent ID, etc.) */
|
|
58
|
+
providerTxId String?
|
|
59
|
+
/** pending | succeeded | failed | refunded */
|
|
60
|
+
status String @default("pending")
|
|
61
|
+
/** Amount in smallest currency unit */
|
|
62
|
+
amount Int
|
|
63
|
+
currency String @default("USD")
|
|
64
|
+
metadata Json?
|
|
65
|
+
createdAt DateTime @default(now())
|
|
66
|
+
order Order @relation(fields: [orderId], references: [id])
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
model WebhookLog {
|
|
70
|
+
id String @id @default(cuid())
|
|
71
|
+
provider String
|
|
72
|
+
eventType String
|
|
73
|
+
providerEventId String? @unique
|
|
74
|
+
payload Json
|
|
75
|
+
/** pending | processed | failed */
|
|
76
|
+
status String @default("pending")
|
|
77
|
+
error String?
|
|
78
|
+
createdAt DateTime @default(now())
|
|
79
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
model Category {
|
|
2
|
+
id String @id @default(cuid())
|
|
3
|
+
name String
|
|
4
|
+
slug String @unique
|
|
5
|
+
order Int @default(0)
|
|
6
|
+
createdAt DateTime @default(now())
|
|
7
|
+
updatedAt DateTime @updatedAt
|
|
8
|
+
posts Post[]
|
|
9
|
+
|
|
10
|
+
@@index([order])
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
model Post {
|
|
14
|
+
id String @id @default(cuid())
|
|
15
|
+
title String
|
|
16
|
+
slug String @unique
|
|
17
|
+
status String @default("draft")
|
|
18
|
+
content String?
|
|
19
|
+
categoryId String?
|
|
20
|
+
coverImageId String?
|
|
21
|
+
authorId String?
|
|
22
|
+
createdAt DateTime @default(now())
|
|
23
|
+
updatedAt DateTime @updatedAt
|
|
24
|
+
category Category? @relation(fields: [categoryId], references: [id])
|
|
25
|
+
coverImage FileAsset? @relation(fields: [coverImageId], references: [id])
|
|
26
|
+
author User? @relation(fields: [authorId], references: [id])
|
|
27
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
model User {
|
|
2
|
+
id String @id @default(cuid())
|
|
3
|
+
email String @unique
|
|
4
|
+
passwordHash String
|
|
5
|
+
/** Stable IdP subject, format `issuerNormalized::sub` — set when user signs in via OIDC. */
|
|
6
|
+
oidcKey String? @unique
|
|
7
|
+
/// Legacy refresh hash; invalidated by session migration.
|
|
8
|
+
refreshTokenHash String?
|
|
9
|
+
sessions Session[]
|
|
10
|
+
name String?
|
|
11
|
+
avatarUrl String?
|
|
12
|
+
status String @default("active")
|
|
13
|
+
createdAt DateTime @default(now())
|
|
14
|
+
updatedAt DateTime @updatedAt
|
|
15
|
+
roles UserRole[]
|
|
16
|
+
posts Post[]
|
|
17
|
+
auditLogs AuditLog[]
|
|
18
|
+
apiTokens ApiToken[]
|
|
19
|
+
notifications Notification[]
|
|
20
|
+
orders Order[]
|
|
21
|
+
passwordResetTokens PasswordResetToken[]
|
|
22
|
+
emailVerificationTokens EmailVerificationToken[]
|
|
23
|
+
aiConversations AiConversation[]
|
|
24
|
+
aiArtifacts AiArtifact[]
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
model Role {
|
|
28
|
+
id String @id @default(cuid())
|
|
29
|
+
/** RBAC partition: e.g. "admin" (back-office), "public" (storefront). Add more realms freely. */
|
|
30
|
+
realm String @default("admin")
|
|
31
|
+
name String
|
|
32
|
+
label String
|
|
33
|
+
users UserRole[]
|
|
34
|
+
permissions RolePermission[]
|
|
35
|
+
|
|
36
|
+
@@unique([name, realm])
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
model Permission {
|
|
40
|
+
id String @id @default(cuid())
|
|
41
|
+
name String @unique
|
|
42
|
+
label String
|
|
43
|
+
roles RolePermission[]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
model UserRole {
|
|
47
|
+
userId String
|
|
48
|
+
roleId String
|
|
49
|
+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
|
50
|
+
role Role @relation(fields: [roleId], references: [id], onDelete: Cascade)
|
|
51
|
+
|
|
52
|
+
@@id([userId, roleId])
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
model RolePermission {
|
|
56
|
+
roleId String
|
|
57
|
+
permissionId String
|
|
58
|
+
role Role @relation(fields: [roleId], references: [id], onDelete: Cascade)
|
|
59
|
+
permission Permission @relation(fields: [permissionId], references: [id], onDelete: Cascade)
|
|
60
|
+
|
|
61
|
+
@@id([roleId, permissionId])
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
model PasswordResetToken {
|
|
65
|
+
id String @id @default(cuid())
|
|
66
|
+
tokenHash String
|
|
67
|
+
expiresAt DateTime
|
|
68
|
+
usedAt DateTime?
|
|
69
|
+
userId String
|
|
70
|
+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
model EmailVerificationToken {
|
|
74
|
+
id String @id @default(cuid())
|
|
75
|
+
tokenHash String
|
|
76
|
+
expiresAt DateTime
|
|
77
|
+
usedAt DateTime?
|
|
78
|
+
userId String
|
|
79
|
+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
model Session {
|
|
83
|
+
id String @id @default(cuid())
|
|
84
|
+
userId String
|
|
85
|
+
realm String
|
|
86
|
+
expiresAt DateTime
|
|
87
|
+
revokedAt DateTime?
|
|
88
|
+
createdAt DateTime @default(now())
|
|
89
|
+
lastUsedAt DateTime @default(now())
|
|
90
|
+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
|
91
|
+
tokens SessionToken[]
|
|
92
|
+
|
|
93
|
+
@@index([userId, realm])
|
|
94
|
+
@@index([expiresAt])
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
model SessionToken {
|
|
98
|
+
id String @id @default(cuid())
|
|
99
|
+
sessionId String
|
|
100
|
+
tokenHash String @unique
|
|
101
|
+
usedAt DateTime?
|
|
102
|
+
createdAt DateTime @default(now())
|
|
103
|
+
session Session @relation(fields: [sessionId], references: [id], onDelete: Cascade)
|
|
104
|
+
|
|
105
|
+
@@index([sessionId])
|
|
106
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"modules": {
|
|
3
|
+
"core": [],
|
|
4
|
+
"audit": [
|
|
5
|
+
"core"
|
|
6
|
+
],
|
|
7
|
+
"settings": [
|
|
8
|
+
"core"
|
|
9
|
+
],
|
|
10
|
+
"media": [
|
|
11
|
+
"core"
|
|
12
|
+
],
|
|
13
|
+
"content": [
|
|
14
|
+
"core",
|
|
15
|
+
"media"
|
|
16
|
+
],
|
|
17
|
+
"tokens": [
|
|
18
|
+
"core"
|
|
19
|
+
],
|
|
20
|
+
"notifications": [
|
|
21
|
+
"core"
|
|
22
|
+
],
|
|
23
|
+
"jobs": [
|
|
24
|
+
"core"
|
|
25
|
+
],
|
|
26
|
+
"commerce": [
|
|
27
|
+
"core"
|
|
28
|
+
],
|
|
29
|
+
"ai": [
|
|
30
|
+
"core"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"optionalRelations": {
|
|
34
|
+
"User": {
|
|
35
|
+
"posts": "content",
|
|
36
|
+
"auditLogs": "audit",
|
|
37
|
+
"apiTokens": "tokens",
|
|
38
|
+
"notifications": "notifications",
|
|
39
|
+
"orders": "commerce",
|
|
40
|
+
"aiConversations": "ai",
|
|
41
|
+
"aiArtifacts": "ai"
|
|
42
|
+
},
|
|
43
|
+
"FileAsset": {
|
|
44
|
+
"posts": "content"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type Provider="postgresql"|"mysql"|"sqlite";
|
|
2
|
+
export type SchemaModule="core"|"audit"|"settings"|"media"|"content"|"tokens"|"notifications"|"jobs"|"commerce"|"ai";
|
|
3
|
+
export const moduleNames:readonly SchemaModule[];
|
|
4
|
+
export const providers:readonly Provider[];
|
|
5
|
+
export function resolveModules(requested?:readonly SchemaModule[]):SchemaModule[];
|
|
6
|
+
export function renderProvider(schema:string,provider:Provider):string;
|
|
7
|
+
export function composeSchema(options?:{provider?:Provider;modules?:readonly SchemaModule[];customFragments?:readonly string[]}):string;
|
|
8
|
+
export function splitSchema(schema:string):{name:string;content:string}[];
|
|
9
|
+
export const legacySqliteJsonFields:Readonly<Record<string,readonly string[]>>;
|
|
10
|
+
export function legacySqliteNormalizationSql():string;
|
|
11
|
+
export function mergeLegacySchema(schema:string,provider:Provider):{schema:string;changes:string[];normalizations:{table:string;field:string}[];clearLegacyRefresh:boolean;revokeLegacyKeys:boolean};
|
|
12
|
+
export function schemaProvider(schema:string):string;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {readFileSync} from "node:fs";
|
|
2
|
+
const load=name=>readFileSync(new URL("../schema/"+name,import.meta.url),"utf8");
|
|
3
|
+
const spec=JSON.parse(load("modules.json"));
|
|
4
|
+
export const moduleNames=Object.freeze(Object.keys(spec.modules));
|
|
5
|
+
export const providers=Object.freeze(["postgresql","mysql","sqlite"]);
|
|
6
|
+
export function resolveModules(requested=moduleNames){
|
|
7
|
+
if(!Array.isArray(requested))throw Error("Modules must be an array");
|
|
8
|
+
const enabled=new Set();
|
|
9
|
+
function add(name){if(!Object.hasOwn(spec.modules,name))throw Error("Unknown schema module: "+name);if(enabled.has(name))return;enabled.add(name);for(const dep of spec.modules[name])add(dep);}
|
|
10
|
+
add("core");for(const name of requested)add(name);
|
|
11
|
+
return moduleNames.filter(name=>enabled.has(name));
|
|
12
|
+
}
|
|
13
|
+
// Locate the provider in a single pass over formatted Prisma lines. Do not
|
|
14
|
+
// use an unbounded, unanchored regex over library-supplied schema text.
|
|
15
|
+
function locateProvider(schema){
|
|
16
|
+
if(typeof schema!=="string")throw Error("Schema must be a string");
|
|
17
|
+
let inside=false,offset=0,found;
|
|
18
|
+
for(const line of schema.split(/(\r?\n)/)){
|
|
19
|
+
if(/^[ \t]*datasource[ \t]+[A-Za-z_]\w*[ \t]*\{[ \t]*(?:\/\/.*)?$/.test(line))inside=true;
|
|
20
|
+
else if(inside&&/^[ \t]*\}/.test(line))inside=false;
|
|
21
|
+
else if(inside){
|
|
22
|
+
const m=line.match(/^[ \t]*provider[ \t]*=[ \t]*"([^"\r\n]+)"[ \t]*(?:\/\/.*)?$/);
|
|
23
|
+
if(m){
|
|
24
|
+
if(found)throw Error("Multiple datasource providers are not supported");
|
|
25
|
+
const start=offset+line.indexOf('"')+1;
|
|
26
|
+
found={provider:m[1],start,end:start+m[1].length};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
offset+=line.length;
|
|
30
|
+
}
|
|
31
|
+
if(!found)throw Error("Format schema with a datasource provider on its own line");
|
|
32
|
+
return found;
|
|
33
|
+
}
|
|
34
|
+
export function schemaProvider(schema){return locateProvider(schema).provider;}
|
|
35
|
+
const scalars=new Set(["String","Boolean","Int","BigInt","Float","Decimal","DateTime","Bytes","Json"]);
|
|
36
|
+
export function renderProvider(schema,provider){
|
|
37
|
+
if(!providers.includes(provider))throw Error("Unsupported provider: "+provider);
|
|
38
|
+
const location=locateProvider(schema);
|
|
39
|
+
let result=schema.slice(0,location.start)+provider+schema.slice(location.end);
|
|
40
|
+
if(provider==="postgresql")return result;
|
|
41
|
+
let model=false,comment=false;
|
|
42
|
+
return result.split(/(\r?\n)/).map(line=>{
|
|
43
|
+
if(/^\s*\/\*/.test(line))comment=true;
|
|
44
|
+
if(comment){if(line.includes("*/"))comment=false;return line;}
|
|
45
|
+
if(/^\s*(model|type)\s+\w+\s*\{/.test(line)){model=true;return line;}
|
|
46
|
+
if(/^\s*}\s*(?:\/\/.*)?$/.test(line)){model=false;return line;}
|
|
47
|
+
if(!model)return line;
|
|
48
|
+
return line.replace(/^(\s*\w+\s+)(\w+)\[\](?=\s|$)/,(match,prefix,type)=>scalars.has(type)?prefix+"Json":match);
|
|
49
|
+
}).join("");
|
|
50
|
+
}
|
|
51
|
+
/** Built-in feature fragments, with explicit inverse relations for disabled features. */
|
|
52
|
+
export function composeSchema({provider="postgresql",modules=moduleNames,customFragments=[]}={}){
|
|
53
|
+
const enabled=resolveModules(modules),names=new Set(enabled);
|
|
54
|
+
const chunks=[load("header.prisma").trim()];
|
|
55
|
+
for(const name of enabled){
|
|
56
|
+
let model="";
|
|
57
|
+
const content=load(name+".prisma").trim().split("\n").filter(line=>{
|
|
58
|
+
const start=line.match(/^model\s+(\w+)/);if(start)model=start[1];
|
|
59
|
+
const field=line.match(/^\s+(\w+)\s+/)?.[1];
|
|
60
|
+
const feature=field&&spec.optionalRelations[model]?.[field];
|
|
61
|
+
return !feature||names.has(feature);
|
|
62
|
+
}).join("\n");
|
|
63
|
+
chunks.push(content);
|
|
64
|
+
}
|
|
65
|
+
for(const fragment of customFragments){
|
|
66
|
+
if(typeof fragment!=="string"||/^\s*(generator|datasource)\s+/m.test(fragment))throw Error("Custom fragments cannot redefine generator or datasource");
|
|
67
|
+
if(fragment.trim())chunks.push(fragment.trim());
|
|
68
|
+
}
|
|
69
|
+
const schema=chunks.join("\n\n")+"\n";
|
|
70
|
+
const declarations=new Set();
|
|
71
|
+
for(const m of schema.matchAll(/^\s*(model|enum|type|view)\s+(\w+)\s*\{/gm)){
|
|
72
|
+
if(declarations.has(m[2]))throw Error("Duplicate schema declaration: "+m[2]);declarations.add(m[2]);
|
|
73
|
+
}
|
|
74
|
+
return renderProvider(schema,provider);
|
|
75
|
+
}
|
|
76
|
+
/** Lossless adoption of an existing monolithic schema; custom declarations are never replaced. */
|
|
77
|
+
export function splitSchema(schema){
|
|
78
|
+
if(typeof schema!=="string"||!schema.trim())throw Error("Empty schema");
|
|
79
|
+
const starts=[...schema.matchAll(/^(generator|datasource|model|enum|type|view)\s+(\w+)\s*\{/gm)];
|
|
80
|
+
if(!starts.length)throw Error("No supported Prisma declarations");
|
|
81
|
+
const chunks=[];
|
|
82
|
+
if(starts[0].index>0)chunks.push({name:"000-preamble",content:schema.slice(0,starts[0].index)});
|
|
83
|
+
for(let i=0;i<starts.length;i++){
|
|
84
|
+
const m=starts[i],end=starts[i+1]?.index??schema.length;
|
|
85
|
+
chunks.push({name:String(i+1).padStart(3,"0")+"-"+m[1]+"-"+m[2],content:schema.slice(m.index,end)});
|
|
86
|
+
}
|
|
87
|
+
if(chunks.map(x=>x.content).join("")!==schema)throw Error("Schema split is not lossless");
|
|
88
|
+
return chunks;
|
|
89
|
+
}
|
|
90
|
+
export const legacySqliteJsonFields=Object.freeze({AuditLog:["before","after"],Setting:["value"],ApiToken:["scopes"],JobLog:["payload"],Product:["metadata"],Order:["metadata"],Transaction:["metadata"],WebhookLog:["payload"],AiMessage:["meta"],AiArtifact:["meta"]});
|
|
91
|
+
/** Legacy codec parsed valid JSON and returned invalid JSON text verbatim. Preserve that meaning. */
|
|
92
|
+
export function legacySqliteNormalizationSql(){
|
|
93
|
+
return Object.entries(legacySqliteJsonFields).flatMap(([table,fields])=>fields.map(field=>'UPDATE "'+table+'" SET "'+field+'" = json_quote("'+field+'") WHERE "'+field+'" IS NOT NULL AND NOT json_valid("'+field+'");')).join("\n")+"\n";
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Prepare an upgrade target from the existing schema, keeping custom models/fields.
|
|
97
|
+
* Only known type changes are applied. Incompatible customizations fail closed.
|
|
98
|
+
* No database connection or filesystem mutation occurs here.
|
|
99
|
+
*/
|
|
100
|
+
export function mergeLegacySchema(schema,provider){
|
|
101
|
+
const actual=schemaProvider(schema);
|
|
102
|
+
if(actual!==provider||!providers.includes(provider))throw Error("Provider mismatch; cross-provider migration is not supported");
|
|
103
|
+
const canonical=composeSchema({provider});
|
|
104
|
+
const models=text=>[...text.matchAll(/^model\s+(\w+)\s*\{[\s\S]*?^}/gm)];
|
|
105
|
+
const before=new Map(models(schema).map(m=>[m[1],m[0]]));
|
|
106
|
+
const after=new Map(models(canonical).map(m=>[m[1],m[0]]));
|
|
107
|
+
const changes=[],normalizations=[];
|
|
108
|
+
const fieldMap=block=>new Map(block.split("\n").flatMap(line=>{const m=line.match(/^\s+(\w+)\s+([A-Za-z][\w]*(?:\[\]|\?)?)(?=\s|$)/);return m?[[m[1],{type:m[2],line}]]:[];}));
|
|
109
|
+
let result=schema;
|
|
110
|
+
for(const [model,target] of after){
|
|
111
|
+
const old=before.get(model);
|
|
112
|
+
if(!old){result=result.trimEnd()+"\n\n"+target+"\n";changes.push("Add model "+model);continue;}
|
|
113
|
+
let next=old;const existing=fieldMap(old),fields=fieldMap(target),additions=[];
|
|
114
|
+
for(const [name,field] of fields){
|
|
115
|
+
const prior=existing.get(name);
|
|
116
|
+
if(!prior){additions.push(field.line);changes.push("Add "+model+"."+name);continue;}
|
|
117
|
+
if(prior.type!==field.type){
|
|
118
|
+
if(provider==="sqlite"&&legacySqliteJsonFields[model]?.includes(name)&&prior.type.replace("?","")==="String"&&field.type.replace("?","")==="Json"&&prior.type.endsWith("?")===field.type.endsWith("?")){
|
|
119
|
+
next=next.replace(prior.line,prior.line.replace(/^(\s+\w+\s+)String(\??)/,(_,prefix,suffix)=>prefix+"Json"+suffix));
|
|
120
|
+
normalizations.push({table:model,field:name});changes.push("Convert "+model+"."+name+" from legacy JSON text");
|
|
121
|
+
}else throw Error("Incompatible customized field "+model+"."+name+": "+prior.type+" vs "+field.type);
|
|
122
|
+
}
|
|
123
|
+
if(model==="ApiToken"&&name==="tokenHash"&&!/\s@unique\b/.test(prior.line)){
|
|
124
|
+
if(/\s\/\//.test(prior.line))throw Error("Review custom ApiToken.tokenHash declaration manually");
|
|
125
|
+
next=next.replace(prior.line,prior.line+" @unique");changes.push("Add ApiToken.tokenHash unique constraint");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const attrs=target.split("\n").filter(l=>/^\s*@@/.test(l));
|
|
129
|
+
const normalize=l=>l.replace(/\s+/g,"");
|
|
130
|
+
for(const attr of attrs)if(!old.split("\n").some(l=>normalize(l)===normalize(attr))){additions.push(attr);changes.push("Add "+model+" "+attr.trim());}
|
|
131
|
+
if(additions.length)next=next.slice(0,next.lastIndexOf("}")).trimEnd()+"\n\n"+additions.join("\n")+"\n}";
|
|
132
|
+
result=result.replace(old,()=>next);
|
|
133
|
+
}
|
|
134
|
+
// Unformatted built-in definitions must not accidentally be added twice.
|
|
135
|
+
const duplicates=new Set();
|
|
136
|
+
for(const m of result.matchAll(/^\s*(?:model|enum|type|view)\s+(\w+)\s*\{/gm)){if(duplicates.has(m[1]))throw Error("Duplicate declaration; format original schema before merging: "+m[1]);duplicates.add(m[1]);}
|
|
137
|
+
return {schema:result,changes,normalizations,clearLegacyRefresh:!before.has("Session"),revokeLegacyKeys:!(/\s@unique\b/.test(fieldMap(before.get("ApiToken")||"").get("tokenHash")?.line||""))};
|
|
138
|
+
}
|