nuxt-auto-crud 1.4.0 → 1.5.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/README.md +91 -82
- package/dist/module.d.mts +26 -22
- package/dist/module.json +1 -1
- package/dist/module.mjs +34 -5
- package/dist/runtime/server/utils/auth.js +1 -1
- package/dist/runtime/server/utils/config.d.ts +2 -2
- package/package.json +5 -5
- package/src/runtime/server/utils/auth.ts +1 -1
- package/src/runtime/server/utils/config.ts +3 -3
package/README.md
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# Nuxt Auto CRUD
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
3
|
> **Note:** This module is currently in its alpha stage. However, you can use it to accelerate MVP development. It has not been tested thoroughly enough for production use; only happy-path testing is performed for each release.
|
|
6
4
|
|
|
7
5
|
Auto-generate RESTful CRUD APIs for your **Nuxt** application based solely on your database schema. Minimal configuration required.
|
|
@@ -19,7 +17,7 @@ Auto-generate RESTful CRUD APIs for your **Nuxt** application based solely on yo
|
|
|
19
17
|
|
|
20
18
|
## 📦 How to install
|
|
21
19
|
|
|
22
|
-
###
|
|
20
|
+
### 1. Fullstack Template (Recommended)
|
|
23
21
|
|
|
24
22
|
Start a new project with everything pre-configured using our template:
|
|
25
23
|
|
|
@@ -33,22 +31,13 @@ bun run dev
|
|
|
33
31
|
|
|
34
32
|
Detailed instructions can be found in [https://auto-crud.clifland.in/](https://auto-crud.clifland.in/)
|
|
35
33
|
|
|
36
|
-
###
|
|
37
|
-
Open Nuxt DevTools (bottom-middle icon) > `...` menu > **Database** icon to add users.
|
|
38
|
-
> **Note:** If the users table doesn't appear, restart the server (`Ctrl + C` and `bun run dev`).
|
|
39
|
-
|
|
40
|
-
That's it! You can now access the APIs:
|
|
41
|
-
|
|
42
|
-
### Test API
|
|
43
|
-
Visit [http://localhost:3000/api/users](http://localhost:3000/api/users).
|
|
44
|
-
|
|
45
|
-
### Existing Project
|
|
34
|
+
### 2. Manual Setup (Existing Project)
|
|
46
35
|
|
|
47
36
|
If you want to add `nuxt-auto-crud` to an existing project, follow these steps:
|
|
48
37
|
|
|
49
38
|
> **Note:** These instructions assume you are using NuxtHub. If you are using a custom SQLite setup (e.g. better-sqlite3, Turso), please see [Custom Setup](./custom-setup.md).
|
|
50
39
|
|
|
51
|
-
|
|
40
|
+
#### Install dependencies
|
|
52
41
|
|
|
53
42
|
```bash
|
|
54
43
|
# Install module and required dependencies
|
|
@@ -60,7 +49,7 @@ bun add nuxt-auto-crud @nuxthub/core@latest drizzle-orm
|
|
|
60
49
|
bun add --dev wrangler drizzle-kit
|
|
61
50
|
```
|
|
62
51
|
|
|
63
|
-
|
|
52
|
+
#### Configure Nuxt
|
|
64
53
|
|
|
65
54
|
Add the modules to your `nuxt.config.ts`:
|
|
66
55
|
|
|
@@ -75,11 +64,12 @@ export default defineNuxtConfig({
|
|
|
75
64
|
|
|
76
65
|
autoCrud: {
|
|
77
66
|
schemaPath: 'server/database/schema', // default value
|
|
67
|
+
auth: false, // Disable auth by default for easy testing
|
|
78
68
|
},
|
|
79
69
|
})
|
|
80
70
|
```
|
|
81
71
|
|
|
82
|
-
|
|
72
|
+
#### Configure Drizzle
|
|
83
73
|
|
|
84
74
|
Add the generation script to your `package.json`:
|
|
85
75
|
|
|
@@ -104,7 +94,7 @@ export default defineConfig({
|
|
|
104
94
|
})
|
|
105
95
|
```
|
|
106
96
|
|
|
107
|
-
|
|
97
|
+
#### Setup Database Connection
|
|
108
98
|
|
|
109
99
|
Create `server/utils/drizzle.ts` to export the database instance:
|
|
110
100
|
|
|
@@ -124,7 +114,7 @@ export function useDrizzle() {
|
|
|
124
114
|
export type User = typeof schema.users.$inferSelect
|
|
125
115
|
```
|
|
126
116
|
|
|
127
|
-
|
|
117
|
+
#### Define your database schema
|
|
128
118
|
|
|
129
119
|
Create `server/database/schema.ts`:
|
|
130
120
|
|
|
@@ -142,7 +132,7 @@ export const users = sqliteTable('users', {
|
|
|
142
132
|
})
|
|
143
133
|
```
|
|
144
134
|
|
|
145
|
-
|
|
135
|
+
#### Run the project
|
|
146
136
|
|
|
147
137
|
```bash
|
|
148
138
|
cd <project-name>
|
|
@@ -150,15 +140,82 @@ bun db:generate
|
|
|
150
140
|
bun run dev
|
|
151
141
|
```
|
|
152
142
|
|
|
153
|
-
That's it! 🎉 Your CRUD APIs are now available
|
|
143
|
+
That's it! 🎉 Your CRUD APIs are now available at `/api/users`.
|
|
144
|
+
|
|
145
|
+
### 3. Backend-only App (API Mode)
|
|
154
146
|
|
|
155
|
-
|
|
156
|
-
- `POST /api/users` - Create a new user
|
|
157
|
-
- `GET /api/users/:id` - Get user by ID
|
|
158
|
-
- `PATCH /api/users/:id` - Update user
|
|
159
|
-
- `DELETE /api/users/:id` - Delete user
|
|
147
|
+
If you are using Nuxt as a backend for a separate client application (e.g., mobile app, SPA), you can use this module to quickly generate REST APIs.
|
|
160
148
|
|
|
161
|
-
|
|
149
|
+
In this case, you might handle authentication differently (e.g., validating tokens in middleware) or disable the built-in auth checks if you have a global auth middleware.
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
export default defineNuxtConfig({
|
|
153
|
+
modules: ['nuxt-auto-crud'],
|
|
154
|
+
autoCrud: {
|
|
155
|
+
auth: false // APIs are public (or handled by your own middleware)
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## 🔐 Authentication Configuration
|
|
161
|
+
|
|
162
|
+
The module supports `auth: false` by default, which exposes all LCRUD APIs. You can enable authentication and authorization as needed.
|
|
163
|
+
|
|
164
|
+
### Session Auth (Default)
|
|
165
|
+
|
|
166
|
+
Requires `nuxt-auth-utils` and `nuxt-authorization`.
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
export default defineNuxtConfig({
|
|
170
|
+
autoCrud: {
|
|
171
|
+
auth: {
|
|
172
|
+
type: 'session',
|
|
173
|
+
authentication: true, // Enables requireUserSession() check
|
|
174
|
+
authorization: true // Enables authorize(model, action) check
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
})
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### JWT Auth
|
|
181
|
+
|
|
182
|
+
Useful for backend-only apps.
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
export default defineNuxtConfig({
|
|
186
|
+
autoCrud: {
|
|
187
|
+
auth: {
|
|
188
|
+
type: 'jwt',
|
|
189
|
+
authentication: true,
|
|
190
|
+
jwtSecret: process.env.JWT_SECRET,
|
|
191
|
+
authorization: true
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
})
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## 🛡️ Resource Configuration (RBAC)
|
|
198
|
+
|
|
199
|
+
You can define fine-grained access control and resource policies using `autocrud.config.ts` in your project root. This file is optional and useful when you need specific rules per resource.
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
// autocrud.config.ts
|
|
203
|
+
export default {
|
|
204
|
+
resources: {
|
|
205
|
+
users: {
|
|
206
|
+
// Access Control
|
|
207
|
+
auth: {
|
|
208
|
+
// Admin has full access
|
|
209
|
+
admin: true,
|
|
210
|
+
// Public (unauthenticated) users can only list and read
|
|
211
|
+
public: ['list', 'read'],
|
|
212
|
+
},
|
|
213
|
+
// Field Visibility
|
|
214
|
+
publicColumns: ['id', 'name', 'avatar'], // Only these columns are returned to public users
|
|
215
|
+
},
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
```
|
|
162
219
|
|
|
163
220
|
## 🎮 Try the Playground
|
|
164
221
|
|
|
@@ -172,14 +229,18 @@ cd nuxt-auto-crud
|
|
|
172
229
|
# Install dependencies (parent folder)
|
|
173
230
|
bun install
|
|
174
231
|
|
|
175
|
-
# Run the playground (
|
|
176
|
-
cd playground
|
|
232
|
+
# Run the playground (Fullstack)
|
|
233
|
+
cd playground
|
|
177
234
|
bun install
|
|
178
235
|
bun db:generate
|
|
179
236
|
bun run dev
|
|
180
|
-
```
|
|
181
237
|
|
|
182
|
-
|
|
238
|
+
# Run the playground (Backend Only)
|
|
239
|
+
cd playground-backendonly
|
|
240
|
+
bun install
|
|
241
|
+
bun db:generate
|
|
242
|
+
bun run dev
|
|
243
|
+
```
|
|
183
244
|
|
|
184
245
|
## 📖 Usage Examples
|
|
185
246
|
|
|
@@ -227,56 +288,6 @@ await $fetch("/api/users/1", {
|
|
|
227
288
|
});
|
|
228
289
|
```
|
|
229
290
|
|
|
230
|
-
## Use Cases
|
|
231
|
-
|
|
232
|
-
### 1. Full-stack App (with Auth)
|
|
233
|
-
|
|
234
|
-
If you are building a full-stack Nuxt application, you can easily integrate `nuxt-auth-utils` and `nuxt-authorization` to secure your auto-generated APIs.
|
|
235
|
-
|
|
236
|
-
First, install the modules:
|
|
237
|
-
|
|
238
|
-
```bash
|
|
239
|
-
npx nuxi@latest module add auth-utils
|
|
240
|
-
npm install nuxt-authorization
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
Then, configure `nuxt-auto-crud` in your `nuxt.config.ts`:
|
|
244
|
-
|
|
245
|
-
```ts
|
|
246
|
-
export default defineNuxtConfig({
|
|
247
|
-
modules: [
|
|
248
|
-
'nuxt-auto-crud',
|
|
249
|
-
'nuxt-auth-utils'
|
|
250
|
-
],
|
|
251
|
-
autoCrud: {
|
|
252
|
-
auth: {
|
|
253
|
-
enabled: true, // Enables requireUserSession() check
|
|
254
|
-
authorization: true // Enables authorize(model, action) check
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
})
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
When `authorization` is enabled, the module will call `authorize(model, action)` where action is one of: `create`, `read`, `update`, `delete`.
|
|
261
|
-
|
|
262
|
-
### 2. Backend-only App (API Mode)
|
|
263
|
-
|
|
264
|
-
If you are using Nuxt as a backend for a separate client application (e.g., mobile app, SPA), you can use this module to quickly generate REST APIs.
|
|
265
|
-
|
|
266
|
-
In this case, you might handle authentication differently (e.g., validating tokens in middleware) or disable the built-in auth checks if you have a global auth middleware.
|
|
267
|
-
|
|
268
|
-
```ts
|
|
269
|
-
export default defineNuxtConfig({
|
|
270
|
-
modules: ['nuxt-auto-crud'],
|
|
271
|
-
autoCrud: {
|
|
272
|
-
auth: {
|
|
273
|
-
enabled: false, // Default
|
|
274
|
-
authorization: false // Default
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
})
|
|
278
|
-
```
|
|
279
|
-
|
|
280
291
|
## Configuration
|
|
281
292
|
|
|
282
293
|
### Module Options
|
|
@@ -327,5 +338,3 @@ Contributions are welcome! Please check out the [contribution guide](/CONTRIBUTI
|
|
|
327
338
|
## 👨💻 Author
|
|
328
339
|
|
|
329
340
|
Made with ❤️ by [Cliford Pereira](https://github.com/clifordpereira)
|
|
330
|
-
|
|
331
|
-
|
package/dist/module.d.mts
CHANGED
|
@@ -14,27 +14,7 @@ interface ModuleOptions {
|
|
|
14
14
|
/**
|
|
15
15
|
* Authentication configuration
|
|
16
16
|
*/
|
|
17
|
-
auth?:
|
|
18
|
-
/**
|
|
19
|
-
* Authentication type
|
|
20
|
-
* @default 'session'
|
|
21
|
-
*/
|
|
22
|
-
type?: 'session' | 'jwt';
|
|
23
|
-
/**
|
|
24
|
-
* JWT Secret (required if type is 'jwt')
|
|
25
|
-
*/
|
|
26
|
-
jwtSecret?: string;
|
|
27
|
-
/**
|
|
28
|
-
* Enable authentication checks (requires nuxt-auth-utils for session)
|
|
29
|
-
* @default false
|
|
30
|
-
*/
|
|
31
|
-
enabled: boolean;
|
|
32
|
-
/**
|
|
33
|
-
* Enable authorization checks (requires nuxt-authorization)
|
|
34
|
-
* @default false
|
|
35
|
-
*/
|
|
36
|
-
authorization?: boolean;
|
|
37
|
-
};
|
|
17
|
+
auth?: boolean | AuthOptions;
|
|
38
18
|
/**
|
|
39
19
|
* Resource-specific configuration
|
|
40
20
|
* Define public access and column visibility
|
|
@@ -55,10 +35,34 @@ interface ModuleOptions {
|
|
|
55
35
|
};
|
|
56
36
|
};
|
|
57
37
|
}
|
|
38
|
+
interface AuthOptions {
|
|
39
|
+
/**
|
|
40
|
+
* Authentication type
|
|
41
|
+
* @default 'session'
|
|
42
|
+
*/
|
|
43
|
+
type?: 'session' | 'jwt';
|
|
44
|
+
/**
|
|
45
|
+
* JWT Secret (required if type is 'jwt')
|
|
46
|
+
*/
|
|
47
|
+
jwtSecret?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Enable authentication checks (requires nuxt-auth-utils for session)
|
|
50
|
+
* @default false
|
|
51
|
+
*/
|
|
52
|
+
authentication: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Enable authorization checks (requires nuxt-authorization)
|
|
55
|
+
* @default false
|
|
56
|
+
*/
|
|
57
|
+
authorization?: boolean;
|
|
58
|
+
}
|
|
59
|
+
interface RuntimeModuleOptions extends Omit<ModuleOptions, 'auth'> {
|
|
60
|
+
auth: AuthOptions;
|
|
61
|
+
}
|
|
58
62
|
|
|
59
63
|
declare module '@nuxt/schema' {
|
|
60
64
|
interface RuntimeConfig {
|
|
61
|
-
autoCrud:
|
|
65
|
+
autoCrud: RuntimeModuleOptions;
|
|
62
66
|
}
|
|
63
67
|
}
|
|
64
68
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -7,7 +7,8 @@ const module$1 = defineNuxtModule({
|
|
|
7
7
|
},
|
|
8
8
|
defaults: {
|
|
9
9
|
schemaPath: "server/database/schema",
|
|
10
|
-
drizzlePath: "server/utils/drizzle"
|
|
10
|
+
drizzlePath: "server/utils/drizzle",
|
|
11
|
+
auth: false
|
|
11
12
|
},
|
|
12
13
|
async setup(options, nuxt) {
|
|
13
14
|
const resolver = createResolver(import.meta.url);
|
|
@@ -27,17 +28,45 @@ const module$1 = defineNuxtModule({
|
|
|
27
28
|
name: "autocrud",
|
|
28
29
|
cwd: nuxt.options.rootDir
|
|
29
30
|
});
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
let mergedAuth = {
|
|
32
|
+
authentication: false,
|
|
33
|
+
authorization: false,
|
|
34
|
+
type: "session"
|
|
33
35
|
};
|
|
36
|
+
if (options.auth === true) {
|
|
37
|
+
mergedAuth = {
|
|
38
|
+
authentication: true,
|
|
39
|
+
authorization: true,
|
|
40
|
+
type: "session",
|
|
41
|
+
...typeof externalConfig?.auth === "object" ? externalConfig.auth : {}
|
|
42
|
+
};
|
|
43
|
+
} else if (options.auth === false) {
|
|
44
|
+
mergedAuth = {
|
|
45
|
+
authentication: false,
|
|
46
|
+
authorization: false,
|
|
47
|
+
type: "session"
|
|
48
|
+
};
|
|
49
|
+
} else {
|
|
50
|
+
mergedAuth = {
|
|
51
|
+
authentication: true,
|
|
52
|
+
// Default to true if object provided? Or undefined?
|
|
53
|
+
// If options.auth is undefined, we might want defaults.
|
|
54
|
+
// But if defaults say auth: false, then options.auth might be undefined.
|
|
55
|
+
// Let's stick to the plan: default is false.
|
|
56
|
+
...typeof externalConfig?.auth === "object" ? externalConfig.auth : {},
|
|
57
|
+
...typeof options.auth === "object" ? options.auth : {}
|
|
58
|
+
};
|
|
59
|
+
if (mergedAuth.authentication === void 0) {
|
|
60
|
+
mergedAuth.authentication = false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
34
63
|
const mergedResources = {
|
|
35
64
|
...externalConfig?.resources,
|
|
36
65
|
...options.resources
|
|
37
66
|
};
|
|
38
67
|
nuxt.options.runtimeConfig.autoCrud = {
|
|
39
68
|
auth: {
|
|
40
|
-
|
|
69
|
+
authentication: mergedAuth.authentication ?? false,
|
|
41
70
|
authorization: mergedAuth.authorization ?? false,
|
|
42
71
|
type: mergedAuth.type ?? "session",
|
|
43
72
|
jwtSecret: mergedAuth.jwtSecret
|
|
@@ -3,7 +3,7 @@ import { useAutoCrudConfig } from "./config.js";
|
|
|
3
3
|
import { verifyJwtToken } from "./jwt.js";
|
|
4
4
|
export async function checkAdminAccess(event, model, action) {
|
|
5
5
|
const { auth } = useAutoCrudConfig();
|
|
6
|
-
if (!auth?.
|
|
6
|
+
if (!auth?.authentication) {
|
|
7
7
|
return true;
|
|
8
8
|
}
|
|
9
9
|
if (auth.type === "jwt") {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const useAutoCrudConfig: () =>
|
|
1
|
+
import type { RuntimeModuleOptions } from '../../../types.js';
|
|
2
|
+
export declare const useAutoCrudConfig: () => RuntimeModuleOptions;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-auto-crud",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
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",
|
|
@@ -35,15 +35,15 @@
|
|
|
35
35
|
],
|
|
36
36
|
"scripts": {
|
|
37
37
|
"prepack": "nuxt-module-build build",
|
|
38
|
-
"dev": "npm run dev:prepare && nuxi dev playground
|
|
39
|
-
"dev:build": "nuxi build playground
|
|
40
|
-
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground
|
|
38
|
+
"dev": "npm run dev:prepare && nuxi dev playground",
|
|
39
|
+
"dev:build": "nuxi build playground",
|
|
40
|
+
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
|
|
41
41
|
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
|
|
42
42
|
"lint": "eslint .",
|
|
43
43
|
"test": "vitest run",
|
|
44
44
|
"test:api": "node scripts/test-api.mjs",
|
|
45
45
|
"test:watch": "vitest watch",
|
|
46
|
-
"test:types": "vue-tsc --noEmit && cd playground
|
|
46
|
+
"test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit",
|
|
47
47
|
"link": "npm link"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
@@ -6,7 +6,7 @@ import { verifyJwtToken } from './jwt'
|
|
|
6
6
|
export async function checkAdminAccess(event: H3Event, model: string, action: string): Promise<boolean> {
|
|
7
7
|
const { auth } = useAutoCrudConfig()
|
|
8
8
|
|
|
9
|
-
if (!auth?.
|
|
9
|
+
if (!auth?.authentication) {
|
|
10
10
|
return true
|
|
11
11
|
}
|
|
12
12
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useRuntimeConfig } from '#imports'
|
|
2
|
-
import type {
|
|
2
|
+
import type { RuntimeModuleOptions } from '../../../types'
|
|
3
3
|
|
|
4
|
-
export const useAutoCrudConfig = ():
|
|
5
|
-
return useRuntimeConfig().autoCrud as
|
|
4
|
+
export const useAutoCrudConfig = (): RuntimeModuleOptions => {
|
|
5
|
+
return useRuntimeConfig().autoCrud as RuntimeModuleOptions
|
|
6
6
|
}
|