Allain Lalonde
Allain Lalonde

Reputation: 93438

Adaptive Database

Are there any rapid Database protoyping tools that don't require me to declare a database schema, but rather create it based on the way I'm using my entities.

For example, assuming an empty database (pseudo code):

user1 = new User()  // Creates the user table with a single id column
user1.firstName = "Allain" // alters the table to have a firstName column as varchar(255)

user2 = new User()  // Reuses the table
user2.firstName = "Bob"
user2.lastName = "Loblaw"  // Alters the table to have a last name column

Since there are logical assumptions that can be made when dynamically creating the schema, and you could always override its choices by using your DB tools to tweak it later.

Also, you could generate your schema by unit testing it this way.

And obviously this is only for prototyping.

Is there anything like this out there?

Upvotes: 3

Views: 525

Answers (6)

Anthony Mastrean
Anthony Mastrean

Reputation: 22404

I agree with the NHibernate approach and auto-database-generation. But, if you want to avoid writing a configuration file, and stay close to the code, use Castle's ActiveRecord. You declare the 'schema' directly on the class with via attributes.

[ActiveRecord]
public class User : ActiveRecordBase<User>
{
     [PrimaryKey]
     public Int32 UserId { get; set; }

     [Property]
     public String FirstName { get; set; }
}

There are a variety of constraints you can apply (validation, bounds, etc) and you can declare relationships between different data model classes. Most of these options are parameters added to the attributes. It's rather simple.

So, you're working with code. Declaring usage in code. And when you're done, let ActiveRecord create the database.

ActiveRecordStarter.Initialize();
ActiveRecordStarter.CreateSchema();

Upvotes: 1

Rik
Rik

Reputation: 29243

You could use an object database.

Upvotes: 0

Nic Wise
Nic Wise

Reputation: 8129

Do you want the schema, but have it generated, or do you actually want NO schema?

For the former I'd go with nhibernate as @tom-carter said. Have it generate your schema for you, and you are all good (atleast until you roll your app out, then look at something like Tarantino and RedGate SQL Diff or whatever it's called to generate update scripts)

If you want the latter.... google app engine does this, as I've discovered this afternoon, and it's very nice. If you want to stick with code under your control, I'd suggest looking at CouchDB, tho it's a bit of upfront work getting it setup. But once you have it, it's a totally, 100% schema-free database. Well, you have an ID and a Version, but thats it - the rest is up to you. http://incubator.apache.org/couchdb/

But by the sounds of it (N)hibernate would suite the best, but I could be wrong.

Upvotes: 0

Ed.T
Ed.T

Reputation: 1705

Grails uses Hibernate to persist domain objects and produces behavior similar to what you describe. To alter the schema you simply modify the domain, in this simple case the file is named User.groovy.

class User {

    String userName
    String firstName
    String lastName
    Date dateCreated
    Date lastUpdated

    static constraints = {
        userName(blank: false, unique: true)
        firstName(blank: false)
        lastName(blank: false)
    }

    String toString() {"$lastName, $firstName"}

}

Saving the file alters the schema automatically. Likewise, if you are using scaffolding it is updated. The prototype process becomes run the application, view the page in your browser, modify the domain, refresh the browser, and see the changes.

Upvotes: 1

Eric Z Beard
Eric Z Beard

Reputation: 38424

Google's Application Engine works like this. When you download the toolkit you get a local copy of the database engine for testing.

Upvotes: 2

Tom Carter
Tom Carter

Reputation: 2976

May be not exactly responding to your general question, but if you used (N)Hibernate then you can automatically generate the database schema from your hbm mapping files.

Its not done directly from your code as you seem to be wanting but Hibernate Schema generation seems to work well for us

Upvotes: 0

Related Questions