@mostajs/setup 2.1.11 → 2.1.13

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/dist/index.js CHANGED
@@ -13,7 +13,7 @@ export { discoverNpmModules } from './lib/discover-modules.js';
13
13
  export { loadSetupJson } from './lib/load-setup-json.js';
14
14
  // Catch-all route factory (replaces individual route files in host app)
15
15
  export { createSetupRoutes } from './api/routes.js';
16
- // NET client (for setup via @mostajs/net)
16
+ // NET client (setup-specific, with loadCollectionMap + resolveCollection)
17
17
  export { NetClient } from './lib/net-client.js';
18
18
  // API route factories (individual — still available for granular use)
19
19
  export { createTestDbHandler } from './api/test-db.route.js';
@@ -1,11 +1,13 @@
1
1
  import type { DialectType } from '../types/index.js';
2
2
  export interface EnvWriterOptions {
3
- dialect: DialectType;
4
- uri: string;
3
+ dialect?: DialectType;
4
+ uri?: string;
5
5
  /** Extra variables to write (e.g. STRIPE_KEY, SMTP_HOST) */
6
6
  extraVars?: Record<string, string>;
7
7
  /** Default app port (default: 3000) */
8
8
  port?: number;
9
+ /** Skip writing DB_DIALECT/SGBD_URI (for NET mode) */
10
+ skipDb?: boolean;
9
11
  }
10
12
  /**
11
13
  * Write or update .env.local with DB_DIALECT + SGBD_URI.
@@ -7,7 +7,7 @@ import path from 'path';
7
7
  * Preserves commented lines. Returns true if dialect changed (needs restart).
8
8
  */
