Skip to content

Commit e617f12

Browse files
committed
feat(build): do not inject missing generate targets as aliases (breaking build)
1 parent da2aae9 commit e617f12

11 files changed

Lines changed: 146 additions & 68 deletions

File tree

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"main": "./dist/module.cjs",
1616
"types": "./dist/types.d.ts",
1717
"files": [
18-
"dist"
18+
"dist",
19+
"templates"
1920
],
2021
"scripts": {
2122
"build": "nuxt-module-build build",
@@ -48,4 +49,4 @@
4849
"nuxt": "^3.8.2",
4950
"vitest": "^0.34.6"
5051
}
51-
}
52+
}

playground/nuxt.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ export default defineNuxtConfig({
1111
tailwindcss: {
1212
viewer: false,
1313
},
14+
typescript: {
15+
includeWorkspace: true,
16+
},
1417
})

playground/pages/blogposts/[id].vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { params } = useRoute()
55
66
const { data: blogpost } = await useAsyncData<BlogPost>(
77
`blogpost-${params.id}`,
8-
async () => await $fetch(`/api/blogpost/${params.id}`),
8+
async () => await $fetch(`/api/blogpost?id=${params.id}`),
99
)
1010
</script>
1111

playground/server/api/blogpost.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { defineEventHandler, getQuery, isMethod, readBody } from 'h3'
2+
import { useEdgeDbQueries } from '#edgedb/server'
3+
import type { BlogPost } from '#edgedb/interfaces'
4+
5+
export default defineEventHandler(async (req) => {
6+
const query = getQuery(req)
7+
const { insertBlogPost, allBlogPosts, deleteBlogPost, getBlogPost } = useEdgeDbQueries(req)
8+
9+
if (isMethod(req, 'POST')) {
10+
const {
11+
title,
12+
description,
13+
content,
14+
} = await readBody(req)
15+
16+
const blogPost = await insertBlogPost({
17+
blogpost_title: title,
18+
blogpost_description: description,
19+
blogpost_content: content,
20+
})
21+
22+
return blogPost
23+
}
24+
25+
if (isMethod(req, 'GET')) {
26+
if (query?.id) {
27+
const blogpost = await getBlogPost({ blogpost_id: query.id.toString() })
28+
return blogpost as BlogPost
29+
}
30+
31+
return await allBlogPosts()
32+
}
33+
34+
if (isMethod(req, 'DELETE') && query?.id) {
35+
await deleteBlogPost({ blogpost_id: query.id.toString() })
36+
return { deleted: query?.id }
37+
}
38+
})

playground/server/api/blogpost/[id].ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

playground/server/api/blogpost/index.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/module.ts

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { existsSync } from 'node:fs'
22
import { addComponentsDir, addImportsDir, addPlugin, addServerHandler, addServerImports, createResolver, defineNuxtModule } from '@nuxt/kit'
3+
import type { ViteDevServer } from 'vite'
34
import { createConsola } from 'consola'
45
import { join } from 'pathe'
56
import chalk from 'chalk'
@@ -224,14 +225,16 @@ export default defineNuxtModule<ModuleOptions>({
224225

225226
if (canPrompt && options.devtools) {
226227
let uiUrl: any | undefined
227-
try {
228-
uiUrl = await execa.execa(`edgedb`, ['ui', '--print-url'])
229-
}
230-
catch (e) {
231-
//
228+
if (!process.env.NUXT_EDGEDB_UI_URL && options.injectDbCredentials) {
229+
try {
230+
uiUrl = await execa.execa(`edgedb`, ['ui', '--print-url'])
231+
}
232+
catch (e) {
233+
//
234+
}
232235
}
233236

234-
if (uiUrl?.stdout) {
237+
if (process.env?.NUXT_EDGEDB_UI_URL || uiUrl?.stdout) {
235238
nuxt.hook('devtools:customTabs', (tabs) => {
236239
tabs.push({
237240
// unique identifier
@@ -244,7 +247,7 @@ export default defineNuxtModule<ModuleOptions>({
244247
// iframe view
245248
view: {
246249
type: 'iframe',
247-
src: uiUrl.stdout,
250+
src: process.env?.NUXT_EDGEDB_UI_URL || uiUrl.stdout,
248251
persistent: true,
249252
},
250253
})
@@ -390,12 +393,23 @@ export default defineNuxtModule<ModuleOptions>({
390393
})
391394
}
392395

396+
const queriesPath = join(dbschemaDir, '/queries.ts')
397+
const interfacesPath = join(dbschemaDir, '/interfaces.ts')
398+
const builderPath = join(dbschemaDir, '/query-builder/index.ts')
399+
400+
const hasQueries = existsSync(queriesPath)
401+
const hasInterfaces = existsSync(interfacesPath)
402+
const hasQueryBuilder = existsSync(queryBuilderDir)
403+
393404
// Inject aliases
394405
const nuxtOptions = nuxt.options
395406
nuxtOptions.alias = nuxtOptions.alias ?? {}
396-
nuxtOptions.alias['#edgedb/queries'] = join(dbschemaDir, '/queries.ts')
397-
nuxtOptions.alias['#edgedb/interfaces'] = join(dbschemaDir, '/interfaces.ts')
398-
nuxtOptions.alias['#edgedb/builder'] = join(dbschemaDir, '/query-builder/index.ts')
407+
if (hasQueries)
408+
nuxtOptions.alias['#edgedb/queries'] = queriesPath
409+
if (hasInterfaces)
410+
nuxtOptions.alias['#edgedb/interfaces'] = interfacesPath
411+
if (hasQueryBuilder)
412+
nuxtOptions.alias['#edgedb/builder'] = builderPath
399413

400414
if (!nuxt.options._prepare) {
401415
await generateInterfaces()
@@ -437,21 +451,34 @@ export default defineNuxtModule<ModuleOptions>({
437451
config.externals.inline ??= []
438452
config.externals.inline.push(resolveLocal('./runtime/server'))
439453

454+
// Fixes for weird cjs query builder imports
455+
if (hasQueryBuilder) {
456+
config.replace ??= {}
457+
config.replace['edgedb/dist/primitives/buffer'] = 'edgedb/dist/primitives/buffer.js'
458+
config.replace['edgedb/dist/reflection/index'] = 'edgedb/dist/reflection/index.js'
459+
}
460+
440461
// Push server aliases
441462
config.alias ??= {}
442463
config.alias['#edgedb/server'] = resolveLocal('./runtime/server/index')
443-
config.alias['#edgedb/queries'] = join(dbschemaDir, '/queries.ts')
444-
config.alias['#edgedb/interfaces'] = join(dbschemaDir, '/interfaces.ts')
445-
config.alias['#edgedb/builder'] = join(dbschemaDir, '/query-builder/index.ts')
464+
if (hasQueries)
465+
config.alias['#edgedb/queries'] = join(dbschemaDir, '/queries.ts')
466+
if (hasInterfaces)
467+
config.alias['#edgedb/interfaces'] = join(dbschemaDir, '/interfaces.ts')
468+
if (hasQueryBuilder)
469+
config.alias['#edgedb/builder'] = join(dbschemaDir, '/query-builder/index.ts')
446470

447471
// Enforce paths on typescript config
448472
config.typescript ??= {}
449473
config.typescript.tsConfig ??= {}
450474
config.typescript.tsConfig.compilerOptions ??= {}
451475
config.typescript.tsConfig.compilerOptions.paths ??= {}
452-
config.typescript.tsConfig.compilerOptions.paths['#edgedb/queries'] = [`${join(dbschemaDir, '/queries.ts')}`]
453-
config.typescript.tsConfig.compilerOptions.paths['#edgedb/interfaces'] = [`${join(dbschemaDir, '/interfaces.ts')}`]
454-
config.typescript.tsConfig.compilerOptions.paths['#edgedb/builder'] = [`${join(dbschemaDir, '/query-builder/index.ts')}`]
476+
if (hasQueries)
477+
config.typescript.tsConfig.compilerOptions.paths['#edgedb/queries'] = [`${join(dbschemaDir, '/queries.ts')}`]
478+
if (hasInterfaces)
479+
config.typescript.tsConfig.compilerOptions.paths['#edgedb/interfaces'] = [`${join(dbschemaDir, '/interfaces.ts')}`]
480+
if (hasQueryBuilder)
481+
config.typescript.tsConfig.compilerOptions.paths['#edgedb/builder'] = [`${join(dbschemaDir, '/query-builder/index.ts')}`]
455482
},
456483
)
457484
}

src/runtime/server/composables/useEdgeDbQueries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type EdgeDbQueries = keyof typeof queries
66

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

1212
return Object.fromEntries(

templates/default-auth.esdl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using extension auth;
2+
3+
module default {
4+
global current_user := (
5+
assert_single((
6+
select User { id, name }
7+
filter .identity = global ext::auth::ClientTokenIdentity
8+
))
9+
);
10+
11+
type User {
12+
required name: str;
13+
required identity: ext::auth::Identity;
14+
multi link posts -> BlogPost {
15+
on source delete delete target;
16+
}
17+
}
18+
19+
type BlogPost {
20+
property content: str {
21+
default := 'My super blog post.';
22+
};
23+
property description: str {
24+
default := 'My blog post description.';
25+
};
26+
property title: str {
27+
default := 'My blog super blog post title.';
28+
};
29+
required author: User {
30+
default := global current_user;
31+
};
32+
access policy author_has_full_access
33+
allow all
34+
using (.author ?= global current_user);
35+
access policy others_read_only
36+
allow select;
37+
}
38+
}

templates/default.esdl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module default {
2+
type BlogPost {
3+
property content: str {
4+
default := 'My super blog post.';
5+
};
6+
property description: str {
7+
default := 'My blog post description.';
8+
};
9+
property title: str {
10+
default := 'My blog super blog post title.';
11+
};
12+
}
13+
}

0 commit comments

Comments
 (0)