webspresso 0.0.16 → 0.0.17
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/core/orm/index.js +75 -29
- package/package.json +1 -1
package/core/orm/index.js
CHANGED
|
@@ -61,10 +61,21 @@ function createDatabase(config) {
|
|
|
61
61
|
process.cwd(),
|
|
62
62
|
];
|
|
63
63
|
|
|
64
|
+
// Also try to resolve from parent directories (for nested projects)
|
|
65
|
+
let currentPath = process.cwd();
|
|
66
|
+
for (let i = 0; i < 5; i++) {
|
|
67
|
+
resolvePaths.push(path.join(currentPath, 'node_modules'));
|
|
68
|
+
const parent = path.dirname(currentPath);
|
|
69
|
+
if (parent === currentPath) break; // Reached root
|
|
70
|
+
currentPath = parent;
|
|
71
|
+
}
|
|
72
|
+
|
|
64
73
|
for (const resolvePath of resolvePaths) {
|
|
65
74
|
try {
|
|
66
75
|
driverPath = require.resolve(driverName, { paths: [resolvePath] });
|
|
67
76
|
// Pre-load the driver so Knex can find it in Module._cache
|
|
77
|
+
// This is critical: Knex uses require() internally, so we need to
|
|
78
|
+
// load it into the cache first
|
|
68
79
|
require(driverPath);
|
|
69
80
|
break;
|
|
70
81
|
} catch (e) {
|
|
@@ -72,22 +83,30 @@ function createDatabase(config) {
|
|
|
72
83
|
}
|
|
73
84
|
}
|
|
74
85
|
|
|
75
|
-
// If still not found,
|
|
86
|
+
// If still not found, try default resolution (might be in webspresso's node_modules)
|
|
76
87
|
if (!driverPath) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
88
|
+
try {
|
|
89
|
+
// Try to find it anywhere in the module resolution path
|
|
90
|
+
driverPath = require.resolve(driverName);
|
|
91
|
+
require(driverPath);
|
|
92
|
+
} catch (e) {
|
|
93
|
+
// Driver not found anywhere
|
|
94
|
+
const installCmd = driverName === 'better-sqlite3'
|
|
95
|
+
? 'npm install better-sqlite3 --save'
|
|
96
|
+
: driverName === 'pg'
|
|
97
|
+
? 'npm install pg --save'
|
|
98
|
+
: driverName === 'mysql2'
|
|
99
|
+
? 'npm install mysql2 --save'
|
|
100
|
+
: `npm install ${driverName} --save`;
|
|
101
|
+
|
|
102
|
+
throw new Error(
|
|
103
|
+
`Database driver "${driverName}" is not installed in your project. ` +
|
|
104
|
+
`Please install it with: ${installCmd}\n` +
|
|
105
|
+
`Note: Database drivers are peer dependencies and must be installed in your project's node_modules, not globally.\n` +
|
|
106
|
+
`Current working directory: ${process.cwd()}\n` +
|
|
107
|
+
`Make sure you run "${installCmd}" in your project directory.`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
91
110
|
}
|
|
92
111
|
}
|
|
93
112
|
|
|
@@ -111,11 +130,29 @@ function createDatabase(config) {
|
|
|
111
130
|
|
|
112
131
|
// Check if driver exists in project's node_modules
|
|
113
132
|
let driverExists = false;
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
133
|
+
let foundDriverPath = null;
|
|
134
|
+
const checkPaths = [
|
|
135
|
+
path.join(process.cwd(), 'node_modules'),
|
|
136
|
+
process.cwd(),
|
|
137
|
+
];
|
|
138
|
+
|
|
139
|
+
// Also check parent directories
|
|
140
|
+
let currentPath = process.cwd();
|
|
141
|
+
for (let i = 0; i < 5; i++) {
|
|
142
|
+
checkPaths.push(path.join(currentPath, 'node_modules'));
|
|
143
|
+
const parent = path.dirname(currentPath);
|
|
144
|
+
if (parent === currentPath) break;
|
|
145
|
+
currentPath = parent;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
for (const checkPath of checkPaths) {
|
|
149
|
+
try {
|
|
150
|
+
foundDriverPath = require.resolve(driverName, { paths: [checkPath] });
|
|
151
|
+
driverExists = true;
|
|
152
|
+
break;
|
|
153
|
+
} catch (resolveError) {
|
|
154
|
+
// Continue checking
|
|
155
|
+
}
|
|
119
156
|
}
|
|
120
157
|
|
|
121
158
|
if (!driverExists) {
|
|
@@ -127,15 +164,24 @@ function createDatabase(config) {
|
|
|
127
164
|
`Make sure you run "${installCmd}" in your project directory.`
|
|
128
165
|
);
|
|
129
166
|
} else {
|
|
130
|
-
// Driver exists but Knex can't find it -
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
167
|
+
// Driver exists but Knex can't find it - try to manually require it
|
|
168
|
+
try {
|
|
169
|
+
// Force load the driver into Module._cache
|
|
170
|
+
require(foundDriverPath);
|
|
171
|
+
// Retry Knex initialization
|
|
172
|
+
knexInstance = knex(config);
|
|
173
|
+
} catch (retryError) {
|
|
174
|
+
throw new Error(
|
|
175
|
+
`Database driver "${driverName}" is installed but Knex cannot find it. ` +
|
|
176
|
+
`This might be a module resolution issue. Try:\n` +
|
|
177
|
+
`1. Delete node_modules and package-lock.json\n` +
|
|
178
|
+
`2. Run "npm install" again\n` +
|
|
179
|
+
`3. Make sure "${driverName}" is in your package.json dependencies\n` +
|
|
180
|
+
`4. Verify the driver is accessible: node -e "require('${driverName}')"\n` +
|
|
181
|
+
`Driver found at: ${foundDriverPath}\n` +
|
|
182
|
+
`Original error: ${e.message}`
|
|
183
|
+
);
|
|
184
|
+
}
|
|
139
185
|
}
|
|
140
186
|
}
|
|
141
187
|
throw e;
|
package/package.json
CHANGED