Skip to main content

Automatic ORM Designing

I believe that all programmers worth their salt, know of the famous quote that Larry Wall published, the three virtues of a programmer is:
  1. Laziness
  2. Impatience, &
  3. Hubris
With this in mind, I would like to share with you a database class I have drafted over the recent years.  To give you some context, the reason I wrote this originally was because, a) I am too lazy to put the entire database schema in to a framework's model, and b) I was tasked to put together an enterprise system which many database systems with enormous models had to interact with each other.

First, what is an ORM?

Object-Relational Mapping; is a technique to wrap your database queries (CRUD) from your object-orientated frameworks. I'm going to go in to a fair bit of detail about the one I have written.

The Premise

Database schemas are written in to the tables, the same way as you store anything else.  Therefore every way a table is structured can be queried and extrapolated.  Typical ORM's require you to write out the whole schema in code - and I began to ask myself why.  Laravel Database Migrations is good, but I think you'll agree this concept is interesting, that it will automatically discover and validate tables, their fields along their datatypes, what other table/fields they link to and even if they are required or not.

Now, I've started this with small to medium sized projects, and I've lacked applying recent updates and to remove the way it was attached to every query, meaning it was always re-discovering on every query that was made, but with a few tips, I'll demonstrate here now, how we can do this process once, and cache the schema's information to a JSON definition file.

 The Basics

I'm going to assume a few things, one is that you have some type of dotenv setup to describe your database type, user, password for its connectivity.  I have my class set up so depending on the type specified in the configuration, I switch these sets of queries, but for the sake of this article, I'm going to base them on MySQL.  Here are some of the basic queries: So as you can see, the first snippet will get a list of all of the table names, the second can be use to get the field definitions for a given table, and finally the third shows how you can list the foreign fields for a given table:field.

Implementing as Middlewear

The way this should come together, is on the consturct of your database class,  it should see if the schema file (e.g. schema.json) exists or not.  If not, let's go in to the creation step:
This is a small example, but it should be fairly simple to see what is going on.  The three object functions being called assume you have used the three queries above to take in their respective arguments and to return the result as an multi-dimensional array.

Conslusion

So if you are like me who is simply too lazy, let the system make it for you.  If you ever change your schema, all you will need to do in order to update your framework is just delete the JSON schema file.  If there is enough demand, I can take you through using the schema it creates in to injesting it to run your queries and to build and validate your forms.

Cheers,
Jesse.

Comments

Popular posts from this blog

question2answer Wordpress Integration

 Today I want to journal my implementation of a WordPress site with the package of "question2answer".  It comes as self-promoted as being able to integrate with WordPress "out of the box".  I'm going to vent a small amount of frustration here, because the only integration going on is the simplicity of configuration with using the same database, along with the user authentication of WordPress.  Otherwise they run as two separate sites/themes. This will not do. So let's get to some context.  I have a new hobby project in mind which requires a open source stack-overflow clone.  Enter question2answer .  Now I don't want to come across as completely ungrateful, this package - while old, ticks all the boxes and looks like it was well maintained, but I need every  page to look the same to have a seamless integration.  So, let's go through this step by step. Forum Index Update This step probably  doesn't need to be done, but I just wanted to make sure th

Machine Learning: Teaching Wisdom of the Crowd

I got lost in an absolute myriad of thoughts the other day, and it essentially wound up wondering if we can teach machines to count, beyond of what it can see in an image, and I've come up with a small experiment that I would absolutely love to collaborate on if anyone (@ Google ?) else is interested. The idea is based on  the concept of the experiments performed using " Wisdom of the Crowd ", commonly in this experiment to use a jar of jelly beans and asking many people to make a guess as to how many is in there.  Machine learning can be used to make predictions from patterns, but it would have nothing to gain looking at one picture of a jelly bean jar to the next and being able to correctly identify that is in fact - a jar of jelly beans. But suppose we feed it several images of jars of jelly beans, along with all of the guesses people have made of how many is in there.  Can we then presume that feeding it a new image, it would be able to give us a fairly accurate c

WooCommerce: Controlling an Asset CDN

Continuing on from my last post , I faced a new issue when it came to adding products and the associated images I was putting in (from Cloudinary ) was getting uploaded to the WordPress media library. Not only that, using the URL from my site instead of the CDN it had come from. Double up on all of my images, what a waste - and I want to host from the CDN to keep costs of bandwidth down.  So let me show you how I overcame it. Separating the herd What was interesting, is that it was keeping a record of the original source location, and I found I could filter these apart from the rest of my media library: With this in mind, I wrote a function around it so I could use it to give me a true/false if the given attachment was from this source. Attaching the hook Next, needed a way that as soon as an image was added, that it would update the attachment (post) pointing to the correct reference, and not to the file on our server. I found the add_attachment hook, which fires only whe