-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor database conditionals for readability #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -11,8 +11,8 @@ import { | |
| } from '../../data'; | ||
| import type { CreateConfiguration, PackageJson } from '../../types'; | ||
| import { getPackageVersion } from '../../utils/getPackageVersion'; | ||
| import { computeFlags } from '../project/computeFlags'; | ||
| import { initTemplates } from '../db/dockerInitTemplates'; | ||
| import { computeFlags } from '../project/computeFlags'; | ||
|
|
||
| type CreatePackageJsonProps = Pick< | ||
| CreateConfiguration, | ||
|
|
@@ -29,6 +29,31 @@ type CreatePackageJsonProps = Pick< | |
| latest: boolean; | ||
| }; | ||
|
|
||
| const dbScripts = { | ||
| cockroachdb: { | ||
| clientCmd: 'cockroach sql --insecure --database=database', | ||
| waitCmd: initTemplates.cockroachdb.wait | ||
| }, | ||
| mariadb: { | ||
| clientCmd: | ||
| 'MYSQL_PWD=userpassword mariadb -h127.0.0.1 -u user database', | ||
| waitCmd: initTemplates.mariadb.wait | ||
| }, | ||
| mssql: { | ||
| clientCmd: | ||
| '/opt/mssql-tools18/bin/sqlcmd -C -S localhost -U sa -P SApassword1', | ||
| waitCmd: initTemplates.mssql.wait | ||
| }, | ||
| mysql: { | ||
| clientCmd: 'MYSQL_PWD=userpassword mysql -h127.0.0.1 -u user database', | ||
| waitCmd: initTemplates.mysql.wait | ||
| }, | ||
| postgresql: { | ||
| clientCmd: 'psql -h localhost -U user -d database', | ||
| waitCmd: initTemplates.postgresql.wait | ||
| } | ||
| } as const; | ||
|
|
||
| export const createPackageJson = ({ | ||
| projectName, | ||
| authProvider, | ||
|
|
@@ -163,107 +188,51 @@ export const createPackageJson = ({ | |
| typecheck: 'bun run tsc --noEmit' | ||
| }; | ||
|
|
||
| if ( | ||
| databaseEngine === 'postgresql' && | ||
| (!databaseHost || databaseHost === 'none') | ||
| ) { | ||
| scripts['db:up'] = | ||
| 'sh -c "docker info >/dev/null 2>&1 || sudo service docker start; docker compose -p postgresql -f db/docker-compose.db.yml up -d db"'; | ||
| scripts['db:down'] = | ||
| 'docker compose -p postgresql -f db/docker-compose.db.yml down'; | ||
| scripts['db:reset'] = | ||
| 'docker compose -p postgresql -f db/docker-compose.db.yml down -v'; | ||
| scripts['db:psql'] = | ||
| "docker compose -p postgresql -f db/docker-compose.db.yml exec db bash -lc 'until pg_isready -U user -h localhost --quiet; do sleep 1; done; exec psql -h localhost -U user -d database'"; | ||
| scripts['predev'] = 'bun db:up'; | ||
| scripts['predb:psql'] = 'bun db:up'; | ||
| scripts['postdev'] = 'bun db:down'; | ||
| scripts['postdb:psql'] = 'bun db:down'; | ||
| } | ||
| const isLocal = !databaseHost || databaseHost === 'none'; | ||
|
|
||
| if ( | ||
| databaseEngine === 'cockroachdb' && | ||
| (!databaseHost || databaseHost === 'none') | ||
| isLocal && | ||
| databaseEngine !== undefined && | ||
| databaseEngine !== 'none' && | ||
| databaseEngine !== 'sqlite' && | ||
| databaseEngine !== 'mongodb' && | ||
| databaseEngine !== 'singlestore' && // TODO: Add support for singlestore | ||
| databaseEngine !== 'gel' // TODO: Add support for gel | ||
| ) { | ||
| scripts['db:up'] = | ||
| 'sh -c "docker info >/dev/null 2>&1 || sudo service docker start; docker compose -p cockroachdb -f db/docker-compose.db.yml up -d db"'; | ||
| scripts['db:down'] = | ||
| 'docker compose -p cockroachdb -f db/docker-compose.db.yml down'; | ||
| scripts['db:reset'] = | ||
| 'docker compose -p cockroachdb -f db/docker-compose.db.yml down -v'; | ||
| scripts['db:cockroach'] = | ||
| "docker compose -p cockroachdb -f db/docker-compose.db.yml exec db bash -lc 'cockroach sql --insecure --database=database'"; | ||
| scripts['predev'] = 'bun db:up'; | ||
| scripts['predb:cockroach'] = 'bun db:up'; | ||
| scripts['postdev'] = 'bun db:down'; | ||
| scripts['postdb:cockroach'] = 'bun db:down'; | ||
| } | ||
| const config = dbScripts[databaseEngine]; | ||
| const dockerPrefix = `docker compose -p ${databaseEngine} -f db/docker-compose.db.yml`; | ||
|
|
||
| if ((databaseEngine === 'mysql' || databaseEngine === 'mariadb') && orm === 'drizzle') { | ||
| dependencies['mysql2'] = resolveVersion('mysql2', '3.14.2'); | ||
| } | ||
| scripts['db:up'] = `${dockerPrefix} up -d db`; | ||
| scripts['postdb:up'] = | ||
| `${dockerPrefix} exec db bash -lc '${config.waitCmd}'`; | ||
| scripts['db:down'] = `${dockerPrefix} down`; | ||
| scripts['db:reset'] = `${dockerPrefix} down -v`; | ||
| scripts[`db:${databaseEngine}`] = | ||
| `${dockerPrefix} exec -it db bash -lc '${config.clientCmd}'`; | ||
|
|
||
| if ( | ||
| databaseEngine === 'mysql' && | ||
| (!databaseHost || databaseHost === 'none') | ||
| ) { | ||
| scripts['db:up'] = | ||
| 'sh -c "docker info >/dev/null 2>&1 || sudo service docker start; docker compose -p mysql -f db/docker-compose.db.yml up -d db"'; | ||
| scripts['db:down'] = | ||
| 'docker compose -p mysql -f db/docker-compose.db.yml down'; | ||
| scripts['db:reset'] = | ||
| 'docker compose -p mysql -f db/docker-compose.db.yml down -v'; | ||
| scripts['db:mysql'] = | ||
| "docker compose -p mysql -f db/docker-compose.db.yml exec -e MYSQL_PWD=rootpassword db bash -lc 'until mysqladmin ping -h127.0.0.1 --silent; do sleep 1; done; exec mysql -h127.0.0.1 -uroot'"; | ||
| scripts['predev'] = 'bun db:up'; | ||
| scripts['predb:mysql'] = 'bun db:up'; | ||
| scripts[`predb:${databaseEngine}`] = 'bun db:up'; | ||
| scripts['postdev'] = 'bun db:down'; | ||
| scripts['postdb:mysql'] = 'bun db:down'; | ||
| scripts[`postdb:${databaseEngine}`] = 'bun db:down'; | ||
| } | ||
|
|
||
| if ( | ||
| databaseEngine === 'mariadb' && | ||
| (!databaseHost || databaseHost === 'none') | ||
| isLocal && | ||
| (databaseEngine === 'mysql' || databaseEngine === 'mariadb') && | ||
| orm === 'drizzle' | ||
| ) { | ||
| scripts['db:up'] = | ||
| 'sh -c "docker info >/dev/null 2>&1 || sudo service docker start; docker compose -p mariadb -f db/docker-compose.db.yml up -d db"'; | ||
| scripts['db:down'] = | ||
| 'docker compose -p mariadb -f db/docker-compose.db.yml down'; | ||
| scripts['db:reset'] = | ||
| 'docker compose -p mariadb -f db/docker-compose.db.yml down -v'; | ||
| scripts['db:mariadb'] = | ||
| "docker compose -p mariadb -f db/docker-compose.db.yml exec -e MYSQL_PWD=rootpassword db bash -lc 'until mariadb-admin ping -h127.0.0.1 --silent; do sleep 1; done; exec mariadb -h127.0.0.1 -uroot'"; | ||
| scripts['predev'] = 'bun db:up'; | ||
| scripts['predb:mariadb'] = 'bun db:up'; | ||
| scripts['postdev'] = 'bun db:down'; | ||
| scripts['postdb:mariadb'] = 'bun db:down'; | ||
| dependencies['mysql2'] = resolveVersion('mysql2', '3.14.2'); | ||
| } | ||
|
|
||
| if ( | ||
| databaseEngine === 'mssql' && | ||
| (!databaseHost || databaseHost === 'none') | ||
| ) { | ||
| const { wait } = initTemplates.mssql; | ||
| if (isLocal && databaseEngine === 'mssql') { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if this does not us drizzle doesnt that mean we can use the bun version of mssql |
||
| dependencies['mssql'] = resolveVersion('mssql', '12.1.0'); | ||
| devDependencies['@types/mssql'] = resolveVersion('@types/mssql', '9.1.8'); | ||
| scripts['db:up'] = | ||
| `sh -c "docker info >/dev/null 2>&1 || sudo service docker start; docker compose -p mssql -f db/docker-compose.db.yml up -d db && docker compose -p mssql -f db/docker-compose.db.yml exec db bash -lc \\"${wait}\\""`; | ||
| scripts['db:down'] = | ||
| 'docker compose -p mssql -f db/docker-compose.db.yml down'; | ||
| scripts['db:reset'] = | ||
| 'docker compose -p mssql -f db/docker-compose.db.yml down -v'; | ||
| scripts['db:mssql'] = | ||
| `docker compose -p mssql -f db/docker-compose.db.yml exec db bash -lc '/opt/mssql-tools18/bin/sqlcmd -C -S localhost -U sa -P SApassword1'`; | ||
| scripts['predev'] = 'bun db:up'; | ||
| scripts['predb:mssql'] = 'bun db:up'; | ||
| scripts['postdev'] = 'bun db:down'; | ||
| scripts['postdb:mssql'] = 'bun db:down'; | ||
| devDependencies['@types/mssql'] = resolveVersion( | ||
| '@types/mssql', | ||
| '9.1.8' | ||
| ); | ||
| } | ||
|
|
||
| if ( | ||
| databaseEngine === 'sqlite' && | ||
| (!databaseHost || databaseHost === 'none') | ||
| ) { | ||
| if (isLocal && databaseEngine === 'sqlite') { | ||
| scripts['db:sqlite'] = 'sqlite3 db/database.sqlite'; | ||
| scripts['db:init'] = 'sqlite3 db/database.sqlite < db/init.sql'; | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.