frontend-hamroun 1.2.16 → 1.2.17

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.
Files changed (158) hide show
  1. package/README.md +4 -0
  2. package/bin/cli.js +673 -0
  3. package/dist/component.d.ts +1 -1
  4. package/dist/context.d.ts +4 -3
  5. package/dist/index.client.d.ts +11 -0
  6. package/dist/index.d.ts +9 -89
  7. package/dist/index.js +396 -67
  8. package/dist/index.js.map +1 -0
  9. package/dist/index.mjs +392 -0
  10. package/dist/index.mjs.map +1 -0
  11. package/dist/jsx-runtime/jsx-runtime.d.ts +0 -1
  12. package/dist/jsx-runtime.d.ts +1 -1
  13. package/dist/renderer.d.ts +0 -10
  14. package/dist/server-renderer.d.ts +0 -3
  15. package/dist/server-types.d.ts +42 -0
  16. package/package.json +69 -41
  17. package/templates/basic-app/index.html +6 -6
  18. package/templates/basic-app/package.json +18 -7
  19. package/templates/basic-app/postcss.config.js +0 -1
  20. package/templates/basic-app/src/main.tsx +1 -10
  21. package/templates/basic-app/tailwind.config.js +2 -23
  22. package/templates/basic-app/tsconfig.json +4 -17
  23. package/templates/basic-app/vite.config.ts +3 -54
  24. package/templates/fullstack-app/api/hello.ts +18 -0
  25. package/templates/fullstack-app/api/users/[id].ts +73 -0
  26. package/templates/fullstack-app/api/users/index.ts +32 -0
  27. package/templates/fullstack-app/package.json +31 -0
  28. package/templates/fullstack-app/server.ts +46 -0
  29. package/templates/fullstack-app/src/pages/index.tsx +59 -0
  30. package/templates/ssr-template/vite.config.ts +1 -11
  31. package/bin/cli.cjs +0 -16
  32. package/bin/cli.mjs +0 -237
  33. package/dist/backend/api-utils.d.ts +0 -38
  34. package/dist/backend/api-utils.js +0 -135
  35. package/dist/backend/auth.d.ts +0 -134
  36. package/dist/backend/auth.js +0 -387
  37. package/dist/backend/database.d.ts +0 -27
  38. package/dist/backend/database.js +0 -91
  39. package/dist/backend/model.d.ts +0 -43
  40. package/dist/backend/model.js +0 -178
  41. package/dist/backend/router.d.ts +0 -27
  42. package/dist/backend/router.js +0 -137
  43. package/dist/backend/server.d.ts +0 -19
  44. package/dist/backend/server.js +0 -268
  45. package/dist/backend/types.d.ts +0 -217
  46. package/dist/backend/types.js +0 -1
  47. package/dist/batch.js +0 -22
  48. package/dist/cli/index.d.ts +0 -2
  49. package/dist/cli/index.js +0 -215
  50. package/dist/component.js +0 -84
  51. package/dist/components/Counter.js +0 -2
  52. package/dist/context.js +0 -18
  53. package/dist/frontend-hamroun.es.js +0 -1378
  54. package/dist/frontend-hamroun.umd.js +0 -66
  55. package/dist/hooks.js +0 -164
  56. package/dist/jsx-runtime/index.d.ts +0 -11
  57. package/dist/jsx-runtime/index.js +0 -19
  58. package/dist/jsx-runtime/jsx-dev-runtime.js +0 -1
  59. package/dist/jsx-runtime/jsx-runtime.js +0 -95
  60. package/dist/jsx-runtime.js +0 -192
  61. package/dist/renderer.js +0 -51
  62. package/dist/server-renderer.js +0 -102
  63. package/dist/types.js +0 -1
  64. package/dist/vdom.js +0 -27
  65. package/scripts/build-cli.js +0 -1199
  66. package/scripts/generate.js +0 -134
  67. package/src/backend/api-utils.ts +0 -178
  68. package/src/backend/auth.ts +0 -544
  69. package/src/backend/database.ts +0 -104
  70. package/src/backend/model.ts +0 -198
  71. package/src/backend/router.ts +0 -176
  72. package/src/backend/server.ts +0 -330
  73. package/src/backend/types.ts +0 -257
  74. package/src/batch.ts +0 -24
  75. package/src/cli/index.js +0 -554
  76. package/src/cli/index.ts +0 -257
  77. package/src/component.ts +0 -98
  78. package/src/components/Counter.tsx +0 -4
  79. package/src/context.ts +0 -29
  80. package/src/hooks.ts +0 -211
  81. package/src/index.ts +0 -144
  82. package/src/jsx-runtime/index.ts +0 -27
  83. package/src/jsx-runtime/jsx-dev-runtime.ts +0 -0
  84. package/src/jsx-runtime/jsx-runtime.ts +0 -104
  85. package/src/jsx-runtime.ts +0 -226
  86. package/src/renderer.ts +0 -55
  87. package/src/server-renderer.ts +0 -114
  88. package/src/shims.d.ts +0 -20
  89. package/src/types/bcrypt.d.ts +0 -30
  90. package/src/types/jsonwebtoken.d.ts +0 -55
  91. package/src/types.d.ts +0 -26
  92. package/src/types.ts +0 -21
  93. package/src/vdom.ts +0 -34
  94. package/templates/basic/.eslintignore +0 -5
  95. package/templates/basic/.eslintrc.json +0 -25
  96. package/templates/basic/docs/rapport_pfe.aux +0 -27
  97. package/templates/basic/docs/rapport_pfe.log +0 -399
  98. package/templates/basic/docs/rapport_pfe.out +0 -10
  99. package/templates/basic/docs/rapport_pfe.pdf +0 -0
  100. package/templates/basic/docs/rapport_pfe.tex +0 -68
  101. package/templates/basic/docs/rapport_pfe.toc +0 -14
  102. package/templates/basic/index.html +0 -12
  103. package/templates/basic/jsconfig.json +0 -14
  104. package/templates/basic/package.json +0 -18
  105. package/templates/basic/postcss.config.js +0 -7
  106. package/templates/basic/src/App.js +0 -105
  107. package/templates/basic/src/App.tsx +0 -65
  108. package/templates/basic/src/api.ts +0 -58
  109. package/templates/basic/src/components/Counter.tsx +0 -26
  110. package/templates/basic/src/components/Header.tsx +0 -9
  111. package/templates/basic/src/components/TodoList.tsx +0 -90
  112. package/templates/basic/src/main.css +0 -3
  113. package/templates/basic/src/main.js +0 -11
  114. package/templates/basic/src/main.ts +0 -20
  115. package/templates/basic/src/main.tsx +0 -144
  116. package/templates/basic/src/server.ts +0 -99
  117. package/templates/basic/tailwind.config.js +0 -32
  118. package/templates/basic/tsconfig.json +0 -20
  119. package/templates/basic/tsconfig.node.json +0 -10
  120. package/templates/basic/vite.config.js +0 -18
  121. package/templates/basic/vite.config.ts +0 -86
  122. package/templates/basic-app/src/App.js +0 -105
  123. package/templates/basic-app/src/App.tsx +0 -143
  124. package/templates/basic-app/src/api.ts +0 -58
  125. package/templates/basic-app/src/components/Counter.tsx +0 -26
  126. package/templates/basic-app/src/components/Header.tsx +0 -9
  127. package/templates/basic-app/src/components/TodoList.tsx +0 -90
  128. package/templates/basic-app/src/main.js +0 -10
  129. package/templates/basic-app/src/main.ts +0 -21
  130. package/templates/basic-app/src/react/index.ts +0 -35
  131. package/templates/basic-app/src/react/jsx-dev-runtime.ts +0 -13
  132. package/templates/basic-app/src/react/jsx-runtime.ts +0 -12
  133. package/templates/basic-app/src/server.ts +0 -99
  134. package/templates/basic-app/src/shims.ts +0 -9
  135. package/templates/basic-app/tsconfig.node.json +0 -10
  136. package/templates/basic-app/vite.config.js +0 -22
  137. package/templates/full-stack/.env.example +0 -11
  138. package/templates/full-stack/README.md +0 -51
  139. package/templates/full-stack/index.html +0 -12
  140. package/templates/full-stack/jsconfig.json +0 -14
  141. package/templates/full-stack/package.json +0 -21
  142. package/templates/full-stack/src/App.js +0 -105
  143. package/templates/full-stack/src/client/App.tsx +0 -50
  144. package/templates/full-stack/src/client/components/Header.tsx +0 -42
  145. package/templates/full-stack/src/client/components/UserList.tsx +0 -29
  146. package/templates/full-stack/src/client/main.tsx +0 -5
  147. package/templates/full-stack/src/main.css +0 -3
  148. package/templates/full-stack/src/main.js +0 -11
  149. package/templates/full-stack/src/main.ts +0 -20
  150. package/templates/full-stack/src/server/index.ts +0 -99
  151. package/templates/full-stack/src/server/routes/auth.ts +0 -39
  152. package/templates/full-stack/src/server/routes/users.ts +0 -48
  153. package/templates/full-stack/src/shims.ts +0 -9
  154. package/templates/full-stack/tsconfig.json +0 -20
  155. package/templates/full-stack/tsconfig.node.json +0 -10
  156. package/templates/full-stack/tsconfig.server.json +0 -15
  157. package/templates/full-stack/vite.config.js +0 -18
  158. package/templates/full-stack/vite.config.ts +0 -85