9
9
  export async function writeEnvLocal(options) {
10
- const { dialect, uri, extraVars, port = 3000 } = options;
10
+ const { dialect, uri, extraVars, port = 3000, skipDb = false } = options;
11
11
  const envPath = path.resolve(process.cwd(), '.env.local');
12
12
  let content = '';
13
13
  let previousDialect = null;
@@ -20,16 +20,24 @@ export async function writeEnvLocal(options) {
20
20
  catch {
21
21
  // .env.local doesn't exist yet
22
22
  }
23
- const schemaValue = dialect !== 'mongodb' ? 'update' : undefined;
24
23
  if (content) {
25
- // Replace only active (uncommented) lines
26
- content = upsertEnvLine(content, 'DB_DIALECT', dialect);
27
- content = upsertEnvLine(content, 'SGBD_URI', uri);
28
- if (schemaValue) {
29
- content = upsertEnvLine(content, 'DB_SCHEMA_STRATEGY', schemaValue);
24
+ if (skipDb) {
25
+ // Mode NET : commenter DB_DIALECT, SGBD_URI, DB_SCHEMA_STRATEGY
26
+ content = content.replace(/^DB_DIALECT=(.+)$/m, '#DB_DIALECT=$1');
27
+ content = content.replace(/^SGBD_URI=(.+)$/m, '#SGBD_URI=$1');
28
+ content = content.replace(/^DB_SCHEMA_STRATEGY=(.+)$/m, '#DB_SCHEMA_STRATEGY=$1');
30
29
  }
31
- else if (/^DB_SCHEMA_STRATEGY=/m.test(content)) {
32
- content = content.replace(/^DB_SCHEMA_STRATEGY=.*$/m, '#DB_SCHEMA_STRATEGY=update');
30
+ else if (dialect && uri) {
31
+ // Mode ORM : écrire DB_DIALECT + SGBD_URI
32
+ content = upsertEnvLine(content, 'DB_DIALECT', dialect);
33
+ content = upsertEnvLine(content, 'SGBD_URI', uri);
34
+ const schemaValue = dialect !== 'mongodb' ? 'update' : undefined;
35
+ if (schemaValue) {
36
+ content = upsertEnvLine(content, 'DB_SCHEMA_STRATEGY', schemaValue);
37
+ }
38
+ else if (/^DB_SCHEMA_STRATEGY=/m.test(content)) {
39
+ content = content.replace(/^DB_SCHEMA_STRATEGY=.*$/m, '#DB_SCHEMA_STRATEGY=update');
40
+ }
33
41
  }
34
42
  // Write extra vars
35
43
  if (extraVars) {
@@ -42,29 +50,25 @@ export async function writeEnvLocal(options) {
42
50
  // Fresh .env.local
43
51
  const { randomBytes } = await import('crypto');
44
52
  const secret = randomBytes(32).toString('base64');
45
- const lines = [
46
- `DB_DIALECT=${dialect}`,
47
- `SGBD_URI=${uri}`,
48
- ...(schemaValue ? [`DB_SCHEMA_STRATEGY=${schemaValue}`] : []),
49
- '',
50
- '# NextAuth Configuration',
51
- `NEXTAUTH_URL=http://localhost:${port}`,
52
- `NEXTAUTH_SECRET=${secret}`,
53
- `AUTH_SECRET=${secret}`,
54
- `NEXT_PUBLIC_APP_URL=http://localhost:${port}`,
55
- '',
56
- '# Environment',
57
- 'NODE_ENV=development',
58
- `PORT=${port}`,
59
- '',
60
- ...(extraVars
61
- ? ['# App Configuration', ...Object.entries(extraVars).map(([k, v]) => `${k}=${v}`), '']
62
- : []),
63
- ];
53
+ const lines = [];
54
+ if (!skipDb && dialect && uri) {
55
+ const schemaValue = dialect !== 'mongodb' ? 'update' : undefined;
56
+ lines.push(`DB_DIALECT=${dialect}`, `SGBD_URI=${uri}`);
57
+ if (schemaValue)
58
+ lines.push(`DB_SCHEMA_STRATEGY=${schemaValue}`);
59
+ lines.push('');
60
+ }
61
+ lines.push('# NextAuth Configuration', `NEXTAUTH_URL=http://localhost:${port}`, `NEXTAUTH_SECRET=${secret}`, `AUTH_SECRET=${secret}`, `NEXT_PUBLIC_APP_URL=http://localhost:${port}`, '', '# Environment', 'NODE_ENV=development', `PORT=${port}`, '');
62
+ if (extraVars) {
63
+ lines.push('# App Configuration');
64
+ for (const [k, v] of Object.entries(extraVars))
65
+ lines.push(`${k}=${v}`);
66
+ lines.push('');
67
+ }
64
68
  content = lines.join('\n') + '\n';
65
69
  }
66
70
  fs.writeFileSync(envPath, content, 'utf-8');
67
- return previousDialect !== null && previousDialect !== dialect;
71
+ return previousDialect !== null && previousDialect !== (dialect ?? previousDialect);
68
72
  }
69
73
  function upsertEnvLine(content, key, value) {
70
74
  const regex = new RegExp(`^${key}=.*$`, 'm');
package/dist/lib/setup.js CHANGED
@@ -121,7 +121,9 @@ async function runNetInstall(installConfig, setupConfig) {
121
121
  apiKey: installConfig.net.apiKey,
122
122
  });
123
123
  // 1. Write .env.local with NET config
124
+ // En mode NET : pas de DB_DIALECT/SGBD_URI — seulement MOSTA_DATA + MOSTA_NET_URL
124
125
  const extraVars = {
126
+ MOSTA_DATA: 'net',
125
127
  MOSTA_NET_URL: installConfig.net.url,
126
128
  MOSTA_NET_TRANSPORT: installConfig.net.transport,
127
129
  ...(installConfig.net.apiKey ? { MOSTA_NET_API_KEY: installConfig.net.apiKey } : {}),
@@ -131,20 +133,14 @@ async function runNetInstall(installConfig, setupConfig) {
131
133
  extraVars['MOSTAJS_MODULES'] = installConfig.modules.join(',');
132
134
  }
133
135
  await writeEnvLocal({
134
- dialect: 'net',
135
- uri: installConfig.net.url,
136
+ skipDb: true, // Ne pas écrire DB_DIALECT/SGBD_URI
136
137
  extraVars,
137
138
  port: setupConfig.defaultPort,
138
139
  });
139
140
  const seeded = [];
140
- // 2. Verify NET server is reachable + load collection mapping
141
+ // 2. Verify NET server is reachable
141
142
  const health = await net.health();
142
- if (!health.entities?.length) {
143
- return { ok: false, error: 'Serveur NET accessible mais aucun schema chargé', needsRestart: false };
144
- }
145
- await net.loadCollectionMap();
146
- // 3. Seed RBAC via NET REST
147
- // Read setup.json directly to get RBAC definitions
143
+ // 3. Read setup.json for RBAC definitions
148
144
  const fs = await import('fs');
149
145
  const path = await import('path');
150
146
  let setupJson = null;
@@ -155,6 +151,48 @@ async function runNetInstall(installConfig, setupConfig) {
155
151
  }
156
152
  catch { }
157
153
  }
154
+ // 4. If server has no entities, try sending schemas via POST /api/upload-schemas-json
155
+ if (!health.entities?.length) {
156
+ let schemasToSend = [];
157
+ // Try schemas.json local
158
+ const schemasJsonPath = path.resolve(process.cwd(), 'schemas.json');
159
+ if (fs.existsSync(schemasJsonPath)) {
160
+ try {
161
+ schemasToSend = JSON.parse(fs.readFileSync(schemasJsonPath, 'utf-8'));
162
+ }
163
+ catch { }
164
+ }
165
+ // Fallback: ORM registry
166
+ if (schemasToSend.length === 0) {
167
+ try {
168
+ const { getAllSchemas } = await import('@mostajs/orm');
169
+ schemasToSend = getAllSchemas();
170
+ }
171
+ catch { }
172
+ }
173
+ if (schemasToSend.length > 0) {
174
+ // Send schemas to NET server so it can register + create tables
175
+ try {
176
+ const res = await fetch(`${installConfig.net.url}/api/upload-schemas-json`, {
177
+ method: 'POST',
178
+ headers: { 'Content-Type': 'application/json' },
179
+ body: JSON.stringify({ schemas: schemasToSend }),
180
+ });
181
+ const result = await res.json();
182
+ if (result.ok)
183
+ seeded.push(`schemas (${schemasToSend.length})`);
184
+ }
185
+ catch (e) {
186
+ console.error('[Setup] Failed to upload schemas to NET server:', e);
187
+ }
188
+ }
189
+ // Re-check
190
+ const health2 = await net.health();
191
+ if (!health2.entities?.length) {
192
+ return { ok: false, error: 'Serveur NET accessible mais aucun schema charge. Placez un schemas.json dans le repertoire du serveur NET et redemarrez-le.', needsRestart: false };
193
+ }
194
+ }
195
+ await net.loadCollectionMap();
158
196
  if (setupJson?.rbac) {
159
197
  const rbac = setupJson.rbac;
160
198
  // 3a. Upsert categories
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mostajs/setup",
3
- "version": "2.1.11",
3
+ "version": "2.1.13",
4
4
  "description": "Reusable setup wizard module — multi-dialect DB configuration, .env.local writer, seed runner",
5
5
  "author": "Dr Hamid MADANI <drmdh@msn.com>",
6
6
  "license": "MIT",
@@ -104,7 +104,8 @@
104
104
  "prepublishOnly": "npm run build"
105
105
  },
106
106
  "dependencies": {
107
- "@mostajs/orm": "^1.4.15",
107
+ "@mostajs/net": "^2.0.0",
108
+ "@mostajs/orm": "^1.7.0",
108
109
  "bcryptjs": "^2.4.3"
109
110
  },
110
111
  "devDependencies": {