Skip to content

Table.mapToClass()

David Fahlander edited this page Mar 14, 2016 · 18 revisions

Map the table to an existing javascript class

Syntax

table.mapToClass(constructor[, structure])

Parameters

constructor: Function Javascript constructor function
structure: Object Definition of the properties available on instances of the class optional

Return Value

Same constructor function as given as argument.

Remarks

Makes any object extracted from this table be instanceof your given constructor function. You could extend the prototype of the given constructor with methods that will be available on all objects returned by the database.

If calling this method before db.open() call, intelligent javascript editors like Visual Studio 2012+ and IntelliJ will be able to do autocomplete for all objects returned by the database, based on the prototype of the constructor and on given structure.

NOTICE!

  • Structure argument only helps with code completion and documentation of the class. It will not instaniate properties on the instances returned from the database.
  • Given constructor function wont be invoked for instances returned from database. Only the inheritance chain will be applied so that methods attached to constructor.prototype can be called upon and the instanceof operator will return true for the constructor.

Sample (ES6)

import Dexie from 'dexie';

var db = new Dexie("FriendsDB");
db.version(1).stores({
    friends: "++id,name,shoeSize,address.city"
});

class Friend {
    log() {
        console.log(JSON.stringify(this));
    }
}

db.friends.mapToClass (Friend);

db.friends.where("name").startsWithIgnoreCase("d").each(function(friend) {
    assert (friend instanceof Friend);
    friend.log();
}).catch(function (e) {
    console.error(e);
});

Sample (ES5 with type structure)

function Friend() {
}

Friend.prototype.log = function () {
    console.log(JSON.stringify(this));
}

var db = new Dexie("FriendsDB");

// The stores() method just specify primary key and indexes
db.version(1).stores({
    friends: "++id,name,shoeSize,address.city"
});

// Using a structure, you may specify non-indexed properties as well and their types
db.friends.mapToClass (Friend, {
    name: String,
    shoeSize: Number,
    cars: [{
        brand: String,
        model: String
    }],
    address: {
        street: String,
        city: String,
        country: String
    }        
});

db.friends.where("name").startsWithIgnoreCase("d").each(function(friend) {
    friend.log();
}).catch(function (e) {
    console.error(e);
});

Sample (Typescript)

import Dexie from 'dexie';

export class FriendsDB extends Dexie {
    friends: Dexie.Table<Friend, number>;

    constructor() {
        super("FriendsDB");
        this.version(1).stores({
            friends: "++id,name,shoeSize,address.city"
        });
        this.friends.mapToClass (Friend);
    }
}

export class Friend {
    name: string;
    shoeSize: number;
    cars: [{
        brand: string,
        model: string
    }];
    address: {
        street: string,
        city: string,
        country: string
    }

    log() {
        console.log(JSON.stringify(this));
    }
}

db.friends.where("name").startsWithIgnoreCase("d").each(function(friend) {
    assert (friend instanceof Friend);
    friend.log();
}).catch(function (e) {
    console.error(e);
});

See Also

Table.defineClass()

Clone this wiki locally