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

New Project Proposal: Create a Type Safe SurrealQL Query Builder #28

Open
MatthewAry opened this issue Dec 14, 2023 · 4 comments
Open

Comments

@MatthewAry
Copy link

MatthewAry commented Dec 14, 2023

One of the things that is making me hesitate to adopt Surreal DB into my stack is a lack of meaningful TypeScript support. There are a variety of ways to go about making this happen, some more complicated then others. For example Prisma is able to introspect the db to generate a prisma schema file (or you can write the schema file yourself without introspection) and it will (among potentially other things) generate types based on your schema and incorporate them into the Prisma client. This type generation process only becomes helpful when used with the Prisma ORM and it seems that SurrealDB is not very interested in making an ORM right now (understandably!)

That's why I think we can look to what Knex.js is doing. It's a query builder/runner for SQL databases. However, Kinex.js is not suitable for SurrealDB because SurrealQL is very different from standard SQL (if anything it's a superset). Yet in theory, you could create a library that applies its own approach to query building. As you build queries using the proposed library, it will also ensure that the outputs produced when executing queries are properly typed. In other words, if you build and execute a query with this lib, what would be returned by it will have a proper type signature describing the shape and types of the object(s) you should expect to have returned.

Anyways, this lib should be separate from the JS, Node, Deno, and WASM connector drivers. Instead when instantiating the query builder/runner object for this lib, would require the instantiated driver object for it to be able to execute any queries you build with it.

I think that if a good enough library like the one proposed is created, the community could potentially build any ORM's that they might want on top of it. For example, Mikro ORM uses Knex.js underneath. I believe that if such a lib were created, that it would help to greatly accelerate adoption of SurrealDB from the JS/TS community.

Related Content:
See https://github.com/igalklebanov/kysely-surrealdb
See knex/knex#5955
See https://github.com/StarlaneStudios/cirql

Ideas:
The builder could use Zod (very popular) or Typebox (which has the best Perf) to help manage types.

@amaster507
Copy link

If I didn't have a thousand on-going projects I would think about taking this up. I wrote a TS query builder for AnnaDB and would be interested in discussing impending of this if someone wants to pick it up with me. I can't commit to the whole library by myself.

@clearfram3
Copy link

clearfram3 commented Dec 15, 2023

Drizzle [1] has to be mentioned. Awesome tool!
https://orm.drizzle.team/docs/overview/

@CanRau
Copy link

CanRau commented Dec 15, 2023

And drizzle-team/drizzle-orm#1310

@MatthewAry
Copy link
Author

Seems that they have a plan starting with this PR surrealdb/surrealdb#3166 I believe their plan is to use this to introspect and generate types. This will enable the creation of a lot of cool and powerful tools. We'll have to wait and see what emerges as a result.

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

No branches or pull requests

4 participants