dolphin-server-modules 1.0.4 → 1.0.5
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 +47 -0
- package/TUTORIAL.md +116 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,6 +21,53 @@ npm install dolphin-server-modules
|
|
|
21
21
|
|
|
22
22
|
---
|
|
23
23
|
|
|
24
|
+
## 🚀 Quick Start (Complete Tutorial)
|
|
25
|
+
|
|
26
|
+
Building a high-performance API with Dolphin is simple. Here is a full example:
|
|
27
|
+
|
|
28
|
+
### 1. Define your Database (Mongoose)
|
|
29
|
+
```typescript
|
|
30
|
+
import mongoose from 'mongoose';
|
|
31
|
+
import { createMongooseAdapter } from 'dolphin-server-modules/adapters/mongoose';
|
|
32
|
+
|
|
33
|
+
const User = mongoose.model('User', new mongoose.Schema({
|
|
34
|
+
email: { type: String, required: true },
|
|
35
|
+
password: { type: String, required: true }
|
|
36
|
+
}));
|
|
37
|
+
|
|
38
|
+
const db = createMongooseAdapter({ User });
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 2. Initialize and Secure your Server
|
|
42
|
+
```typescript
|
|
43
|
+
import { createDolphinServer } from 'dolphin-server-modules/server';
|
|
44
|
+
import { createAuth } from 'dolphin-server-modules/auth';
|
|
45
|
+
|
|
46
|
+
const app = createDolphinServer();
|
|
47
|
+
const auth = createAuth({ secret: 'SUPER_SECRET' });
|
|
48
|
+
|
|
49
|
+
// Global Middleware
|
|
50
|
+
app.use((ctx, next) => {
|
|
51
|
+
console.log(`🐬 ${ctx.req.method} ${ctx.req.url}`);
|
|
52
|
+
next();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Hello World
|
|
56
|
+
app.get('/', (ctx) => ctx.json({ message: "Welcome to Dolphin!" }));
|
|
57
|
+
|
|
58
|
+
// Secure Route
|
|
59
|
+
app.get('/profile', auth.middleware(), (ctx) => {
|
|
60
|
+
ctx.json({ user: ctx.req.user });
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Dynamic Params
|
|
64
|
+
app.get('/users/:id', (ctx) => ctx.json({ id: ctx.params.id }));
|
|
65
|
+
|
|
66
|
+
app.listen(3000, () => console.log("Dolphin swimming on port 3000!"));
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
24
71
|
## 🛠️ Key Features
|
|
25
72
|
|
|
26
73
|
### ⚡ 1. Native High-Performance Server (`/server`)
|
package/TUTORIAL.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# Dolphin Framework Tutorial 🐬
|
|
2
|
+
|
|
3
|
+
Welcome to the official tutorial for the **Dolphin Framework**. This guide will take you from zero to a production-ready API using our native, high-performance modules.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 1. Project Setup
|
|
8
|
+
Create a new directory and initialize your project:
|
|
9
|
+
```bash
|
|
10
|
+
mkdir my-dolphin-app && cd my-dolphin-app
|
|
11
|
+
npm init -y
|
|
12
|
+
npm install dolphin-server-modules mongoose zod
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## 2. Database Setup (Mongoose)
|
|
18
|
+
Define your models using Mongoose:
|
|
19
|
+
```typescript
|
|
20
|
+
// models.ts
|
|
21
|
+
import mongoose from 'mongoose';
|
|
22
|
+
|
|
23
|
+
const UserSchema = new mongoose.Schema({
|
|
24
|
+
email: { type: String, required: true, unique: true },
|
|
25
|
+
password: { type: String, required: true }
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export const User = mongoose.model('User', UserSchema);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## 3. Initialize Dolphin
|
|
34
|
+
Create your main entry point and connect the components:
|
|
35
|
+
```typescript
|
|
36
|
+
// index.ts
|
|
37
|
+
import { createDolphinServer } from 'dolphin-server-modules/server';
|
|
38
|
+
import { createMongooseAdapter } from 'dolphin-server-modules/adapters/mongoose';
|
|
39
|
+
import { createAuth } from 'dolphin-server-modules/auth';
|
|
40
|
+
import { User } from './models';
|
|
41
|
+
|
|
42
|
+
// 1. Setup Adapter
|
|
43
|
+
const db = createMongooseAdapter({ User });
|
|
44
|
+
|
|
45
|
+
// 2. Setup Auth
|
|
46
|
+
const auth = createAuth({ secret: 'SUPER_SECRET' });
|
|
47
|
+
|
|
48
|
+
// 3. Setup Server
|
|
49
|
+
const app = createDolphinServer();
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## 4. Routing with Context (ctx)
|
|
55
|
+
Dolphin uses a unified `ctx` object for ultra-clean handlers.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
// Simple Route
|
|
59
|
+
app.get('/', (ctx) => {
|
|
60
|
+
ctx.json({ message: "Welcome to Dolphin!" });
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Dynamic Route with Params
|
|
64
|
+
app.get('/users/:id', (ctx) => {
|
|
65
|
+
const { id } = ctx.params;
|
|
66
|
+
ctx.json({ userId: id });
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Post Request with Body
|
|
70
|
+
app.post('/echo', (ctx) => {
|
|
71
|
+
ctx.json({ youSent: ctx.body });
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## 5. Adding Authentication Middleware
|
|
78
|
+
Secure your routes with the `auth` module:
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// Protecting a route
|
|
82
|
+
app.get('/profile', auth.middleware(), (ctx) => {
|
|
83
|
+
// ctx.req.user is automatically populated
|
|
84
|
+
ctx.json({ user: ctx.req.user });
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## 6. Global Middleware
|
|
91
|
+
Add logging or custom logic to every request:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
app.use((ctx, next) => {
|
|
95
|
+
console.log(`${ctx.req.method} ${ctx.req.url}`);
|
|
96
|
+
next();
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## 7. Starting the Server
|
|
103
|
+
```typescript
|
|
104
|
+
app.listen(3000, () => {
|
|
105
|
+
console.log("Dolphin is swimming on http://localhost:3000 🐬");
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## 🚀 Performance Tips
|
|
112
|
+
- Use **Context (ctx)** directly for JSON responses.
|
|
113
|
+
- Keep your routes organized using prefixing (coming in v1.1.0).
|
|
114
|
+
- Use the **Zod Middleware** for automatic type-safe validation.
|
|
115
|
+
|
|
116
|
+
Happy Coding!
|