sebastianwessel / surrealdb-client-generator

Tool which generates a typescript client for SurrealDB and zod schema of a given database
MIT License
77 stars 12 forks source link

Incomplete generation #8

Closed zvictor closed 5 months ago

zvictor commented 5 months ago

Thanks for creating this lib!

I have just given it a try and I am not sure I am getting the results I was supposed to.

This is the command I am running:

surql-gen --schemaFile product.surql

product.surql:

-- Define the product table with a schema
DEFINE TABLE product SCHEMAFULL;

-- Define the fields for the product table
DEFINE FIELD title ON TABLE product TYPE string;
DEFINE FIELD description ON TABLE product TYPE string;
DEFINE FIELD price ON TABLE product TYPE object;
DEFINE FIELD image ON TABLE product TYPE string;
DEFINE FIELD vendors ON TABLE product TYPE array;

-- Define the schema for the Vendor array
DEFINE FIELD vendors.name ON TABLE product TYPE string;
DEFINE FIELD vendors.price ON TABLE product TYPE object;
DEFINE FIELD vendors.link ON TABLE product TYPE string;
DEFINE FIELD vendors.delivery ON TABLE product TYPE object;

-- Define the schema for the DeliveryInfo object
DEFINE FIELD vendors.delivery.message ON TABLE product TYPE string;
DEFINE FIELD vendors.delivery.price ON TABLE product TYPE object;

-- Define the schema for the Price object within Vendor and DeliveryInfo
DEFINE FIELD vendors.price.amount ON TABLE product TYPE number;
DEFINE FIELD vendors.price.currency ON TABLE product TYPE string;
DEFINE FIELD vendors.delivery.price.amount ON TABLE product TYPE number;
DEFINE FIELD vendors.delivery.price.currency ON TABLE product TYPE string;

-- Define the schema for the Price object
DEFINE FIELD price.amount ON TABLE product TYPE number;
  VALUE <future> {
    LET $lowest_vendor = (SELECT vendors[WHERE price.amount = math::min(vendors.*.price.amount)][0] FROM this);
    $lowest_vendor.price.amount;
  };

DEFINE FIELD price.currency ON TABLE product TYPE string;
  VALUE <future> {
    LET $lowest_vendor = (SELECT vendors[WHERE price.amount = math::min(vendors.*.price.amount)][0] FROM this);
    $lowest_vendor.price.currency;
  };

To my surprise:

  1. all the types generated are z.any.
  2. the client folder is empty
image image
sebastianwessel commented 5 months ago

Will have a look into it, but from first guess

There are ; between type definition and value, which might not create the schema you like to get.

Should be this:

-- Define the schema for the Price object
DEFINE FIELD price.amount ON TABLE product TYPE number
  VALUE <future> {
    LET $lowest_vendor = (SELECT vendors[WHERE price.amount = math::min(vendors.*.price.amount)][0] FROM this);
    $lowest_vendor.price.amount;
  };

DEFINE FIELD price.currency ON TABLE product TYPE string
  VALUE <future> {
    LET $lowest_vendor = (SELECT vendors[WHERE price.amount = math::min(vendors.*.price.amount)][0] FROM this);
    $lowest_vendor.price.currency;
  };

But I think there is a issue - at least some error handling is missing

zvictor commented 5 months ago

Thanks for the quick reply! I have tried the schema with the changes you wrote, but the output was the same.

Nonetheless, I can confirm that the fields in Surreal have been defined properly:

image
sebastianwessel commented 5 months ago

I'm working on the fix - hope to get it done today/tomorrow.

There is an issue during field type extraction, which is the root cause for the any()

I'm currently re-writing this functionality which should also bring some improvements

sebastianwessel commented 5 months ago

Version 2.1.0 is available.

Release-Notes:
https://github.com/sebastianwessel/surrealdb-client-generator/releases/tag/2.1.0

Several issues are fixed, and the generation should work in general.

If you like @zvictor , you can try it out. I did some small tests with your schema and it at least generated schemas, which are not only z.any(). Also, the generation of the client should now be fixed.

Sorry, for the bugs, but it is sometimes really hard to keep up to date and everything working

zvictor commented 5 months ago

Your commitment is impressive! I didn't expect my issue report to lead to such a considerable refactoring and in such a short time.

I'm super grateful for the attention you have put into it, but I wish you didn't apologize for the bug. Working with Open source can be very hard and ungrateful most of the time, so please don't let it consume the best of you ❤️

Nonetheless, thank you again for the work done. I'll test the release first thing tomorrow (It's 3am for me right now)

zvictor commented 5 months ago

It works very well! Thank you so much!

I will later create another issue with a minor improvement suggestion, but in the meantime I will keep trying further, looking for more points to share with you.

sebastianwessel commented 5 months ago

I'm happy to get some feedback!