-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a219a6
commit 1417942
Showing
12 changed files
with
339 additions
and
215 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import type Configure from '@adonisjs/core/commands/configure' | ||
|
||
/** | ||
* We only allow configuring the one's thoroughly tested | ||
* inside the Lucid codebase. Knex supports more and one | ||
* can reference knex docs to configure additional | ||
* dialects. | ||
*/ | ||
const DIALECTS = ['sqlite', 'mysql', 'postgres', 'mssql'] as const | ||
const DIALECTS_INFO: { | ||
[K in (typeof DIALECTS)[number]]: { | ||
envVars?: Record<string, number | string> | ||
envValidations?: Record<string, string> | ||
pkg: string | ||
} | ||
} = { | ||
sqlite: { | ||
pkg: 'sqlite3', | ||
}, | ||
mysql: { | ||
pkg: 'mysql2', | ||
envVars: { | ||
DB_HOST: '127.0.0.1', | ||
DB_PORT: 3306, | ||
DB_USER: 'root', | ||
DB_PASSWORD: '', | ||
DB_DATABASE: '', | ||
}, | ||
envValidations: { | ||
DB_HOST: `Env.schema.string({ format: 'host' })`, | ||
DB_PORT: `Env.schema.number()`, | ||
DB_USER: 'Env.schema.string()', | ||
DB_PASSWORD: 'Env.schema.string()', | ||
DB_DATABASE: 'Env.schema.string()', | ||
}, | ||
}, | ||
postgres: { | ||
envVars: { | ||
DB_HOST: '127.0.0.1', | ||
DB_PORT: 5432, | ||
DB_USER: 'postgres', | ||
DB_PASSWORD: '', | ||
DB_DATABASE: '', | ||
}, | ||
envValidations: { | ||
DB_HOST: `Env.schema.string({ format: 'host' })`, | ||
DB_PORT: `Env.schema.number()`, | ||
DB_USER: 'Env.schema.string()', | ||
DB_PASSWORD: 'Env.schema.string()', | ||
DB_DATABASE: 'Env.schema.string()', | ||
}, | ||
pkg: 'pg', | ||
}, | ||
mssql: { | ||
envVars: { | ||
DB_HOST: '127.0.0.1', | ||
DB_PORT: 1433, | ||
DB_USER: 'sa', | ||
DB_PASSWORD: '', | ||
DB_DATABASE: '', | ||
}, | ||
envValidations: { | ||
DB_HOST: `Env.schema.string({ format: 'host' })`, | ||
DB_PORT: `Env.schema.number()`, | ||
DB_USER: 'Env.schema.string()', | ||
DB_PASSWORD: 'Env.schema.string()', | ||
DB_DATABASE: 'Env.schema.string()', | ||
}, | ||
pkg: 'tedious', | ||
}, | ||
} | ||
|
||
/** | ||
* Configures the package | ||
*/ | ||
export async function configure(command: Configure) { | ||
const codemods = await command.createCodemods() | ||
await codemods.updateRcFile((rcFile) => { | ||
rcFile.addProvider('@adonisjs/lucid/database_provider') | ||
}) | ||
|
||
/** | ||
* Prompt to select the dialect to use | ||
*/ | ||
const dialect = | ||
(await command.prompt.choice('Select the database you want to use', DIALECTS, { | ||
hint: 'You can always change it later', | ||
})) || 'postgres' | ||
|
||
const { pkg, envVars, envValidations } = DIALECTS_INFO[dialect] | ||
|
||
/** | ||
* Define environment variables | ||
*/ | ||
if (envVars) { | ||
codemods.defineEnvVariables(envVars) | ||
} | ||
|
||
/** | ||
* Define environment validations | ||
*/ | ||
if (envValidations) { | ||
codemods.defineEnvValidations({ | ||
variables: envValidations, | ||
leadingComment: 'Variables for configuring ally package', | ||
}) | ||
} | ||
|
||
/** | ||
* Publish config | ||
*/ | ||
await command.publishStub('config.stub', { dialect: dialect }) | ||
|
||
/** | ||
* Prompt to install package | ||
*/ | ||
if (await command.prompt.confirm(`Do you want to install npm package "${pkg}"?`)) { | ||
await command.installPackages([{ name: pkg, isDevDependency: false }]) | ||
} else { | ||
command.listPackagesToInstall([{ name: pkg, isDevDependency: false }]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
to: {{ app.configPath('database.ts') }} | ||
--- | ||
import env from '#start/env' | ||
import app from '@adonisjs/core/services/app' | ||
import { defineConfig } from '@adonisjs/lucid' | ||
|
||
const allyConfig = defineConfig({ | ||
connection: '{{ dialect }}', | ||
|
||
connections: { | ||
{{#if dialect === 'sqlite'}} | ||
sqlite: { | ||
client: 'sqlite', | ||
connection: { | ||
filename: app.tmpPath('db.sqlite3') | ||
}, | ||
useNullAsDefault: true, | ||
migrations: { | ||
naturalSort: true, | ||
paths: ['database/migrations'], | ||
}, | ||
} | ||
{{#elif dialect === 'postgres'}} | ||
postgres: { | ||
client: 'pg', | ||
connection: { | ||
host: env.get('DB_HOST'), | ||
port: env.get('DB_PORT'), | ||
user: env.get('DB_USER'), | ||
password: env.get('DB_PASSWORD'), | ||
database: env.get('DB_DATABASE'), | ||
}, | ||
migrations: { | ||
naturalSort: true, | ||
paths: ['database/migrations'], | ||
} | ||
} | ||
{{#elif dialect === 'mysql'}} | ||
mysql: { | ||
client: 'mysql2', | ||
connection: { | ||
host: env.get('DB_HOST'), | ||
port: env.get('DB_PORT'), | ||
user: env.get('DB_USER'), | ||
password: env.get('DB_PASSWORD'), | ||
database: env.get('DB_DATABASE'), | ||
}, | ||
migrations: { | ||
naturalSort: true, | ||
paths: ['database/migrations'], | ||
} | ||
} | ||
{{#elif dialect === 'mssql'}} | ||
mssql: { | ||
client: 'tedious', | ||
connection: { | ||
server: env.get('DB_HOST'), | ||
port: env.get('DB_PORT'), | ||
user: env.get('DB_USER'), | ||
password: env.get('DB_PASSWORD'), | ||
database: env.get('DB_DATABASE'), | ||
}, | ||
migrations: { | ||
naturalSort: true, | ||
paths: ['database/migrations'], | ||
} | ||
} | ||
{{/if}} | ||
} | ||
}) |
Oops, something went wrong.