@deessejs/collections 0.0.45 → 0.0.47

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.
@@ -2,4 +2,3 @@ import { Config } from "./types";
2
2
  export declare const defineConfig: (config: Config) => import("drizzle-orm/node-postgres").NodePgDatabase<Record<string, never>> & {
3
3
  $client: import("pg").Pool;
4
4
  };
5
- export declare const waitConfig: () => Promise<Config>;
@@ -1,18 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.waitConfig = exports.defineConfig = void 0;
3
+ exports.defineConfig = void 0;
4
+ // src/core/index.ts (ou là où est défini defineConfig)
4
5
  const node_postgres_1 = require("drizzle-orm/node-postgres");
5
- let _config;
6
6
  const defineConfig = (config) => {
7
- _config = config;
8
7
  const db = (0, node_postgres_1.drizzle)(config.databaseUrl);
8
+ // Attacher la config cachée pour que le worker puisse la lire
9
+ // sans avoir besoin de parser l'AST manuellement
10
+ Object.defineProperty(db, '_config', {
11
+ value: config,
12
+ enumerable: false, // Pour ne pas polluer les logs de console.log(db)
13
+ writable: false
14
+ });
15
+ // Optionnel : Génération initiale synchrone si on veut bloquer le démarrage
16
+ // generateShadowSchema(config.collections);
9
17
  return db;
10
18
  };
11
19
  exports.defineConfig = defineConfig;
12
- const waitConfig = async () => {
13
- while (!_config) {
14
- await new Promise((r) => setTimeout(r, 50));
15
- }
16
- return _config;
17
- };
18
- exports.waitConfig = waitConfig;
@@ -9,16 +9,50 @@ const constants_1 = require("next/constants");
9
9
  const path_1 = __importDefault(require("path"));
10
10
  const withCollections = (phase, config) => {
11
11
  const isDev = phase === constants_1.PHASE_DEVELOPMENT_SERVER;
12
+ // 1. Lancer le worker en arrière-plan (votre code actuel)
12
13
  if (isDev && !global.__collections_worker_started) {
13
14
  global.__collections_worker_started = true;
14
- const workerPath = path_1.default.join(__dirname, "../worker/index.js");
15
- console.log("[withCollections] Spawning background worker:", workerPath);
15
+ // Assurez-vous que ce chemin pointe vers le JS compilé de votre lib
16
+ const workerPath = path_1.default.resolve(__dirname, "../worker/index.js");
17
+ console.log("[Deesse] Spawning background worker:", workerPath);
16
18
  (0, child_process_1.spawn)("node", [workerPath], {
17
19
  stdio: "inherit",
20
+ env: { ...process.env },
18
21
  });
19
22
  }
23
+ // Chemin absolu vers le schéma généré
24
+ const shadowSchemaPath = path_1.default.join(process.cwd(), ".deesse", "shadow", "schema.ts");
20
25
  return {
21
26
  ...config,
27
+ // 2. Configuration pour Turbopack (Next.js 16+)
28
+ turbopack: {
29
+ ...(config.turbopack || {}),
30
+ resolveAlias: {
31
+ ...(config.turbopack?.resolveAlias || {}),
32
+ // Création de l'alias pour Turbopack
33
+ "@deesse/schema": shadowSchemaPath,
34
+ },
35
+ },
36
+ // 3. Configuration pour Webpack (Next.js < 16 ou --webpack)
37
+ webpack: (webpackConfig, options) => {
38
+ // Appliquer la config utilisateur existante s'il y en a une
39
+ if (typeof config.webpack === "function") {
40
+ webpackConfig = config.webpack(webpackConfig, options);
41
+ }
42
+ // Création de l'alias pour Webpack
43
+ webpackConfig.resolve.alias["@deesse/schema"] = shadowSchemaPath;
44
+ return webpackConfig;
45
+ },
46
+ // 4. Empêcher le bundling des paquets serveur (Drizzle, PG, etc.)
47
+ // C'est crucial pour éviter les erreurs de compilation côté Next
48
+ serverExternalPackages: [
49
+ ...(config.serverExternalPackages || []),
50
+ "drizzle-orm",
51
+ "drizzle-kit",
52
+ "pg",
53
+ "jiti", // Important pour que Jiti ne soit pas bundlé si utilisé côté serveur
54
+ "@deessejs/collections", // Votre lib
55
+ ],
22
56
  };
23
57
  };
