Native database schema

IndexedDB is a NoSQL database and requires no schema to pre-define. However to support query, object store must be indexed before querying. This is in fact, similar to defining query-able feed or COLUMN in SQL TABLE. For portability between IndexedDB and WebSQL, we define database schema are in JSON. Additionally the schema can be used to define relationship between stores, of which we discuss in other sections.

Database schema

Database schema has the following fields:

database_schema = {
    version: 1, // optional, but recommended.
    size: 5 * 1024 * 1024, // 5 MB, optional estimated database size in byte
    stores: [store_schema] // list of store schema as array
}

Notice that the JSON format is minimal as defined in IndexedDB specification for opening database and creating object store. WebSQL also similarly defined.

Store schema

Store schema is defined as:

store_schema = {
    name: 'store name',
    keyPath: 'id', // optional, can also be dotted path like 'entry.id.$t'
    autoIncrement: true, // optional. use when keyPath is not specified.
    indexes: [index_schema] // optional, list of index schema as array.
}

If keyPath is not defined, it will be automatically generated by the browser in IndexedDB and this wrapper will automatically generated for WebSQL.

Index schema

Index schema is defined as:

index_schema = {
 name: 'field name',
 type: 'TEXT', // data type of the field, default to 'TEXT'
 unique: true
}

It is not necessary to specify data type in IndexedDB, however WebSql TABLE definition requires to specify data type in defining COLUMN. For IndexedDB only user, it is optional. Possible values of type are 'TEXT', 'REAL' and 'INTEGER'. If not specified, 'TEXT' is assumed. Note, keyPath field also need to be index, if key range query is required.  

Schema for relationships

Optionally, object stores can be related by using relationships and enforced by annotating with the following keywords in the schema. See relationships for usage.   

Keyword Description
parent parent key annotated in child store, parent-child consistency enforce
reference key to refer one-to-many relationship
embedded key to refer one-to-many relationship with convenient retrieval and enforce consistency
referenceList key to refer many-to-many relationship
embeddedList key to refer many-to-many relationship with convenient retrieval and enforce consistency