Continuing on from my Previous Posts, I’ve started a personal project to have a play with some things and ideas that I find interesting at the moment. These posts outline the decisions and challenges I stumble across along the way. It’s mostly just me thinking out loud but hopefully somebody will find my rambling interesting or useful…
After much hemming and hawing, tooing-and-froing over which DBMS to use and what overall approach to take to database interaction I have settled on using Oracle. In order to keep a clean separation between the database layer and the application, all communication will be through stored procedures – no direct table access. Hopefully this should minimise the dependencies on the data model itself, making refactoring much simpler should the need arise.
There is a fairly immediate downside to this approach, however. Since I won’t be accessing the tables directly I can’t really utilise Hibernate (or any ORM for that matter). Hibernate makes data retrieval and persistence ridiculously easy and mostly transparent to the developer. This can be a massive timesaver during the development phase but I tend to lean towards static code generation. Code generation gives similar benefits in terms of a reduction in dev effort – especially given the monotonous nature of wiring up your DAOs to your data model. The reasons I prefer code generation are number one, it’s faster at runtime – there’s no intermediate layers, no extra marshaling. Secondly, it’s easier to read/debug and finally, it’s easier to tweak by hand if needs be. If your model changes you can always regenerate anyway so I’m not too pushed on the need for a live mapping at runtime.
The first thing I did, after my eureka database selection moment was to hunt down a good tool to start putting together my data model and generate the DDL. Instantly I started to have flashbacks of Oracle’s delightful Designer and the hardship and misery that usually followed taking the fateful decision to click that seemingly innocent little icon. I may be placing a tad too much emotion and just a little superstition on what, after all, is just an inanimate piece of software. Maybe, but sweet merciful Jesus, the misery and heartache that bloody application caused me.
It’s been a good 2.5 years since I’ve had to even think about designing a database in Oracle so I’m glad to say that the situation is much improved. Oracle now offer a product called “Oracle SQL Developer Data Modeler” which so far, seems pretty solid. I’ve also used MySQL Workbench over the last few years and feel it deserves a mention as it too is a great little bit of kit.
Anyway, (late) last night, with my tools at the ready, I dived into creating my data model. It started off with easy questions, do I use the singular or plural form for my table names? Do I prefix my objects? Do I prefix my column names? After that the questions started to get a bit more interesting. Since I’m aiming for this to be a hosted solution, it’s going to have to deal with multiple users’ datasets. What’s the best way to segregate everyone’s data? I have a table in my model which lists the different “instances” of the application and some metadata around that instance, such as URL, status and what not. It’s fairly easy then to associate other records with an instance by having a foreign key on all the main entities – users, projects, customers to tie them back to a particular instance. There is a risk with this approach. If I make a mess of one of my queries and forget to filter to the current instance’s data only I could expose one customer’s data to another and I don’t think they’d be best pleased with “sorry it was 2am when I wrote that code” as an excuse. Another more laborious approach would be to physically separate each instance’s data and have a DB schema per instance. This would reduce the chance of a coding error leaking one person’s data to another. The maintenance of this design would be a great deal more complicated. Another problem with this approach is the permissions needed to create each schema. Ideally the schema setup would be automated, just a task that gets carried out in the database when saving a new instance’s details to the database. Suddenly you need your database user to have much greater privileges which creates more risk of divilment in the event the application/server/database/random staff member is compromised.
My main reason for starting this project was to play with new technologies and new approaches to application design but what I’ve ended up with is a boring old, seen it a million times before, not sexy, bog standard relational database in an old school, been around the block RDBMS – but it’s the right tool for the job.
My next step is to look into the architecture of the client. I’ve decided the client will be a HTML5 + JavaScript app that consumes JSON data from a REST API. There are a number of JavaScript frameworks out there aimed at developing rich apps in this fashion such as extJS, JavaScriptMVC and even frameworks like Cappuccino (a JavaScript implementation of Apple’s Cocoa frameworks). Now I just need to pick one…