-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add entity records CRUD commands #259
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
Open
ayal
wants to merge
10
commits into
main
Choose a base branch
from
feat/entity-records-crud
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3bd687f
feat: add entity records CRUD commands
ayal a5c6b92
fix: resolve CI failures (lint, knip, test capture)
ayal 787e5f0
docs: update README with missing commands and accurate descriptions
ayal f675b83
Merge branch 'main' into feat/entity-records-crud
kfirstri 0a32ce9
small typecheck fixes
kfirstri 9b24161
address review: looseObject schema, shared parseRecordData util, clac…
github-actions[bot] 8629bcc
make exampleHint optional in parseRecordData, only use in error hints…
github-actions[bot] 35fa023
refactor: use token exchange instead of admin entities router
a17b8fd
Merge remote-tracking branch 'origin/main' into feat/entity-records-crud
74fdc4e
fix: biome formatting in index.ts and Base44APIMock.ts
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
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { Command } from "commander"; | ||
| import type { CLIContext } from "@/cli/types.js"; | ||
| import { getEntitiesPushCommand } from "./push.js"; | ||
| import { getRecordsCommand } from "./records/index.js"; | ||
|
|
||
| export function getEntitiesCommand(context: CLIContext): Command { | ||
| return new Command("entities") | ||
| .description("Manage project entities") | ||
| .addCommand(getEntitiesPushCommand(context)) | ||
| .addCommand(getRecordsCommand(context)); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { log } from "@clack/prompts"; | ||
| import { Command } from "commander"; | ||
| import type { CLIContext } from "@/cli/types.js"; | ||
| import { runCommand, runTask } from "@/cli/utils/index.js"; | ||
| import type { RunCommandResult } from "@/cli/utils/runCommand.js"; | ||
| import { createRecord } from "@/core/resources/entity/index.js"; | ||
| import { parseRecordData } from "./parseRecordData.js"; | ||
|
|
||
| interface CreateRecordCommandOptions { | ||
| data?: string; | ||
| file?: string; | ||
| } | ||
|
|
||
| async function createRecordAction( | ||
| entityName: string, | ||
| options: CreateRecordCommandOptions, | ||
| ): Promise<RunCommandResult> { | ||
| const data = await parseRecordData( | ||
| options, | ||
| '{"name": "John", "email": "john@example.com"}', | ||
| ); | ||
|
|
||
| const record = await runTask( | ||
| `Creating ${entityName} record...`, | ||
| async () => { | ||
| return await createRecord(entityName, data); | ||
| }, | ||
| { | ||
| successMessage: `Created ${entityName} record`, | ||
| errorMessage: `Failed to create ${entityName} record`, | ||
| }, | ||
| ); | ||
|
|
||
| log.info(JSON.stringify(record, null, 2)); | ||
|
|
||
| return { outroMessage: `Record created with ID: ${record.id}` }; | ||
| } | ||
|
|
||
| export function getRecordsCreateCommand(context: CLIContext): Command { | ||
| return new Command("create") | ||
| .description("Create a new entity record") | ||
| .argument("<entity-name>", "Name of the entity (e.g. Users, Products)") | ||
| .option("-d, --data <json>", "JSON object with record data") | ||
| .option("--file <path>", "Read record data from a JSON/JSONC file") | ||
| .action(async (entityName: string, options: CreateRecordCommandOptions) => { | ||
| await runCommand( | ||
| () => createRecordAction(entityName, options), | ||
| { requireAuth: true }, | ||
| context, | ||
| ); | ||
| }); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { confirm } from "@clack/prompts"; | ||
| import { Command } from "commander"; | ||
| import { CLIExitError } from "@/cli/errors.js"; | ||
| import type { CLIContext } from "@/cli/types.js"; | ||
| import { runCommand, runTask } from "@/cli/utils/index.js"; | ||
| import type { RunCommandResult } from "@/cli/utils/runCommand.js"; | ||
| import { deleteRecord } from "@/core/resources/entity/index.js"; | ||
|
|
||
| interface DeleteRecordCommandOptions { | ||
| yes?: boolean; | ||
| } | ||
|
|
||
| async function deleteRecordAction( | ||
| entityName: string, | ||
| recordId: string, | ||
| options: DeleteRecordCommandOptions, | ||
| ): Promise<RunCommandResult> { | ||
| if (!options.yes) { | ||
| const confirmed = await confirm({ | ||
| message: `Delete ${entityName} record ${recordId}?`, | ||
| }); | ||
|
|
||
| if (confirmed !== true) { | ||
| throw new CLIExitError(0); | ||
| } | ||
| } | ||
|
|
||
| await runTask( | ||
| `Deleting ${entityName} record...`, | ||
| async () => { | ||
| return await deleteRecord(entityName, recordId); | ||
| }, | ||
| { | ||
| successMessage: `Deleted ${entityName} record`, | ||
| errorMessage: `Failed to delete ${entityName} record`, | ||
| }, | ||
| ); | ||
|
|
||
| return { outroMessage: `Record ${recordId} deleted` }; | ||
| } | ||
|
|
||
| export function getRecordsDeleteCommand(context: CLIContext): Command { | ||
| return new Command("delete") | ||
| .description("Delete an entity record") | ||
| .argument("<entity-name>", "Name of the entity (e.g. Users, Products)") | ||
| .argument("<record-id>", "ID of the record to delete") | ||
| .option("-y, --yes", "Skip confirmation prompt") | ||
| .action( | ||
| async ( | ||
| entityName: string, | ||
| recordId: string, | ||
| options: DeleteRecordCommandOptions, | ||
| ) => { | ||
| await runCommand( | ||
| () => deleteRecordAction(entityName, recordId, options), | ||
| { requireAuth: true }, | ||
| context, | ||
| ); | ||
| }, | ||
| ); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { log } from "@clack/prompts"; | ||
| import { Command } from "commander"; | ||
| import type { CLIContext } from "@/cli/types.js"; | ||
| import { runCommand, runTask } from "@/cli/utils/index.js"; | ||
| import type { RunCommandResult } from "@/cli/utils/runCommand.js"; | ||
| import { getRecord } from "@/core/resources/entity/index.js"; | ||
|
|
||
| async function getRecordAction( | ||
| entityName: string, | ||
| recordId: string, | ||
| ): Promise<RunCommandResult> { | ||
| const record = await runTask( | ||
| `Fetching ${entityName} record...`, | ||
| async () => { | ||
| return await getRecord(entityName, recordId); | ||
| }, | ||
| { | ||
| successMessage: `Fetched ${entityName} record`, | ||
| errorMessage: `Failed to fetch ${entityName} record`, | ||
| }, | ||
| ); | ||
|
|
||
| log.info(JSON.stringify(record, null, 2)); | ||
|
|
||
| return {}; | ||
| } | ||
|
|
||
| export function getRecordsGetCommand(context: CLIContext): Command { | ||
| return new Command("get") | ||
| .description("Get a single entity record by ID") | ||
| .argument("<entity-name>", "Name of the entity (e.g. Users, Products)") | ||
| .argument("<record-id>", "ID of the record") | ||
| .action(async (entityName: string, recordId: string) => { | ||
| await runCommand( | ||
| () => getRecordAction(entityName, recordId), | ||
| { requireAuth: true }, | ||
| context, | ||
| ); | ||
| }); | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { Command } from "commander"; | ||
| import type { CLIContext } from "@/cli/types.js"; | ||
| import { getRecordsCreateCommand } from "./create.js"; | ||
| import { getRecordsDeleteCommand } from "./delete.js"; | ||
| import { getRecordsGetCommand } from "./get.js"; | ||
| import { getRecordsListCommand } from "./list.js"; | ||
| import { getRecordsUpdateCommand } from "./update.js"; | ||
|
|
||
| export function getRecordsCommand(context: CLIContext): Command { | ||
| return new Command("records") | ||
| .description("CRUD operations on entity records") | ||
| .addCommand(getRecordsListCommand(context)) | ||
| .addCommand(getRecordsGetCommand(context)) | ||
| .addCommand(getRecordsCreateCommand(context)) | ||
| .addCommand(getRecordsUpdateCommand(context)) | ||
| .addCommand(getRecordsDeleteCommand(context)); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { log } from "@clack/prompts"; | ||
| import { Command } from "commander"; | ||
| import type { CLIContext } from "@/cli/types.js"; | ||
| import { runCommand, runTask } from "@/cli/utils/index.js"; | ||
| import type { RunCommandResult } from "@/cli/utils/runCommand.js"; | ||
| import { listRecords } from "@/core/resources/entity/index.js"; | ||
|
|
||
| interface ListRecordsCommandOptions { | ||
| filter?: string; | ||
| sort?: string; | ||
| limit?: string; | ||
| skip?: string; | ||
| fields?: string; | ||
| } | ||
|
|
||
| async function listRecordsAction( | ||
| entityName: string, | ||
| options: ListRecordsCommandOptions, | ||
| ): Promise<RunCommandResult> { | ||
| const records = await runTask( | ||
| `Fetching ${entityName} records...`, | ||
| async () => { | ||
| return await listRecords(entityName, { | ||
| filter: options.filter, | ||
| sort: options.sort, | ||
| limit: options.limit ? Number(options.limit) : 50, | ||
| skip: options.skip ? Number(options.skip) : undefined, | ||
| fields: options.fields, | ||
| }); | ||
| }, | ||
| { | ||
| successMessage: `Fetched ${entityName} records`, | ||
| errorMessage: `Failed to fetch ${entityName} records`, | ||
| }, | ||
| ); | ||
|
|
||
| log.info(JSON.stringify(records, null, 2)); | ||
|
|
||
| return { outroMessage: `Found ${records.length} ${entityName} record(s)` }; | ||
| } | ||
|
|
||
| export function getRecordsListCommand(context: CLIContext): Command { | ||
| return new Command("list") | ||
| .description("List entity records") | ||
| .argument("<entity-name>", "Name of the entity (e.g. Users, Products)") | ||
| .option( | ||
| "-f, --filter <json>", | ||
| 'JSON filter object (e.g. \'{"status":"active"}\' or \'{"age":{"$gt":18}}\')', | ||
| ) | ||
| .option( | ||
| "-s, --sort <field>", | ||
| "Sort field name, prefix with - for descending (e.g. -created_date)", | ||
| ) | ||
| .option("-l, --limit <n>", "Max number of records to return", "50") | ||
| .option("--skip <n>", "Number of records to skip (for pagination)") | ||
| .option( | ||
| "--fields <fields>", | ||
| "Comma-separated list of fields to return (e.g. id,name,email)", | ||
| ) | ||
| .action(async (entityName: string, options: ListRecordsCommandOptions) => { | ||
| await runCommand( | ||
| () => listRecordsAction(entityName, options), | ||
| { requireAuth: true }, | ||
| context, | ||
| ); | ||
| }); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import JSON5 from "json5"; | ||
| import { InvalidInputError } from "@/core/errors.js"; | ||
| import { readJsonFile } from "@/core/utils/fs.js"; | ||
|
|
||
| interface RecordDataOptions { | ||
| data?: string; | ||
| file?: string; | ||
| } | ||
|
|
||
| export async function parseRecordData( | ||
| options: RecordDataOptions, | ||
| exampleHint?: string, | ||
| ): Promise<Record<string, unknown>> { | ||
| if (options.data && options.file) { | ||
| throw new InvalidInputError( | ||
| "Cannot use both --data and --file. Choose one.", | ||
| { | ||
| hints: [ | ||
| { | ||
| message: `Pass --data for inline JSON or --file for a JSON file path, not both.`, | ||
| }, | ||
| ], | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| if (options.data) { | ||
| try { | ||
| return JSON5.parse(options.data); | ||
| } catch { | ||
| throw new InvalidInputError( | ||
| "Invalid JSON in --data flag. Provide valid JSON.", | ||
| exampleHint | ||
| ? { hints: [{ message: `Example: --data '${exampleHint}'` }] } | ||
| : undefined, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (options.file) { | ||
| return readJsonFile(options.file) as Promise<Record<string, unknown>>; | ||
| } | ||
|
|
||
| throw new InvalidInputError( | ||
| "Provide record data with --data or --file flag", | ||
| exampleHint | ||
| ? { | ||
| hints: [ | ||
| { | ||
| message: `Example: --data '${exampleHint}' or --file record.json`, | ||
| }, | ||
| ], | ||
| } | ||
| : undefined, | ||
| ); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { log } from "@clack/prompts"; | ||
| import { Command } from "commander"; | ||
| import type { CLIContext } from "@/cli/types.js"; | ||
| import { runCommand, runTask } from "@/cli/utils/index.js"; | ||
| import type { RunCommandResult } from "@/cli/utils/runCommand.js"; | ||
| import { updateRecord } from "@/core/resources/entity/index.js"; | ||
| import { parseRecordData } from "./parseRecordData.js"; | ||
|
|
||
| interface UpdateRecordCommandOptions { | ||
| data?: string; | ||
| file?: string; | ||
| } | ||
|
|
||
| async function updateRecordAction( | ||
| entityName: string, | ||
| recordId: string, | ||
| options: UpdateRecordCommandOptions, | ||
| ): Promise<RunCommandResult> { | ||
| const data = await parseRecordData(options, '{"status": "active"}'); | ||
|
|
||
| const record = await runTask( | ||
| `Updating ${entityName} record...`, | ||
| async () => { | ||
| return await updateRecord(entityName, recordId, data); | ||
| }, | ||
| { | ||
| successMessage: `Updated ${entityName} record`, | ||
| errorMessage: `Failed to update ${entityName} record`, | ||
| }, | ||
| ); | ||
|
|
||
| log.info(JSON.stringify(record, null, 2)); | ||
|
|
||
| return { outroMessage: `Record ${recordId} updated` }; | ||
| } | ||
|
|
||
| export function getRecordsUpdateCommand(context: CLIContext): Command { | ||
| return new Command("update") | ||
| .description("Update an entity record") | ||
| .argument("<entity-name>", "Name of the entity (e.g. Users, Products)") | ||
| .argument("<record-id>", "ID of the record to update") | ||
| .option("-d, --data <json>", "JSON object with fields to update") | ||
| .option("--file <path>", "Read update data from a JSON/JSONC file") | ||
| .action( | ||
| async ( | ||
| entityName: string, | ||
| recordId: string, | ||
| options: UpdateRecordCommandOptions, | ||
| ) => { | ||
| await runCommand( | ||
| () => updateRecordAction(entityName, recordId, options), | ||
| { requireAuth: true }, | ||
| context, | ||
| ); | ||
| }, | ||
| ); | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want any validation for this thing? I guess the backend will throw if entity does not exists