-
-
Notifications
You must be signed in to change notification settings - Fork 641
Table.mapToClass()
Map the table to an existing javascript class
table.mapToClass(constructor[, structure])
constructor: Function | Javascript constructor function | |
structure: Object | Definition of the properties available on instances of the class | optional |
Same constructor function as given as argument.
Makes any object extracted from this table become instanceof
your given constructor function so that prototype methods and properties are possible to call on the extracted objects. Works with both ES5 and ES6 classes. Under the hood, the raw database objects are shallow copied into a new instance of your class constructed via Object.create (YourClass.prototype).
Applies only to instances returned by Table.get(), Table.toArray(), Table.each(), Collection.toArray(), Collection.each(), Collection.first(), Collection.last() but not for callbacks that are part of the filtering query: Collection.filter(), Collection.and() and Collection.modify() or if Collection.raw() is being used. In all the latter cases, the methods will emit plain javascript Object instances directly from the database without cloning it into an instance of the mapped class.
- 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.
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);
});
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);
});
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);
});
Dexie.js - minimalistic and bullet proof indexedDB library