nuxt-auto-crud 1.2.1 → 1.3.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 CHANGED
@@ -5,73 +5,141 @@
5
5
  [![License][license-src]][license-href]
6
6
  [![Nuxt][nuxt-src]][nuxt-href]
7
7
 
8
- Auto-generate RESTful CRUD APIs for your Nuxt app based solely on your database schema. No configuration needed!
8
+ Auto-generate RESTful CRUD APIs for your **Nuxt** application based solely on your database schema. No configuration needed!
9
9
 
10
10
  - [✨ Release Notes](/CHANGELOG.md)
11
11
  - [🎮 Try the Playground](/playground)
12
12
 
13
- ## Features
13
+ ## 🚀 CRUD APIs are ready to use without code
14
14
 
15
- - 🔄 **Auto-Detection** - Automatically detects all tables from your Drizzle schema
16
- - 🚀 **Zero Configuration** - Just define your schema, APIs are generated automatically
17
- - 🛡️ **Protected Fields** - Automatically protects `id` and `createdAt` fields from updates
18
- - 📝 **Full CRUD** - Complete Create, Read, Update, Delete operations out of the box
19
- - 🎯 **Type-Safe** - Fully typed with TypeScript support
20
- - 🔌 **Works with NuxtHub** - Seamlessly integrates with NuxtHub database
15
+ - `GET /api/:model` - List all records
16
+ - `POST /api/:model` - Create a new record
17
+ - `GET /api/:model/:id` - Get record by ID
18
+ - `PATCH /api/:model/:id` - Update record
19
+ - `DELETE /api/:model/:id` - Delete record
21
20
 
22
- ## 📦 Quick Setup
21
+ ## 📦 How to install
23
22
 
24
- ### 1. Install the module
23
+ ### New Project (Recommended)
24
+
25
+ Start a new project with everything pre-configured using our template:
25
26
 
26
27
  ```bash
27
- bun add nuxt-auto-crud
28
- # or
29
- npm install nuxt-auto-crud
28
+ npx nuxi init -t gh:clifordpereira/nuxt-auto-crud_template <project-name>
29
+ cd <project-name>
30
+ bun db:generate
31
+ bun run dev
30
32
  ```
31
33
 
