@mostajs/octoswitcher 1.0.0 → 1.0.1
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 +101 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +21 -10
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# @mostajs/octoswitcher
|
|
2
|
+
|
|
3
|
+
> Data access switcher — ORM direct or NET transport, one interface, zero config in modules.
|
|
4
|
+
|
|
5
|
+
## Principe
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
MOSTA_DATA=orm → @mostajs/orm → SQL direct (SQLite, PostgreSQL, Oracle, MSSQL, etc.)
|
|
9
|
+
MOSTA_DATA=net → @mostajs/net → Transport distant (REST, GraphQL, gRPC, WS, MCP)
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Les modules @mostajs (auth, rbac, audit, settings, ticketing, secu) appellent `getDialect()` depuis octoswitcher — ils ne savent pas si les données viennent d'une base locale ou d'un serveur distant.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @mostajs/octoswitcher
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage dans un module
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { getDialect } from '@mostajs/octoswitcher'
|
|
24
|
+
import { UserRepository } from './repositories/user.repository.js'
|
|
25
|
+
|
|
26
|
+
const dialect = await getDialect()
|
|
27
|
+
const repo = new UserRepository(dialect)
|
|
28
|
+
const users = await repo.findAll()
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Configuration (dans l'app, pas dans le module)
|
|
32
|
+
|
|
33
|
+
### Mode ORM (accès direct base de données)
|
|
34
|
+
|
|
35
|
+
```env
|
|
36
|
+
MOSTA_DATA=orm
|
|
37
|
+
DB_DIALECT=sqlite
|
|
38
|
+
SGBD_URI=./data/app.db
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Mode NET (accès distant via transport)
|
|
42
|
+
|
|
43
|
+
```env
|
|
44
|
+
MOSTA_DATA=net
|
|
45
|
+
MOSTA_NET_URL=https://mcp.amia.fr/astro_08/
|
|
46
|
+
MOSTA_NET_TRANSPORT=rest
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## API
|
|
50
|
+
|
|
51
|
+
### `getDialect(): Promise<IDataDialect>`
|
|
52
|
+
Retourne le dialect singleton — ORM ou NET selon `MOSTA_DATA`.
|
|
53
|
+
|
|
54
|
+
### `getDataMode(): DataMode`
|
|
55
|
+
Retourne `'orm'` ou `'net'`.
|
|
56
|
+
|
|
57
|
+
### `isNetMode(): boolean`
|
|
58
|
+
Raccourci pour `getDataMode() === 'net'`.
|
|
59
|
+
|
|
60
|
+
### `isOrmMode(): boolean`
|
|
61
|
+
Raccourci pour `getDataMode() === 'orm'`.
|
|
62
|
+
|
|
63
|
+
### `setDialect(dialect): void`
|
|
64
|
+
Injecte un dialect externe (pour les tests).
|
|
65
|
+
|
|
66
|
+
### `resetDialect(): void`
|
|
67
|
+
Réinitialise le singleton (pour les tests).
|
|
68
|
+
|
|
69
|
+
## Architecture
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
App (.env.local)
|
|
73
|
+
├── MOSTA_DATA=net + MOSTA_NET_URL=...
|
|
74
|
+
│
|
|
75
|
+
├── @mostajs/auth → @mostajs/rbac → octoswitcher → getDialect()
|
|
76
|
+
├── @mostajs/rbac → octoswitcher → getDialect()
|
|
77
|
+
├── @mostajs/audit → octoswitcher → getDialect()
|
|
78
|
+
├── @mostajs/settings → octoswitcher → getDialect()
|
|
79
|
+
├── @mostajs/ticketing → octoswitcher → getDialect()
|
|
80
|
+
└── @mostajs/secu → octoswitcher → getDialect()
|
|
81
|
+
│
|
|
82
|
+
┌─────────┴─────────┐
|
|
83
|
+
│ │
|
|
84
|
+
MOSTA_DATA=orm MOSTA_DATA=net
|
|
85
|
+
@mostajs/orm @mostajs/net
|
|
86
|
+
SQL direct REST/GraphQL/gRPC
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Modules migrés
|
|
90
|
+
|
|
91
|
+
| Module | Avant | Après |
|
|
92
|
+
|--------|-------|-------|
|
|
93
|
+
| rbac | 212 lignes (data-mode.ts + NET factories) | 106 lignes |
|
|
94
|
+
| audit | 130 lignes (data-mode.ts + NET factories) | 41 lignes |
|
|
95
|
+
| settings | 119 lignes (data-mode.ts + NET factories) | 41 lignes |
|
|
96
|
+
| ticketing | hardcodé ORM | 1 import changé |
|
|
97
|
+
| secu | hardcodé ORM | 1 import changé |
|
|
98
|
+
|
|
99
|
+
## Licence
|
|
100
|
+
|
|
101
|
+
AGPL-3.0-or-later — Dr Hamid MADANI <drmdh@msn.com>
|
package/dist/index.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export declare function isOrmMode(): boolean;
|
|
|
38
38
|
* - MOSTA_DATA=orm (default): loads @mostajs/orm, connects via DB_DIALECT + SGBD_URI
|
|
39
39
|
* - MOSTA_DATA=net: loads @mostajs/net/client, creates NetDialectProxy via MOSTA_NET_URL + MOSTA_NET_TRANSPORT
|
|
40
40
|
*
|
|
41
|
-
*
|
|
41
|
+
* Global singleton — survives module re-imports and Next.js bundler splitting.
|
|
42
42
|
* All @mostajs modules (auth, rbac, audit, settings) call this function.
|
|
43
43
|
*/
|
|
44
44
|
export declare function getDialect(): Promise<IDataDialect>;
|
|
@@ -48,6 +48,6 @@ export declare function getDialect(): Promise<IDataDialect>;
|
|
|
48
48
|
*/
|
|
49
49
|
export declare function setDialect(dialect: IDataDialect): void;
|
|
50
50
|
/**
|
|
51
|
-
* Reset the singleton (for testing).
|
|
51
|
+
* Reset the singleton (for testing or after disconnectDialect).
|
|
52
52
|
*/
|
|
53
53
|
export declare function resetDialect(): void;
|
package/dist/index.js
CHANGED
|
@@ -19,39 +19,50 @@ export function isOrmMode() {
|
|
|
19
19
|
// ============================================================
|
|
20
20
|
// Singleton dialect
|
|
21
21
|
// ============================================================
|
|
22
|
-
|
|
22
|
+
// Global singleton — survives module re-imports and bundler splitting
|
|
23
|
+
// Uses globalThis to ensure a single dialect instance across the entire process
|
|
24
|
+
const GLOBAL_KEY = '__mostajs_octoswitcher_dialect__';
|
|
25
|
+
function getGlobalDialect() {
|
|
26
|
+
return globalThis[GLOBAL_KEY] ?? null;
|
|
27
|
+
}
|
|
28
|
+
function setGlobalDialect(dialect) {
|
|
29
|
+
globalThis[GLOBAL_KEY] = dialect;
|
|
30
|
+
}
|
|
23
31
|
/**
|
|
24
32
|
* Get the data dialect — ORM or NET, determined by MOSTA_DATA env var.
|
|
25
33
|
*
|
|
26
34
|
* - MOSTA_DATA=orm (default): loads @mostajs/orm, connects via DB_DIALECT + SGBD_URI
|
|
27
35
|
* - MOSTA_DATA=net: loads @mostajs/net/client, creates NetDialectProxy via MOSTA_NET_URL + MOSTA_NET_TRANSPORT
|
|
28
36
|
*
|
|
29
|
-
*
|
|
37
|
+
* Global singleton — survives module re-imports and Next.js bundler splitting.
|
|
30
38
|
* All @mostajs modules (auth, rbac, audit, settings) call this function.
|
|
31
39
|
*/
|
|
32
40
|
export async function getDialect() {
|
|
33
|
-
|
|
34
|
-
|
|
41
|
+
const cached = getGlobalDialect();
|
|
42
|
+
if (cached)
|
|
43
|
+
return cached;
|
|
44
|
+
let dialect;
|
|
35
45
|
if (isNetMode()) {
|
|
36
|
-
|
|
46
|
+
dialect = await initNetDialect();
|
|
37
47
|
}
|
|
38
48
|
else {
|
|
39
|
-
|
|
49
|
+
dialect = await initOrmDialect();
|
|
40
50
|
}
|
|
41
|
-
|
|
51
|
+
setGlobalDialect(dialect);
|
|
52
|
+
return dialect;
|
|
42
53
|
}
|
|
43
54
|
/**
|
|
44
55
|
* Inject an external dialect (for testing or custom implementations).
|
|
45
56
|
* After this call, getDialect() returns the injected dialect.
|
|
46
57
|
*/
|
|
47
58
|
export function setDialect(dialect) {
|
|
48
|
-
|
|
59
|
+
setGlobalDialect(dialect);
|
|
49
60
|
}
|
|
50
61
|
/**
|
|
51
|
-
* Reset the singleton (for testing).
|
|
62
|
+
* Reset the singleton (for testing or after disconnectDialect).
|
|
52
63
|
*/
|
|
53
64
|
export function resetDialect() {
|
|
54
|
-
|
|
65
|
+
setGlobalDialect(null);
|
|
55
66
|
}
|
|
56
67
|
// ============================================================
|
|
57
68
|
// ORM mode — direct database access
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mostajs/octoswitcher",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Data access switcher — ORM direct or NET transport, one interface, zero config in modules",
|
|
5
5
|
"author": "Dr Hamid MADANI <drmdh@msn.com>",
|
|
6
6
|
"license": "AGPL-3.0-or-later",
|