From 1b21af3b3621d5508bc69c89eff9fe3e8fe092ad Mon Sep 17 00:00:00 2001 From: JYC Date: Fri, 29 Dec 2023 13:55:20 +0100 Subject: [PATCH] Feat/add cmd pull (#77) * add cmd * update readme (Update Readme #28) * rmv pull --- .changeset/spicy-avocados-march.md | 5 ++ .env.example | 9 +++ readme.md | 15 +++-- src/cli.ts | 97 +++++++++++++++++------------- 4 files changed, 78 insertions(+), 48 deletions(-) create mode 100644 .changeset/spicy-avocados-march.md create mode 100644 .env.example diff --git a/.changeset/spicy-avocados-march.md b/.changeset/spicy-avocados-march.md new file mode 100644 index 0000000..ca8a306 --- /dev/null +++ b/.changeset/spicy-avocados-march.md @@ -0,0 +1,5 @@ +--- +"remult-cli": patch +--- + +add cmd "pull" diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..3358315 --- /dev/null +++ b/.env.example @@ -0,0 +1,9 @@ +# DATABASE_URL = "postgres://postgres:example@127.0.0.1:5432/my-db" + +# TABLE_PROPS = "allowApiCrud: (r) => r?.authenticated() ?? false" +# WITH_ENUMS = "false" +# DEFAULT_ORDER_BY = "order,name,nom,username" +# CUSTOM_DECORATORS = '{"@Fields.string":"@KitFields.string#@kitql/remult","@Fields.dateOnly":"@KitFields.dateOnly#@kitql/remult"}' + +# SCHEMAS = "auth" +# EXCLUDE = "pg_stat_statements, pg_stat_statements_info, _prisma_migrations, _remult_migrations, auth_email_verification_token, auth_password_reset_token, auth_user, auth_user_key, auth_user_session" \ No newline at end of file diff --git a/readme.md b/readme.md index 7f89354..8511757 100644 --- a/readme.md +++ b/readme.md @@ -21,7 +21,7 @@ Inspired by `prisma db pull`, this tool streamlines the creation of Remult entit Generate Remult entities using the default settings: ```bash -npx remult-cli +npx remult-cli pull ``` ### Advanced Usage @@ -36,10 +36,10 @@ npx remult-cli --help ```bash # All entities from two schemas: -npx remult-cli --schemas auth public +npx remult-cli pull --schemas auth public # All entities from all schemas: -npx remult-cli --schemas '*' +npx remult-cli pull --schemas '*' ``` #### Options @@ -47,6 +47,9 @@ npx remult-cli --schemas '*' - `--connectionString`: Your PostgreSQL database connection string. Only PostgreSQL databases are supported. - `--tableProps`: Customize properties for generated tables (default: `allowApiCrud: true`). - `--output`: Define the output directory for the generated Remult entities (default: `./src/shared`). +- `--defaultOrderBy`: Will put a default order by if we see one of this column name. (default: `order,name`). +- `--customDecorators`: Will replace the default decorator by a custom one. (Should be a stringified JSON object). + Let's describe this example: `{"@Fields.string":"@KitFields.string#@kitql/remult"}`, here we replace the default `@Fields.string` decorator by `@KitFields.string` from the `@kitql/remult` import. ### Environmental Variables @@ -54,14 +57,16 @@ Alternatively, you can utilize a `.env` file to set configuration values. Exampl ```env DATABASE_URL=postgres://user:pass@host:port/db-name -OUTPUT=./fancy/place TABLE_PROPS=allowApiCrud: (r) => r?.authenticated() ?? false +OUTPUT=./fancy/place +DEFAULT_ORDER_BY = "order,name,nom,username" +CUSTOM_DECORATORS = '{"@Fields.string":"@KitFields.string#@kitql/remult","@Fields.dateOnly":"@KitFields.dateOnly#@kitql/remult"}' ``` After setting up your `.env` file, run the following command: ```bash -npx remult-cli +npx remult-cli pull ``` ## Development diff --git a/src/cli.ts b/src/cli.ts index f9e64ac..d87d9ce 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -63,8 +63,8 @@ You can use it to replace the default decorators by your own, extending Remult o ? process.env["SCHEMAS_PREFIX"] === "NEVER" ? "NEVER" : process.env["SCHEMAS_PREFIX"] === "ALWAYS" - ? "ALWAYS" - : "SMART" + ? "ALWAYS" + : "SMART" : "SMART", description: `You want to ALWAYS prefix with schema or NEVER?. By defaut, it's SMART, prefixing only when not public.`, }, @@ -85,6 +85,21 @@ You can use it to replace the default decorators by your own, extending Remult o } as const; async function main() { + p.intro("🎉 Welcome to remult-cli!"); + + const cmd = yargs(process.argv.slice(2)) + .scriptName("remult-cli") + .command("pull", "pull tables from the database and generate entities") + .demandCommand(1, "Please provide a command (pull for example!)") + .options(options) + .example([ + [ + "remult-cli pull --connectionString postgres://user:pass@host:port/db-name", + ], + ]); + + const parsed = await cmd.parse(); + const { output, tableProps, @@ -96,13 +111,7 @@ async function main() { exclude, include, ...args - } = await yargs(process.argv.slice(2)) - .options(options) - .example([ - ["remult-cli --connectionString postgres://user:pass@host:port/db-name"], - ]).argv; - - p.intro("🎉 Welcome to remult-cli!"); + } = parsed; args.connectionString ??= await getConnectionStringFromPrompt(); @@ -115,43 +124,45 @@ async function main() { } } - const spinner = p.spinner(); - spinner.start("Generating everything for you"); - - let provider: SqlDatabase | null = null; - try { - provider = await createPostgresDataProvider({ - connectionString: args.connectionString, - }); - } catch (error) { - throw new Error( - "Could not connect to the database, check your connectionString", - ); - } + if (parsed._[0] === "pull") { + const spinner = p.spinner(); + spinner.start("Generating everything for you"); + + let provider: SqlDatabase | null = null; + try { + provider = await createPostgresDataProvider({ + connectionString: args.connectionString, + }); + } catch (error) { + throw new Error( + "Could not connect to the database, check your connectionString", + ); + } - try { - const report = await getEntitiesTypescriptPostgres( - provider, - output, - tableProps, - defaultOrderBy, - customDecoratorsJSON, - withEnums, - schemas, - schemasPrefix, - exclude, - include, - ); - spinner.stop(`Generation done ${green("✓")}`); - - logReport("full", report); - } catch (error: unknown) { - if (error instanceof Error) { - pCancel(error.message); + try { + const report = await getEntitiesTypescriptPostgres( + provider, + output, + tableProps, + defaultOrderBy, + customDecoratorsJSON, + withEnums, + schemas, + schemasPrefix, + exclude, + include, + ); + spinner.stop(`Generation done ${green("✓")}`); + + logReport("full", report); + } catch (error: unknown) { + if (error instanceof Error) { + pCancel(error.message); + } } - } - p.outro(`🎉 Everything is ready!`); + p.outro(`🎉 Everything is ready!`); + } process.exit(0); }