dolphin-server-modules 1.0.0 → 1.0.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 +120 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# Dolphin Server Modules 🐬
|
|
2
|
+
|
|
3
|
+
A world-class, extremely lightweight, and 100% modular backend utility package for Node.js. It provides plug-and-play modules for Authentication, standard CRUD operations, Next.js/Express Controllers, and Zod validation.
|
|
4
|
+
|
|
5
|
+
Build production-grade APIs in minutes!
|
|
6
|
+
|
|
7
|
+
## 📦 Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install dolphin-server-modules
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
You will also need to install `argon2` and `zod` if you are using the auth and validation modules respectively:
|
|
14
|
+
```bash
|
|
15
|
+
npm install argon2 zod
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 🚀 100% Modular Structure
|
|
19
|
+
You can import exactly what you need without bloating your project.
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { createAuth } from 'dolphin-server-modules/auth';
|
|
23
|
+
import { createCRUD } from 'dolphin-server-modules/crud';
|
|
24
|
+
import { createDolphinController } from 'dolphin-server-modules/controller';
|
|
25
|
+
import { validateStructure } from 'dolphin-server-modules/middleware/zod';
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 🔒 1. Auth Module (`/auth`)
|
|
31
|
+
Production-ready authentication supporting **argon2 password hashing**, **JWT**, and **TOTP (2FA)**. It also ships with internal memory LRU caches for protecting against token-reuse and rate-limiting.
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { createAuth } from 'dolphin-server-modules/auth';
|
|
35
|
+
|
|
36
|
+
const auth = createAuth({
|
|
37
|
+
secret: 'YOUR_SUPER_SECRET_KEY',
|
|
38
|
+
cookieMaxAge: 7 * 86400000 // 7 days
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Register
|
|
42
|
+
const user = await auth.register(db, { email: 'user@example.com', password: 'password123' });
|
|
43
|
+
|
|
44
|
+
// Login (Supports 2FA Totp)
|
|
45
|
+
const session = await auth.login(db, { email: 'user@example.com', password: 'password123' });
|
|
46
|
+
console.log(session.accessToken);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## 💾 2. CRUD Module (`/crud`)
|
|
52
|
+
A powerful Factory interface for databases providing instant pagination, soft-deletes, and ownership-checks out of the box.
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { createCRUD } from 'dolphin-server-modules/crud';
|
|
56
|
+
|
|
57
|
+
const crud = createCRUD(myDatabaseAdapter, {
|
|
58
|
+
softDelete: true,
|
|
59
|
+
enforceOwnership: true
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Automatically creates a record with generated IDs and createdAt fields
|
|
63
|
+
const newPost = await crud.create('posts', { title: 'Hello World' }, 'user_id_1');
|
|
64
|
+
|
|
65
|
+
// Read with Advanced Filtering
|
|
66
|
+
const posts = await crud.read('posts', {
|
|
67
|
+
$or: [{ title: { $like: 'Hello' } }, { rating: { $gt: 4 } }]
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Soft Delete
|
|
71
|
+
await crud.deleteOne('posts', newPost.id, 'user_id_1');
|
|
72
|
+
|
|
73
|
+
// Restore Soft Deleted
|
|
74
|
+
await crud.restore('posts', newPost.id, 'user_id_1');
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## 🎮 3. Controller Module (`/controller`)
|
|
80
|
+
Easily convert your `crud` instance into instant API Controllers. Supports Next.js App Router, Pages API, and Express out of the box.
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { createDolphinController, createNextAppRoute } from 'dolphin-server-modules/controller';
|
|
84
|
+
|
|
85
|
+
const postController = createDolphinController(crud, 'posts');
|
|
86
|
+
|
|
87
|
+
// Next.js App Router API Route (app/api/posts/route.ts)
|
|
88
|
+
export const { GET, POST, PUT, DELETE } = createNextAppRoute(postController);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## ✅ 4. Zod Validation Middleware (`/middleware/zod`)
|
|
94
|
+
Validate your data effortlessly using `zod`. Protect your endpoints from bad data.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import { z } from 'zod';
|
|
98
|
+
import { validateAppRoute } from 'dolphin-server-modules/middleware/zod';
|
|
99
|
+
|
|
100
|
+
const userSchema = z.object({
|
|
101
|
+
name: z.string().min(2),
|
|
102
|
+
age: z.number().gte(18)
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// App Router Handler wrapped with validation
|
|
106
|
+
const myPostHandler = validateAppRoute(userSchema, async (req, validatedData) => {
|
|
107
|
+
console.log(validatedData.name); // 100% typed and safe
|
|
108
|
+
return new Response('Success');
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## 🌐 Hosting Docs via GitHub Pages
|
|
115
|
+
To host this documentation cleanly:
|
|
116
|
+
1. Go to your repository **Settings** on GitHub.
|
|
117
|
+
2. Click on **Pages** on the left sidebar.
|
|
118
|
+
3. Under **Build and deployment**, select **Deploy from a branch**.
|
|
119
|
+
4. Select `main` branch and `/ (root)` folder.
|
|
120
|
+
5. Click **Save**. GitHub will automatically host this README as your documentation website!
|