@@ -4,7 +4,14 @@ import type { AstroConfig, AstroIntegrationLogger } from 'astro';
4
4
import { type SQL , sql } from 'drizzle-orm' ;
5
5
import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core' ;
6
6
import { normalizeDatabaseUrl } from '../../runtime/index.js' ;
7
- import { DB_CLIENTS , DB_PATH , RUNTIME_IMPORT , RUNTIME_VIRTUAL_IMPORT , VIRTUAL_CLIENT_MODULE_ID , VIRTUAL_MODULE_ID } from '../consts.js' ;
7
+ import {
8
+ DB_CLIENTS ,
9
+ DB_PATH ,
10
+ RUNTIME_IMPORT ,
11
+ RUNTIME_VIRTUAL_IMPORT ,
12
+ VIRTUAL_CLIENT_MODULE_ID ,
13
+ VIRTUAL_MODULE_ID ,
14
+ } from '../consts.js' ;
8
15
import { createClient } from '../db-client/libsql-local.js' ;
9
16
import { getResolvedFileUrl } from '../load-file.js' ;
10
17
import { getCreateIndexQueries , getCreateTableQuery , SEED_DEV_FILE_NAME } from '../queries.js' ;
@@ -77,6 +84,7 @@ export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
77
84
tables : params . tables . get ( ) ,
78
85
isBuild : command === 'build' ,
79
86
output : params . output ,
87
+ localExecution : false ,
80
88
} ) ;
81
89
}
82
90
@@ -87,6 +95,7 @@ export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
87
95
return getLocalVirtualModContents ( {
88
96
root : params . root ,
89
97
tables : params . tables . get ( ) ,
98
+ localExecution : false ,
90
99
} ) ;
91
100
}
92
101
@@ -108,6 +117,7 @@ export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
108
117
return getLocalVirtualModContents ( {
109
118
root : params . root ,
110
119
tables : params . tables . get ( ) ,
120
+ localExecution : false ,
111
121
} ) ;
112
122
} ,
113
123
} ;
@@ -117,23 +127,43 @@ export function getConfigVirtualModContents() {
117
127
return `export * from ${ RUNTIME_VIRTUAL_IMPORT } ` ;
118
128
}
119
129
130
+ /**
131
+ * Get the module import for the DB client.
132
+ * This is used to pick which module to import based on whether
133
+ * the DB client is being used by the CLI, or in the Astro runtime.
134
+ *
135
+ * This is important for the `astro db execute` command to work correctly.
136
+ *
137
+ * @param localExecution - Whether the DB client is being used in a local execution context (e.g. CLI commands).
138
+ * @returns The module import string for the DB client.
139
+ */
140
+ function getDBModule ( localExecution : boolean ) {
141
+ return localExecution
142
+ ? `import { createClient } from '${ DB_CLIENTS . node } ';`
143
+ : `import { createClient } from '${ VIRTUAL_CLIENT_MODULE_ID } ';` ;
144
+ }
145
+
120
146
export function getLocalVirtualModContents ( {
121
147
tables,
122
148
root,
123
- localExecution = false ,
149
+ localExecution,
124
150
} : {
125
151
tables : DBTables ;
126
152
root : URL ;
127
- // Request module to be loaded immediately in process
128
- localExecution ?: boolean ;
153
+ /**
154
+ * Used for the execute command to import the client directly.
155
+ * In other cases, we use the runtime only vite virtual module.
156
+ *
157
+ * This is used to ensure that the client is imported correctly
158
+ * when executing commands like `astro db execute`.
159
+ */
160
+ localExecution : boolean ;
129
161
} ) {
130
162
const { ASTRO_DATABASE_FILE } = getAstroEnv ( ) ;
131
163
const dbUrl = new URL ( DB_PATH , root ) ;
132
164
133
165
// If this is for the execute command, we need to import the client directly instead of using the runtime only virtual module.
134
- const clientImport = localExecution
135
- ? `import { createClient } from '${ DB_CLIENTS . node } ';`
136
- : `import { createClient } from '${ VIRTUAL_CLIENT_MODULE_ID } ';` ;
166
+ const clientImport = getDBModule ( localExecution ) ;
137
167
138
168
return `
139
169
import { asDrizzleTable, normalizeDatabaseUrl } from ${ RUNTIME_IMPORT } ;
@@ -153,14 +183,20 @@ export function getRemoteVirtualModContents({
153
183
appToken,
154
184
isBuild,
155
185
output,
156
- localExecution = false , // Used for execute command
186
+ localExecution,
157
187
} : {
158
188
tables : DBTables ;
159
189
appToken : string ;
160
190
isBuild : boolean ;
161
191
output : AstroConfig [ 'output' ] ;
162
- // Request module to be loaded immediately in process
163
- localExecution ?: boolean ;
192
+ /**
193
+ * Used for the execute command to import the client directly.
194
+ * In other cases, we use the runtime only vite virtual module.
195
+ *
196
+ * This is used to ensure that the client is imported correctly
197
+ * when executing commands like `astro db execute`.
198
+ */
199
+ localExecution : boolean ;
164
200
} ) {
165
201
const dbInfo = getRemoteDatabaseInfo ( ) ;
166
202
@@ -190,9 +226,7 @@ export function getRemoteVirtualModContents({
190
226
}
191
227
192
228
// If this is for the execute command, we need to import the client directly instead of using the runtime only virtual module.
193
- const clientImport = localExecution
194
- ? `import { createClient } from '${ DB_CLIENTS . node } ';`
195
- : `import { createClient } from '${ VIRTUAL_CLIENT_MODULE_ID } ';` ;
229
+ const clientImport = getDBModule ( localExecution ) ;
196
230
197
231
return `
198
232
import {asDrizzleTable} from ${ RUNTIME_IMPORT } ;
0 commit comments