deploy-bbc 1.2.4 → 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 +49 -4
- package/dist/index.js +122 -67
- package/dist/templates/base/.env.example +1 -1
- package/dist/templates/base/package.json +2 -1
- package/dist/templates/base/src/config/index.ts +1 -1
- package/dist/templates/base/src/controllers/user/create-user.controller.ts +84 -0
- package/dist/templates/base/src/controllers/user/delete-user.controller.ts +60 -0
- package/dist/templates/base/src/controllers/user/get-user.controller.ts +74 -0
- package/dist/templates/base/src/controllers/user/get-users.controller.ts +41 -0
- package/dist/templates/base/src/controllers/user/update-user.controller.ts +111 -0
- package/dist/templates/base/src/models/user.model.ts +83 -0
- package/dist/templates/base/src/routes/index.ts +5 -0
- package/dist/templates/base/src/routes/user.route.ts +17 -0
- package/dist/templates/base/src/types/common/api-response.types.ts +30 -0
- package/dist/templates/base/src/types/index.ts +5 -2
- package/dist/templates/base/src/types/models/user.types.ts +26 -0
- package/dist/templates/base-bun-native/.env.example +1 -1
- package/dist/templates/base-bun-native/package.json +2 -1
- package/dist/templates/base-bun-native/src/config/index.ts +1 -1
- package/dist/templates/base-bun-native/src/controllers/user/create-user.controller.ts +76 -0
- package/dist/templates/base-bun-native/src/controllers/user/delete-user.controller.ts +53 -0
- package/dist/templates/base-bun-native/src/controllers/user/get-user.controller.ts +67 -0
- package/dist/templates/base-bun-native/src/controllers/user/get-users.controller.ts +40 -0
- package/dist/templates/base-bun-native/src/controllers/user/update-user.controller.ts +101 -0
- package/dist/templates/base-bun-native/src/index.ts +3 -3
- package/dist/templates/base-bun-native/src/models/user.model.ts +83 -0
- package/dist/templates/base-bun-native/src/routes/index.ts +10 -6
- package/dist/templates/base-bun-native/src/routes/user.route.ts +50 -0
- package/dist/templates/base-bun-native/src/types/common/api-response.types.ts +30 -0
- package/dist/templates/base-bun-native/src/types/index.ts +5 -2
- package/dist/templates/base-bun-native/src/types/models/user.types.ts +26 -0
- package/dist/templates/base-express/.env.example +1 -1
- package/dist/templates/base-express/package.json +2 -1
- package/dist/templates/base-express/src/config/index.ts +1 -1
- package/dist/templates/base-express/src/controllers/user/create-user.controller.ts +75 -0
- package/dist/templates/base-express/src/controllers/user/delete-user.controller.ts +56 -0
- package/dist/templates/base-express/src/controllers/user/get-user.controller.ts +70 -0
- package/dist/templates/base-express/src/controllers/user/get-users.controller.ts +41 -0
- package/dist/templates/base-express/src/controllers/user/update-user.controller.ts +102 -0
- package/dist/templates/base-express/src/models/user.model.ts +83 -0
- package/dist/templates/base-express/src/routes/index.ts +5 -0
- package/dist/templates/base-express/src/routes/user.route.ts +17 -0
- package/dist/templates/base-express/src/types/common/api-response.types.ts +30 -0
- package/dist/templates/base-express/src/types/index.ts +5 -2
- package/dist/templates/base-express/src/types/models/user.types.ts +26 -0
- package/dist/templates/extras/database/mongodb/src/db/index.ts +70 -0
- package/dist/templates/extras/database/mongodb/src/db/models/user.model.ts +87 -0
- package/dist/templates/extras/database/mysql/src/db/index.ts +106 -0
- package/dist/templates/extras/database/postgres/src/db/index.ts +96 -0
- package/dist/templates/extras/database/redis/src/db/redis.ts +215 -0
- package/dist/templates/extras/database/sqlite/src/db/index.ts +185 -0
- package/dist/templates/templates/base/.dockerignore +45 -0
- package/dist/templates/templates/base/src/models/user.model.ts +1 -1
- package/dist/templates/templates/base-bun-native/.dockerignore +45 -0
- package/dist/templates/templates/base-bun-native/src/models/user.model.ts +1 -1
- package/dist/templates/templates/base-express/.dockerignore +45 -0
- package/dist/templates/templates/base-express/src/models/user.model.ts +1 -1
- package/dist/templates/templates/extras/database/mongodb/src/db/index.ts +70 -0
- package/dist/templates/templates/extras/database/mongodb/src/db/models/user.model.ts +87 -0
- package/dist/templates/templates/extras/database/mysql/src/db/index.ts +106 -0
- package/dist/templates/templates/extras/database/postgres/src/db/index.ts +96 -0
- package/dist/templates/templates/extras/database/redis/src/db/redis.ts +215 -0
- package/dist/templates/templates/extras/database/sqlite/src/db/index.ts +185 -0
- package/package.json +1 -1
- package/dist/templates/extras/database/mysql/drizzle.config.ts +0 -0
- package/dist/templates/templates/extras/database/mysql/drizzle.config.ts +0 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { createClient } from "redis";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Redis client setup
|
|
5
|
+
* Docs: https://github.com/redis/node-redis
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const REDIS_URL = process.env.REDIS_URL || "redis://localhost:6379";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Create Redis client instance
|
|
12
|
+
*/
|
|
13
|
+
export const redis_client = createClient({
|
|
14
|
+
url: REDIS_URL,
|
|
15
|
+
socket: {
|
|
16
|
+
reconnectStrategy: (retries) => {
|
|
17
|
+
if (retries > 10) {
|
|
18
|
+
console.error("❌ Redis connection failed after 10 retries");
|
|
19
|
+
return new Error("Redis connection failed");
|
|
20
|
+
}
|
|
21
|
+
// Exponential backoff: 50ms, 100ms, 200ms, etc.
|
|
22
|
+
return Math.min(retries * 50, 3000);
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Connect to Redis
|
|
29
|
+
*/
|
|
30
|
+
export async function connect_redis(): Promise<void> {
|
|
31
|
+
try {
|
|
32
|
+
await redis_client.connect();
|
|
33
|
+
console.log("✅ Redis connected successfully");
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error("❌ Redis connection failed:", error);
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Disconnect from Redis
|
|
42
|
+
* Call this when shutting down the application
|
|
43
|
+
*/
|
|
44
|
+
export async function disconnect_redis(): Promise<void> {
|
|
45
|
+
await redis_client.quit();
|
|
46
|
+
console.log("Redis connection closed");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Test Redis connection
|
|
51
|
+
*/
|
|
52
|
+
export async function test_connection(): Promise<boolean> {
|
|
53
|
+
try {
|
|
54
|
+
await redis_client.ping();
|
|
55
|
+
console.log("✅ Redis ping successful");
|
|
56
|
+
return true;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error("❌ Redis ping failed:", error);
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Event listeners
|
|
64
|
+
redis_client.on("error", (err) => {
|
|
65
|
+
console.error("Redis Client Error:", err);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
redis_client.on("connect", () => {
|
|
69
|
+
console.log("Redis client connected");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
redis_client.on("ready", () => {
|
|
73
|
+
console.log("Redis client ready");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
redis_client.on("reconnecting", () => {
|
|
77
|
+
console.log("Redis client reconnecting...");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Helper functions for common operations
|
|
81
|
+
export const redis = {
|
|
82
|
+
/**
|
|
83
|
+
* Set a key-value pair
|
|
84
|
+
*/
|
|
85
|
+
set: async (key: string, value: string, expireSeconds?: number) => {
|
|
86
|
+
if (expireSeconds) {
|
|
87
|
+
return await redis_client.setEx(key, expireSeconds, value);
|
|
88
|
+
}
|
|
89
|
+
return await redis_client.set(key, value);
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Get value by key
|
|
94
|
+
*/
|
|
95
|
+
get: async (key: string) => {
|
|
96
|
+
return await redis_client.get(key);
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Delete a key
|
|
101
|
+
*/
|
|
102
|
+
del: async (key: string) => {
|
|
103
|
+
return await redis_client.del(key);
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Check if key exists
|
|
108
|
+
*/
|
|
109
|
+
exists: async (key: string) => {
|
|
110
|
+
return await redis_client.exists(key);
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Set expiration on a key
|
|
115
|
+
*/
|
|
116
|
+
expire: async (key: string, seconds: number) => {
|
|
117
|
+
return await redis_client.expire(key, seconds);
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Get all keys matching pattern
|
|
122
|
+
*/
|
|
123
|
+
keys: async (pattern: string) => {
|
|
124
|
+
return await redis_client.keys(pattern);
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Increment a value
|
|
129
|
+
*/
|
|
130
|
+
incr: async (key: string) => {
|
|
131
|
+
return await redis_client.incr(key);
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Decrement a value
|
|
136
|
+
*/
|
|
137
|
+
decr: async (key: string) => {
|
|
138
|
+
return await redis_client.decr(key);
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Hash operations
|
|
143
|
+
*/
|
|
144
|
+
hash: {
|
|
145
|
+
set: async (key: string, field: string, value: string) => {
|
|
146
|
+
return await redis_client.hSet(key, field, value);
|
|
147
|
+
},
|
|
148
|
+
get: async (key: string, field: string) => {
|
|
149
|
+
return await redis_client.hGet(key, field);
|
|
150
|
+
},
|
|
151
|
+
getAll: async (key: string) => {
|
|
152
|
+
return await redis_client.hGetAll(key);
|
|
153
|
+
},
|
|
154
|
+
del: async (key: string, field: string) => {
|
|
155
|
+
return await redis_client.hDel(key, field);
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* List operations
|
|
161
|
+
*/
|
|
162
|
+
list: {
|
|
163
|
+
push: async (key: string, ...values: string[]) => {
|
|
164
|
+
return await redis_client.lPush(key, values);
|
|
165
|
+
},
|
|
166
|
+
pop: async (key: string) => {
|
|
167
|
+
return await redis_client.lPop(key);
|
|
168
|
+
},
|
|
169
|
+
range: async (key: string, start: number, stop: number) => {
|
|
170
|
+
return await redis_client.lRange(key, start, stop);
|
|
171
|
+
},
|
|
172
|
+
length: async (key: string) => {
|
|
173
|
+
return await redis_client.lLen(key);
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Set operations
|
|
179
|
+
*/
|
|
180
|
+
set_ops: {
|
|
181
|
+
add: async (key: string, ...members: string[]) => {
|
|
182
|
+
return await redis_client.sAdd(key, members);
|
|
183
|
+
},
|
|
184
|
+
members: async (key: string) => {
|
|
185
|
+
return await redis_client.sMembers(key);
|
|
186
|
+
},
|
|
187
|
+
isMember: async (key: string, member: string) => {
|
|
188
|
+
return await redis_client.sIsMember(key, member);
|
|
189
|
+
},
|
|
190
|
+
remove: async (key: string, ...members: string[]) => {
|
|
191
|
+
return await redis_client.sRem(key, members);
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// Example usage:
|
|
197
|
+
/*
|
|
198
|
+
// Simple key-value
|
|
199
|
+
await redis.set("user:1:name", "John Doe");
|
|
200
|
+
await redis.set("session:abc123", "user_data", 3600); // Expires in 1 hour
|
|
201
|
+
const name = await redis.get("user:1:name");
|
|
202
|
+
|
|
203
|
+
// Hash (for objects)
|
|
204
|
+
await redis.hash.set("user:1", "email", "john@example.com");
|
|
205
|
+
await redis.hash.set("user:1", "age", "30");
|
|
206
|
+
const userData = await redis.hash.getAll("user:1");
|
|
207
|
+
|
|
208
|
+
// Lists (queues)
|
|
209
|
+
await redis.list.push("tasks", "task1", "task2");
|
|
210
|
+
const task = await redis.list.pop("tasks");
|
|
211
|
+
|
|
212
|
+
// Sets (unique values)
|
|
213
|
+
await redis.set_ops.add("online_users", "user1", "user2");
|
|
214
|
+
const isOnline = await redis.set_ops.isMember("online_users", "user1");
|
|
215
|
+
*/
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* SQLite database connection using Bun's built-in sqlite module
|
|
5
|
+
* Docs: https://bun.sh/docs/api/sqlite
|
|
6
|
+
*
|
|
7
|
+
* Benefits of bun:sqlite:
|
|
8
|
+
* - Zero dependencies (built into Bun)
|
|
9
|
+
* - Extremely fast (native implementation)
|
|
10
|
+
* - Type-safe prepared statements
|
|
11
|
+
* - Automatic statement caching
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const DATABASE_PATH = process.env.DATABASE_PATH || "./data/app.db";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Create SQLite database instance
|
|
18
|
+
* Options:
|
|
19
|
+
* - create: true - creates the database file if it doesn't exist
|
|
20
|
+
* - readwrite: allows both reading and writing
|
|
21
|
+
* - strict: enables strict mode for better type safety
|
|
22
|
+
*/
|
|
23
|
+
export const db = new Database(DATABASE_PATH, {
|
|
24
|
+
create: true,
|
|
25
|
+
readwrite: true,
|
|
26
|
+
strict: true,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Enable Write-Ahead Logging (WAL) mode for better concurrency
|
|
30
|
+
db.run("PRAGMA journal_mode = WAL");
|
|
31
|
+
|
|
32
|
+
// Enable foreign key constraints
|
|
33
|
+
db.run("PRAGMA foreign_keys = ON");
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Test database connection
|
|
37
|
+
*/
|
|
38
|
+
export function test_connection(): boolean {
|
|
39
|
+
try {
|
|
40
|
+
const result = db.query("SELECT 1 as test").get() as { test: number };
|
|
41
|
+
console.log("✅ SQLite connected successfully");
|
|
42
|
+
return result.test === 1;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error("❌ SQLite connection failed:", error);
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Close database connection
|
|
51
|
+
* Call this when shutting down the application
|
|
52
|
+
*/
|
|
53
|
+
export function close_connection(): void {
|
|
54
|
+
db.close();
|
|
55
|
+
console.log("SQLite connection closed");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Helper function to run migrations/DDL statements
|
|
60
|
+
*/
|
|
61
|
+
export function migrate(sql: string): void {
|
|
62
|
+
db.run(sql);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Example usage - uncomment and modify as needed
|
|
66
|
+
/*
|
|
67
|
+
// Create users table
|
|
68
|
+
export function create_users_table() {
|
|
69
|
+
db.run(`
|
|
70
|
+
CREATE TABLE IF NOT EXISTS users (
|
|
71
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
72
|
+
email TEXT UNIQUE NOT NULL,
|
|
73
|
+
name TEXT NOT NULL,
|
|
74
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
75
|
+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
76
|
+
)
|
|
77
|
+
`);
|
|
78
|
+
|
|
79
|
+
// Create index on email
|
|
80
|
+
db.run(`CREATE INDEX IF NOT EXISTS idx_users_email ON users(email)`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Get all users
|
|
84
|
+
export function get_all_users() {
|
|
85
|
+
return db.query("SELECT * FROM users").all();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Get user by ID
|
|
89
|
+
export function get_user_by_id(id: number) {
|
|
90
|
+
const stmt = db.query("SELECT * FROM users WHERE id = ?");
|
|
91
|
+
return stmt.get(id);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Get user by email
|
|
95
|
+
export function get_user_by_email(email: string) {
|
|
96
|
+
const stmt = db.query("SELECT * FROM users WHERE email = ?");
|
|
97
|
+
return stmt.get(email);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Create user
|
|
101
|
+
export function create_user(email: string, name: string) {
|
|
102
|
+
const stmt = db.prepare(`
|
|
103
|
+
INSERT INTO users (email, name)
|
|
104
|
+
VALUES (?, ?)
|
|
105
|
+
`);
|
|
106
|
+
|
|
107
|
+
const result = stmt.run(email, name);
|
|
108
|
+
return {
|
|
109
|
+
id: result.lastInsertRowid,
|
|
110
|
+
email,
|
|
111
|
+
name,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Update user
|
|
116
|
+
export function update_user(id: number, email: string, name: string) {
|
|
117
|
+
const stmt = db.prepare(`
|
|
118
|
+
UPDATE users
|
|
119
|
+
SET email = ?, name = ?, updated_at = CURRENT_TIMESTAMP
|
|
120
|
+
WHERE id = ?
|
|
121
|
+
`);
|
|
122
|
+
|
|
123
|
+
stmt.run(email, name, id);
|
|
124
|
+
return get_user_by_id(id);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Delete user
|
|
128
|
+
export function delete_user(id: number) {
|
|
129
|
+
const stmt = db.prepare("DELETE FROM users WHERE id = ?");
|
|
130
|
+
const result = stmt.run(id);
|
|
131
|
+
return result.changes > 0;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Transaction example
|
|
135
|
+
export function create_user_with_profile(
|
|
136
|
+
email: string,
|
|
137
|
+
name: string,
|
|
138
|
+
bio: string
|
|
139
|
+
) {
|
|
140
|
+
const transaction = db.transaction((email, name, bio) => {
|
|
141
|
+
// Create user
|
|
142
|
+
const userStmt = db.prepare("INSERT INTO users (email, name) VALUES (?, ?)");
|
|
143
|
+
const userResult = userStmt.run(email, name);
|
|
144
|
+
const userId = userResult.lastInsertRowid;
|
|
145
|
+
|
|
146
|
+
// Create profile
|
|
147
|
+
const profileStmt = db.prepare("INSERT INTO profiles (user_id, bio) VALUES (?, ?)");
|
|
148
|
+
profileStmt.run(userId, bio);
|
|
149
|
+
|
|
150
|
+
return userId;
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
return transaction(email, name, bio);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Batch insert example
|
|
157
|
+
export function create_users_batch(users: Array<{ email: string; name: string }>) {
|
|
158
|
+
const stmt = db.prepare("INSERT INTO users (email, name) VALUES (?, ?)");
|
|
159
|
+
|
|
160
|
+
const insertMany = db.transaction((users) => {
|
|
161
|
+
for (const user of users) {
|
|
162
|
+
stmt.run(user.email, user.name);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
insertMany(users);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Type-safe query example
|
|
170
|
+
type User = {
|
|
171
|
+
id: number;
|
|
172
|
+
email: string;
|
|
173
|
+
name: string;
|
|
174
|
+
created_at: string;
|
|
175
|
+
updated_at: string;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function get_users_typed(): User[] {
|
|
179
|
+
return db.query<User, []>("SELECT * FROM users").all();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function get_user_by_id_typed(id: number): User | null {
|
|
183
|
+
return db.query<User, [number]>("SELECT * FROM users WHERE id = ?").get(id);
|
|
184
|
+
}
|
|
185
|
+
*/
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Dependencies
|
|
2
|
+
node_modules
|
|
3
|
+
|
|
4
|
+
# Environment variables
|
|
5
|
+
.env
|
|
6
|
+
.env.local
|
|
7
|
+
.env.*.local
|
|
8
|
+
|
|
9
|
+
# Build artifacts
|
|
10
|
+
dist
|
|
11
|
+
*.log
|
|
12
|
+
|
|
13
|
+
# Development
|
|
14
|
+
.git
|
|
15
|
+
.gitignore
|
|
16
|
+
README.md
|
|
17
|
+
.vscode
|
|
18
|
+
.idea
|
|
19
|
+
|
|
20
|
+
# Testing
|
|
21
|
+
coverage
|
|
22
|
+
*.test.ts
|
|
23
|
+
*.spec.ts
|
|
24
|
+
|
|
25
|
+
# Docker
|
|
26
|
+
Dockerfile
|
|
27
|
+
docker-compose.yml
|
|
28
|
+
.dockerignore
|
|
29
|
+
|
|
30
|
+
# OS
|
|
31
|
+
.DS_Store
|
|
32
|
+
Thumbs.db
|
|
33
|
+
|
|
34
|
+
# SQLite databases (if using SQLite)
|
|
35
|
+
*.db
|
|
36
|
+
*.sqlite
|
|
37
|
+
*.sqlite3
|
|
38
|
+
data/*.db
|
|
39
|
+
|
|
40
|
+
# Logs
|
|
41
|
+
logs
|
|
42
|
+
*.log
|
|
43
|
+
npm-debug.log*
|
|
44
|
+
yarn-debug.log*
|
|
45
|
+
yarn-error.log*
|
|
@@ -4,7 +4,7 @@ import type { User, CreateUserInput, UpdateUserInput } from "../types/models/use
|
|
|
4
4
|
* User model
|
|
5
5
|
*
|
|
6
6
|
* This is a simple in-memory implementation.
|
|
7
|
-
* Replace with your database
|
|
7
|
+
* Replace with your actual database implementation in production.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
// In-memory storage (for demonstration)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Dependencies
|
|
2
|
+
node_modules
|
|
3
|
+
|
|
4
|
+
# Environment variables
|
|
5
|
+
.env
|
|
6
|
+
.env.local
|
|
7
|
+
.env.*.local
|
|
8
|
+
|
|
9
|
+
# Build artifacts
|
|
10
|
+
dist
|
|
11
|
+
*.log
|
|
12
|
+
|
|
13
|
+
# Development
|
|
14
|
+
.git
|
|
15
|
+
.gitignore
|
|
16
|
+
README.md
|
|
17
|
+
.vscode
|
|
18
|
+
.idea
|
|
19
|
+
|
|
20
|
+
# Testing
|
|
21
|
+
coverage
|
|
22
|
+
*.test.ts
|
|
23
|
+
*.spec.ts
|
|
24
|
+
|
|
25
|
+
# Docker
|
|
26
|
+
Dockerfile
|
|
27
|
+
docker-compose.yml
|
|
28
|
+
.dockerignore
|
|
29
|
+
|
|
30
|
+
# OS
|
|
31
|
+
.DS_Store
|
|
32
|
+
Thumbs.db
|
|
33
|
+
|
|
34
|
+
# SQLite databases (if using SQLite)
|
|
35
|
+
*.db
|
|
36
|
+
*.sqlite
|
|
37
|
+
*.sqlite3
|
|
38
|
+
data/*.db
|
|
39
|
+
|
|
40
|
+
# Logs
|
|
41
|
+
logs
|
|
42
|
+
*.log
|
|
43
|
+
npm-debug.log*
|
|
44
|
+
yarn-debug.log*
|
|
45
|
+
yarn-error.log*
|
|
@@ -4,7 +4,7 @@ import type { User, CreateUserInput, UpdateUserInput } from "../types/models/use
|
|
|
4
4
|
* User model
|
|
5
5
|
*
|
|
6
6
|
* This is a simple in-memory implementation.
|
|
7
|
-
* Replace with your database
|
|
7
|
+
* Replace with your actual database implementation in production.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
// In-memory storage (for demonstration)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Dependencies
|
|
2
|
+
node_modules
|
|
3
|
+
|
|
4
|
+
# Environment variables
|
|
5
|
+
.env
|
|
6
|
+
.env.local
|
|
7
|
+
.env.*.local
|
|
8
|
+
|
|
9
|
+
# Build artifacts
|
|
10
|
+
dist
|
|
11
|
+
*.log
|
|
12
|
+
|
|
13
|
+
# Development
|
|
14
|
+
.git
|
|
15
|
+
.gitignore
|
|
16
|
+
README.md
|
|
17
|
+
.vscode
|
|
18
|
+
.idea
|
|
19
|
+
|
|
20
|
+
# Testing
|
|
21
|
+
coverage
|
|
22
|
+
*.test.ts
|
|
23
|
+
*.spec.ts
|
|
24
|
+
|
|
25
|
+
# Docker
|
|
26
|
+
Dockerfile
|
|
27
|
+
docker-compose.yml
|
|
28
|
+
.dockerignore
|
|
29
|
+
|
|
30
|
+
# OS
|
|
31
|
+
.DS_Store
|
|
32
|
+
Thumbs.db
|
|
33
|
+
|
|
34
|
+
# SQLite databases (if using SQLite)
|
|
35
|
+
*.db
|
|
36
|
+
*.sqlite
|
|
37
|
+
*.sqlite3
|
|
38
|
+
data/*.db
|
|
39
|
+
|
|
40
|
+
# Logs
|
|
41
|
+
logs
|
|
42
|
+
*.log
|
|
43
|
+
npm-debug.log*
|
|
44
|
+
yarn-debug.log*
|
|
45
|
+
yarn-error.log*
|
|
@@ -4,7 +4,7 @@ import type { User, CreateUserInput, UpdateUserInput } from "../types/models/use
|
|
|
4
4
|
* User model
|
|
5
5
|
*
|
|
6
6
|
* This is a simple in-memory implementation.
|
|
7
|
-
* Replace with your database
|
|
7
|
+
* Replace with your actual database implementation in production.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
// In-memory storage (for demonstration)
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* MongoDB database connection using Mongoose
|
|
5
|
+
* Docs: https://mongoosejs.com/docs/connections.html
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const MONGODB_URI = process.env.MONGODB_URI;
|
|
9
|
+
|
|
10
|
+
if (!MONGODB_URI) {
|
|
11
|
+
throw new Error("MONGODB_URI environment variable is required");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Connect to MongoDB
|
|
16
|
+
*/
|
|
17
|
+
export async function connect_mongodb(): Promise<void> {
|
|
18
|
+
try {
|
|
19
|
+
await mongoose.connect(MONGODB_URI, {
|
|
20
|
+
maxPoolSize: 10, // Maximum number of connections in the pool
|
|
21
|
+
minPoolSize: 2, // Minimum number of connections
|
|
22
|
+
serverSelectionTimeoutMS: 5000, // Timeout for initial connection
|
|
23
|
+
socketTimeoutMS: 45000, // Socket timeout
|
|
24
|
+
});
|
|
25
|
+
console.log("✅ MongoDB connected successfully");
|
|
26
|
+
} catch (error) {
|
|
27
|
+
console.error("❌ MongoDB connection failed:", error);
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Disconnect from MongoDB
|
|
34
|
+
* Call this when shutting down the application
|
|
35
|
+
*/
|
|
36
|
+
export async function disconnect_mongodb(): Promise<void> {
|
|
37
|
+
await mongoose.disconnect();
|
|
38
|
+
console.log("MongoDB connection closed");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Test database connection
|
|
43
|
+
*/
|
|
44
|
+
export async function test_connection(): Promise<boolean> {
|
|
45
|
+
try {
|
|
46
|
+
if (mongoose.connection.readyState !== 1) {
|
|
47
|
+
await connect_mongodb();
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error("❌ MongoDB connection test failed:", error);
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Connection event listeners
|
|
57
|
+
mongoose.connection.on("connected", () => {
|
|
58
|
+
console.log("Mongoose connected to MongoDB");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
mongoose.connection.on("error", (err) => {
|
|
62
|
+
console.error("Mongoose connection error:", err);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
mongoose.connection.on("disconnected", () => {
|
|
66
|
+
console.log("Mongoose disconnected from MongoDB");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Export mongoose instance for model creation
|
|
70
|
+
export { mongoose };
|