[dojo-contributors] dojo.data Schema API

Brian Douglas Skinner skinner at dojotoolkit.org
Tue Aug 21 15:52:56 EDT 2007


In some recent dojo.data IRC meetings, Jared and Alex and I started 
talking about what we might want the Schema API to look like.

The main purpose of the Schema API is just to provide metadata about all 
the attributes that different kinds of items can have.  For example, in 
a bookstore database, you might have different kinds of items for 
representing Customers and Books.  The Customer items would have 
attributes like Name, Phone Number, and Billing Address.  The Book items 
would have attributes like Title, Author, and ISBN.

We'd like to get feedback from more people about what the Schema API 
should look like.  We haven't yet talked about many of the details, but 
here's a rough approximation of the sort of approach we've been talking 
about...

The Schema API might have just two methods:

   store.getSchema();
   store.getKindOfItem(item);  // or maybe just getKind(item)

Calling store.getSchema() would return a JSON-like object structure with 
all the schema info for the entire datastore.

For example, say you have a datastore that's connected to a relational 
database, and the database has two tables that look like these:

   TABLE Country:
             ABBR  NAME
             ----  -------
             UKR   Ukraine
             USA   United States

   TABLE State:
             NAME  CODE  COUNTRY  POPULATION
             ----  ----  -------  ----------
             Utah  UT    USA       2,233,169
             Ohio  OH    USA      11,353,140

If you call getSchema() like this:

    var schema = store.getSchema();

Then the schema object you get back would look something like this:

     schema == {
         kinds: {
             Country: {
                 attributes: {
                     ABBR: {type: 'String'},
                     NAME: {type: 'String'}
                 }
             },
             State: {
                 attributes: {
                     NAME: {type: 'String'},
                     CODE: {type: 'String'},
                     COUNTRY: {kind: 'Country'},
                     POPULATION: {type: 'Number'}
                 }
             }
         }
     }

Or it could perhaps have additional metadata:

     schema == {
         kinds: {
             Country: {
                 label: ['NAME'],
                 identifier: ['ABBR'],
                 attributes: {
                     ABBR: {
                         label: 'Abbreviation',
                         summary: 'An ISO 3166-1 country code',
                         type: 'String',
                         sourceType: 'char(3)'
                     },
                     NAME: {
                         label: 'Country Name',
                         type: 'String',
                         sourceType: 'varchar(255)'
                     }
                 }
             },
             State: {
                 label: ['NAME'],
                 identifier: ['CODE'],
                 attributes: {
                     NAME: {
                         label: 'State Name',
                         type: 'String',
                         sourceType: 'varchar(255)'
                     },
                     CODE: {
                         label: 'Postal Code',
                         type:'String',
                         sourceType: 'char(2)'
                     },
                     COUNTRY: {
                         summary: 'Country this State is part of',
                         kind: 'Country'
                     },
                     POPULATION: {
                         label: 'Population',
                         type: 'Number',
                         sourceType: 'int'
                     }
                 }
             }
         }
     }





More information about the dojo-contributors mailing list