surrealdb / surrealdb.node

A Node.js engine for the SurrealDB JavaScript SDK
https://surrealdb.com
Apache License 2.0
61 stars 10 forks source link

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

Open MatthewAry opened 11 months ago

MatthewAry commented 11 months ago

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 https://github.com/knex/knex/issues/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 commented 11 months ago

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 commented 11 months ago

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

CanRau commented 11 months ago

And https://github.com/drizzle-team/drizzle-orm/issues/1310

MatthewAry commented 10 months ago

Seems that they have a plan starting with this PR https://github.com/surrealdb/surrealdb/pull/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.