nitrostack 1.0.0 → 1.0.2

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 (164) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/dist/cli/index.js +4 -1
  3. package/dist/cli/index.js.map +1 -1
  4. package/package.json +1 -1
  5. package/src/studio/README.md +140 -0
  6. package/src/studio/app/api/auth/fetch-metadata/route.ts +71 -0
  7. package/src/studio/app/api/auth/register-client/route.ts +67 -0
  8. package/src/studio/app/api/chat/route.ts +123 -0
  9. package/src/studio/app/api/health/checks/route.ts +42 -0
  10. package/src/studio/app/api/health/route.ts +13 -0
  11. package/src/studio/app/api/init/route.ts +85 -0
  12. package/src/studio/app/api/ping/route.ts +13 -0
  13. package/src/studio/app/api/prompts/[name]/route.ts +21 -0
  14. package/src/studio/app/api/prompts/route.ts +13 -0
  15. package/src/studio/app/api/resources/[...uri]/route.ts +18 -0
  16. package/src/studio/app/api/resources/route.ts +13 -0
  17. package/src/studio/app/api/roots/route.ts +13 -0
  18. package/src/studio/app/api/sampling/route.ts +14 -0
  19. package/src/studio/app/api/tools/[name]/call/route.ts +41 -0
  20. package/src/studio/app/api/tools/route.ts +23 -0
  21. package/src/studio/app/api/widget-examples/route.ts +44 -0
  22. package/src/studio/app/auth/callback/page.tsx +160 -0
  23. package/src/studio/app/auth/page.tsx +543 -0
  24. package/src/studio/app/chat/page.tsx +530 -0
  25. package/src/studio/app/chat/page.tsx.backup +390 -0
  26. package/src/studio/app/globals.css +410 -0
  27. package/src/studio/app/health/page.tsx +177 -0
  28. package/src/studio/app/layout.tsx +48 -0
  29. package/src/studio/app/page.tsx +337 -0
  30. package/src/studio/app/page.tsx.backup +346 -0
  31. package/src/studio/app/ping/page.tsx +204 -0
  32. package/src/studio/app/prompts/page.tsx +228 -0
  33. package/src/studio/app/resources/page.tsx +313 -0
  34. package/src/studio/components/EnlargeModal.tsx +116 -0
  35. package/src/studio/components/Sidebar.tsx +133 -0
  36. package/src/studio/components/ToolCard.tsx +108 -0
  37. package/src/studio/components/WidgetRenderer.tsx +99 -0
  38. package/src/studio/lib/api.ts +207 -0
  39. package/src/studio/lib/llm-service.ts +361 -0
  40. package/src/studio/lib/mcp-client.ts +168 -0
  41. package/src/studio/lib/store.ts +192 -0
  42. package/src/studio/lib/theme-provider.tsx +50 -0
  43. package/src/studio/lib/types.ts +107 -0
  44. package/src/studio/lib/widget-loader.ts +90 -0
  45. package/src/studio/middleware.ts +27 -0
  46. package/src/studio/next.config.js +16 -0
  47. package/src/studio/package-lock.json +2696 -0
  48. package/src/studio/package.json +34 -0
  49. package/src/studio/postcss.config.mjs +10 -0
  50. package/src/studio/tailwind.config.ts +67 -0
  51. package/src/studio/tsconfig.json +41 -0
  52. package/templates/typescript-auth/.env.example +23 -0
  53. package/templates/typescript-auth/src/app.module.ts +103 -0
  54. package/templates/typescript-auth/src/db/database.ts +163 -0
  55. package/templates/typescript-auth/src/db/seed.ts +374 -0
  56. package/templates/typescript-auth/src/db/setup.ts +87 -0
  57. package/templates/typescript-auth/src/events/analytics.service.ts +52 -0
  58. package/templates/typescript-auth/src/events/notification.service.ts +40 -0
  59. package/templates/typescript-auth/src/filters/global-exception.filter.ts +28 -0
  60. package/templates/typescript-auth/src/guards/README.md +75 -0
  61. package/templates/typescript-auth/src/guards/jwt.guard.ts +105 -0
  62. package/templates/typescript-auth/src/health/database.health.ts +41 -0
  63. package/templates/typescript-auth/src/index.ts +26 -0
  64. package/templates/typescript-auth/src/interceptors/transform.interceptor.ts +24 -0
  65. package/templates/typescript-auth/src/middleware/logging.middleware.ts +42 -0
  66. package/templates/typescript-auth/src/modules/addresses/addresses.module.ts +16 -0
  67. package/templates/typescript-auth/src/modules/addresses/addresses.prompts.ts +114 -0
  68. package/templates/typescript-auth/src/modules/addresses/addresses.resources.ts +40 -0
  69. package/templates/typescript-auth/src/modules/addresses/addresses.tools.ts +241 -0
  70. package/templates/typescript-auth/src/modules/auth/auth.module.ts +16 -0
  71. package/templates/typescript-auth/src/modules/auth/auth.prompts.ts +147 -0
  72. package/templates/typescript-auth/src/modules/auth/auth.resources.ts +84 -0
  73. package/templates/typescript-auth/src/modules/auth/auth.tools.ts +139 -0
  74. package/templates/typescript-auth/src/modules/cart/cart.module.ts +16 -0
  75. package/templates/typescript-auth/src/modules/cart/cart.prompts.ts +95 -0
  76. package/templates/typescript-auth/src/modules/cart/cart.resources.ts +44 -0
  77. package/templates/typescript-auth/src/modules/cart/cart.tools.ts +281 -0
  78. package/templates/typescript-auth/src/modules/orders/orders.module.ts +16 -0
  79. package/templates/typescript-auth/src/modules/orders/orders.prompts.ts +88 -0
  80. package/templates/typescript-auth/src/modules/orders/orders.resources.ts +48 -0
  81. package/templates/typescript-auth/src/modules/orders/orders.tools.ts +281 -0
  82. package/templates/typescript-auth/src/modules/products/products.module.ts +16 -0
  83. package/templates/typescript-auth/src/modules/products/products.prompts.ts +146 -0
  84. package/templates/typescript-auth/src/modules/products/products.resources.ts +98 -0
  85. package/templates/typescript-auth/src/modules/products/products.tools.ts +266 -0
  86. package/templates/typescript-auth/src/pipes/validation.pipe.ts +42 -0
  87. package/templates/typescript-auth/src/services/database.service.ts +90 -0
  88. package/templates/typescript-auth/src/widgets/app/add-to-cart/page.tsx +122 -0
  89. package/templates/typescript-auth/src/widgets/app/address-added/page.tsx +116 -0
  90. package/templates/typescript-auth/src/widgets/app/address-deleted/page.tsx +105 -0
  91. package/templates/typescript-auth/src/widgets/app/address-list/page.tsx +139 -0
  92. package/templates/typescript-auth/src/widgets/app/address-updated/page.tsx +153 -0
  93. package/templates/typescript-auth/src/widgets/app/cart-cleared/page.tsx +86 -0
  94. package/templates/typescript-auth/src/widgets/app/cart-updated/page.tsx +116 -0
  95. package/templates/typescript-auth/src/widgets/app/categories/page.tsx +134 -0
  96. package/templates/typescript-auth/src/widgets/app/layout.tsx +21 -0
  97. package/templates/typescript-auth/src/widgets/app/login-result/page.tsx +129 -0
  98. package/templates/typescript-auth/src/widgets/app/order-confirmation/page.tsx +206 -0
  99. package/templates/typescript-auth/src/widgets/app/order-details/page.tsx +225 -0
  100. package/templates/typescript-auth/src/widgets/app/order-history/page.tsx +218 -0
  101. package/templates/typescript-auth/src/widgets/app/product-card/page.tsx +121 -0
  102. package/templates/typescript-auth/src/widgets/app/products-grid/page.tsx +173 -0
  103. package/templates/typescript-auth/src/widgets/app/shopping-cart/page.tsx +187 -0
  104. package/templates/typescript-auth/src/widgets/app/whoami/page.tsx +165 -0
  105. package/templates/typescript-auth/src/widgets/next.config.js +38 -0
  106. package/templates/typescript-auth/src/widgets/package.json +18 -0
  107. package/templates/typescript-auth/src/widgets/styles/ecommerce.ts +169 -0
  108. package/templates/typescript-auth/src/widgets/tsconfig.json +28 -0
  109. package/templates/typescript-auth/src/widgets/types/tool-data.ts +141 -0
  110. package/templates/typescript-auth/src/widgets/widget-manifest.json +464 -0
  111. package/templates/typescript-auth/tsconfig.json +27 -0
  112. package/templates/typescript-auth-api-key/.env +15 -0
  113. package/templates/typescript-auth-api-key/.env.example +4 -0
  114. package/templates/typescript-auth-api-key/src/app.module.ts +38 -0
  115. package/templates/typescript-auth-api-key/src/guards/apikey.guard.ts +47 -0
  116. package/templates/typescript-auth-api-key/src/guards/multi-auth.guard.ts +157 -0
  117. package/templates/typescript-auth-api-key/src/health/system.health.ts +55 -0
  118. package/templates/typescript-auth-api-key/src/index.ts +47 -0
  119. package/templates/typescript-auth-api-key/src/modules/calculator/calculator.module.ts +12 -0
  120. package/templates/typescript-auth-api-key/src/modules/calculator/calculator.prompts.ts +73 -0
  121. package/templates/typescript-auth-api-key/src/modules/calculator/calculator.resources.ts +60 -0
  122. package/templates/typescript-auth-api-key/src/modules/calculator/calculator.tools.ts +71 -0
  123. package/templates/typescript-auth-api-key/src/modules/demo/demo.module.ts +18 -0
  124. package/templates/typescript-auth-api-key/src/modules/demo/demo.tools.ts +155 -0
  125. package/templates/typescript-auth-api-key/src/modules/demo/multi-auth.tools.ts +123 -0
  126. package/templates/typescript-auth-api-key/src/widgets/app/calculator-operations/page.tsx +133 -0
  127. package/templates/typescript-auth-api-key/src/widgets/app/calculator-result/page.tsx +134 -0
  128. package/templates/typescript-auth-api-key/src/widgets/app/layout.tsx +14 -0
  129. package/templates/typescript-auth-api-key/src/widgets/next.config.js +37 -0
  130. package/templates/typescript-auth-api-key/src/widgets/package.json +24 -0
  131. package/templates/typescript-auth-api-key/src/widgets/tsconfig.json +28 -0
  132. package/templates/typescript-auth-api-key/src/widgets/widget-manifest.json +48 -0
  133. package/templates/typescript-auth-api-key/tsconfig.json +23 -0
  134. package/templates/typescript-oauth/.env.example +91 -0
  135. package/templates/typescript-oauth/src/app.module.ts +89 -0
  136. package/templates/typescript-oauth/src/guards/oauth.guard.ts +127 -0
  137. package/templates/typescript-oauth/src/index.ts +74 -0
  138. package/templates/typescript-oauth/src/modules/demo/demo.module.ts +16 -0
  139. package/templates/typescript-oauth/src/modules/demo/demo.tools.ts +190 -0
  140. package/templates/typescript-oauth/src/widgets/app/calculator-operations/page.tsx +133 -0
  141. package/templates/typescript-oauth/src/widgets/app/calculator-result/page.tsx +134 -0
  142. package/templates/typescript-oauth/src/widgets/app/layout.tsx +14 -0
  143. package/templates/typescript-oauth/src/widgets/next.config.js +37 -0
  144. package/templates/typescript-oauth/src/widgets/package.json +24 -0
  145. package/templates/typescript-oauth/src/widgets/tsconfig.json +28 -0
  146. package/templates/typescript-oauth/src/widgets/widget-manifest.json +48 -0
  147. package/templates/typescript-oauth/tsconfig.json +23 -0
  148. package/templates/typescript-starter/.env.example +4 -0
  149. package/templates/typescript-starter/src/app.module.ts +34 -0
  150. package/templates/typescript-starter/src/health/system.health.ts +55 -0
  151. package/templates/typescript-starter/src/index.ts +27 -0
  152. package/templates/typescript-starter/src/modules/calculator/calculator.module.ts +12 -0
  153. package/templates/typescript-starter/src/modules/calculator/calculator.prompts.ts +73 -0
  154. package/templates/typescript-starter/src/modules/calculator/calculator.resources.ts +60 -0
  155. package/templates/typescript-starter/src/modules/calculator/calculator.tools.ts +71 -0
  156. package/templates/typescript-starter/src/widgets/app/calculator-operations/page.tsx +133 -0
  157. package/templates/typescript-starter/src/widgets/app/calculator-result/page.tsx +134 -0
  158. package/templates/typescript-starter/src/widgets/app/layout.tsx +14 -0
  159. package/templates/typescript-starter/src/widgets/next.config.js +37 -0
  160. package/templates/typescript-starter/src/widgets/package.json +24 -0
  161. package/templates/typescript-starter/src/widgets/tsconfig.json +28 -0
  162. package/templates/typescript-starter/src/widgets/widget-manifest.json +48 -0
  163. package/templates/typescript-starter/tsconfig.json +23 -0
  164. package/LICENSE_URLS_UPDATE_COMPLETE.md +0 -388
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@nitrostack/studio",
3
+ "version": "3.1.0",
4
+ "description": "NitroStack Studio - Visual MCP Server Inspector",
5
+ "private": true,
6
+ "type": "module",
7
+ "scripts": {
8
+ "dev": "next dev -p 3000",
9
+ "build": "next build",
10
+ "start": "next start",
11
+ "lint": "next lint",
12
+ "export": "next build && next export"
13
+ },
14
+ "dependencies": {
15
+ "@fontsource/inter": "^5.2.8",
16
+ "@fontsource/jetbrains-mono": "^5.2.8",
17
+ "@modelcontextprotocol/sdk": "^1.0.4",
18
+ "clsx": "^2.1.0",
19
+ "lucide-react": "^0.546.0",
20
+ "next": "^14.2.5",
21
+ "react": "^18.3.1",
22
+ "react-dom": "^18.3.1",
23
+ "zustand": "^4.5.0"
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "^22.10.5",
27
+ "@types/react": "^18.3.3",
28
+ "@types/react-dom": "^18.3.0",
29
+ "autoprefixer": "^10.4.19",
30
+ "postcss": "^8.4.38",
31
+ "tailwindcss": "^3.4.4",
32
+ "typescript": "^5.3.3"
33
+ }
34
+ }
@@ -0,0 +1,10 @@
1
+ /** @type {import('postcss-load-config').Config} */
2
+ const config = {
3
+ plugins: {
4
+ tailwindcss: {},
5
+ autoprefixer: {},
6
+ },
7
+ };
8
+
9
+ export default config;
10
+
@@ -0,0 +1,67 @@
1
+ import type { Config } from 'tailwindcss';
2
+
3
+ const config: Config = {
4
+ darkMode: 'class',
5
+ content: [
6
+ './pages/**/*.{js,ts,jsx,tsx,mdx}',
7
+ './components/**/*.{js,ts,jsx,tsx,mdx}',
8
+ './app/**/*.{js,ts,jsx,tsx,mdx}',
9
+ ],
10
+ theme: {
11
+ extend: {
12
+ colors: {
13
+ // Brand colors - Gold & Black
14
+ primary: {
15
+ DEFAULT: '#d4af37', // Gold
16
+ 50: '#fdfcf8',
17
+ 100: '#f9f7ed',
18
+ 200: '#f2ecd3',
19
+ 300: '#ead9a1',
20
+ 400: '#e2c870',
21
+ 500: '#d4af37', // Main gold
22
+ 600: '#b8962b',
23
+ 700: '#967824',
24
+ 800: '#7a6020',
25
+ 900: '#654f1d',
26
+ },
27
+ background: 'hsl(var(--background))',
28
+ foreground: 'hsl(var(--foreground))',
29
+ card: {
30
+ DEFAULT: 'hsl(var(--card))',
31
+ foreground: 'hsl(var(--card-foreground))',
32
+ },
33
+ popover: {
34
+ DEFAULT: 'hsl(var(--popover))',
35
+ foreground: 'hsl(var(--popover-foreground))',
36
+ },
37
+ secondary: {
38
+ DEFAULT: 'hsl(var(--secondary))',
39
+ foreground: 'hsl(var(--secondary-foreground))',
40
+ },
41
+ muted: {
42
+ DEFAULT: 'hsl(var(--muted))',
43
+ foreground: 'hsl(var(--muted-foreground))',
44
+ },
45
+ accent: {
46
+ DEFAULT: 'hsl(var(--accent))',
47
+ foreground: 'hsl(var(--accent-foreground))',
48
+ },
49
+ destructive: {
50
+ DEFAULT: 'hsl(var(--destructive))',
51
+ foreground: 'hsl(var(--destructive-foreground))',
52
+ },
53
+ border: 'hsl(var(--border))',
54
+ input: 'hsl(var(--input))',
55
+ ring: 'hsl(var(--ring))',
56
+ },
57
+ borderRadius: {
58
+ lg: 'var(--radius)',
59
+ md: 'calc(var(--radius) - 2px)',
60
+ sm: 'calc(var(--radius) - 4px)',
61
+ },
62
+ },
63
+ },
64
+ plugins: [],
65
+ };
66
+
67
+ export default config;
@@ -0,0 +1,41 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "lib": [
5
+ "dom",
6
+ "dom.iterable",
7
+ "esnext"
8
+ ],
9
+ "allowJs": true,
10
+ "skipLibCheck": true,
11
+ "strict": true,
12
+ "noEmit": true,
13
+ "esModuleInterop": true,
14
+ "module": "esnext",
15
+ "moduleResolution": "bundler",
16
+ "resolveJsonModule": true,
17
+ "isolatedModules": true,
18
+ "jsx": "preserve",
19
+ "incremental": true,
20
+ "plugins": [
21
+ {
22
+ "name": "next"
23
+ }
24
+ ],
25
+ "paths": {
26
+ "@/*": [
27
+ "./*"
28
+ ]
29
+ }
30
+ },
31
+ "include": [
32
+ "next-env.d.ts",
33
+ "**/*.ts",
34
+ "**/*.tsx",
35
+ ".next/types/**/*.ts",
36
+ "../../dist/studio/types/**/*.ts"
37
+ ],
38
+ "exclude": [
39
+ "node_modules"
40
+ ]
41
+ }
@@ -0,0 +1,23 @@
1
+ # E-commerce MCP Server Configuration
2
+
3
+ # Server
4
+ PORT=3000
5
+
6
+ # JWT Authentication (REQUIRED for this template)
7
+ # Generate with: npm run setup-db (this will generate a secret for you)
8
+ JWT_SECRET=your-jwt-secret-here-change-this
9
+ JWT_AUDIENCE=ecommerce-mcp-server
10
+ JWT_ISSUER=ecommerce-app
11
+
12
+ # Database
13
+ # Note: Use absolute path for production, relative path works for development
14
+ DATABASE_PATH=./data/ecommerce.db
15
+
16
+ # Logging
17
+ LOG_LEVEL=info
18
+
19
+ # Test Users (created by setup-db script)
20
+ # Username: alice@example.com | Password: password123
21
+ # Username: bob@example.com | Password: password123
22
+ # Username: charlie@example.com | Password: password123
23
+
@@ -0,0 +1,103 @@
1
+ import { McpApp, Module, ConfigModule, JWTModule } from 'nitrostack';
2
+
3
+ // Services
4
+ import { DatabaseService } from './services/database.service.js';
5
+
6
+ // Modules
7
+ import { AuthModule } from './modules/auth/auth.module.js';
8
+ import { ProductsModule } from './modules/products/products.module.js';
9
+ import { CartModule } from './modules/cart/cart.module.js';
10
+ import { OrdersModule } from './modules/orders/orders.module.js';
11
+ import { AddressesModule } from './modules/addresses/addresses.module.js';
12
+
13
+ // Middleware
14
+ import { LoggingMiddleware } from './middleware/logging.middleware.js';
15
+
16
+ // Interceptors
17
+ import { TransformInterceptor } from './interceptors/transform.interceptor.js';
18
+
19
+ // Pipes
20
+ import { ValidationPipe } from './pipes/validation.pipe.js';
21
+
22
+ // Filters
23
+ import { GlobalExceptionFilter } from './filters/global-exception.filter.js';
24
+
25
+ // Events
26
+ import { AnalyticsService } from './events/analytics.service.js';
27
+ import { NotificationService } from './events/notification.service.js';
28
+
29
+ // Health
30
+ import { DatabaseHealthCheck } from './health/database.health.js';
31
+
32
+ /**
33
+ * Root Application Module
34
+ *
35
+ * This is the main module that bootstraps the MCP server.
36
+ * It registers all feature modules, services, and global providers.
37
+ */
38
+ @McpApp({
39
+ module: AppModule,
40
+ server: {
41
+ name: 'E-commerce MCP Server',
42
+ version: '3.0.0',
43
+ },
44
+ logging: {
45
+ level: (process.env.LOG_LEVEL as any) || 'info',
46
+ },
47
+ })
48
+ @Module({
49
+ name: 'app',
50
+ description: 'Root application module',
51
+ imports: [
52
+ // Configuration
53
+ ConfigModule.forRoot({
54
+ envFilePath: '.env',
55
+ defaults: {
56
+ DATABASE_PATH: './data/ecommerce.db',
57
+ LOG_LEVEL: 'info',
58
+ },
59
+ }),
60
+
61
+ // JWT Authentication
62
+ JWTModule.forRoot({
63
+ secret: process.env.JWT_SECRET || 'your-secret-key',
64
+ expiresIn: '7d',
65
+ issuer: process.env.JWT_ISSUER || 'ecommerce-app',
66
+ audience: process.env.JWT_AUDIENCE || 'ecommerce-mcp-server',
67
+ }),
68
+
69
+ // Feature Modules
70
+ AuthModule,
71
+ ProductsModule,
72
+ CartModule,
73
+ OrdersModule,
74
+ AddressesModule,
75
+ ],
76
+ controllers: [],
77
+ providers: [
78
+ // Global Services
79
+ DatabaseService,
80
+
81
+ // Global Middleware
82
+ LoggingMiddleware,
83
+
84
+ // Global Interceptors
85
+ TransformInterceptor,
86
+
87
+ // Global Pipes
88
+ ValidationPipe,
89
+
90
+ // Global Filters
91
+ GlobalExceptionFilter,
92
+
93
+ // Event Handlers
94
+ AnalyticsService,
95
+ NotificationService,
96
+
97
+ // Health Checks
98
+ DatabaseHealthCheck,
99
+ ],
100
+ exports: [DatabaseService],
101
+ })
102
+ export class AppModule {}
103
+
@@ -0,0 +1,163 @@
1
+ import Database from 'better-sqlite3';
2
+ import path from 'path';
3
+ import fs from 'fs';
4
+
5
+ /**
6
+ * Database connection and utilities
7
+ */
8
+
9
+ let db: Database.Database | null = null;
10
+
11
+ export function getDatabase(): Database.Database {
12
+ if (!db) {
13
+ const envPath = process.env.DATABASE_PATH || './data/ecommerce.db';
14
+
15
+ // Resolve relative paths from the project root (process.cwd())
16
+ // Absolute paths are used as-is
17
+ const dbPath = path.isAbsolute(envPath)
18
+ ? envPath
19
+ : path.resolve(process.cwd(), envPath);
20
+
21
+ const dbDir = path.dirname(dbPath);
22
+
23
+ // Ensure data directory exists
24
+ if (!fs.existsSync(dbDir)) {
25
+ fs.mkdirSync(dbDir, { recursive: true });
26
+ }
27
+
28
+ db = new Database(dbPath);
29
+ db.pragma('journal_mode = WAL');
30
+ db.pragma('foreign_keys = ON');
31
+ }
32
+
33
+ return db;
34
+ }
35
+
36
+ export function closeDatabase(): void {
37
+ if (db) {
38
+ db.close();
39
+ db = null;
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Initialize database schema
45
+ */
46
+ export function initializeSchema(): void {
47
+ const db = getDatabase();
48
+
49
+ // Users table
50
+ db.exec(`
51
+ CREATE TABLE IF NOT EXISTS users (
52
+ id TEXT PRIMARY KEY,
53
+ email TEXT UNIQUE NOT NULL,
54
+ password_hash TEXT NOT NULL,
55
+ name TEXT NOT NULL,
56
+ profile_picture TEXT,
57
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP
58
+ )
59
+ `);
60
+
61
+ // Products table
62
+ db.exec(`
63
+ CREATE TABLE IF NOT EXISTS products (
64
+ id TEXT PRIMARY KEY,
65
+ name TEXT NOT NULL,
66
+ description TEXT,
67
+ price REAL NOT NULL,
68
+ category TEXT NOT NULL,
69
+ stock INTEGER NOT NULL DEFAULT 0,
70
+ image_url TEXT,
71
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP
72
+ )
73
+ `);
74
+
75
+ // Addresses table
76
+ db.exec(`
77
+ CREATE TABLE IF NOT EXISTS addresses (
78
+ id TEXT PRIMARY KEY,
79
+ user_id TEXT NOT NULL,
80
+ full_name TEXT NOT NULL,
81
+ street TEXT NOT NULL,
82
+ city TEXT NOT NULL,
83
+ state TEXT NOT NULL,
84
+ zip_code TEXT NOT NULL,
85
+ country TEXT NOT NULL DEFAULT 'USA',
86
+ phone TEXT NOT NULL,
87
+ is_default INTEGER DEFAULT 0,
88
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
89
+ FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
90
+ )
91
+ `);
92
+
93
+ // Carts table
94
+ db.exec(`
95
+ CREATE TABLE IF NOT EXISTS carts (
96
+ id TEXT PRIMARY KEY,
97
+ user_id TEXT NOT NULL,
98
+ product_id TEXT NOT NULL,
99
+ quantity INTEGER NOT NULL DEFAULT 1,
100
+ added_at DATETIME DEFAULT CURRENT_TIMESTAMP,
101
+ FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
102
+ FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE,
103
+ UNIQUE(user_id, product_id)
104
+ )
105
+ `);
106
+
107
+ // Orders table
108
+ db.exec(`
109
+ CREATE TABLE IF NOT EXISTS orders (
110
+ id TEXT PRIMARY KEY,
111
+ user_id TEXT NOT NULL,
112
+ address_id TEXT NOT NULL,
113
+ total_amount REAL NOT NULL,
114
+ status TEXT NOT NULL DEFAULT 'pending',
115
+ payment_method TEXT NOT NULL DEFAULT 'cash_on_delivery',
116
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
117
+ updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
118
+ FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
119
+ FOREIGN KEY (address_id) REFERENCES addresses(id)
120
+ )
121
+ `);
122
+
123
+ // Order items table
124
+ db.exec(`
125
+ CREATE TABLE IF NOT EXISTS order_items (
126
+ id TEXT PRIMARY KEY,
127
+ order_id TEXT NOT NULL,
128
+ product_id TEXT NOT NULL,
129
+ product_name TEXT NOT NULL,
130
+ product_price REAL NOT NULL,
131
+ quantity INTEGER NOT NULL,
132
+ subtotal REAL NOT NULL,
133
+ FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE,
134
+ FOREIGN KEY (product_id) REFERENCES products(id)
135
+ )
136
+ `);
137
+
138
+ // Create indexes
139
+ db.exec(`
140
+ CREATE INDEX IF NOT EXISTS idx_carts_user_id ON carts(user_id);
141
+ CREATE INDEX IF NOT EXISTS idx_orders_user_id ON orders(user_id);
142
+ CREATE INDEX IF NOT EXISTS idx_order_items_order_id ON order_items(order_id);
143
+ CREATE INDEX IF NOT EXISTS idx_addresses_user_id ON addresses(user_id);
144
+ `);
145
+ }
146
+
147
+ /**
148
+ * Clear all data (for testing)
149
+ */
150
+ export function clearAllData(): void {
151
+ const db = getDatabase();
152
+
153
+ db.exec(`
154
+ DELETE FROM order_items;
155
+ DELETE FROM orders;
156
+ DELETE FROM carts;
157
+ DELETE FROM addresses;
158
+ DELETE FROM products;
159
+ DELETE FROM users;
160
+ `);
161
+ }
162
+
163
+