24
58
  exports.withCollections = withCollections;
@@ -3,9 +3,56 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const jiti_1 = require("jiti");
6
7
  const path_1 = __importDefault(require("path"));
7
- const collectionsDir = path_1.default.join(process.cwd(), "src", "collections");
8
- console.log(`[worker] Loading collections from ${collectionsDir}`);
9
- (async () => {
10
- console.log(`[worker] Shadow schema generated for collections.`);
11
- })();
8
+ const chokidar_1 = __importDefault(require("chokidar"));
9
+ const fs_1 = __importDefault(require("fs"));
10
+ const generate_1 = require("../drizzle/generate");
11
+ // Chemins relatifs au projet de l'utilisateur (process.cwd())
12
+ const PROJECT_ROOT = process.cwd();
13
+ const CONFIG_PATH = path_1.default.join(PROJECT_ROOT, "src", "api", "index.ts");
14
+ const COLLECTIONS_DIR = path_1.default.join(PROJECT_ROOT, "src", "collections");
15
+ // Initialiser Jiti pour charger du TS à la volée
16
+ const jiti = (0, jiti_1.createJiti)(PROJECT_ROOT, {
17
+ interopDefault: true,
18
+ requireCache: false, // Désactiver le cache pour le hot-reload
19
+ });
20
+ async function runGenerator() {
21
+ try {
22
+ // Vérifier si le fichier existe avant
23
+ if (!fs_1.default.existsSync(CONFIG_PATH)) {
24
+ console.warn(`[Deesse] Config file not found at ${CONFIG_PATH}`);
25
+ return;
26
+ }
27
+ // 1. Importer la config utilisateur via Jiti
28
+ // Jiti compile le TS en JS à la volée, indépendamment de Next.js
29
+ const mod = await jiti.import(CONFIG_PATH);
30
+ // 2. Récupérer l'instance db exportée
31
+ const dbInstance = mod.db || mod.default;
32
+ // Note : Vous devrez modifier votre `defineConfig` pour qu'il attache la config brute à l'objet retourné
33
+ // Exemple dans src/api/index.ts : (db as any)._config = config;
34
+ if (!dbInstance || !dbInstance._config) {
35
+ // Si c'est le premier run, on ne spam pas l'erreur, l'utilisateur est peut-être en train d'écrire
36
+ return;
37
+ }
38
+ const collections = dbInstance._config.collections;
39
+ // 3. Générer le fichier de schéma
40
+ console.log("[Deesse] ⚡ Updating Drizzle schema...");
41
+ (0, generate_1.generateShadowSchema)(collections);
42
+ }
43
+ catch (error) {
44
+ console.error("[Deesse] ❌ Error in schema generation worker:", error);
45
+ }
46
+ }
47
+ // --- Démarrage ---
48
+ // 1. Génération initiale
49
+ runGenerator();
50
+ // 2. Mode Watch
51
+ console.log(`[Deesse] 👀 Watching changes in: src/collections`);
52
+ const watcher = chokidar_1.default.watch([CONFIG_PATH, COLLECTIONS_DIR], {
53
+ ignoreInitial: true,
54
+ ignored: /(^|[\/\\])\../,
55
+ });
56
+ watcher.on("all", () => {
57
+ runGenerator();
58
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deessejs/collections",
3
- "version": "0.0.45",
3
+ "version": "0.0.47",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -66,6 +66,7 @@
66
66
  "chokidar": "^5.0.0",
67
67
  "dotenv": "^17.2.3",
68
68
  "drizzle-orm": "^0.45.0",
69
+ "jiti": "^2.6.1",
69
70
  "pg": "^8.16.3"
70
71
  }
71
72
  }