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

Allow declaration-only tables for TS users. #1136

Merged
merged 1 commit into from Oct 14, 2020

Conversation

dfahlander
Copy link
Collaborator

@dfahlander dfahlander commented Oct 1, 2020

Due to how @babel/plugin-transform-typescript transpiles class fields, we had to change the Typescript recommendation of how to subclass Dexie. This commit makes it possible to go back to the previous recommendation in the docs - do only declare the classes and not have to manually initialize the tables in the constructor.

Plan: Wait with the docs updates until this commit has been released and then for another while until enough people have upgraded their Dexie to that version.

The docs should then be updated to propose a plain declaration of table properties, with an exclamation mark to hint about that the field is there despite not being assigned to in the constructor.

Current recommendation:

class MyAppDatabase extends Dexie {
    contacts: Dexie.Table<IContact, number>; // number = type of the primkey

    constructor () {
        super("MyAppDatabase");
        this.version(1).stores({
            contacts: '++id, first, last',
        });

        // The following line is needed if your typescript
        // is compiled using babel instead of tsc:
        this.contacts = this.table("contacts");
    }
}

New possible recommendation:

...when this change has reached enough end users:

class MyAppDatabase extends Dexie {
    contacts!: Dexie.Table<IContact, number>; // number = type of the primkey

    constructor () {
        super("MyAppDatabase");
        this.version(1).stores({
            contacts: '++id, first, last',
        });
    }
}

Due to how @babel/plugin-transform-typescript transpiles class fields, we had to change the Typescript recommendation of how to subclass Dexie. This commit makes it possible to go back to the previous recommendation in the docs - do only declare the classes and not have to manually initialize the tables in the constructor.

Plan: Wait with the docs updates until this commit has been released and then for another while until enough people have upgraded their Dexie to that version.

The docs should then be updated to propose a plain declaration of table properties, with an exclamation mark to hint about that the field is there despite not being assigned to in the constructor.
@dfahlander dfahlander merged commit 807c644 into master Oct 14, 2020
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

Successfully merging this pull request may close these issues.

None yet

1 participant