32
- ### 2. Add to your Nuxt config
34
+ ### Add User
35
+ Open Nuxt DevTools (bottom-middle icon) > `...` menu > **Database** icon to add users.
36
+ > **Note:** If the users table doesn't appear, restart the server (`Ctrl + C` and `bun run dev`).
37
+
38
+ That's it! You can now access the APIs:
39
+
40
+ ### Test API
41
+ Visit [http://localhost:3000/api/users](http://localhost:3000/api/users).
42
+
43
+ ### Existing Project
44
+
45
+ If you want to add `nuxt-auto-crud` to an existing project, follow these steps:
46
+
47
+ ### 1. Install dependencies
48
+
49
+ ```bash
50
+ # Install module and required dependencies
51
+ bun add nuxt-auto-crud @nuxthub/core@latest drizzle-orm
52
+ bun add --dev wrangler drizzle-kit
53
+ ```
54
+
55
+ ### 2. Configure Nuxt
56
+
57
+ Add the modules to your `nuxt.config.ts`:
33
58
 
34
59
  ```typescript
35
60
  // nuxt.config.ts
36
61
  export default defineNuxtConfig({
37
- modules: ["@nuxthub/core", "nuxt-auto-crud"],
62
+ modules: ['@nuxthub/core', 'nuxt-auto-crud'],
38
63
 
39
64
  hub: {
40
65
  database: true,
41
66
  },
42
67
 
43
68
  autoCrud: {
44
- schemaPath: "server/database/schema", // default value
69
+ schemaPath: 'server/database/schema', // default value
45
70
  },
46
- });
71
+ })
72
+ ```
73
+
74
+ ### 3. Configure Drizzle
75
+
76
+ Add the generation script to your `package.json`:
77
+
78
+ ```json
79
+ {
80
+ "scripts": {
81
+ "db:generate": "drizzle-kit generate"
82
+ }
83
+ }
84
+ ```
85
+
86
+ Create `drizzle.config.ts` in your project root:
87
+
88
+ ```typescript
89
+ // drizzle.config.ts
90
+ import { defineConfig } from 'drizzle-kit'
91
+
92
+ export default defineConfig({
93
+ dialect: 'sqlite',
94
+ schema: './server/database/schema.ts',
95
+ out: './server/database/migrations'
96
+ })
47
97
  ```
48
98
 
49
- ### 3. Define your database schema
99
+ ### 4. Setup Database Connection
100
+
101
+ Create `server/utils/drizzle.ts` to export the database instance:
102
+
103
+ ```typescript
104
+ // server/utils/drizzle.ts
105
+ import { drizzle } from 'drizzle-orm/d1'
106
+ export { sql, eq, and, or } from 'drizzle-orm'
107
+
108
+ import * as schema from '../database/schema'
109
+
110
+ export const tables = schema
111
+
112
+ export function useDrizzle() {
113
+ return drizzle(hubDatabase(), { schema })
114
+ }
115
+
116
+ export type User = typeof schema.users.$inferSelect
117
+ ```
118
+
119
+ ### 5. Define your database schema
120
+
121
+ Create `server/database/schema.ts`:
50
122
 
51
123
  ```typescript
52
124
  // server/database/schema.ts
53
- import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";
54
-
55
- export const users = sqliteTable("users", {
56
- id: integer("id").primaryKey({ autoIncrement: true }),
57
- name: text("name").notNull(),
58
- email: text("email").notNull().unique(),
59
- bio: text("bio"),
60
- createdAt: integer("created_at", { mode: "timestamp" }).$defaultFn(
61
- () => new Date()
62
- ),
63
- });
125
+ import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core'
126
+
127
+ export const users = sqliteTable('users', {
128
+ id: integer('id').primaryKey({ autoIncrement: true }),
129
+ name: text('name').notNull(),
130
+ email: text('email').notNull().unique(),
131
+ password: text('password').notNull(),
132
+ avatar: text('avatar').notNull(),
133
+ createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
134
+ })
135
+ ```
64
136
 
65
- export const posts = sqliteTable("posts", {
66
- id: integer("id").primaryKey({ autoIncrement: true }),
67
- title: text("title").notNull(),
68
- content: text("content").notNull(),
69
- published: integer("published", { mode: "boolean" }).default(false),
70
- authorId: integer("author_id").references(() => users.id),
71
- createdAt: integer("created_at", { mode: "timestamp" }).$defaultFn(
72
- () => new Date()
73
- ),
74
- });
137
+ ### 6. Run the project
138
+
139
+ ```bash
140
+ cd <project-name>
141
+ bun db:generate
142
+ bun run dev
75
143
  ```
76
144
 
77
145
  That's it! 🎉 Your CRUD APIs are now available:
@@ -99,6 +167,7 @@ bun install
99
167
  # Run the playground
100
168
  cd playground
101
169
  bun install
170
+ bun db:generate
102
171
  bun run dev
103
172
  ```
104
173
 
@@ -112,9 +181,9 @@ The playground includes a sample schema with users, posts, and comments tables,
112
181
  const user = await $fetch("/api/users", {
113
182
  method: "POST",
114
183
  body: {
115
- name: "John Doe",
116
- email: "john@example.com",
117
- bio: "Software developer",
184
+ name: "Cliford Pereira",
185
+ email: "clifordpereira@gmail.com",
186
+ bio: "Full-Stack Developer",
118
187
  },
119
188
  });
120
189
  ```
@@ -183,35 +252,6 @@ You can customize updatable fields in your schema by modifying the `modelMapper.
183
252
 
184
253
  Contributions are welcome! Please check out the [contribution guide](/CONTRIBUTING.md).
185
254
 
186
- <details>
187
- <summary>Local development</summary>
188
-
189
- ```bash
190
- # Install dependencies
191
- bun install
192
-
193
- # Generate type stubs
194
- bun run dev:prepare
195
-
196
- # Develop with the playground
197
- bun run dev
198
-
199
- # Build the playground
200
- bun run dev:build
201
-
202
- # Run ESLint
203
- bun run lint
204
-
205
- # Run Vitest
206
- bun run test
207
- bun run test:watch
208
-
209
- # Release new version
210
- bun run release
211
- ```
212
-
213
- </details>
214
-
215
255
  ## 📝 License
216
256
 
217
257
  [MIT License](./LICENSE)
@@ -220,13 +260,4 @@ Contributions are welcome! Please check out the [contribution guide](/CONTRIBUTI
220
260
 
221
261
  Made with ❤️ by [Cliford Pereira](https://github.com/clifordpereira)
222
262
 
223
- <!-- Badges -->
224
263
 
225
- [npm-version-src]: https://img.shields.io/npm/v/nuxt-auto-crud/latest.svg?style=flat&colorA=020420&colorB=00DC82
226
- [npm-version-href]: https://npmjs.com/package/nuxt-auto-crud
227
- [npm-downloads-src]: https://img.shields.io/npm/dm/nuxt-auto-crud.svg?style=flat&colorA=020420&colorB=00DC82
228
- [npm-downloads-href]: https://npm.chart.dev/nuxt-auto-crud
229
- [license-src]: https://img.shields.io/npm/l/nuxt-auto-crud.svg?style=flat&colorA=020420&colorB=00DC82
230
- [license-href]: https://npmjs.com/package/nuxt-auto-crud
231
- [nuxt-src]: https://img.shields.io/badge/Nuxt-020420?logo=nuxt.js
232
- [nuxt-href]: https://nuxt.com
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-auto-crud",
3
3
  "configKey": "autoCrud",
4
- "version": "1.2.1",
4
+ "version": "1.3.0",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-auto-crud",
3
- "version": "1.2.1",
3
+ "version": "1.3.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",
@@ -47,13 +47,13 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "@nuxt/kit": "^4.2.1",
50
- "@types/pluralize": "^0.0.33"
51
- },
52
- "peerDependencies": {
53
- "drizzle-orm": "^0.30.0",
50
+ "@types/pluralize": "^0.0.33",
54
51
  "pluralize": "^8.0.0",
55
52
  "scule": "^1.0.0"
56
53
  },
54
+ "peerDependencies": {
55
+ "drizzle-orm": "^0.30.0"
56
+ },
57
57
  "devDependencies": {
58
58
  "@nuxt/devtools": "^3.1.0",
59
59
  "@nuxt/eslint-config": "^1.10.0",