From e617f12b7bcb5018d1b5326695edcec1080a7ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C3=ABl=20Guilloux?= Date: Mon, 11 Dec 2023 17:51:29 +0100 Subject: [PATCH] feat(build): do not inject missing generate targets as aliases (breaking build) --- package.json | 5 +- playground/nuxt.config.ts | 3 + playground/pages/blogposts/[id].vue | 2 +- playground/server/api/blogpost.ts | 38 ++++++++++++ playground/server/api/blogpost/[id].ts | 25 -------- playground/server/api/blogpost/index.ts | 22 ------- src/module.ts | 59 ++++++++++++++----- .../server/composables/useEdgeDbQueries.ts | 2 +- templates/default-auth.esdl | 38 ++++++++++++ templates/default.esdl | 13 ++++ tsconfig.json | 7 ++- 11 files changed, 146 insertions(+), 68 deletions(-) create mode 100644 playground/server/api/blogpost.ts delete mode 100644 playground/server/api/blogpost/[id].ts delete mode 100644 playground/server/api/blogpost/index.ts create mode 100644 templates/default-auth.esdl create mode 100644 templates/default.esdl diff --git a/package.json b/package.json index 5b03884..1f7ea20 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "main": "./dist/module.cjs", "types": "./dist/types.d.ts", "files": [ - "dist" + "dist", + "templates" ], "scripts": { "build": "nuxt-module-build build", @@ -48,4 +49,4 @@ "nuxt": "^3.8.2", "vitest": "^0.34.6" } -} \ No newline at end of file +} diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 8ca8e97..e41ac9d 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -11,4 +11,7 @@ export default defineNuxtConfig({ tailwindcss: { viewer: false, }, + typescript: { + includeWorkspace: true, + }, }) diff --git a/playground/pages/blogposts/[id].vue b/playground/pages/blogposts/[id].vue index d3d3cd7..44784f3 100644 --- a/playground/pages/blogposts/[id].vue +++ b/playground/pages/blogposts/[id].vue @@ -5,7 +5,7 @@ const { params } = useRoute() const { data: blogpost } = await useAsyncData( `blogpost-${params.id}`, - async () => await $fetch(`/api/blogpost/${params.id}`), + async () => await $fetch(`/api/blogpost?id=${params.id}`), ) diff --git a/playground/server/api/blogpost.ts b/playground/server/api/blogpost.ts new file mode 100644 index 0000000..6210c17 --- /dev/null +++ b/playground/server/api/blogpost.ts @@ -0,0 +1,38 @@ +import { defineEventHandler, getQuery, isMethod, readBody } from 'h3' +import { useEdgeDbQueries } from '#edgedb/server' +import type { BlogPost } from '#edgedb/interfaces' + +export default defineEventHandler(async (req) => { + const query = getQuery(req) + const { insertBlogPost, allBlogPosts, deleteBlogPost, getBlogPost } = useEdgeDbQueries(req) + + if (isMethod(req, 'POST')) { + const { + title, + description, + content, + } = await readBody(req) + + const blogPost = await insertBlogPost({ + blogpost_title: title, + blogpost_description: description, + blogpost_content: content, + }) + + return blogPost + } + + if (isMethod(req, 'GET')) { + if (query?.id) { + const blogpost = await getBlogPost({ blogpost_id: query.id.toString() }) + return blogpost as BlogPost + } + + return await allBlogPosts() + } + + if (isMethod(req, 'DELETE') && query?.id) { + await deleteBlogPost({ blogpost_id: query.id.toString() }) + return { deleted: query?.id } + } +}) diff --git a/playground/server/api/blogpost/[id].ts b/playground/server/api/blogpost/[id].ts deleted file mode 100644 index 30bbe91..0000000 --- a/playground/server/api/blogpost/[id].ts +++ /dev/null @@ -1,25 +0,0 @@ -import { H3Error, defineEventHandler, getRouterParams } from 'h3' -import { useEdgeDb } from '#edgedb/server' -import type { BlogPost } from '#edgedb/interfaces' - -export default defineEventHandler(async (req) => { - const params = getRouterParams(req) - const client = useEdgeDb(req) - - if (params.id) { - const blogpost = await client.querySingle(` - select BlogPost { - title, - description, - content, - } filter .id = $blogpost_id - `, { blogpost_id: params.id }) - - return blogpost as BlogPost - } - else { - const err = new H3Error('No domain found in query.') - err.statusCode = 400 - return err - } -}) diff --git a/playground/server/api/blogpost/index.ts b/playground/server/api/blogpost/index.ts deleted file mode 100644 index 2a7d3d8..0000000 --- a/playground/server/api/blogpost/index.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { defineEventHandler, isMethod, readBody } from 'h3' -import { useEdgeDbQueries } from '#edgedb/server' - -export default defineEventHandler(async (req) => { - if (isMethod(req, 'POST')) { - const { - title, - description, - content, - } = await readBody(req) - - const { insertBlogPost } = useEdgeDbQueries(req) - - const blogPost = await insertBlogPost({ - blogpost_title: title, - blogpost_description: description, - blogpost_content: content, - }) - - return blogPost - } -}) diff --git a/src/module.ts b/src/module.ts index b7f9c67..27b54b6 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,5 +1,6 @@ import { existsSync } from 'node:fs' import { addComponentsDir, addImportsDir, addPlugin, addServerHandler, addServerImports, createResolver, defineNuxtModule } from '@nuxt/kit' +import type { ViteDevServer } from 'vite' import { createConsola } from 'consola' import { join } from 'pathe' import chalk from 'chalk' @@ -224,14 +225,16 @@ export default defineNuxtModule({ if (canPrompt && options.devtools) { let uiUrl: any | undefined - try { - uiUrl = await execa.execa(`edgedb`, ['ui', '--print-url']) - } - catch (e) { - // + if (!process.env.NUXT_EDGEDB_UI_URL && options.injectDbCredentials) { + try { + uiUrl = await execa.execa(`edgedb`, ['ui', '--print-url']) + } + catch (e) { + // + } } - if (uiUrl?.stdout) { + if (process.env?.NUXT_EDGEDB_UI_URL || uiUrl?.stdout) { nuxt.hook('devtools:customTabs', (tabs) => { tabs.push({ // unique identifier @@ -244,7 +247,7 @@ export default defineNuxtModule({ // iframe view view: { type: 'iframe', - src: uiUrl.stdout, + src: process.env?.NUXT_EDGEDB_UI_URL || uiUrl.stdout, persistent: true, }, }) @@ -390,12 +393,23 @@ export default defineNuxtModule({ }) } + const queriesPath = join(dbschemaDir, '/queries.ts') + const interfacesPath = join(dbschemaDir, '/interfaces.ts') + const builderPath = join(dbschemaDir, '/query-builder/index.ts') + + const hasQueries = existsSync(queriesPath) + const hasInterfaces = existsSync(interfacesPath) + const hasQueryBuilder = existsSync(queryBuilderDir) + // Inject aliases const nuxtOptions = nuxt.options nuxtOptions.alias = nuxtOptions.alias ?? {} - nuxtOptions.alias['#edgedb/queries'] = join(dbschemaDir, '/queries.ts') - nuxtOptions.alias['#edgedb/interfaces'] = join(dbschemaDir, '/interfaces.ts') - nuxtOptions.alias['#edgedb/builder'] = join(dbschemaDir, '/query-builder/index.ts') + if (hasQueries) + nuxtOptions.alias['#edgedb/queries'] = queriesPath + if (hasInterfaces) + nuxtOptions.alias['#edgedb/interfaces'] = interfacesPath + if (hasQueryBuilder) + nuxtOptions.alias['#edgedb/builder'] = builderPath if (!nuxt.options._prepare) { await generateInterfaces() @@ -437,21 +451,34 @@ export default defineNuxtModule({ config.externals.inline ??= [] config.externals.inline.push(resolveLocal('./runtime/server')) + // Fixes for weird cjs query builder imports + if (hasQueryBuilder) { + config.replace ??= {} + config.replace['edgedb/dist/primitives/buffer'] = 'edgedb/dist/primitives/buffer.js' + config.replace['edgedb/dist/reflection/index'] = 'edgedb/dist/reflection/index.js' + } + // Push server aliases config.alias ??= {} config.alias['#edgedb/server'] = resolveLocal('./runtime/server/index') - config.alias['#edgedb/queries'] = join(dbschemaDir, '/queries.ts') - config.alias['#edgedb/interfaces'] = join(dbschemaDir, '/interfaces.ts') - config.alias['#edgedb/builder'] = join(dbschemaDir, '/query-builder/index.ts') + if (hasQueries) + config.alias['#edgedb/queries'] = join(dbschemaDir, '/queries.ts') + if (hasInterfaces) + config.alias['#edgedb/interfaces'] = join(dbschemaDir, '/interfaces.ts') + if (hasQueryBuilder) + config.alias['#edgedb/builder'] = join(dbschemaDir, '/query-builder/index.ts') // Enforce paths on typescript config config.typescript ??= {} config.typescript.tsConfig ??= {} config.typescript.tsConfig.compilerOptions ??= {} config.typescript.tsConfig.compilerOptions.paths ??= {} - config.typescript.tsConfig.compilerOptions.paths['#edgedb/queries'] = [`${join(dbschemaDir, '/queries.ts')}`] - config.typescript.tsConfig.compilerOptions.paths['#edgedb/interfaces'] = [`${join(dbschemaDir, '/interfaces.ts')}`] - config.typescript.tsConfig.compilerOptions.paths['#edgedb/builder'] = [`${join(dbschemaDir, '/query-builder/index.ts')}`] + if (hasQueries) + config.typescript.tsConfig.compilerOptions.paths['#edgedb/queries'] = [`${join(dbschemaDir, '/queries.ts')}`] + if (hasInterfaces) + config.typescript.tsConfig.compilerOptions.paths['#edgedb/interfaces'] = [`${join(dbschemaDir, '/interfaces.ts')}`] + if (hasQueryBuilder) + config.typescript.tsConfig.compilerOptions.paths['#edgedb/builder'] = [`${join(dbschemaDir, '/query-builder/index.ts')}`] }, ) } diff --git a/src/runtime/server/composables/useEdgeDbQueries.ts b/src/runtime/server/composables/useEdgeDbQueries.ts index c529bd8..53ef9de 100644 --- a/src/runtime/server/composables/useEdgeDbQueries.ts +++ b/src/runtime/server/composables/useEdgeDbQueries.ts @@ -6,7 +6,7 @@ export type EdgeDbQueries = keyof typeof queries export function useEdgeDbQueries( req: H3Event | undefined = undefined, -): { [K in EdgeDbQueries]: (arg: Parameters[1]) => ReturnType } { +): { [K in EdgeDbQueries]: (arg?: Parameters[1]) => ReturnType } { const client = useEdgeDb(req) return Object.fromEntries( diff --git a/templates/default-auth.esdl b/templates/default-auth.esdl new file mode 100644 index 0000000..fcbf8dc --- /dev/null +++ b/templates/default-auth.esdl @@ -0,0 +1,38 @@ +using extension auth; + +module default { + global current_user := ( + assert_single(( + select User { id, name } + filter .identity = global ext::auth::ClientTokenIdentity + )) + ); + + type User { + required name: str; + required identity: ext::auth::Identity; + multi link posts -> BlogPost { + on source delete delete target; + } + } + + type BlogPost { + property content: str { + default := 'My super blog post.'; + }; + property description: str { + default := 'My blog post description.'; + }; + property title: str { + default := 'My blog super blog post title.'; + }; + required author: User { + default := global current_user; + }; + access policy author_has_full_access + allow all + using (.author ?= global current_user); + access policy others_read_only + allow select; + } +} diff --git a/templates/default.esdl b/templates/default.esdl new file mode 100644 index 0000000..32ab333 --- /dev/null +++ b/templates/default.esdl @@ -0,0 +1,13 @@ +module default { + type BlogPost { + property content: str { + default := 'My super blog post.'; + }; + property description: str { + default := 'My blog post description.'; + }; + property title: str { + default := 'My blog super blog post title.'; + }; + } +} diff --git a/tsconfig.json b/tsconfig.json index 4b34df1..396ae43 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,3 +1,8 @@ { - "extends": "./.nuxt/tsconfig.json" + "extends": "./.nuxt/tsconfig.json", + "references": [ + { + "path": "./playground/.nuxt/tsconfig.server.json" + } + ] }