@@ -4,30 +4,30 @@ import BaseCommand from '../base-command.js'
4
4
import { getExtension , getExtensionInstallations , getSiteConfiguration , installExtension } from './utils.js'
5
5
import { initDrizzle } from './drizzle.js'
6
6
7
- const NETLIFY_DATABASE_EXTENSION_SLUG = '-94w9m6w- netlify-database-extension '
7
+ const NETLIFY_DATABASE_EXTENSION_SLUG = '7jjmnqyo- netlify-neon '
8
8
9
9
const init = async ( _options : OptionValues , command : BaseCommand ) => {
10
- process . env . UNSTABLE_NETLIFY_DATABASE_EXTENSION_HOST_SITE_URL = 'http://localhost:8989'
11
-
12
10
if ( ! command . siteId ) {
13
11
console . error ( `The project must be linked with netlify link before initializing a database.` )
14
12
return
15
13
}
16
14
17
15
const initialOpts = command . opts ( )
18
16
19
- const answers = await inquirer . prompt (
20
- [
21
- {
22
- type : 'confirm' ,
23
- name : 'drizzle' ,
24
- message : 'Use Drizzle?' ,
25
- } ,
26
- ] . filter ( ( q ) => ! initialOpts [ q . name ] ) ,
27
- )
17
+ if ( initialOpts . drizzle !== false ) {
18
+ const answers = await inquirer . prompt (
19
+ [
20
+ {
21
+ type : 'confirm' ,
22
+ name : 'drizzle' ,
23
+ message : 'Use Drizzle?' ,
24
+ } ,
25
+ ] . filter ( ( q ) => ! initialOpts [ q . name ] ) ,
26
+ )
28
27
29
- if ( ! initialOpts . drizzle ) {
30
- command . setOptionValue ( 'drizzle' , answers . drizzle )
28
+ if ( ! initialOpts . drizzle ) {
29
+ command . setOptionValue ( 'drizzle' , answers . drizzle )
30
+ }
31
31
}
32
32
const opts = command . opts ( )
33
33
if ( opts . drizzle && command . project . root ) {
@@ -37,8 +37,7 @@ const init = async (_options: OptionValues, command: BaseCommand) => {
37
37
if ( ! command . netlify . api . accessToken ) {
38
38
throw new Error ( `No access token found, please login with netlify login` )
39
39
}
40
- console . log ( `Initializing a new database for site: ${ command . siteId }
41
- Please wait...` )
40
+
42
41
let site : Awaited < ReturnType < typeof command . netlify . api . getSite > >
43
42
try {
44
43
// @ts -expect-error -- feature_flags is not in the types
@@ -48,10 +47,14 @@ Please wait...`)
48
47
cause : e ,
49
48
} )
50
49
}
50
+ // console.log('site', site)
51
51
if ( ! site . account_id ) {
52
52
throw new Error ( `No account id found for site ${ command . siteId } ` )
53
53
}
54
54
55
+ console . log ( `Initializing a new database for site: ${ command . siteId } on account ${ site . account_id }
56
+ Please wait...` )
57
+
55
58
const netlifyToken = command . netlify . api . accessToken . replace ( 'Bearer ' , '' )
56
59
const extension = await getExtension ( {
57
60
accountId : site . account_id ,
@@ -63,20 +66,7 @@ Please wait...`)
63
66
throw new Error ( `Failed to get extension host site url when installing extension` )
64
67
}
65
68
66
- const installations = await getExtensionInstallations ( {
67
- accountId : site . account_id ,
68
- siteId : command . siteId ,
69
- token : netlifyToken ,
70
- } )
71
- const dbExtensionInstallation = (
72
- installations as {
73
- integrationSlug : string
74
- } [ ]
75
- ) . find ( ( installation ) => installation . integrationSlug === NETLIFY_DATABASE_EXTENSION_SLUG )
76
-
77
- if ( ! dbExtensionInstallation ) {
78
- console . log ( `Netlify Database extension not installed on team ${ site . account_id } , attempting to install now...` )
79
-
69
+ if ( ! extension . installedOnTeam ) {
80
70
const answers = await inquirer . prompt ( [
81
71
{
82
72
type : 'confirm' ,
@@ -115,23 +105,24 @@ Please wait...`)
115
105
// no op, env var does not exist, so we just continue
116
106
}
117
107
118
- console . log ( 'Initializing a new database for site: ' , command . siteId )
119
-
120
- const initEndpoint = new URL (
121
- '/cli-db-init' ,
122
- process . env . UNSTABLE_NETLIFY_DATABASE_EXTENSION_HOST_SITE_URL ?? extension . hostSiteUrl ,
123
- ) . toString ( )
108
+ const extensionSiteUrl = process . env . UNSTABLE_NETLIFY_DATABASE_EXTENSION_HOST_SITE_URL ?? extension . hostSiteUrl
124
109
110
+ const initEndpoint = new URL ( '/cli-db-init' , extensionSiteUrl ) . toString ( )
111
+ console . log ( 'initEndpoint' , initEndpoint )
125
112
const req = await fetch ( initEndpoint , {
126
113
method : 'POST' ,
127
114
headers : {
128
115
'Content-Type' : 'application/json' ,
129
- Authorization : `Bearer ${ netlifyToken } ` ,
130
- 'x- nf-db-site-id' : command . siteId ,
131
- 'x- nf-db-account-id' : site . account_id ,
116
+ 'nf-db-token' : netlifyToken ,
117
+ 'nf-db-site-id' : command . siteId ,
118
+ 'nf-db-account-id' : site . account_id ,
132
119
} ,
133
120
} )
134
121
122
+ if ( ! req . ok ) {
123
+ throw new Error ( `Failed to initialize DB: ${ await req . text ( ) } ` )
124
+ }
125
+
135
126
const res = await req . json ( )
136
127
console . log ( res )
137
128
return
@@ -198,6 +189,7 @@ export const createDatabaseCommand = (program: BaseCommand) => {
198
189
. command ( 'init' )
199
190
. description ( 'Initialize a new database' )
200
191
. option ( '--drizzle' , 'Sets up drizzle-kit and drizzle-orm in your project' )
192
+ . option ( '--no-drizzle' , 'Skips drizzle' )
201
193
. action ( init )
202
194
203
195
dbCommand . command ( 'status' ) . description ( 'Check the status of the database' ) . action ( status )
0 commit comments