Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How I created AI-powered ORM for PostgreSQL, MySQL and SQLite and why you shouldn't use it

Posted on Oct 13 tl;drI created AI-powered ORM for Node with TypeScript or JavaScript called ormgpt. It works, it's silly and please don't use it.In the last few years number of new ORMs (object relation mappers) and Query builders has grown like crazy. A few years ago, the golden standard was either an ORM like Sequelize or a Query Builder like Knex. Since then we got TypeORM, Bookshelf, Objection, Mikro-ORM, Prisma, Drizzle, kysely and many, many more. While I agree that more option is good since anyone can choose the best-suited solution for their needs, it also creates many copy-alike libs.At this point, I think ORMs have become the new days since the last javascript frameworks but for the backend.Another hot topic, wider than just the javascript ecosystem is AI. Entire group of algorithms to recognize patterns, predict output and generate things. Now tech startup not only must store data in the hot blockchain, NoSQL or vector database, but compute on edge computing using quantum technology. Must be also AI - artificially intelligent. My thought was, what if I create a hot, new lib to access data like ORMs or query builders but using AI? So anyone can access data using plain language like:or even in other languages like for example Germanso I messed a little with OpenAI API to call with prompts likebut that was too general. I tried thenstill, often it was returning with invalid queries or additional comments. So I went even stricter passing also dialect, entire db schema and asking to not write any other response than a query.And that worked quite well. So the next part was to prepare methods to call OpenAI programmatically and adapters for database engines. Method calling OpenAI was pretty simple and using built-in fetch:I know OpenAI has also SDK lib but I prefer simple calls instead of another dependency since it's hard to manage them in the long term. API allows direct access to the resource, SDK package would have to be updated separately and eventually can be abandoned.For the database engine, I choose to support Postgres, MySQL and SQLite out of the box. They are the most popular and I worked with all of them before with success. The first was SQLite which allowed me to experiment with different interfaces of adapter. With such an interface, anyone can create their own adapter for other engines like Oracle, ClickHouse, CouchDB etc. I decided to stick with the smallest possible set of methods in the interface, leaving other responsibilities than executing queries to native clients:Then I created silly adapters:Now it's possible to run.For example for request to SQLite database with simple schema of users, posts, comments and likes:generated query:and after execution response from the database:It's kind of hard to test such app because it's non-deterministic. The only way I thought about is to test short, precise statements like "create x with y and z" and then look up db if it's there. Here we come to the conclusion, of why this lib is useless for now. Even if OpenAI is really good at writing performant queries, it's hard to get the same query over and over. And if you look for something more complex like joins, nested subqueries or engine-related queries with the current state of GPT is not possible. You can minimize randomness by being very strict about the requirements in your statement.Anyway, as an experimental project, I decided to finish it. So the last part was to allow fine-tuning model parameters:and prepare Postgres and MySQL adapters. The last part was to publish lib. The name ormGPT comes from ORM + GPT model but in fact it's neither orm nor query builder. The proper ORM should "map" the database into objects. Then maybe it's "intelligent" query builder? Also no. Query builder usually allows you to chain query object before generating sql. You can chain plain string, but is that enough? Maybe it should be chatGPTtoQueryFacade.js? Too much thinking, not enough willingness. Published as ormGPT. That's it. Tiny afternoon project, you shouldn't use in your production application. Or maybe you should? At the end you can tell your clients, you are using cutting-edge technologies and advanced AI.You can find the lib on the NPM: https://www.npmjs.com/package/ormgptor at the GitHub repository:An ORM based on OpenAI that translates plain human language into SQL queries and executes them on a database.Currently supports database dialects: MySQL, PostgreSQL, and SQLite.Supported languages: English, German, French, Spanish, Polish, Italian, Dutch, Portuguese, Ukrainian, Arabic, Chinese, Japanese, Korean, Turkish and many more.Generated query:Response:Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Jakub Andrzejewski - Oct 9 Hasan Elsherbiny - Oct 2 Yeom suyun - Oct 6 Matheus Chaves - Oct 10 Once suspended, meatboy will not be able to comment or publish posts until their suspension is removed. Once unsuspended, meatboy will be able to comment and publish posts again. Once unpublished, all posts by meatboy will become hidden and only accessible to themselves. If meatboy is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Meat Boy. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag meatboy: meatboy consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging meatboy will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

How I created AI-powered ORM for PostgreSQL, MySQL and SQLite and why you shouldn't use it

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×