DEV Community

Cover image for CRUD Generator for AI Agents
Thomas Hansen
Thomas Hansen

Posted on • Originally published at ainiro.io

CRUD Generator for AI Agents

Our CRUD generator can generate a CRUD API wrapping your SQL database in a couple of seconds. CRUD again is the axiom required to deal with data and translates into Create, Read, Update, and Delete. If you've got CRUD, you've arguably got everything required to "manage your data".

Connect Magic to your database, use the CRUD generator, and watch it produce thousands of lines of code for you automatically in a couple of seconds.

Meta Programming

Most people assumes our CRUD generator is based upon AI. And while we do have a lot of AI in our platform, the CRUD generator is using "meta programming". This implies it'll use 0.1% of the resources required by AI, and there will never be an AI hallucination in it. Below is a screenshot of how it looks like.

Magic Cloud's CRUD Generator

The CRUD generator allows you to create the following HTTP endpoints.

  • Create
  • Read (supports filtering, paging, and sorting)
  • Update
  • Delete
  • Count (matches Read endpoint on arguments)
  • Aggregate by some column, choosing column name and aggregate function
  • Distinct (returns unique values for some column)
  • Distinct count (count equivalent of above)
  • Keyword Density Search (works only for SQLite at the moment)

The above 9 HTTP endpoints will be generated correctly for every single table you've got in your database, as long as it's got a primary key and can be updated and deleted after creation. To give you an idea of its powers, realise the Chinook database has 11 tables. When we use the CRUD generator on it, the result is more than 6,000 lines of code. These are 6,000 lines of code you don't have to write manually, that our system generated 100% automatically for you.

The process is fairly simple to understand.

  1. Connect your database to Magic
  2. Magic retrieves the schema for your tables
  3. Magic generates HTTP API endpoints wrapping each table

The end result is 1,000x quality compared to AI generated code, to the point where it's barely possible for the end result to even have bugs - Assuming your database doesn't use "very weird types". Our CRUD generator supports the following databases.

  • MySQL
  • PostgreSQL
  • SQL Server
  • SQLite

And if you don't have a database from before, you can use SQL Studio from Magic to create one visually, or even use generative AI to have the AI suggest a database schema for you. Allowing you to literally create your entire backend from a couple of simple natural language based prompts.

SQL Studio allowing you to visually design your database

Higher Quality

Since the entire process is deterministic, this implies there's literally zero bugs in the resulting code, assuming Magic understands your database schema and can deal with it. The resulting code again is Hyperlambda, which again happens to be 5 to 10 times "simpler" than Python, something you can see below.

Python requiring 6.27 times as many tokens as Hyperlambda

This implies the resulting code becomes that much easier to maintain, while at the same time the thing is literally 5 to 7 times faster than Python, and significantly more scalable. You see, Python is slooooooow. Very, very slow. This has negative effects when creating server-side backend code such as APIs. Hyperlambda again, is almost on pair with C#, and in fact if you couple C# with Entity Framework, than Hyperlambda is actually faster.

Performance of C# with EF versus Hyperlambda

Using Python on my MacBook M3, I can execute a Hyperlambda HTTP endpoint reading items from my database 166 times in 60 seconds. The same code only runs 159 times if I use C# with EF.

Yes, our CRUD generator will literally create higher quality code than your senior C# software developer

If you compare token count between C# and Hyperlambda of course, it's basically a murder ...

C# versus Hyperlambda on token count

Interestingly, most C# developers will claim the above C# is easier to read than Hyperlambda. I'm not even sure how to respond to such claims. But using objective neutral parameters.

  • Hyperlambda is more beautiful than both Python and C# (yes, beauty can be quantified)
  • Hyperlambda is faster than both Python and C#
  • Hyperlambda is roughly 10 times easier to maintain than C# and 5 times easier than Python

And these are objective truths, extrapolated by data based upon math and observation. You're of course free to argue as much as you wish, but the above are objective truths based upon math and science!

Below is an example HTTP endpoint created by our CRUD generator if you're interested in seeing Hyperlambda.

// Read or list records from your Album table in your chinook database
.arguments

   // Number of records to return, defaults to 25 if not specified. Pass in -1 to return all records.
   limit:long

   // Offset into the dataset of where to start retrieving records
   offset:long

   // Fully qualified name of column to order by, must be in TABLE_NAME.COLUMN_NAME format
   order:string

   // What direction to order, can be 'asc' or 'desc' implying ascending and descending
   direction:string
   Album.AlbumId.eq:long
   Album.Title.like:string
   Album.Title.eq:string
   Album.ArtistId.eq:long

// Meta data description for endpoint
.description:Read or list records from your Album table in your chinook database

// Type of endpoint
.type:crud-read

// Verifying user is authorized to access endpoint.
auth.ticket.verify:root,admin

// Opening up our database connection.
data.connect:[generic|chinook]
   database-type:sqlite

   // Parametrising our read invocation with ordering arguments if specified.
   add:x:./*/data.read
      get-nodes:x:@.arguments/*/order
      get-nodes:x:@.arguments/*/direction
   remove-nodes:x:@.arguments/*/order
   remove-nodes:x:@.arguments/*/direction

   // Parametrising our read invocation with paging arguments if specified.
   add:x:./*/data.read
      get-nodes:x:@.arguments/*/limit
      get-nodes:x:@.arguments/*/offset
   remove-nodes:x:@.arguments/*/limit
   remove-nodes:x:@.arguments/*/offset

   // Parametrising our read invocation with filtering arguments.
   add:x:./*/data.read/*/where/*
      get-nodes:x:@.arguments/*

   // Reading data from database.
   data.read
      database-type:sqlite
      table:Album
      columns
         Album.AlbumId
         Album.Title
         Album.ArtistId
      where
         and

   // Returning result of above read invocation to caller.
   return-nodes:x:@data.read/*
Enter fullscreen mode Exit fullscreen mode

Adding CRUD support to your AI agent

Once you're done creating your CRUD API, you can export its OpenAPI specification and simply copy and paste it into a custom GPT for instance as an "action", at which point you'll have a custom GPT capable of dealing with your database. Or you can associate each "function" with your machine learning type, resulting in an AINIRO AI Agent. Below is a screenshot of how to accomplish the latter.

Associating each Hyperlambda function with your AI agent

The complete process becomes as follows;

  1. Connect Magic to your database
  2. Generate a CRUD API (one click)
  3. Add your CRUD API to your AI Agent (one click)
  4. Vectorise your type (one click)

So in total, you're looking at 3/4 button clicks, and the end result becomes an AI agent capable of dealing with all data in your database according to your natural language input instructions.

Wrapping up

So not only will you become 1,000 times more productive using our CRUD generator, but your end code will become more beautiful, more easily maintained, have better performance, and in general simply be "better" in all regards. And the process of creating software is literally reduced to clicking 3 simple buttons.

In addition, since we're storing such functions in our RAG/VSS database, this allows us to deliver AI agents with "infinite" tools. And if you're afraid of lockins, the whole platform is open source. You can find it below if you want to look at its code.

Top comments (0)