Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mapToClass is not working for ES6 class modules #75

Open
gimzani opened this issue Apr 21, 2021 · 3 comments
Open

mapToClass is not working for ES6 class modules #75

gimzani opened this issue Apr 21, 2021 · 3 comments

Comments

@gimzani
Copy link

gimzani commented Apr 21, 2021

I have a class like this:

export default class MyClass {
  constructor(options) {
    this.prop = null;

    if(options) {
      this.prop = options.prop || this.prop;
    }
  }
}

Then I set it up like:

import MyClass from './some/loc/MyClass';

db.MyTable.mapToClass(MyClass);

When I call the mapToClass, I have the imported Class ready - but it does not work. I call the data with 'get' and just get the raw data. It seems to just ignore it all together. Am I just not understanding how this works?

@dfahlander
Copy link
Collaborator

mapToClass() should work with ES6 classes. Please link to an example in jsitor.com or similar showing what you do and explain what you were expecting.

@gimzani
Copy link
Author

gimzani commented Apr 22, 2021

I can't find an online code editor that allows JS module imports.

I'm using WebPack and I'm importing one file into another:

The Module in this case is "Friend.js"

// 'models/Friend.js'
//----------------------------------------------
export default class Friend {
  constructor(options) {
    this.id = 0;
    this.name = null;
    this.shoeSize = null;

    if(options) {
      this.id = options.id || this.id;
      this.name = options.name || this.name;
      this.shoeSize = options.shoeSize || this.shoeSize;
    }
  }
}

The main file is 'main.js' - (from your example)

// main.js
//----------------------------------------------
import Dexie from 'dexie';
import Friend from './models/Friend'

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

db.friends.mapToClass(Friend);  // it's like this gets ignored - or maybe it can't call constructor()?

db.friends.where("name").startsWithIgnoreCase("d").each(function(friend) {

    assert (friend instanceof Friend);  // = false
	
    friend.log();
}).catch(function (e) {
    console.error(e);
});

@dfahlander
Copy link
Collaborator

ok thanks. I'll try repro it. In jsitor you can just include dexie in a script tag in the html part. For webpack style online editor, check codesandbox. For example cloning this Dexie-based project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants