nuxt-auto-crud 1.17.3 → 1.18.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 +18 -20
- package/dist/module.json +1 -1
- package/dist/module.mjs +16 -2
- package/dist/runtime/server/stubs/auth.d.ts +7 -0
- package/dist/runtime/server/stubs/auth.js +7 -0
- package/dist/runtime/server/utils/handler.js +1 -8
- package/package.json +3 -2
- package/src/runtime/server/stubs/auth.ts +7 -0
- package/src/runtime/server/utils/handler.ts +2 -14
package/README.md
CHANGED
|
@@ -57,7 +57,7 @@ If you want to add `nuxt-auto-crud` to an existing project, follow these steps:
|
|
|
57
57
|
|
|
58
58
|
```bash
|
|
59
59
|
# Install module and required dependencies
|
|
60
|
-
npm install nuxt-auto-crud @nuxthub/core
|
|
60
|
+
npm install nuxt-auto-crud @nuxthub/core@^0.10.0 drizzle-orm
|
|
61
61
|
|
|
62
62
|
# Optional: Install auth dependencies if using Session Auth (Recommended)
|
|
63
63
|
npm install nuxt-auth-utils nuxt-authorization
|
|
@@ -80,11 +80,11 @@ export default defineNuxtConfig({
|
|
|
80
80
|
modules: ['@nuxthub/core', 'nuxt-auto-crud'],
|
|
81
81
|
|
|
82
82
|
hub: {
|
|
83
|
-
|
|
83
|
+
db: 'sqlite',
|
|
84
84
|
},
|
|
85
85
|
|
|
86
86
|
autoCrud: {
|
|
87
|
-
schemaPath: 'server/
|
|
87
|
+
schemaPath: 'server/db/schema',
|
|
88
88
|
// auth: false,
|
|
89
89
|
auth: {
|
|
90
90
|
type: 'session', // for Normal Authentication with nuxt-auth-utils
|
|
@@ -102,7 +102,7 @@ Add the generation script to your `package.json`:
|
|
|
102
102
|
```json
|
|
103
103
|
{
|
|
104
104
|
"scripts": {
|
|
105
|
-
"db:generate": "
|
|
105
|
+
"db:generate": "nuxt db generate"
|
|
106
106
|
}
|
|
107
107
|
// ...
|
|
108
108
|
}
|
|
@@ -116,8 +116,8 @@ import { defineConfig } from 'drizzle-kit'
|
|
|
116
116
|
|
|
117
117
|
export default defineConfig({
|
|
118
118
|
dialect: 'sqlite',
|
|
119
|
-
schema: './server/
|
|
120
|
-
out: './server/
|
|
119
|
+
schema: './server/db/schema/index.ts', // Point to your schema index file
|
|
120
|
+
out: './server/db/migrations'
|
|
121
121
|
})
|
|
122
122
|
```
|
|
123
123
|
|
|
@@ -127,15 +127,13 @@ Create `server/utils/drizzle.ts` to export the database instance:
|
|
|
127
127
|
|
|
128
128
|
```typescript
|
|
129
129
|
// server/utils/drizzle.ts
|
|
130
|
-
import {
|
|
130
|
+
import { db, schema } from 'hub:db'
|
|
131
131
|
export { sql, eq, and, or } from 'drizzle-orm'
|
|
132
132
|
|
|
133
|
-
import * as schema from '../database/schema'
|
|
134
|
-
|
|
135
133
|
export const tables = schema
|
|
136
134
|
|
|
137
135
|
export function useDrizzle() {
|
|
138
|
-
return
|
|
136
|
+
return db
|
|
139
137
|
}
|
|
140
138
|
|
|
141
139
|
export type User = typeof schema.users.$inferSelect
|
|
@@ -143,10 +141,10 @@ export type User = typeof schema.users.$inferSelect
|
|
|
143
141
|
|
|
144
142
|
#### Define your database schema
|
|
145
143
|
|
|
146
|
-
Create your schema files in `server/
|
|
144
|
+
Create your schema files in `server/db/schema/`. For example, `server/db/schema/users.ts`:
|
|
147
145
|
|
|
148
146
|
```typescript
|
|
149
|
-
// server/
|
|
147
|
+
// server/db/schema/users.ts
|
|
150
148
|
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core'
|
|
151
149
|
|
|
152
150
|
export const users = sqliteTable('users', {
|
|
@@ -174,7 +172,7 @@ That's it! 🎉 Your CRUD APIs are now available at `/api/users`.
|
|
|
174
172
|
To add a new table (e.g., `posts`), simply create a new file in your schema directory:
|
|
175
173
|
|
|
176
174
|
```typescript
|
|
177
|
-
// server/
|
|
175
|
+
// server/db/schema/posts.ts
|
|
178
176
|
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core'
|
|
179
177
|
import { users } from './users'
|
|
180
178
|
|
|
@@ -187,10 +185,10 @@ export const posts = sqliteTable('posts', {
|
|
|
187
185
|
})
|
|
188
186
|
```
|
|
189
187
|
|
|
190
|
-
Then, ensure it is exported in your `server/
|
|
188
|
+
Then, ensure it is exported in your `server/db/schema/index.ts` (if you are using an index file) or that your `drizzle.config.ts` is pointing to the correct location.
|
|
191
189
|
|
|
192
190
|
```typescript
|
|
193
|
-
// server/
|
|
191
|
+
// server/db/schema/index.ts
|
|
194
192
|
export * from './users'
|
|
195
193
|
export * from './posts'
|
|
196
194
|
```
|
|
@@ -217,7 +215,7 @@ In this case, you might handle authentication differently (e.g., validating toke
|
|
|
217
215
|
export default defineNuxtConfig({
|
|
218
216
|
modules: ['nuxt-auto-crud'],
|
|
219
217
|
autoCrud: {
|
|
220
|
-
schemaPath: 'server/
|
|
218
|
+
schemaPath: 'server/db/schema',
|
|
221
219
|
// auth: false, // Uncomment this line for testing APIs without auth
|
|
222
220
|
auth: {
|
|
223
221
|
type: 'jwt', // for app providing backend apis only
|
|
@@ -239,8 +237,8 @@ import { defineConfig } from 'drizzle-kit'
|
|
|
239
237
|
|
|
240
238
|
export default defineConfig({
|
|
241
239
|
dialect: 'sqlite',
|
|
242
|
-
schema: './server/
|
|
243
|
-
out: './server/
|
|
240
|
+
schema: './server/db/schema/index.ts',
|
|
241
|
+
out: './server/db/migrations',
|
|
244
242
|
tablesFilter: ['!_hub_migrations'],
|
|
245
243
|
})
|
|
246
244
|
```
|
|
@@ -423,7 +421,7 @@ await $fetch("/api/users/1", {
|
|
|
423
421
|
export default defineNuxtConfig({
|
|
424
422
|
autoCrud: {
|
|
425
423
|
// Path to your database schema file (relative to project root)
|
|
426
|
-
schemaPath: "server/
|
|
424
|
+
schemaPath: "server/db/schema", // default
|
|
427
425
|
|
|
428
426
|
// Authentication configuration (see "Authentication Configuration" section)
|
|
429
427
|
auth: {
|
|
@@ -464,7 +462,7 @@ You can customize hidden fields by modifying the `modelMapper.ts` utility.
|
|
|
464
462
|
|
|
465
463
|
- Nuxt 3 or 4
|
|
466
464
|
- Drizzle ORM (SQLite)
|
|
467
|
-
- NuxtHub (Recommended) or [Custom SQLite Setup](./custom-setup.md)
|
|
465
|
+
- NuxtHub >= 0.10.0 (Recommended) or [Custom SQLite Setup](./custom-setup.md)
|
|
468
466
|
|
|
469
467
|
## 🔗 Other Helpful Links
|
|
470
468
|
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defineNuxtModule, createResolver, addImportsDir, addServerHandler, addServerImportsDir } from '@nuxt/kit';
|
|
1
|
+
import { defineNuxtModule, createResolver, addImportsDir, hasNuxtModule, addServerImports, addServerHandler, addServerImportsDir } from '@nuxt/kit';
|
|
2
2
|
|
|
3
3
|
const module$1 = defineNuxtModule({
|
|
4
4
|
meta: {
|
|
@@ -6,7 +6,7 @@ const module$1 = defineNuxtModule({
|
|
|
6
6
|
configKey: "autoCrud"
|
|
7
7
|
},
|
|
8
8
|
defaults: {
|
|
9
|
-
schemaPath: "server/
|
|
9
|
+
schemaPath: "server/db/schema",
|
|
10
10
|
drizzlePath: "server/utils/drizzle",
|
|
11
11
|
auth: false
|
|
12
12
|
},
|
|
@@ -23,6 +23,20 @@ const module$1 = defineNuxtModule({
|
|
|
23
23
|
);
|
|
24
24
|
nuxt.options.alias["#site/drizzle"] = drizzlePath;
|
|
25
25
|
addImportsDir(resolver.resolve(nuxt.options.rootDir, "shared/utils"));
|
|
26
|
+
const stubsPath = resolver.resolve("./runtime/server/stubs/auth");
|
|
27
|
+
if (!hasNuxtModule("nuxt-auth-utils")) {
|
|
28
|
+
addServerImports([
|
|
29
|
+
{ name: "requireUserSession", from: stubsPath },
|
|
30
|
+
{ name: "getUserSession", from: stubsPath }
|
|
31
|
+
]);
|
|
32
|
+
}
|
|
33
|
+
if (!hasNuxtModule("nuxt-authorization")) {
|
|
34
|
+
addServerImports([
|
|
35
|
+
{ name: "allows", from: stubsPath },
|
|
36
|
+
{ name: "abilities", from: stubsPath },
|
|
37
|
+
{ name: "abilityLogic", from: stubsPath }
|
|
38
|
+
]);
|
|
39
|
+
}
|
|
26
40
|
nuxt.options.alias["#authorization"] ||= "nuxt-authorization/utils";
|
|
27
41
|
const mergedAuth = options.auth === false ? { authentication: false, authorization: false, type: "session" } : {
|
|
28
42
|
authentication: true,
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const requireUserSession = () => {
|
|
2
|
+
throw new Error("nuxt-auth-utils not installed");
|
|
3
|
+
};
|
|
4
|
+
export const getUserSession = () => Promise.resolve({ user: null });
|
|
5
|
+
export const allows = () => Promise.resolve(true);
|
|
6
|
+
export const abilities = null;
|
|
7
|
+
export const abilityLogic = null;
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { createError } from "h3";
|
|
2
|
-
import { useAutoCrudConfig } from "./config.js";
|
|
3
2
|
import { checkAdminAccess } from "./auth.js";
|
|
4
3
|
import { filterHiddenFields, filterPublicColumns } from "./modelMapper.js";
|
|
5
|
-
import { getUserSession } from "#imports";
|
|
6
4
|
export async function ensureResourceAccess(event, model, action) {
|
|
7
|
-
const { auth } = useAutoCrudConfig();
|
|
8
5
|
const isAuthorized = await checkAdminAccess(event, model, action);
|
|
9
6
|
if (!isAuthorized) {
|
|
10
7
|
throw createError({
|
|
@@ -12,11 +9,7 @@ export async function ensureResourceAccess(event, model, action) {
|
|
|
12
9
|
message: "Unauthorized"
|
|
13
10
|
});
|
|
14
11
|
}
|
|
15
|
-
|
|
16
|
-
return true;
|
|
17
|
-
}
|
|
18
|
-
const session = await getUserSession(event);
|
|
19
|
-
return !!session.user;
|
|
12
|
+
return true;
|
|
20
13
|
}
|
|
21
14
|
export function formatResourceResult(model, data, isAdmin) {
|
|
22
15
|
if (isAdmin) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-auto-crud",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.18.1",
|
|
4
4
|
"description": "Exposes RESTful CRUD APIs for your Nuxt app based solely on your database migrations.",
|
|
5
5
|
"author": "Cliford Pereira",
|
|
6
6
|
"license": "MIT",
|
|
@@ -63,12 +63,13 @@
|
|
|
63
63
|
"@iconify-json/lucide": "^1.2.77",
|
|
64
64
|
"@iconify-json/simple-icons": "^1.2.61",
|
|
65
65
|
"@iconify-json/vscode-icons": "^1.2.37",
|
|
66
|
+
"@libsql/client": "^0.15.15",
|
|
66
67
|
"@nuxt/devtools": "^3.1.0",
|
|
67
68
|
"@nuxt/eslint-config": "^1.10.0",
|
|
68
69
|
"@nuxt/module-builder": "^1.0.2",
|
|
69
70
|
"@nuxt/schema": "^4.2.1",
|
|
70
71
|
"@nuxt/test-utils": "^3.20.1",
|
|
71
|
-
"@nuxthub/core": "^0.
|
|
72
|
+
"@nuxthub/core": "^0.10.1",
|
|
72
73
|
"@types/better-sqlite3": "^7.6.13",
|
|
73
74
|
"@types/node": "latest",
|
|
74
75
|
"better-sqlite3": "^12.5.0",
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const requireUserSession = () => {
|
|
2
|
+
throw new Error('nuxt-auth-utils not installed')
|
|
3
|
+
}
|
|
4
|
+
export const getUserSession = () => Promise.resolve({ user: null })
|
|
5
|
+
export const allows = () => Promise.resolve(true)
|
|
6
|
+
export const abilities = null
|
|
7
|
+
export const abilityLogic = null
|
|
@@ -1,15 +1,10 @@
|
|
|
1
1
|
import { createError } from 'h3'
|
|
2
2
|
import type { H3Event } from 'h3'
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
import { checkAdminAccess } from './auth'
|
|
5
5
|
import { filterHiddenFields, filterPublicColumns } from './modelMapper'
|
|
6
6
|
|
|
7
|
-
// @ts-expect-error - #imports is available in runtime
|
|
8
|
-
import { getUserSession } from '#imports'
|
|
9
|
-
|
|
10
7
|
export async function ensureResourceAccess(event: H3Event, model: string, action: string): Promise<boolean> {
|
|
11
|
-
const { auth } = useAutoCrudConfig()
|
|
12
|
-
|
|
13
8
|
// This throws 403 if not authorized
|
|
14
9
|
const isAuthorized = await checkAdminAccess(event, model, action)
|
|
15
10
|
if (!isAuthorized) {
|
|
@@ -19,14 +14,7 @@ export async function ensureResourceAccess(event: H3Event, model: string, action
|
|
|
19
14
|
})
|
|
20
15
|
}
|
|
21
16
|
|
|
22
|
-
|
|
23
|
-
if (!auth?.authentication) {
|
|
24
|
-
return true
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Check if user is authenticated
|
|
28
|
-
const session = await getUserSession(event)
|
|
29
|
-
return !!session.user
|
|
17
|
+
return true
|
|
30
18
|
}
|
|
31
19
|
|
|
32
20
|
export function formatResourceResult(model: string, data: Record<string, unknown>, isAdmin: boolean) {
|