Skip to content
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

Add option to load schemas from file in generate command #160

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

akmalmzamri
Copy link

@akmalmzamri akmalmzamri commented Mar 5, 2024

Background

This PR will allow users to load schemas from a JSON file when running generate command, hence skipping the manual data entry. This will make it easy to move from one project to another that has shared schemas

Sample usage

kirimase generate -f <path-to-schema.json>

Notable changes

  • Added Zod
  • Added new options -f, --from-file to generate command
  • Moved table column types definition from src/types.d.ts to src/utils.ts so it can be reused in schema validation
  • The buildSchema will process array of schemas instead of a single one. The generateResources will process the schema array one by one
  • The printGenerateNextSteps function now process schema array. The output is updated accordingly
    image

Input sample

Here's a sample of a valid schema.json (basically the shape of Schema)

[
  {
    "tableName": "products",
    "fields": [
      { "name": "name", "type": "varchar", "notNull": true },
      { "name": "price", "type": "number", "notNull": true }
    ],
    "index": "name",
    "belongsToUser": false,
    "includeTimestamps": true,
    "children": []
  }
]

Validation

This PR also comes with the necessary validation such as requiring the table name to be snakeCase. The validation is done by Zod. If errors are present in the schema, Kirimase will stop the execution and log the errors appropriately, such as

[
  {
    "message": "Field name must be in snake_case if more than one word.",
    "field": "fields.0.name",
    "tableName": "products.product_discounts.product_discount_analytics"
  },
  {
    "message": "Table name must be in snake_case if more than one word, and plural.",
    "field": "tableName",
    "tableName": "products.productReviews"
  }
]

Note on using Zod

Since strict mode is not enabled in this project, we will not be able to fully utilize Zod, such as type inferring

@akmalmzamri akmalmzamri changed the title Add generate option to load schemas from file Add option to load schemas from file in generate command Mar 5, 2024
@flipvh
Copy link

flipvh commented May 6, 2024

this is pretty great @akmalmzamri ! I am also interested in this for a project of mine, perhaps we can connect if you are also still interested in this? Can you send me a dm on twitter or an email flip[@]shareworks.nl? cheers Flip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants