Skip to content

Commit

Permalink
Feat/add cmd pull (#77)
Browse files Browse the repository at this point in the history
* add cmd

* update readme (Update Readme #28)

* rmv pull
  • Loading branch information
jycouet authored Dec 29, 2023
1 parent d027c16 commit 1b21af3
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-avocados-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"remult-cli": patch
---

add cmd "pull"
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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"
15 changes: 10 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,32 +36,37 @@ 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

- `--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

Alternatively, you can utilize a `.env` file to set configuration values. Example:

```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
Expand Down
97 changes: 54 additions & 43 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
},
Expand All @@ -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,
Expand All @@ -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();

Expand All @@ -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);
}
Expand Down

0 comments on commit 1b21af3

Please sign in to comment.