Skip to content

Commit

Permalink
feat(build): do not inject missing generate targets as aliases (break…
Browse files Browse the repository at this point in the history
…ing build)
  • Loading branch information
Tahul committed Dec 11, 2023
1 parent da2aae9 commit e617f12
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 68 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"main": "./dist/module.cjs",
"types": "./dist/types.d.ts",
"files": [
"dist"
"dist",
"templates"
],
"scripts": {
"build": "nuxt-module-build build",
Expand Down Expand Up @@ -48,4 +49,4 @@
"nuxt": "^3.8.2",
"vitest": "^0.34.6"
}
}
}
3 changes: 3 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ export default defineNuxtConfig({
tailwindcss: {
viewer: false,
},
typescript: {
includeWorkspace: true,
},
})
2 changes: 1 addition & 1 deletion playground/pages/blogposts/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { params } = useRoute()
const { data: blogpost } = await useAsyncData<BlogPost>(
`blogpost-${params.id}`,
async () => await $fetch(`/api/blogpost/${params.id}`),
async () => await $fetch(`/api/blogpost?id=${params.id}`),
)
</script>

Expand Down
38 changes: 38 additions & 0 deletions playground/server/api/blogpost.ts
Original file line number Diff line number Diff line change
@@ -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 }
}
})
25 changes: 0 additions & 25 deletions playground/server/api/blogpost/[id].ts

This file was deleted.

22 changes: 0 additions & 22 deletions playground/server/api/blogpost/index.ts

This file was deleted.

59 changes: 43 additions & 16 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -224,14 +225,16 @@ export default defineNuxtModule<ModuleOptions>({

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
Expand All @@ -244,7 +247,7 @@ export default defineNuxtModule<ModuleOptions>({
// iframe view
view: {
type: 'iframe',
src: uiUrl.stdout,
src: process.env?.NUXT_EDGEDB_UI_URL || uiUrl.stdout,
persistent: true,
},
})
Expand Down Expand Up @@ -390,12 +393,23 @@ export default defineNuxtModule<ModuleOptions>({
})
}

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()
Expand Down Expand Up @@ -437,21 +451,34 @@ export default defineNuxtModule<ModuleOptions>({
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')}`]
},
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/composables/useEdgeDbQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type EdgeDbQueries = keyof typeof queries

export function useEdgeDbQueries(
req: H3Event<EventHandlerRequest> | undefined = undefined,
): { [K in EdgeDbQueries]: (arg: Parameters<typeof queries[K]>[1]) => ReturnType<typeof queries[K]> } {
): { [K in EdgeDbQueries]: (arg?: Parameters<typeof queries[K]>[1]) => ReturnType<typeof queries[K]> } {
const client = useEdgeDb(req)

return Object.fromEntries(
Expand Down
38 changes: 38 additions & 0 deletions templates/default-auth.esdl
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 13 additions & 0 deletions templates/default.esdl
Original file line number Diff line number Diff line change
@@ -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.';
};
}
}
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"extends": "./.nuxt/tsconfig.json"
"extends": "./.nuxt/tsconfig.json",
"references": [
{
"path": "./playground/.nuxt/tsconfig.server.json"
}
]
}

0 comments on commit e617f12

Please sign in to comment.