@deessejs/collections 0.0.49 → 0.0.50
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/config/index.d.ts +2 -3
- package/dist/config/index.js +39 -7
- package/dist/drizzle/generate.js +21 -2
- package/package.json +1 -1
package/dist/config/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
+
import { drizzle } from "drizzle-orm/node-postgres";
|
|
1
2
|
import { Config } from "./types";
|
|
2
|
-
export declare const defineConfig: (config: Config) =>
|
|
3
|
-
$client: import("pg").Pool;
|
|
4
|
-
};
|
|
3
|
+
export declare const defineConfig: (config: Config) => ReturnType<typeof drizzle<any>>;
|
package/dist/config/index.js
CHANGED
|
@@ -1,19 +1,51 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.defineConfig = void 0;
|
|
4
|
-
// src/core/index.ts (ou là où est défini defineConfig)
|
|
5
7
|
const node_postgres_1 = require("drizzle-orm/node-postgres");
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
// Fonction utilitaire pour charger le schéma de manière "lazy"
|
|
10
|
+
const loadGeneratedSchema = () => {
|
|
11
|
+
try {
|
|
12
|
+
// 1. Essayer via l'alias Webpack (fonctionne dans Next.js)
|
|
13
|
+
// On utilise un require dynamique pour éviter que le bundler ne râle à la compilation
|
|
14
|
+
// si le fichier n'est pas là.
|
|
15
|
+
return require("@deesse/schema");
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
try {
|
|
19
|
+
// 2. Fallback : Essayer le chemin physique (si on est hors contexte webpack, ex: scripts)
|
|
20
|
+
const localPath = path_1.default.join(process.cwd(), ".deesse", "shadow", "schema.ts");
|
|
21
|
+
// Note: require de fichier .ts natif nécessite jiti ou ts-node,
|
|
22
|
+
// mais en prod Next.js ce sera compilé.
|
|
23
|
+
// En dev, Next.js gère le TS.
|
|
24
|
+
return require(localPath);
|
|
25
|
+
}
|
|
26
|
+
catch (e2) {
|
|
27
|
+
// 3. Premier lancement : le schéma n'existe pas encore
|
|
28
|
+
console.warn("[Deesse] ⚠️ Schema not found. 'db.query' will be empty until schema generation completes.");
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
6
33
|
const defineConfig = (config) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
//
|
|
34
|
+
// 1. Charger le schéma généré
|
|
35
|
+
const schema = loadGeneratedSchema();
|
|
36
|
+
// 2. Initialiser Drizzle AVEC le schéma
|
|
37
|
+
const db = (0, node_postgres_1.drizzle)(config.databaseUrl, {
|
|
38
|
+
schema,
|
|
39
|
+
// logger: true // utile pour debug
|
|
40
|
+
});
|
|
41
|
+
// 3. Attacher la config pour le worker (votre code existant)
|
|
10
42
|
Object.defineProperty(db, '_config', {
|
|
11
43
|
value: config,
|
|
12
|
-
enumerable: false,
|
|
44
|
+
enumerable: false,
|
|
13
45
|
writable: false
|
|
14
46
|
});
|
|
15
|
-
//
|
|
16
|
-
//
|
|
47
|
+
// 4. On caste le retour pour faire croire à TS qu'on a le bon typage
|
|
48
|
+
// (Voir section Typage ci-dessous)
|
|
17
49
|
return db;
|
|
18
50
|
};
|
|
19
51
|
exports.defineConfig = defineConfig;
|
package/dist/drizzle/generate.js
CHANGED
|
@@ -11,12 +11,14 @@ const SHADOW_DIR = path_1.default.join(process.cwd(), ".deesse", "shadow");
|
|
|
11
11
|
const SCHEMA_PATH = path_1.default.join(SHADOW_DIR, "schema.ts");
|
|
12
12
|
const generateShadowSchema = (collections) => {
|
|
13
13
|
fs_1.default.mkdirSync(SHADOW_DIR, { recursive: true });
|
|
14
|
+
// 1. Générer le code des tables
|
|
14
15
|
const tablesCode = collections.map((col) => {
|
|
15
16
|
const columns = Object.entries(col.fields)
|
|
16
17
|
.map(([name, field]) => {
|
|
17
18
|
const kind = field.type.dsl.kind;
|
|
18
19
|
const drizzleType = Object.keys(_1.DrizzleTypes).includes(kind)
|
|
19
|
-
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
? `p.${kind}()` // TODO: gérer les params (varchar(255), etc.)
|
|
20
22
|
: `p.text()`;
|
|
21
23
|
return ` ${name}: ${drizzleType},`;
|
|
22
24
|
})
|
|
@@ -26,12 +28,29 @@ export const ${col.slug} = p.pgTable("${col.slug}", {
|
|
|
26
28
|
${columns}
|
|
27
29
|
});`;
|
|
28
30
|
});
|
|
31
|
+
// 2. Générer le code des relations (Pour l'instant vide ou basique)
|
|
32
|
+
// Drizzle a besoin de "relations" importé de "drizzle-orm"
|
|
33
|
+
const relationsCode = `
|
|
34
|
+
import { relations } from "drizzle-orm";
|
|
35
|
+
|
|
36
|
+
// Placeholder pour les relations futures
|
|
37
|
+
export const schemaRelations = relations(posts, ({ one, many }) => ({
|
|
38
|
+
// Logique à implémenter plus tard en lisant les champs "relationship" de vos collections
|
|
39
|
+
}));
|
|
40
|
+
`;
|
|
41
|
+
// 3. Assembler le fichier
|
|
29
42
|
const schemaFileContent = `
|
|
30
43
|
import * as p from "drizzle-orm/pg-core";
|
|
44
|
+
import { relations } from "drizzle-orm";
|
|
31
45
|
|
|
46
|
+
// --- Tables ---
|
|
32
47
|
${tablesCode.join("\n")}
|
|
48
|
+
|
|
49
|
+
// --- Relations ---
|
|
50
|
+
// (Note: Pour l'instant on ne génère pas de relations complexes automatiquement,
|
|
51
|
+
// mais on prépare le terrain)
|
|
33
52
|
`;
|
|
34
53
|
fs_1.default.writeFileSync(SCHEMA_PATH, schemaFileContent);
|
|
35
|
-
console.log("[Deesse] Shadow schema generated at", SCHEMA_PATH);
|
|
54
|
+
console.log("[Deesse] ✅ Shadow schema generated at", SCHEMA_PATH);
|
|
36
55
|
};
|
|
37
56
|
exports.generateShadowSchema = generateShadowSchema;
|