opacacms 0.1.0
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/bun.lock +34 -0
- package/dist/admin/api-client.d.ts +8 -0
- package/dist/admin/auth-client.d.ts +940 -0
- package/dist/admin/custom-field.d.ts +71 -0
- package/dist/admin/index.d.ts +11 -0
- package/dist/admin/react.d.ts +3 -0
- package/dist/admin/router.d.ts +7 -0
- package/dist/admin/stores/admin-queries.d.ts +32 -0
- package/dist/admin/stores/auth.d.ts +33 -0
- package/dist/admin/stores/column-visibility.d.ts +21 -0
- package/dist/admin/stores/config.d.ts +7 -0
- package/dist/admin/stores/media.d.ts +44 -0
- package/dist/admin/stores/query.d.ts +4 -0
- package/dist/admin/stores/ui.d.ts +11 -0
- package/dist/admin/ui/admin-client.d.ts +7 -0
- package/dist/admin/ui/admin-layout.d.ts +14 -0
- package/dist/admin/ui/components/ColumnVisibilityToggle.d.ts +10 -0
- package/dist/admin/ui/components/DataDetailSheet.d.ts +13 -0
- package/dist/admin/ui/components/DataDetailView.d.ts +9 -0
- package/dist/admin/ui/components/Table.d.ts +10 -0
- package/dist/admin/ui/components/fields/ArrayField.d.ts +13 -0
- package/dist/admin/ui/components/fields/BlocksField.d.ts +17 -0
- package/dist/admin/ui/components/fields/BooleanField.d.ts +13 -0
- package/dist/admin/ui/components/fields/CollapsibleField.d.ts +16 -0
- package/dist/admin/ui/components/fields/DateField.d.ts +13 -0
- package/dist/admin/ui/components/fields/FileField.d.ts +23 -0
- package/dist/admin/ui/components/fields/GroupField.d.ts +13 -0
- package/dist/admin/ui/components/fields/JoinField.d.ts +15 -0
- package/dist/admin/ui/components/fields/NumberField.d.ts +14 -0
- package/dist/admin/ui/components/fields/RadioField.d.ts +17 -0
- package/dist/admin/ui/components/fields/RelationshipField.d.ts +16 -0
- package/dist/admin/ui/components/fields/RowField.d.ts +12 -0
- package/dist/admin/ui/components/fields/SelectField.d.ts +18 -0
- package/dist/admin/ui/components/fields/TabsField.d.ts +15 -0
- package/dist/admin/ui/components/fields/TextAreaField.d.ts +14 -0
- package/dist/admin/ui/components/fields/TextField.d.ts +14 -0
- package/dist/admin/ui/components/fields/VirtualField.d.ts +8 -0
- package/dist/admin/ui/components/fields/index.d.ts +28 -0
- package/dist/admin/ui/components/fields/richtext-editor/index.d.ts +10 -0
- package/dist/admin/ui/components/fields/richtext-editor/nodes/ImageComponent.d.ts +7 -0
- package/dist/admin/ui/components/fields/richtext-editor/nodes/ImageNode.d.ts +27 -0
- package/dist/admin/ui/components/fields/richtext-editor/plugins/ComponentPickerPlugin.d.ts +1 -0
- package/dist/admin/ui/components/fields/richtext-editor/plugins/EditableSyncPlugin.d.ts +5 -0
- package/dist/admin/ui/components/fields/richtext-editor/plugins/NotionToolbarPlugin.d.ts +1 -0
- package/dist/admin/ui/components/fields/richtext-editor/plugins/SimpleToolbarPlugin.d.ts +1 -0
- package/dist/admin/ui/components/fields/richtext-editor/plugins/ValueSyncPlugin.d.ts +5 -0
- package/dist/admin/ui/components/fields/utils.d.ts +1 -0
- package/dist/admin/ui/components/link.d.ts +8 -0
- package/dist/admin/ui/components/media/AssetManagerModal.d.ts +17 -0
- package/dist/admin/ui/components/toast.d.ts +10 -0
- package/dist/admin/ui/components/ui/accordion.d.ts +11 -0
- package/dist/admin/ui/components/ui/alert-dialog.d.ts +12 -0
- package/dist/admin/ui/components/ui/blocks.d.ts +5 -0
- package/dist/admin/ui/components/ui/breadcrumbs.d.ts +7 -0
- package/dist/admin/ui/components/ui/button.d.ts +7 -0
- package/dist/admin/ui/components/ui/collapsible.d.ts +16 -0
- package/dist/admin/ui/components/ui/dialog.d.ts +27 -0
- package/dist/admin/ui/components/ui/group.d.ts +6 -0
- package/dist/admin/ui/components/ui/index.d.ts +17 -0
- package/dist/admin/ui/components/ui/input.d.ts +5 -0
- package/dist/admin/ui/components/ui/join.d.ts +7 -0
- package/dist/admin/ui/components/ui/label.d.ts +3 -0
- package/dist/admin/ui/components/ui/radio-group.d.ts +13 -0
- package/dist/admin/ui/components/ui/relationship-detail-sheet.d.ts +9 -0
- package/dist/admin/ui/components/ui/relationship.d.ts +8 -0
- package/dist/admin/ui/components/ui/scroll-area.d.ts +7 -0
- package/dist/admin/ui/components/ui/select.d.ts +37 -0
- package/dist/admin/ui/components/ui/separator.d.ts +8 -0
- package/dist/admin/ui/components/ui/sheet.d.ts +28 -0
- package/dist/admin/ui/components/ui/tabs.d.ts +17 -0
- package/dist/admin/ui/components/ui/utils.d.ts +1 -0
- package/dist/admin/ui/hooks/use-debounce.d.ts +1 -0
- package/dist/admin/ui/views/collection-list-view.d.ts +5 -0
- package/dist/admin/ui/views/dashboard-view.d.ts +10 -0
- package/dist/admin/ui/views/document-edit-view.d.ts +7 -0
- package/dist/admin/ui/views/global-edit-view.d.ts +19 -0
- package/dist/admin/ui/views/init-view.d.ts +4 -0
- package/dist/admin/ui/views/login-view.d.ts +4 -0
- package/dist/admin/ui/views/media-registry-view.d.ts +7 -0
- package/dist/admin/ui/views/settings-view.d.ts +7 -0
- package/dist/admin/webcomponent.d.ts +1 -0
- package/dist/api.d.ts +6 -0
- package/dist/auth/index.d.ts +2107 -0
- package/dist/auth/migrations.d.ts +5 -0
- package/dist/auth/premissions.d.ts +6 -0
- package/dist/chunk-16vgcf3k.js +88 -0
- package/dist/chunk-2zm8cy1w.js +9482 -0
- package/dist/chunk-5gvbp2qa.js +167 -0
- package/dist/chunk-62ev8gnc.js +41 -0
- package/dist/chunk-6dhs73zq.js +126 -0
- package/dist/chunk-6ew02s0c.js +472 -0
- package/dist/chunk-7a9kn0np.js +116 -0
- package/dist/chunk-8gkhn1d4.js +309 -0
- package/dist/chunk-8sqjbsgt.js +42 -0
- package/dist/chunk-9kxpbcb1.js +85 -0
- package/dist/chunk-cvdd4eqh.js +110 -0
- package/dist/chunk-d3ffeqp9.js +87 -0
- package/dist/chunk-dy5t83hr.js +261 -0
- package/dist/chunk-f3nvxn63.js +17 -0
- package/dist/chunk-hmhcense.js +1352 -0
- package/dist/chunk-j4d50hrx.js +20 -0
- package/dist/chunk-jwjk85ze.js +15 -0
- package/dist/chunk-kwp83w8b.js +250 -0
- package/dist/chunk-s8mqwnm1.js +14 -0
- package/dist/chunk-srsac177.js +85 -0
- package/dist/chunk-v521d72w.js +10 -0
- package/dist/chunk-xa7rjsn2.js +20 -0
- package/dist/chunk-xg35h5a3.js +15 -0
- package/dist/chunk-ybbbqj63.js +130 -0
- package/dist/chunk-zvwb67nd.js +332 -0
- package/dist/cli/commands/generate-types.d.ts +1 -0
- package/dist/cli/commands/init.d.ts +1 -0
- package/dist/cli/commands/migrate-commands.d.ts +5 -0
- package/dist/cli/commands/seed-command.d.ts +2 -0
- package/dist/cli/d1-mock.d.ts +30 -0
- package/dist/cli/index.d.ts +5 -0
- package/dist/cli/index.test.d.ts +1 -0
- package/dist/cli/r2-mock.d.ts +46 -0
- package/dist/cli/seeding.d.ts +17 -0
- package/dist/client.d.ts +51 -0
- package/dist/config-utils.d.ts +6 -0
- package/dist/config.d.ts +10 -0
- package/dist/db/adapter.d.ts +34 -0
- package/dist/db/better-sqlite.d.ts +40 -0
- package/dist/db/bun-sqlite.d.ts +40 -0
- package/dist/db/d1.d.ts +42 -0
- package/dist/db/kysely/data-mapper.d.ts +6 -0
- package/dist/db/kysely/field-mapper.d.ts +22 -0
- package/dist/db/kysely/migration-generator.d.ts +9 -0
- package/dist/db/kysely/query-builder.d.ts +9 -0
- package/dist/db/kysely/schema-builder.d.ts +15 -0
- package/dist/db/kysely/sql-utils.d.ts +1 -0
- package/dist/db/postgres.d.ts +51 -0
- package/dist/db/sqlite.d.ts +41 -0
- package/dist/db/system-schema.d.ts +2 -0
- package/dist/index.d.ts +6 -0
- package/dist/runtimes/bun.d.ts +17 -0
- package/dist/runtimes/cloudflare-workers.d.ts +10 -0
- package/dist/runtimes/next.d.ts +16 -0
- package/dist/runtimes/node.d.ts +18 -0
- package/dist/schema/collection.d.ts +100 -0
- package/dist/schema/fields/base.d.ts +83 -0
- package/dist/schema/fields/index.d.ts +135 -0
- package/dist/schema/global.d.ts +82 -0
- package/dist/schema/index.d.ts +4 -0
- package/dist/schema/infer.d.ts +55 -0
- package/dist/server/admin-router.d.ts +9 -0
- package/dist/server/admin.d.ts +18 -0
- package/dist/server/assets.d.ts +47 -0
- package/dist/server/collection-router.d.ts +14 -0
- package/dist/server/handlers.d.ts +76 -0
- package/dist/server/middlewares/admin.d.ts +6 -0
- package/dist/server/middlewares/auth.d.ts +16 -0
- package/dist/server/middlewares/context.d.ts +9 -0
- package/dist/server/middlewares/cors.d.ts +3 -0
- package/dist/server/middlewares/database-init.d.ts +11 -0
- package/dist/server/middlewares/rate-limit.d.ts +3 -0
- package/dist/server/router.d.ts +7 -0
- package/dist/server/setup-middlewares.d.ts +17 -0
- package/dist/server/system-router.d.ts +9 -0
- package/dist/server.d.ts +6 -0
- package/dist/src/admin/index.css +47 -0
- package/dist/src/admin/index.js +176 -0
- package/dist/src/admin/webcomponent.js +19 -0
- package/dist/src/api.js +27 -0
- package/dist/src/cli/index.js +157 -0
- package/dist/src/client.js +9 -0
- package/dist/src/db/bun-sqlite.js +523 -0
- package/dist/src/db/d1.js +568 -0
- package/dist/src/db/postgres.js +520 -0
- package/dist/src/db/sqlite.js +534 -0
- package/dist/src/index.js +20 -0
- package/dist/src/runtimes/bun.js +36 -0
- package/dist/src/runtimes/cloudflare-workers.js +29 -0
- package/dist/src/runtimes/next.js +26 -0
- package/dist/src/runtimes/node.js +38 -0
- package/dist/src/server.js +27 -0
- package/dist/src/storage/index.js +355 -0
- package/dist/storage/adapters/cloudflare-r2.d.ts +6 -0
- package/dist/storage/adapters/local.d.ts +6 -0
- package/dist/storage/adapters/s3.d.ts +13 -0
- package/dist/storage/errors.d.ts +12 -0
- package/dist/storage/index.d.ts +5 -0
- package/dist/storage/types.d.ts +31 -0
- package/dist/types.d.ts +484 -0
- package/dist/utils/lexical.d.ts +5 -0
- package/dist/utils/logger.d.ts +35 -0
- package/dist/validation.d.ts +300 -0
- package/dist/validator.d.ts +9 -0
- package/global.d.ts +11 -0
- package/package.json +151 -0
- package/src/admin/api-client.ts +63 -0
- package/src/admin/auth-client.ts +40 -0
- package/src/admin/custom-field.ts +179 -0
- package/src/admin/index.ts +15 -0
- package/src/admin/react.tsx +72 -0
- package/src/admin/router.ts +9 -0
- package/src/admin/stores/admin-queries.ts +121 -0
- package/src/admin/stores/auth.ts +61 -0
- package/src/admin/stores/column-visibility.ts +67 -0
- package/src/admin/stores/config.ts +15 -0
- package/src/admin/stores/media.ts +95 -0
- package/src/admin/stores/query.ts +13 -0
- package/src/admin/stores/ui.ts +29 -0
- package/src/admin/ui/admin-client.tsx +283 -0
- package/src/admin/ui/admin-layout.tsx +276 -0
- package/src/admin/ui/components/ColumnVisibilityToggle.tsx +141 -0
- package/src/admin/ui/components/DataDetailSheet.tsx +141 -0
- package/src/admin/ui/components/DataDetailView.tsx +175 -0
- package/src/admin/ui/components/Table.tsx +67 -0
- package/src/admin/ui/components/fields/ArrayField.tsx +166 -0
- package/src/admin/ui/components/fields/BlocksField.tsx +202 -0
- package/src/admin/ui/components/fields/BooleanField.tsx +50 -0
- package/src/admin/ui/components/fields/CollapsibleField.tsx +75 -0
- package/src/admin/ui/components/fields/DateField.tsx +45 -0
- package/src/admin/ui/components/fields/FileField.tsx +322 -0
- package/src/admin/ui/components/fields/GroupField.tsx +50 -0
- package/src/admin/ui/components/fields/JoinField.tsx +23 -0
- package/src/admin/ui/components/fields/NumberField.tsx +46 -0
- package/src/admin/ui/components/fields/RadioField.tsx +62 -0
- package/src/admin/ui/components/fields/RelationshipField.tsx +278 -0
- package/src/admin/ui/components/fields/RowField.tsx +40 -0
- package/src/admin/ui/components/fields/SelectField.tsx +59 -0
- package/src/admin/ui/components/fields/TabsField.tsx +101 -0
- package/src/admin/ui/components/fields/TextAreaField.tsx +54 -0
- package/src/admin/ui/components/fields/TextField.tsx +49 -0
- package/src/admin/ui/components/fields/VirtualField.tsx +53 -0
- package/src/admin/ui/components/fields/index.tsx +371 -0
- package/src/admin/ui/components/fields/richtext-editor/index.tsx +211 -0
- package/src/admin/ui/components/fields/richtext-editor/nodes/ImageComponent.tsx +142 -0
- package/src/admin/ui/components/fields/richtext-editor/nodes/ImageNode.tsx +95 -0
- package/src/admin/ui/components/fields/richtext-editor/plugins/ComponentPickerPlugin.tsx +226 -0
- package/src/admin/ui/components/fields/richtext-editor/plugins/EditableSyncPlugin.tsx +16 -0
- package/src/admin/ui/components/fields/richtext-editor/plugins/NotionToolbarPlugin.tsx +184 -0
- package/src/admin/ui/components/fields/richtext-editor/plugins/SimpleToolbarPlugin.tsx +240 -0
- package/src/admin/ui/components/fields/richtext-editor/plugins/ValueSyncPlugin.tsx +40 -0
- package/src/admin/ui/components/fields/utils.ts +1 -0
- package/src/admin/ui/components/link.tsx +41 -0
- package/src/admin/ui/components/media/AssetManagerModal.tsx +334 -0
- package/src/admin/ui/components/toast.tsx +72 -0
- package/src/admin/ui/components/ui/accordion.tsx +51 -0
- package/src/admin/ui/components/ui/alert-dialog.tsx +98 -0
- package/src/admin/ui/components/ui/blocks.tsx +32 -0
- package/src/admin/ui/components/ui/breadcrumbs.tsx +59 -0
- package/src/admin/ui/components/ui/button.tsx +26 -0
- package/src/admin/ui/components/ui/collapsible.tsx +124 -0
- package/src/admin/ui/components/ui/dialog.tsx +79 -0
- package/src/admin/ui/components/ui/group.tsx +20 -0
- package/src/admin/ui/components/ui/index.ts +17 -0
- package/src/admin/ui/components/ui/input.tsx +12 -0
- package/src/admin/ui/components/ui/join.tsx +53 -0
- package/src/admin/ui/components/ui/label.tsx +11 -0
- package/src/admin/ui/components/ui/radio-group.tsx +75 -0
- package/src/admin/ui/components/ui/relationship-detail-sheet.tsx +122 -0
- package/src/admin/ui/components/ui/relationship.tsx +58 -0
- package/src/admin/ui/components/ui/scroll-area.tsx +19 -0
- package/src/admin/ui/components/ui/select.tsx +187 -0
- package/src/admin/ui/components/ui/separator.tsx +21 -0
- package/src/admin/ui/components/ui/sheet.tsx +106 -0
- package/src/admin/ui/components/ui/tabs.tsx +116 -0
- package/src/admin/ui/components/ui/utils.ts +3 -0
- package/src/admin/ui/hooks/use-debounce.ts +15 -0
- package/src/admin/ui/styles/_locale-switcher.scss +33 -0
- package/src/admin/ui/styles/accordion.scss +60 -0
- package/src/admin/ui/styles/animations.scss +41 -0
- package/src/admin/ui/styles/asset-manager.scss +547 -0
- package/src/admin/ui/styles/badge.scss +13 -0
- package/src/admin/ui/styles/base.scss +22 -0
- package/src/admin/ui/styles/button.scss +161 -0
- package/src/admin/ui/styles/card.scss +13 -0
- package/src/admin/ui/styles/collapsible.scss +75 -0
- package/src/admin/ui/styles/data-detail.scss +92 -0
- package/src/admin/ui/styles/dialog.scss +102 -0
- package/src/admin/ui/styles/empty-state.scss +22 -0
- package/src/admin/ui/styles/group.scss +19 -0
- package/src/admin/ui/styles/index.scss +33 -0
- package/src/admin/ui/styles/input.scss +80 -0
- package/src/admin/ui/styles/label.scss +12 -0
- package/src/admin/ui/styles/layout.scss +56 -0
- package/src/admin/ui/styles/lexical.scss +469 -0
- package/src/admin/ui/styles/loading.scss +102 -0
- package/src/admin/ui/styles/media-registry.scss +597 -0
- package/src/admin/ui/styles/pagination.scss +20 -0
- package/src/admin/ui/styles/radio-group.scss +66 -0
- package/src/admin/ui/styles/row.scss +17 -0
- package/src/admin/ui/styles/scrollbar.scss +36 -0
- package/src/admin/ui/styles/select.scss +121 -0
- package/src/admin/ui/styles/separator.scss +14 -0
- package/src/admin/ui/styles/sheet.scss +152 -0
- package/src/admin/ui/styles/sidebar.scss +148 -0
- package/src/admin/ui/styles/switch.scss +59 -0
- package/src/admin/ui/styles/table.scss +207 -0
- package/src/admin/ui/styles/tabs.scss +62 -0
- package/src/admin/ui/styles/toast.scss +45 -0
- package/src/admin/ui/styles/variables.scss +24 -0
- package/src/admin/ui/views/collection-list-view.tsx +720 -0
- package/src/admin/ui/views/dashboard-view.tsx +263 -0
- package/src/admin/ui/views/document-edit-view.tsx +384 -0
- package/src/admin/ui/views/global-edit-view.tsx +226 -0
- package/src/admin/ui/views/init-view.tsx +182 -0
- package/src/admin/ui/views/login-view.tsx +123 -0
- package/src/admin/ui/views/media-registry-view.tsx +1104 -0
- package/src/admin/ui/views/settings-view.tsx +729 -0
- package/src/admin/webcomponent.tsx +15 -0
- package/src/api.ts +9 -0
- package/src/auth/index.ts +194 -0
- package/src/auth/migrations.ts +87 -0
- package/src/auth/premissions.ts +46 -0
- package/src/cli/commands/generate-types.ts +116 -0
- package/src/cli/commands/init.ts +95 -0
- package/src/cli/commands/migrate-commands.ts +160 -0
- package/src/cli/commands/seed-command.ts +11 -0
- package/src/cli/d1-mock.ts +101 -0
- package/src/cli/index.test.ts +84 -0
- package/src/cli/index.ts +183 -0
- package/src/cli/r2-mock.ts +217 -0
- package/src/cli/seeding.ts +405 -0
- package/src/client.ts +181 -0
- package/src/config-utils.ts +102 -0
- package/src/config.ts +49 -0
- package/src/db/adapter.ts +53 -0
- package/src/db/better-sqlite.ts +630 -0
- package/src/db/bun-sqlite.ts +646 -0
- package/src/db/d1.ts +711 -0
- package/src/db/kysely/data-mapper.ts +142 -0
- package/src/db/kysely/field-mapper.ts +148 -0
- package/src/db/kysely/migration-generator.ts +223 -0
- package/src/db/kysely/query-builder.ts +92 -0
- package/src/db/kysely/schema-builder.ts +439 -0
- package/src/db/kysely/sql-utils.ts +13 -0
- package/src/db/postgres.ts +621 -0
- package/src/db/sqlite.ts +658 -0
- package/src/db/system-schema.ts +121 -0
- package/src/index.ts +13 -0
- package/src/runtimes/README.md +59 -0
- package/src/runtimes/bun.ts +49 -0
- package/src/runtimes/cloudflare-workers.ts +38 -0
- package/src/runtimes/next.ts +26 -0
- package/src/runtimes/node.ts +52 -0
- package/src/schema/collection.ts +184 -0
- package/src/schema/fields/base.ts +164 -0
- package/src/schema/fields/index.ts +427 -0
- package/src/schema/global.ts +145 -0
- package/src/schema/index.ts +4 -0
- package/src/schema/infer.ts +72 -0
- package/src/server/admin-router.ts +20 -0
- package/src/server/admin.ts +142 -0
- package/src/server/assets.ts +306 -0
- package/src/server/collection-router.ts +55 -0
- package/src/server/handlers.ts +722 -0
- package/src/server/middlewares/admin.ts +27 -0
- package/src/server/middlewares/auth.ts +89 -0
- package/src/server/middlewares/context.ts +17 -0
- package/src/server/middlewares/cors.ts +24 -0
- package/src/server/middlewares/database-init.ts +74 -0
- package/src/server/middlewares/rate-limit.ts +71 -0
- package/src/server/router.ts +47 -0
- package/src/server/setup-middlewares.ts +58 -0
- package/src/server/system-router.ts +35 -0
- package/src/server.ts +9 -0
- package/src/storage/adapters/cloudflare-r2.ts +136 -0
- package/src/storage/adapters/local.ts +146 -0
- package/src/storage/adapters/s3.ts +186 -0
- package/src/storage/errors.ts +46 -0
- package/src/storage/index.ts +5 -0
- package/src/storage/types.ts +39 -0
- package/src/types.ts +577 -0
- package/src/utils/lexical.ts +37 -0
- package/src/utils/logger.ts +73 -0
- package/src/validation.ts +429 -0
- package/src/validator.ts +179 -0
- package/test/admin-custom-field.test.ts +162 -0
- package/test/admin-react-field.test.tsx +134 -0
- package/test/api-features.test.ts +78 -0
- package/test/api.test.ts +178 -0
- package/test/auth.test.ts +62 -0
- package/test/cli-integration.test.ts +146 -0
- package/test/cli.test.ts +25 -0
- package/test/db/postgres.test.ts +95 -0
- package/test/db/sqlite-filter.test.ts +53 -0
- package/test/db/sqlite.test.ts +82 -0
- package/test/engine-features.test.ts +79 -0
- package/test/globals.test.ts +74 -0
- package/test/integration-tmp/db-app/opacacms.config.ts +15 -0
- package/test/integration-tmp/my-sqlite-app/opacacms.config.ts +25 -0
- package/test/integration-tmp/my-test-app/index.ts +8 -0
- package/test/integration-tmp/my-test-app/opacacms.config.ts +16 -0
- package/test/integration-tmp/my-test-app/package.json +12 -0
- package/test/populate.test.ts +79 -0
- package/test/runtimes.test.ts +43 -0
- package/test/schema-builder.test.ts +107 -0
- package/test/schema-features.test.ts +63 -0
- package/test/seeding.test.ts +68 -0
- package/test/storage/local.test.ts +72 -0
- package/test/storage/s3.test.ts +60 -0
- package/test/structural-data.test.ts +100 -0
- package/test/test-setup.ts +11 -0
- package/test/validation.test.ts +162 -0
- package/tsconfig.json +42 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { toSnakeCase } from "./field-mapper";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts a snake_case string to camelCase.
|
|
5
|
+
*/
|
|
6
|
+
export function toCamelCase(str: string): string {
|
|
7
|
+
if (str.startsWith("_")) return str; // Keep system prefixes
|
|
8
|
+
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function flattenPayload(payload: any, prefix = "", excludeKeys: string[] = []): any {
|
|
12
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return payload;
|
|
13
|
+
const result: any = {};
|
|
14
|
+
|
|
15
|
+
for (const key in payload) {
|
|
16
|
+
if (Object.hasOwn(payload, key)) {
|
|
17
|
+
const value = payload[key];
|
|
18
|
+
if (value === undefined) continue;
|
|
19
|
+
|
|
20
|
+
// Do not flatten Date objects or arrays
|
|
21
|
+
if (
|
|
22
|
+
typeof value === "object" &&
|
|
23
|
+
value !== null &&
|
|
24
|
+
!Array.isArray(value) &&
|
|
25
|
+
!(value instanceof Date) &&
|
|
26
|
+
!excludeKeys.includes(key)
|
|
27
|
+
) {
|
|
28
|
+
// Only flatten objects that aren't system keys (like _access)
|
|
29
|
+
if (key.startsWith("_") && prefix === "") {
|
|
30
|
+
result[key] = value;
|
|
31
|
+
} else {
|
|
32
|
+
// It's a group, flatten it
|
|
33
|
+
const flatNested = flattenPayload(value, `${prefix}${key}__`);
|
|
34
|
+
Object.assign(result, flatNested);
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
if (value === undefined) continue;
|
|
38
|
+
// IMPORTANT: Convert column name to snake_case when saving
|
|
39
|
+
const colName = `${prefix}${toSnakeCase(key)}`;
|
|
40
|
+
|
|
41
|
+
// Serialize arrays and objects to JSON strings for SQLite/D1
|
|
42
|
+
if (typeof value === "object" && value !== null) {
|
|
43
|
+
result[colName] = JSON.stringify(value);
|
|
44
|
+
} else {
|
|
45
|
+
result[colName] = value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function unflattenRow(row: any): any {
|
|
54
|
+
if (typeof row !== "object" || row === null || Array.isArray(row)) return row;
|
|
55
|
+
const result: any = {};
|
|
56
|
+
|
|
57
|
+
for (const key in row) {
|
|
58
|
+
if (Object.hasOwn(row, key)) {
|
|
59
|
+
let value = row[key];
|
|
60
|
+
|
|
61
|
+
// Try to parse JSON strings back to objects/arrays
|
|
62
|
+
if (typeof value === "string") {
|
|
63
|
+
const trimmed = value.trim();
|
|
64
|
+
if (
|
|
65
|
+
(trimmed.startsWith("[") && trimmed.endsWith("]")) ||
|
|
66
|
+
(trimmed.startsWith("{") && trimmed.endsWith("}"))
|
|
67
|
+
) {
|
|
68
|
+
try {
|
|
69
|
+
value = JSON.parse(value);
|
|
70
|
+
} catch (e) {
|
|
71
|
+
// Not valid JSON, keep as string
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// If it's a direct match for timestamps or system fields, keep as is
|
|
77
|
+
if (
|
|
78
|
+
key === "created_at" ||
|
|
79
|
+
key === "updated_at" ||
|
|
80
|
+
key === "createdAt" ||
|
|
81
|
+
key === "updatedAt"
|
|
82
|
+
) {
|
|
83
|
+
if (typeof value === "string" && !value.includes("T") && value.includes(" ")) {
|
|
84
|
+
// Normalize SQLite/D1 "YYYY-MM-DD HH:MM:SS" to ISO "YYYY-MM-DDTHH:MM:SSZ"
|
|
85
|
+
value = `${value.replace(" ", "T")}Z`;
|
|
86
|
+
}
|
|
87
|
+
result[toCamelCase(key)] = value;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (key.startsWith("_")) {
|
|
92
|
+
result[key] = value;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const parts = key.split("__");
|
|
97
|
+
if (parts.length === 1) {
|
|
98
|
+
result[toCamelCase(key)] = value;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Try to unflatten, but detect collisions (e.g., "email" vs "email_verified")
|
|
103
|
+
let current = result;
|
|
104
|
+
let collision = false;
|
|
105
|
+
const path: any[] = [];
|
|
106
|
+
|
|
107
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
108
|
+
const part = toCamelCase(parts[i]!);
|
|
109
|
+
if (
|
|
110
|
+
current[part] !== undefined &&
|
|
111
|
+
(typeof current[part] !== "object" || current[part] === null)
|
|
112
|
+
) {
|
|
113
|
+
collision = true;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
path.push({ obj: current, key: part });
|
|
117
|
+
if (!current[part]) {
|
|
118
|
+
current[part] = {};
|
|
119
|
+
}
|
|
120
|
+
current = current[part];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!collision) {
|
|
124
|
+
const lastPart = toCamelCase(parts[parts.length - 1]!);
|
|
125
|
+
if (current[lastPart] !== undefined && typeof current[lastPart] === "object") {
|
|
126
|
+
collision = true;
|
|
127
|
+
} else {
|
|
128
|
+
current[lastPart] = value;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (collision) {
|
|
133
|
+
// If we hit a collision, we must undo any structural changes made for this key
|
|
134
|
+
// and just use the full camelCase key at the root.
|
|
135
|
+
// Actually, just setting it at the root is mostly safe as long as we don't leave
|
|
136
|
+
// empty objects that shouldn't be there. But for simplicity and robustness:
|
|
137
|
+
result[toCamelCase(key)] = value;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { Field } from "../../types";
|
|
2
|
+
|
|
3
|
+
export function toSnakeCase(str: string): string {
|
|
4
|
+
return str
|
|
5
|
+
.replace(/([A-Z])/g, "_$1")
|
|
6
|
+
.toLowerCase()
|
|
7
|
+
.replace(/^_/, "");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Maps an OpacaCMS FieldType to a Kysely column data type for PostgreSQL.
|
|
12
|
+
*/
|
|
13
|
+
export function mapFieldToPostgresType(field: Field): string {
|
|
14
|
+
switch (field.type) {
|
|
15
|
+
case "text":
|
|
16
|
+
case "richtext":
|
|
17
|
+
case "select":
|
|
18
|
+
case "radio":
|
|
19
|
+
case "relationship":
|
|
20
|
+
return "text";
|
|
21
|
+
case "number":
|
|
22
|
+
return "double precision";
|
|
23
|
+
case "boolean":
|
|
24
|
+
return "boolean";
|
|
25
|
+
case "date":
|
|
26
|
+
return "timestamp with time zone";
|
|
27
|
+
case "json":
|
|
28
|
+
case "file":
|
|
29
|
+
return "jsonb";
|
|
30
|
+
default:
|
|
31
|
+
return "text";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Maps an OpacaCMS FieldType to a Kysely column data type for SQLite/D1.
|
|
37
|
+
*/
|
|
38
|
+
export function mapFieldToSQLiteType(field: Field): string {
|
|
39
|
+
switch (field.type) {
|
|
40
|
+
case "text":
|
|
41
|
+
case "richtext":
|
|
42
|
+
case "select":
|
|
43
|
+
case "radio":
|
|
44
|
+
case "relationship":
|
|
45
|
+
case "date": // SQLite stores dates as text
|
|
46
|
+
case "json":
|
|
47
|
+
case "file":
|
|
48
|
+
return "text";
|
|
49
|
+
case "number":
|
|
50
|
+
return "numeric";
|
|
51
|
+
case "boolean":
|
|
52
|
+
return "integer"; // 0 or 1
|
|
53
|
+
default:
|
|
54
|
+
return "text";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Recursively flattens a field list, collecting only fields with a 'name'.
|
|
60
|
+
* This skips layout fields like 'row', 'collapsible', 'tabs' while preserving their nested named fields.
|
|
61
|
+
* Group and Blocks fields are also flattened, prefixing their children's names to avoid collisions.
|
|
62
|
+
* It explicitly excludes virtual fields like 'join' since they are resolved at runtime.
|
|
63
|
+
*/
|
|
64
|
+
export function flattenFields(fields: Field[], prefix = ""): Field[] {
|
|
65
|
+
const result: Field[] = [];
|
|
66
|
+
|
|
67
|
+
for (const field of fields) {
|
|
68
|
+
if (field.type === "join" || field.type === "virtual") continue;
|
|
69
|
+
|
|
70
|
+
const currentName = field.name ? `${prefix}${field.name}` : undefined;
|
|
71
|
+
|
|
72
|
+
if (field.type === "group") {
|
|
73
|
+
if (field.fields && Array.isArray(field.fields)) {
|
|
74
|
+
const nextPrefix = currentName ? `${currentName}__` : "";
|
|
75
|
+
result.push(...flattenFields(field.fields, nextPrefix));
|
|
76
|
+
}
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (field.type === "blocks") {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (field.type === "relationship" && "hasMany" in field && field.hasMany) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (currentName) {
|
|
89
|
+
result.push({ ...field, name: currentName });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (field.type === "row" || field.type === "collapsible") {
|
|
93
|
+
if (field.fields && Array.isArray(field.fields)) {
|
|
94
|
+
result.push(...flattenFields(field.fields, prefix));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (field.type === "tabs" && field.tabs && Array.isArray(field.tabs)) {
|
|
99
|
+
for (const tab of field.tabs) {
|
|
100
|
+
if (tab.fields && Array.isArray(tab.fields)) {
|
|
101
|
+
result.push(...flattenFields(tab.fields, prefix));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Recursively find all relational fields (relationship with hasMany or blocks).
|
|
112
|
+
* This is used by adapters to fetch related data during findOne/find.
|
|
113
|
+
*/
|
|
114
|
+
export function getRelationalFields(fields: Field[], prefix = ""): Field[] {
|
|
115
|
+
const result: Field[] = [];
|
|
116
|
+
|
|
117
|
+
for (const field of fields) {
|
|
118
|
+
const currentName = field.name ? `${prefix}${field.name}` : undefined;
|
|
119
|
+
|
|
120
|
+
if (
|
|
121
|
+
(field.type === "relationship" && "hasMany" in field && field.hasMany) ||
|
|
122
|
+
field.type === "blocks"
|
|
123
|
+
) {
|
|
124
|
+
if (currentName) {
|
|
125
|
+
result.push({ ...field, name: currentName });
|
|
126
|
+
}
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (field.type === "group" || field.type === "row" || field.type === "collapsible") {
|
|
131
|
+
if (field.fields && Array.isArray(field.fields)) {
|
|
132
|
+
const nextPrefix = field.type === "group" && field.name ? `${currentName}__` : prefix;
|
|
133
|
+
result.push(...getRelationalFields(field.fields, nextPrefix));
|
|
134
|
+
}
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (field.type === "tabs" && field.tabs && Array.isArray(field.tabs)) {
|
|
139
|
+
for (const tab of field.tabs) {
|
|
140
|
+
if (tab.fields && Array.isArray(tab.fields)) {
|
|
141
|
+
result.push(...getRelationalFields(tab.fields, prefix));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import type { Collection, Global } from "../../types";
|
|
2
|
+
import {
|
|
3
|
+
flattenFields,
|
|
4
|
+
mapFieldToPostgresType,
|
|
5
|
+
mapFieldToSQLiteType,
|
|
6
|
+
toSnakeCase,
|
|
7
|
+
} from "./field-mapper";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Generates Kysely migration code for 'up' and 'down' functions based on schema definitions.
|
|
11
|
+
*/
|
|
12
|
+
export function generateMigrationCode(
|
|
13
|
+
collections: Collection[],
|
|
14
|
+
globals: Global[] = [],
|
|
15
|
+
dialect: "postgres" | "sqlite" | "d1",
|
|
16
|
+
): string {
|
|
17
|
+
const allSchemas = [...collections, ...globals];
|
|
18
|
+
const mapType = dialect === "postgres" ? mapFieldToPostgresType : mapFieldToSQLiteType;
|
|
19
|
+
const tsType = dialect === "postgres" ? "timestamp with time zone" : "text";
|
|
20
|
+
|
|
21
|
+
let upCode = "";
|
|
22
|
+
let downCode = "";
|
|
23
|
+
|
|
24
|
+
for (const collection of allSchemas) {
|
|
25
|
+
const slug = collection.slug;
|
|
26
|
+
const flattenedFields = flattenFields(collection.fields);
|
|
27
|
+
|
|
28
|
+
// Main Table
|
|
29
|
+
upCode += ` await db.schema.createTable('${slug}')\n`;
|
|
30
|
+
upCode += ` .addColumn('id', 'text', (col) => col.primaryKey())\n`;
|
|
31
|
+
|
|
32
|
+
for (const field of flattenedFields) {
|
|
33
|
+
if (field.type === "relationship" && "hasMany" in field && field.hasMany) continue;
|
|
34
|
+
const colName = toSnakeCase(field.name!);
|
|
35
|
+
if (colName === "id") continue;
|
|
36
|
+
|
|
37
|
+
const colType = mapType(field);
|
|
38
|
+
const constraints: string[] = [];
|
|
39
|
+
if (field.required) constraints.push("notNull()");
|
|
40
|
+
if (field.unique) constraints.push("unique()");
|
|
41
|
+
|
|
42
|
+
if (field.defaultValue !== undefined) {
|
|
43
|
+
let val = field.defaultValue;
|
|
44
|
+
if (typeof val === "string") {
|
|
45
|
+
val = `'${val}'`;
|
|
46
|
+
} else if (typeof val === "boolean") {
|
|
47
|
+
val = dialect === "sqlite" || dialect === "d1" ? (val ? 1 : 0) : val;
|
|
48
|
+
}
|
|
49
|
+
constraints.push(`defaultTo(${val})`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const constraintChain = constraints.length > 0 ? `.${constraints.join(".")}` : "";
|
|
53
|
+
upCode += ` .addColumn('${colName}', '${colType}', (col) => col${constraintChain})\n`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Timestamps
|
|
57
|
+
const ts = (collection as any).timestamps !== false;
|
|
58
|
+
if (ts) {
|
|
59
|
+
const config =
|
|
60
|
+
typeof (collection as any).timestamps === "object" ? (collection as any).timestamps : {};
|
|
61
|
+
const createdField = toSnakeCase(config.createdAt || "createdAt");
|
|
62
|
+
const updatedField = toSnakeCase(config.updatedAt || "updatedAt");
|
|
63
|
+
upCode += ` .addColumn('${createdField}', '${tsType}', (col) => col.defaultTo(sql\`CURRENT_TIMESTAMP\`))\n`;
|
|
64
|
+
upCode += ` .addColumn('${updatedField}', '${tsType}', (col) => col.defaultTo(sql\`CURRENT_TIMESTAMP\`))\n`;
|
|
65
|
+
}
|
|
66
|
+
upCode += ` .execute();\n\n`;
|
|
67
|
+
|
|
68
|
+
downCode = ` await db.schema.dropTable('${slug}').execute();\n` + downCode;
|
|
69
|
+
|
|
70
|
+
// Join Tables
|
|
71
|
+
for (const field of flattenedFields) {
|
|
72
|
+
if (field.type === "relationship" && "hasMany" in field && field.hasMany) {
|
|
73
|
+
const colName = toSnakeCase(field.name!);
|
|
74
|
+
const joinTableName = `${slug}_${colName}_relations`.toLowerCase();
|
|
75
|
+
upCode += ` await db.schema.createTable('${joinTableName}')\n`;
|
|
76
|
+
upCode += ` .addColumn('id', 'text', (col) => col.primaryKey())\n`;
|
|
77
|
+
upCode += ` .addColumn('source_id', 'text', (col) => col.notNull())\n`;
|
|
78
|
+
upCode += ` .addColumn('target_id', 'text', (col) => col.notNull())\n`;
|
|
79
|
+
upCode += ` .addColumn('order', 'integer', (col) => col.defaultTo(0))\n`;
|
|
80
|
+
upCode += ` .execute();\n\n`;
|
|
81
|
+
|
|
82
|
+
downCode = ` await db.schema.dropTable('${joinTableName}').execute();\n` + downCode;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Block Tables
|
|
87
|
+
for (const field of collection.fields) {
|
|
88
|
+
if (field.type === "blocks" && field.blocks && Array.isArray(field.blocks)) {
|
|
89
|
+
for (const block of field.blocks) {
|
|
90
|
+
const blockName = toSnakeCase(field.name!);
|
|
91
|
+
const blockTableName = `${slug}_${blockName}_${block.slug}`.toLowerCase();
|
|
92
|
+
|
|
93
|
+
upCode += ` await db.schema.createTable('${blockTableName}')\n`;
|
|
94
|
+
upCode += ` .addColumn('id', 'text', (col) => col.primaryKey())\n`;
|
|
95
|
+
upCode += ` .addColumn('_parent_id', 'text', (col) => col.notNull())\n`;
|
|
96
|
+
upCode += ` .addColumn('_order', 'integer', (col) => col.defaultTo(0))\n`;
|
|
97
|
+
upCode += ` .addColumn('block_type', 'text', (col) => col.notNull())\n`;
|
|
98
|
+
|
|
99
|
+
const blockFlattened = flattenFields(block.fields);
|
|
100
|
+
for (const bField of blockFlattened) {
|
|
101
|
+
const bColName = toSnakeCase(bField.name!);
|
|
102
|
+
if (bColName === "id") continue;
|
|
103
|
+
const bColType = mapType(bField);
|
|
104
|
+
|
|
105
|
+
const bConstraints: string[] = [];
|
|
106
|
+
if (bField.required) bConstraints.push("notNull()");
|
|
107
|
+
if (bField.unique) bConstraints.push("unique()");
|
|
108
|
+
if (bField.defaultValue !== undefined) {
|
|
109
|
+
let bVal = bField.defaultValue;
|
|
110
|
+
if (typeof bVal === "string") bVal = `'${bVal}'`;
|
|
111
|
+
else if (typeof bVal === "boolean")
|
|
112
|
+
bVal = dialect === "sqlite" || dialect === "d1" ? (bVal ? 1 : 0) : bVal;
|
|
113
|
+
bConstraints.push(`defaultTo(${bVal})`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const bConstraintChain = bConstraints.length > 0 ? `.${bConstraints.join(".")}` : "";
|
|
117
|
+
upCode += ` .addColumn('${bColName}', '${bColType}', (col) => col${bConstraintChain})\n`;
|
|
118
|
+
}
|
|
119
|
+
upCode += ` .addColumn('created_at', '${tsType}', (col) => col.defaultTo(sql\`CURRENT_TIMESTAMP\`))\n`;
|
|
120
|
+
upCode += ` .addColumn('updated_at', '${tsType}', (col) => col.defaultTo(sql\`CURRENT_TIMESTAMP\`))\n`;
|
|
121
|
+
upCode += ` .execute();\n\n`;
|
|
122
|
+
|
|
123
|
+
downCode = ` await db.schema.dropTable('${blockTableName}').execute();\n` + downCode;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return `import { Kysely, sql } from 'kysely';
|
|
130
|
+
|
|
131
|
+
export async function up(db: Kysely<any>): Promise<void> {
|
|
132
|
+
${upCode}}
|
|
133
|
+
|
|
134
|
+
export async function down(db: Kysely<any>): Promise<void> {
|
|
135
|
+
${downCode}}
|
|
136
|
+
`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Generates raw SQL migration code for Cloudflare D1.
|
|
141
|
+
*/
|
|
142
|
+
export function generateSQLCode(collections: Collection[], globals: Global[] = []): string {
|
|
143
|
+
const allSchemas = [...collections, ...globals];
|
|
144
|
+
let sql = "-- OpacaCMS Auto-generated SQL Migration\n\n";
|
|
145
|
+
|
|
146
|
+
for (const collection of allSchemas) {
|
|
147
|
+
const slug = collection.slug;
|
|
148
|
+
const flattenedFields = flattenFields(collection.fields);
|
|
149
|
+
|
|
150
|
+
sql += `CREATE TABLE IF NOT EXISTS "${slug}" (\n`;
|
|
151
|
+
sql += ` "id" TEXT PRIMARY KEY`;
|
|
152
|
+
|
|
153
|
+
for (const field of flattenedFields) {
|
|
154
|
+
if (field.type === "relationship" && "hasMany" in field && field.hasMany) continue;
|
|
155
|
+
const colName = toSnakeCase(field.name!);
|
|
156
|
+
if (colName === "id") continue;
|
|
157
|
+
|
|
158
|
+
const colType = mapFieldToSQLiteType(field);
|
|
159
|
+
sql += `,\n "${colName}" ${colType.toUpperCase()}`;
|
|
160
|
+
if (field.required) sql += " NOT NULL";
|
|
161
|
+
if (field.unique) sql += " UNIQUE";
|
|
162
|
+
if (field.defaultValue !== undefined) {
|
|
163
|
+
let val = field.defaultValue;
|
|
164
|
+
if (typeof val === "string") val = `'${val}'`;
|
|
165
|
+
else if (typeof val === "boolean") val = val ? 1 : 0;
|
|
166
|
+
sql += ` DEFAULT ${val}`;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const ts = (collection as any).timestamps !== false;
|
|
171
|
+
if (ts) {
|
|
172
|
+
const config =
|
|
173
|
+
typeof (collection as any).timestamps === "object" ? (collection as any).timestamps : {};
|
|
174
|
+
const createdField = toSnakeCase(config.createdAt || "createdAt");
|
|
175
|
+
const updatedField = toSnakeCase(config.updatedAt || "updatedAt");
|
|
176
|
+
sql += `,\n "${createdField}" TEXT DEFAULT CURRENT_TIMESTAMP`;
|
|
177
|
+
sql += `,\n "${updatedField}" TEXT DEFAULT CURRENT_TIMESTAMP`;
|
|
178
|
+
}
|
|
179
|
+
sql += "\n);\n\n";
|
|
180
|
+
|
|
181
|
+
// Join Tables
|
|
182
|
+
for (const field of flattenedFields) {
|
|
183
|
+
if (field.type === "relationship" && "hasMany" in field && field.hasMany) {
|
|
184
|
+
const colName = toSnakeCase(field.name!);
|
|
185
|
+
const joinTableName = `${slug}_${colName}_relations`.toLowerCase();
|
|
186
|
+
sql += `CREATE TABLE IF NOT EXISTS "${joinTableName}" (\n`;
|
|
187
|
+
sql += ` "id" TEXT PRIMARY KEY,\n`;
|
|
188
|
+
sql += ` "source_id" TEXT NOT NULL,\n`;
|
|
189
|
+
sql += ` "target_id" TEXT NOT NULL,\n`;
|
|
190
|
+
sql += ` "order" INTEGER DEFAULT 0\n`;
|
|
191
|
+
sql += `);\n\n`;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Block Tables
|
|
196
|
+
for (const field of collection.fields) {
|
|
197
|
+
if (field.type === "blocks" && field.blocks && Array.isArray(field.blocks)) {
|
|
198
|
+
for (const block of field.blocks) {
|
|
199
|
+
const blockName = toSnakeCase(field.name!);
|
|
200
|
+
const blockTableName = `${slug}_${blockName}_${block.slug}`.toLowerCase();
|
|
201
|
+
sql += `CREATE TABLE IF NOT EXISTS "${blockTableName}" (\n`;
|
|
202
|
+
sql += ` "id" TEXT PRIMARY KEY,\n`;
|
|
203
|
+
sql += ` "_parent_id" TEXT NOT NULL,\n`;
|
|
204
|
+
sql += ` "_order" INTEGER DEFAULT 0,\n`;
|
|
205
|
+
sql += ` "block_type" TEXT NOT NULL`;
|
|
206
|
+
|
|
207
|
+
const blockFlattened = flattenFields(block.fields);
|
|
208
|
+
for (const bField of blockFlattened) {
|
|
209
|
+
const bColName = toSnakeCase(bField.name!);
|
|
210
|
+
if (bColName === "id") continue;
|
|
211
|
+
const bColType = mapFieldToSQLiteType(bField);
|
|
212
|
+
sql += `,\n "${bColName}" ${bColType.toUpperCase()}`;
|
|
213
|
+
}
|
|
214
|
+
sql += `,\n "created_at" TEXT DEFAULT CURRENT_TIMESTAMP,\n`;
|
|
215
|
+
sql += ` "updated_at" TEXT DEFAULT CURRENT_TIMESTAMP\n`;
|
|
216
|
+
sql += `);\n\n`;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return sql;
|
|
223
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { ExpressionBuilder } from "kysely";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Builds a Kysely expression from a generic query object.
|
|
5
|
+
*
|
|
6
|
+
* @param eb The Kysely ExpressionBuilder (e.g., from .where((eb) => buildKyselyWhere(eb, query)))
|
|
7
|
+
* @param query The query object (e.g., { age: { gt: 18 }, name: 'John' })
|
|
8
|
+
* @returns A single Expression combining all conditions with AND, or undefined.
|
|
9
|
+
*/
|
|
10
|
+
export function buildKyselyWhere(
|
|
11
|
+
eb: ExpressionBuilder<any, any>,
|
|
12
|
+
query: Record<string, any> | undefined,
|
|
13
|
+
): any {
|
|
14
|
+
if (!query || Object.keys(query).length === 0) return undefined;
|
|
15
|
+
|
|
16
|
+
const conditions: any[] = [];
|
|
17
|
+
|
|
18
|
+
for (const [key, value] of Object.entries(query)) {
|
|
19
|
+
if (value === undefined) continue;
|
|
20
|
+
|
|
21
|
+
if (key === "or" && Array.isArray(value)) {
|
|
22
|
+
const orConditions = value.map((v) => buildKyselyWhere(eb, v)).filter((c) => c !== undefined);
|
|
23
|
+
if (orConditions.length > 0) {
|
|
24
|
+
conditions.push(eb.or(orConditions));
|
|
25
|
+
}
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (key === "and" && Array.isArray(value)) {
|
|
30
|
+
const andConditions = value
|
|
31
|
+
.map((v) => buildKyselyWhere(eb, v))
|
|
32
|
+
.filter((c) => c !== undefined);
|
|
33
|
+
if (andConditions.length > 0) {
|
|
34
|
+
conditions.push(eb.and(andConditions));
|
|
35
|
+
}
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (
|
|
40
|
+
typeof value === "object" &&
|
|
41
|
+
value !== null &&
|
|
42
|
+
!Array.isArray(value) &&
|
|
43
|
+
!(value instanceof Date)
|
|
44
|
+
) {
|
|
45
|
+
for (const [op, val] of Object.entries(value)) {
|
|
46
|
+
if (val === undefined) continue;
|
|
47
|
+
switch (op) {
|
|
48
|
+
case "gt":
|
|
49
|
+
conditions.push(eb(key, ">", val));
|
|
50
|
+
break;
|
|
51
|
+
case "gte":
|
|
52
|
+
conditions.push(eb(key, ">=", val));
|
|
53
|
+
break;
|
|
54
|
+
case "lt":
|
|
55
|
+
conditions.push(eb(key, "<", val));
|
|
56
|
+
break;
|
|
57
|
+
case "lte":
|
|
58
|
+
conditions.push(eb(key, "<=", val));
|
|
59
|
+
break;
|
|
60
|
+
case "like":
|
|
61
|
+
conditions.push(eb(key, "like", String(val)));
|
|
62
|
+
break;
|
|
63
|
+
case "ne":
|
|
64
|
+
if (val === null) conditions.push(eb(key, "is not", null));
|
|
65
|
+
else conditions.push(eb(key, "!=", val));
|
|
66
|
+
break;
|
|
67
|
+
case "in":
|
|
68
|
+
if (Array.isArray(val)) conditions.push(eb(key, "in", val));
|
|
69
|
+
break;
|
|
70
|
+
case "is":
|
|
71
|
+
if (val === null) conditions.push(eb(key, "is", null));
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
if (val === null) conditions.push(eb(key, "is", null));
|
|
75
|
+
else conditions.push(eb(key, "=", val));
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
} else {
|
|
80
|
+
if (value === null) {
|
|
81
|
+
conditions.push(eb(key, "is", null));
|
|
82
|
+
} else {
|
|
83
|
+
conditions.push(eb(key, "=", value));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (conditions.length === 0) return undefined;
|
|
89
|
+
if (conditions.length === 1) return conditions[0];
|
|
90
|
+
|
|
91
|
+
return eb.and(conditions);
|
|
92
|
+
}
|