-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Initial changes to download dab binary * feat: add "data-api-location" flag support for 'swa start' * feat: proxy requests made to "/data-api" to dab-cli * fix: unzipping of DAB-package and routing issue * nit: nit comments * feat: add support for "staticwebapp.db.config.json" and nit changes * chore: nit changes and handling staticwebapp.database.config.json * chore: nit comments * fix: temp url for data-api binary download fix: authentication headers in dab routing fix: swa start --help * feat: add separate executable based on the OS and nit changes * fix: ERR_STREAM_WRITE_AFTER_END error while writing stream * nit: resolve comments * fix: rename dab binary to new binary Name * fix: Linux permission issue for dab-executable * fix: fix naming while running dab command * nit: resolve comments * Added swa db init command (#6) * feat: add 'swa db init' command * added support for custom folder names, adjusted template for schema file, adjusted usage of cosmosdb params to fix bug * Using execFileCommand instead of execSync to prevent shell injection attacks in case of untrusted user input * merge: get latest changes * fix: added cosmosdb_postgresql and refactored * chore: nit comments * fix: more checks to handle corner cases * fix: add validDBcheck * fix: nit comments * Changed rest path in db init to /rest and fixed bug causing error to throw when valid database type is passed --------- Co-authored-by: sgollapudi77 <85578033+sgollapudi77@users.noreply.github.com> * updating register command changes for dab * build error * reverting few package-lock changes * move registerdb out to keep it consistent with others * refactor: moved dab init files to subfolder to accommodate dab add, dab update commands later * nit: addressing comments from older PR * fix: add support for downloading latest tag/specific version * fix: comment pinned version code * nit: triggering new build --------- Co-authored-by: sgollapudi77 <85578033+sgollapudi77@users.noreply.github.com> Co-authored-by: Sai Vamsi Krishna Gollapudi <sgollapudi@microsoft.com> Co-authored-by: Thomas Gauvin <35609369+thomasgauvin@users.noreply.github.com>
- Loading branch information
1 parent
bb57394
commit a77bd50
Showing
25 changed files
with
910 additions
and
159 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
export * from "./init"; |
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,2 @@ | ||
export * from "./init"; | ||
export { default as registerDb } from "./register"; |
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,126 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { | ||
DATA_API_BUILDER_BINARY_NAME, | ||
DATA_API_BUILDER_DATABASE_TYPES, | ||
DATA_API_BUILDER_DEFAULT_CONFIG_FILE_NAME, | ||
DATA_API_BUILDER_DEFAULT_FOLDER, | ||
DATA_API_BUILDER_DEFAULT_REST_PATH, | ||
DATA_API_BUILDER_DEFAULT_SCHEMA_FILE_NAME, | ||
DEFAULT_DATA_API_BUILDER_SCHEMA_CONTENT, | ||
} from "../../../../core/constants"; | ||
import { execFileCommand, logger } from "../../../../core"; | ||
import { getDataApiBuilderBinaryPath } from "../../../../core/dataApiBuilder"; | ||
|
||
export async function init(options: SWACLIConfig) { | ||
let { databaseType, connectionString, cosmosdb_nosqlContainer, cosmosdb_nosqlDatabase } = options; | ||
|
||
if (databaseType === undefined || !isValidDatabaseType(databaseType)) { | ||
logger.error( | ||
`--database-type is a required field. Please provide the type of the database you want to connect (mssql, postgresql, cosmosdb_nosql, mysql, cosmosdb_postgresql).`, | ||
true | ||
); | ||
return; | ||
} | ||
|
||
// 1. create folder swa-db-connections if it doesn't exist | ||
const folderName = options?.folderName ? options.folderName : DATA_API_BUILDER_DEFAULT_FOLDER; | ||
const directory = path.join(process.cwd(), folderName); | ||
|
||
if (!fs.existsSync(directory)) { | ||
logger.log(`Creating database connections configuration folder ${folderName}`, "swa"); | ||
fs.mkdirSync(directory); | ||
} else { | ||
logger.log(`Folder ${folderName} already exists, using that folder for creating data-api files`, "swa"); | ||
} | ||
|
||
// 2. create file staticwebapp.database.config.json by calling dab init and passing through options | ||
const configFile = path.join(directory, DATA_API_BUILDER_DEFAULT_CONFIG_FILE_NAME); | ||
|
||
if (fs.existsSync(configFile)) { | ||
logger.error(`Config file ${configFile} already exists. Please provide a different name or remove the existing config file.`, true); | ||
} | ||
|
||
logger.log(`Creating ${DATA_API_BUILDER_DEFAULT_CONFIG_FILE_NAME} configuration file`, "swa"); | ||
|
||
const dataApiBinary = await getDataApiBuilderBinaryPath(); | ||
if (!dataApiBinary) { | ||
logger.error( | ||
`Could not find or install ${DATA_API_BUILDER_BINARY_NAME} binary. | ||
If you already have data-api-builder installed, try running "dab init" directly to generate the config file. Exiting!!`, | ||
true | ||
); | ||
} | ||
|
||
let args: string[] = [ | ||
"init", | ||
"--database-type", | ||
databaseType, | ||
"--config", | ||
DATA_API_BUILDER_DEFAULT_CONFIG_FILE_NAME, | ||
"--rest.path", | ||
DATA_API_BUILDER_DEFAULT_REST_PATH, | ||
]; | ||
if (connectionString) { | ||
args = [...args, "--connection-string", connectionString]; | ||
} | ||
|
||
if (cosmosdb_nosqlContainer) { | ||
args = [...args, "--cosmosdb_nosql-container", cosmosdb_nosqlContainer]; | ||
|
||
if (databaseType != DATA_API_BUILDER_DATABASE_TYPES.CosmosDbNoSql) { | ||
logger.warn(`Database type is not ${DATA_API_BUILDER_DATABASE_TYPES.CosmosDbNoSql}, --cosmosdb_nosql-container will be ignored.`); | ||
} | ||
} | ||
|
||
if (cosmosdb_nosqlDatabase) { | ||
args = [...args, "--cosmosdb_nosql-database", cosmosdb_nosqlDatabase]; | ||
|
||
if (databaseType != DATA_API_BUILDER_DATABASE_TYPES.CosmosDbNoSql) { | ||
logger.warn(`Database type is not ${DATA_API_BUILDER_DATABASE_TYPES.CosmosDbNoSql}, --cosmosdb_nosql-database will be ignored.`); | ||
} | ||
} | ||
|
||
if (databaseType === DATA_API_BUILDER_DATABASE_TYPES.CosmosDbNoSql) { | ||
if (!cosmosdb_nosqlDatabase) { | ||
logger.error( | ||
`--cosmosdb_nosql-database is required when database-type is cosmosdb_nosql, ${DATA_API_BUILDER_DEFAULT_CONFIG_FILE_NAME} will not be created`, | ||
true | ||
); | ||
} | ||
// create file staticwebapp.database.schema.json directly if database type cosmosdb_nosql since needed argument | ||
const schemaFile = path.join(directory, DATA_API_BUILDER_DEFAULT_SCHEMA_FILE_NAME); | ||
|
||
if (fs.existsSync(schemaFile)) { | ||
logger.warn(`Schema file exists ${schemaFile}. This content will be replaced.`); | ||
} | ||
|
||
logger.info(`Creating ${DATA_API_BUILDER_DEFAULT_SCHEMA_FILE_NAME} schema file`, "swa"); | ||
try { | ||
fs.writeFileSync(schemaFile, DEFAULT_DATA_API_BUILDER_SCHEMA_CONTENT); | ||
} catch (ex) { | ||
logger.warn(`Unable to write/modify schema file. Exception : ${ex}`); | ||
} | ||
args = [...args, "--graphql-schema", DATA_API_BUILDER_DEFAULT_SCHEMA_FILE_NAME]; | ||
} | ||
|
||
// todo:DAB CLI doesn't return an error code when it fails, so we need to allow stdio to be inherited (this will be fixed in the March release) | ||
// It would be better to have our own logs since DAB CLI refers to itself in its success messages | ||
// which may lead to confusion for swa cli users ex: `SUGGESTION: Use 'dab add [entity-name] [options]' to add new entities in your config.` | ||
execFileCommand(dataApiBinary, directory, args); | ||
|
||
// not logging anything here since DAB CLI logs success messages or error messages and we can't catch an error | ||
} | ||
|
||
function isValidDatabaseType(databaseType: string): boolean { | ||
if ( | ||
databaseType == DATA_API_BUILDER_DATABASE_TYPES.CosmosDbNoSql || | ||
databaseType == DATA_API_BUILDER_DATABASE_TYPES.CosmosDbPostGreSql || | ||
databaseType == DATA_API_BUILDER_DATABASE_TYPES.MsSql || | ||
databaseType == DATA_API_BUILDER_DATABASE_TYPES.MySql || | ||
databaseType == DATA_API_BUILDER_DATABASE_TYPES.PostGreSql | ||
) { | ||
return true; | ||
} | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Command } from "commander"; | ||
import { configureOptions } from "../../../../core/utils"; | ||
import { init } from "./init"; | ||
import { DEFAULT_CONFIG } from "../../../../config"; | ||
|
||
export default function registerCommand(program: Command) { | ||
const dbCommand = program.command("db [command] [options]").description("Manage your database"); | ||
const dbInitCommand = new Command() | ||
.command("init") | ||
.usage("[options]") | ||
.description("initialize database connection configurations for your static web app") | ||
.requiredOption( | ||
"-t, --database-type <database type>", | ||
"(Required) The type of the database you want to connect (mssql, postgresql, cosmosdb_nosql, mysql, cosmosdb_postgresql)." | ||
) | ||
.option( | ||
"-f, --folder-name <folder name>", | ||
"A folder name to override the convention database connection configuration folder name (ensure that you update your CI/CD workflow files accordingly).", | ||
DEFAULT_CONFIG.folderName | ||
) | ||
.option("-cs, --connection-string <connection string>", "The connection string of the database you want to connect.") | ||
.option( | ||
"-nd, --cosmosdb_nosql-database <cosmosdb nosql database>", | ||
"The database of your cosmosdb account you want to connect (only needed if using cosmosdb_nosql database type)." | ||
) | ||
.option("-nc, --cosmosdb_nosql-container <cosmosdb nosql container>", "The container of your cosmosdb account you want to connect.") | ||
.action(async (_options: SWACLIConfig, command: Command) => { | ||
const options = await configureOptions(undefined, command.optsWithGlobals(), command, "db init", false); | ||
|
||
await init(options); | ||
}) | ||
.addHelpText( | ||
"after", | ||
` | ||
Examples: | ||
swa db init --database-type mssql --connection-string $YOUR_CONNECTION_STRING_ENV | ||
swa db init --database-type cosmosdb_nosql --cosmosdb_nosql-database myCosmosDB --connection-string $YOUR_CONNECTION_STRING_ENV | ||
` | ||
); | ||
|
||
dbCommand.addCommand(dbInitCommand).copyInheritedSettings; // For supporting dab init | ||
} |
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
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
Oops, something went wrong.