@@ -1,387 +0,0 @@
1
- const jwt = require('jsonwebtoken');
2
- const bcrypt = require('bcrypt');
3
- const crypto = require('crypto');
4
- /**
5
- * Authentication service
6
- */
7
- export class Auth {
8
- constructor(options) {
9
- this.loginAttempts = new Map();
10
- /**
11
- * Login middleware to authenticate users
12
- */
13
- this.login = async (req, res) => {
14
- try {
15
- const { username, password } = req.body;
16
- const ip = req.ip || req.connection.remoteAddress || '';
17
- // Check rate limiting
18
- if (!this.checkRateLimit(ip)) {
19
- res.status(429).json({
20
- success: false,
21
- message: 'Too many login attempts. Please try again later.'
22
- });
23
- return;
24
- }
25
- if (!username || !password) {
26
- res.status(400).json({
27
- success: false,
28
- message: 'Username and password are required'
29
- });
30
- return;
31
- }
32
- // Use custom find user function if provided
33
- if (!this.options.findUser) {
34
- res.status(500).json({
35
- success: false,
36
- message: 'User finder function not configured'
37
- });
38
- return;
39
- }
40
- const user = await this.options.findUser(username);
41
- if (!user) {
42
- res.status(401).json({
43
- success: false,
44
- message: 'Invalid credentials'
45
- });
46
- return;
47
- }
48
- // Verify password
49
- const isPasswordValid = await this.options.verifyPassword(password, user.password);
50
- if (!isPasswordValid) {
51
- res.status(401).json({
52
- success: false,
53
- message: 'Invalid credentials'
54
- });
55
- return;
56
- }
57
- // Create a sanitized user object (without password)
58
- const userWithoutPassword = { ...user };
59
- delete userWithoutPassword.password;
60
- // Generate tokens
61
- const tokenPair = this.generateTokenPair({
62
- id: user.id || user._id,
63
- username: user.username,
64
- role: user.role || 'user'
65
- });
66
- // Save refresh token if the function is provided
67
- if (this.options.saveRefreshToken) {
68
- const refreshExpiry = new Date();
69
- refreshExpiry.setSeconds(refreshExpiry.getSeconds() +
70
- this.getExpirationSeconds(this.options.refreshExpiration || '7d'));
71
- await this.options.saveRefreshToken(user.id || user._id, tokenPair.refreshToken, refreshExpiry);
72
- }
73
- // Set authentication cookies if using cookie-based auth
74
- if (req.body.useCookies) {
75
- this.setAuthCookies(res, tokenPair);
76
- }
77
- res.json({
78
- success: true,
79
- message: 'Authentication successful',
80
- tokens: tokenPair,
81
- user: userWithoutPassword
82
- });
83
- }
84
- catch (error) {
85
- console.error('Authentication error:', error);
86
- res.status(500).json({
87
- success: false,
88
- message: 'Authentication failed',
89
- error: process.env.NODE_ENV !== 'production' ? error.message : undefined
90
- });
91
- }
92
- };
93
- /**
94
- * Refresh token handler
95
- */
96
- this.refreshToken = async (req, res) => {
97
- try {
98
- // Get refresh token from cookies or request body
99
- const refreshToken = req.cookies?.refreshToken || req.body.refreshToken;
100
- if (!refreshToken) {
101
- res.status(401).json({
102
- success: false,
103
- message: 'Refresh token required'
104
- });
105
- return;
106
- }
107
- // Verify the refresh token
108
- const decoded = this.verifyToken(refreshToken, 'refresh');
109
- // Check if token is still valid in database if verification function provided
110
- if (this.options.verifyRefreshToken) {
111
- const isValid = await this.options.verifyRefreshToken(decoded.id, refreshToken);
112
- if (!isValid) {
113
- this.clearAuthCookies(res);
114
- res.status(401).json({
115
- success: false,
116
- message: 'Invalid refresh token'
117
- });
118
- return;
119
- }
120
- }
121
- // Generate new token pair
122
- const tokenPair = this.generateTokenPair({
123
- id: decoded.id,
124
- // We need to fetch the user data again for complete payload
125
- ...(this.options.findUser ? await this.options.findUser(decoded.id) : {})
126
- });
127
- // Update refresh token in database if save function provided
128
- if (this.options.saveRefreshToken) {
129
- const refreshExpiry = new Date();
130
- refreshExpiry.setSeconds(refreshExpiry.getSeconds() +
131
- this.getExpirationSeconds(this.options.refreshExpiration || '7d'));
132
- await this.options.saveRefreshToken(decoded.id, tokenPair.refreshToken, refreshExpiry);
133
- }
134
- // Set cookies if cookie-based auth is used
135
- const useCookies = req.cookies?.accessToken || req.body.useCookies;
136
- if (useCookies) {
137
- this.setAuthCookies(res, tokenPair);
138
- }
139
- res.json({
140
- success: true,
141
- message: 'Token refreshed successfully',
142
- tokens: tokenPair
143
- });
144
- }
145
- catch (error) {
146
- this.clearAuthCookies(res);
147
- res.status(401).json({
148
- success: false,
149
- message: 'Invalid or expired refresh token',
150
- error: process.env.NODE_ENV !== 'production' ? error.message : undefined
151
- });
152
- }
153
- };
154
- /**
155
- * Logout handler
156
- */
157
- this.logout = async (req, res) => {
158
- try {
159
- // Clear cookies
160
- this.clearAuthCookies(res);
161
- // Invalidate refresh token if verification function provided
162
- const refreshToken = req.cookies?.refreshToken || req.body.refreshToken;
163
- if (refreshToken && this.options.saveRefreshToken) {
164
- try {
165
- const decoded = this.verifyToken(refreshToken, 'refresh');
166
- // Check if invalidateToken function exists, otherwise we just remove it from storage
167
- if (typeof this.options.saveRefreshToken === 'function') {
168
- // We pass null as the token to indicate deletion/invalidation
169
- await this.options.saveRefreshToken(decoded.id, '', new Date());
170
- }
171
- }
172
- catch (e) {
173
- // Token already invalid, nothing to do
174
- }
175
- }
176
- res.json({ success: true, message: 'Logged out successfully' });
177
- }
178
- catch (error) {
179
- console.error('Logout error:', error);
180
- res.status(500).json({ success: false, message: 'Logout failed', error: error.message });
181
- }
182
- };
183
- /**
184
- * Middleware to verify user is authenticated
185
- */
186
- this.authenticate = (req, res, next) => {
187
- try {
188
- // Get token from Authorization header or cookies
189
- let token = req.cookies?.accessToken;
190
- if (!token) {
191
- const authHeader = req.headers.authorization;
192
- if (authHeader && authHeader.startsWith('Bearer ')) {
193
- token = authHeader.split(' ')[1];
194
- }
195
- }
196
- if (!token) {
197
- res.status(401).json({
198
- success: false,
199
- message: 'Authentication required'
200
- });
201
- return;
202
- }
203
- const decoded = this.verifyToken(token, 'access');
204
- // Add user info to request
205
- req.user = decoded;
206
- next();
207
- }
208
- catch (error) {
209
- res.status(401).json({
210
- success: false,
211
- message: 'Invalid or expired token',
212
- error: process.env.NODE_ENV !== 'production' ? error.message : undefined
213
- });
214
- }
215
- };
216
- /**
217
- * Middleware to check if user has required role
218
- */
219
- this.hasRole = (role) => {
220
- return (req, res, next) => {
221
- const user = req.user;
222
- if (!user) {
223
- res.status(401).json({
224
- success: false,
225
- message: 'Authentication required'
226
- });
227
- return;
228
- }
229
- const roles = Array.isArray(role) ? role : [role];
230
- if (roles.includes(user.role)) {
231
- next();
232
- }
233
- else {
234
- res.status(403).json({
235
- success: false,
236
- message: 'Insufficient permissions'
237
- });
238
- }
239
- };
240
- };
241
- this.options = {
242
- tokenExpiration: '15m',
243
- refreshExpiration: '7d',
244
- saltRounds: 10,
245
- secureCookies: process.env.NODE_ENV === 'production',
246
- httpOnlyCookies: true,
247
- rateLimit: true,
248
- ...options,
249
- refreshSecret: options.refreshSecret || options.jwtSecret
250
- };
251
- if (!options.jwtSecret) {
252
- throw new Error('JWT secret is required for authentication');
253
- }
254
- // Set default password verification if not provided
255
- if (!this.options.verifyPassword) {
256
- this.options.verifyPassword = this.verifyPasswordWithBcrypt;
257
- }
258
- }
259
- /**
260
- * Hash a password using bcrypt
261
- */
262
- async hashPassword(password) {
263
- return await bcrypt.hash(password, this.options.saltRounds || 10);
264
- }
265
- /**
266
- * Verify a password against a hash using bcrypt
267
- */
268
- async verifyPasswordWithBcrypt(password, hashedPassword) {
269
- return await bcrypt.compare(password, hashedPassword);
270
- }
271
- /**
272
- * Generate a cryptographically secure random token
273
- */
274
- generateSecureToken(length = 32) {
275
- return crypto.randomBytes(length).toString('hex');
276
- }
277
- /**
278
- * Generate token pair (access token + refresh token)
279
- */
280
- generateTokenPair(payload) {
281
- // Calculate expiration times
282
- const expiresIn = this.getExpirationSeconds(this.options.tokenExpiration || '15m');
283
- // Generate the access token
284
- const accessToken = jwt.sign({ ...payload, type: 'access' }, this.options.jwtSecret, { expiresIn: this.options.tokenExpiration });
285
- // Generate the refresh token
286
- const refreshToken = jwt.sign({ id: payload.id, type: 'refresh' }, this.options.refreshSecret, { expiresIn: this.options.refreshExpiration });
287
- return {
288
- accessToken,
289
- refreshToken,
290
- expiresIn
291
- };
292
- }
293
- /**
294
- * Convert JWT expiration time to seconds
295
- */
296
- getExpirationSeconds(expiration) {
297
- const unit = expiration.charAt(expiration.length - 1);
298
- const value = parseInt(expiration.slice(0, -1));
299
- switch (unit) {
300
- case 's': return value;
301
- case 'm': return value * 60;
302
- case 'h': return value * 60 * 60;
303
- case 'd': return value * 60 * 60 * 24;
304
- default: return 3600; // Default 1 hour
305
- }
306
- }
307
- /**
308
- * Verify a JWT token
309
- */
310
- verifyToken(token, type = 'access') {
311
- try {
312
- const secret = type === 'access' ? this.options.jwtSecret : this.options.refreshSecret;
313
- const decoded = jwt.verify(token, secret);
314
- // Verify token type matches expected type
315
- if (typeof decoded === 'object' && decoded.type !== type) {
316
- throw new Error('Invalid token type');
317
- }
318
- return decoded;
319
- }
320
- catch (error) {
321
- throw new Error('Invalid or expired token');
322
- }
323
- }
324
- /**
325
- * Set authentication cookies
326
- */
327
- setAuthCookies(res, tokens) {
328
- // Set access token cookie
329
- res.cookie('accessToken', tokens.accessToken, {
330
- httpOnly: this.options.httpOnlyCookies,
331
- secure: this.options.secureCookies,
332
- domain: this.options.cookieDomain,
333
- sameSite: 'strict',
334
- maxAge: tokens.expiresIn * 1000
335
- });
336
- // Set refresh token cookie with longer expiration
337
- const refreshExpiresIn = this.getExpirationSeconds(this.options.refreshExpiration || '7d');
338
- res.cookie('refreshToken', tokens.refreshToken, {
339
- httpOnly: true, // Always HTTP only for refresh tokens
340
- secure: this.options.secureCookies,
341
- domain: this.options.cookieDomain,
342
- sameSite: 'strict',
343
- maxAge: refreshExpiresIn * 1000,
344
- path: '/api/auth/refresh' // Restrict to refresh endpoint
345
- });
346
- }
347
- /**
348
- * Clear authentication cookies
349
- */
350
- clearAuthCookies(res) {
351
- res.clearCookie('accessToken');
352
- res.clearCookie('refreshToken', { path: '/api/auth/refresh' });
353
- }
354
- /**
355
- * Check and handle rate limiting
356
- */
357
- checkRateLimit(ip) {
358
- if (!this.options.rateLimit) {
359
- return true;
360
- }
361
- const now = Date.now();
362
- const attempt = this.loginAttempts.get(ip);
363
- if (!attempt) {
364
- this.loginAttempts.set(ip, { count: 1, resetTime: now + 3600000 });
365
- return true;
366
- }
367
- // Reset if time expired
368
- if (now > attempt.resetTime) {
369
- this.loginAttempts.set(ip, { count: 1, resetTime: now + 3600000 });
370
- return true;
371
- }
372
- // Check attempts
373
- if (attempt.count >= 5) {
374
- return false;
375
- }
376
- // Increment counter
377
- attempt.count++;
378
- this.loginAttempts.set(ip, attempt);
379
- return true;
380
- }
381
- }
382
- /**
383
- * Create an authentication service
384
- */
385
- export function createAuth(options) {
386
- return new Auth(options);
387
- }
@@ -1,27 +0,0 @@
1
- import mongoose from 'mongoose';
2
- import { DatabaseOptions } from './types';
3
- /**
4
- * Database connector for MongoDB using Mongoose
5
- */
6
- export declare class DatabaseConnector {
7
- private options;
8
- private connection;
9
- private _connected;
10
- constructor(options: DatabaseOptions);
11
- /**
12
- * Connect to the database
13
- */
14
- connect(): Promise<mongoose.Connection>;
15
- /**
16
- * Disconnect from the database
17
- */
18
- disconnect(): Promise<void>;
19
- /**
20
- * Check if connected to the database
21
- */
22
- isConnected(): boolean;
23
- /**
24
- * Get the mongoose connection
25
- */
26
- getConnection(): mongoose.Connection | null;
27
- }
@@ -1,91 +0,0 @@
1
- import mongoose from 'mongoose';
2
- /**
3
- * Database connector for MongoDB using Mongoose
4
- */
5
- export class DatabaseConnector {
6
- constructor(options) {
7
- this.connection = null;
8
- this._connected = false; // Renamed from isConnected to _connected
9
- this.options = {
10
- retryAttempts: 3,
11
- retryDelay: 1000,
12
- connectionTimeout: 10000,
13
- autoIndex: true,
14
- ...options
15
- };
16
- }
17
- /**
18
- * Connect to the database
19
- */
20
- async connect() {
21
- try {
22
- if (this._connected && this.connection) {
23
- return this.connection;
24
- }
25
- // Set Mongoose options
26
- mongoose.set('strictQuery', true);
27
- // Connect with retry logic
28
- let attempts = 0;
29
- while (attempts < (this.options.retryAttempts || 3)) {
30
- try {
31
- await mongoose.connect(this.options.uri, {
32
- dbName: this.options.name,
33
- connectTimeoutMS: this.options.connectionTimeout,
34
- autoIndex: this.options.autoIndex,
35
- ...this.options.options
36
- });
37
- break;
38
- }
39
- catch (error) {
40
- attempts++;
41
- if (attempts >= (this.options.retryAttempts || 3)) {
42
- throw error;
43
- }
44
- console.log(`Connection attempt ${attempts} failed. Retrying in ${this.options.retryDelay}ms...`);
45
- await new Promise(resolve => setTimeout(resolve, this.options.retryDelay));
46
- }
47
- }
48
- this.connection = mongoose.connection;
49
- this._connected = true;
50
- // Log successful connection
51
- console.log(`Connected to MongoDB at ${this.options.uri}/${this.options.name}`);
52
- // Handle connection events
53
- this.connection.on('error', (err) => {
54
- console.error('MongoDB connection error:', err);
55
- this._connected = false;
56
- });
57
- this.connection.on('disconnected', () => {
58
- console.log('MongoDB disconnected');
59
- this._connected = false;
60
- });
61
- return this.connection;
62
- }
63
- catch (error) {
64
- console.error('Failed to connect to MongoDB:', error);
65
- throw error;
66
- }
67
- }
68
- /**
69
- * Disconnect from the database
70
- */
71
- async disconnect() {
72
- if (this.connection) {
73
- await mongoose.disconnect();
74
- this._connected = false;
75
- this.connection = null;
76
- console.log('Disconnected from MongoDB');
77
- }
78
- }
79
- /**
80
- * Check if connected to the database
81
- */
82
- isConnected() {
83
- return this._connected;
84
- }
85
- /**
86
- * Get the mongoose connection
87
- */
88
- getConnection() {
89
- return this.connection;
90
- }
91
- }
@@ -1,43 +0,0 @@
1
- import { Schema, Document } from 'mongoose';
2
- import { Model } from './types';
3
- /**
4
- * Creates a data model from a Mongoose schema
5
- * @param name Model name
6
- * @param schema Mongoose schema definition
7
- * @returns Model implementation with CRUD operations
8
- */
9
- export declare function createModel<T extends Document>(name: string, schema: Schema<T>): Model<T>;
10
- /**
11
- * Schema field types for simplified schema creation
12
- */
13
- export declare const FieldTypes: {
14
- String: {
15
- type: StringConstructor;
16
- };
17
- Number: {
18
- type: NumberConstructor;
19
- };
20
- Boolean: {
21
- type: BooleanConstructor;
22
- };
23
- Date: {
24
- type: DateConstructor;
25
- };
26
- ObjectId: {
27
- type: StringConstructor;
28
- };
29
- Required: (fieldType: any) => any;
30
- Unique: (fieldType: any) => any;
31
- Ref: (model: string) => {
32
- type: StringConstructor;
33
- ref: string;
34
- };
35
- Enum: (values: any[]) => {
36
- type: StringConstructor;
37
- enum: any[];
38
- };
39
- Default: (fieldType: any, defaultValue: any) => any;
40
- Array: (fieldType: any) => {
41
- type: any[];
42
- };
43
- };