nylas / nylas-mail

:love_letter: An extensible desktop mail app built on the modern web. Forks welcome!
https://nylas.com/nylas-mail/
MIT License
24.8k stars 1.38k forks source link

Can a plugin define DB tables to store its own data? #3582

Open miguelrs opened 7 years ago

miguelrs commented 7 years ago

I'm writing a plugin for Nylas to send to-dos to Wunderlist, and I would like to persist some data related to the Wunderlist account locally.

I've seen other plugins use localStorage to store that stuff, but I was thinking it'd be better to use the DB if it's there! Is it possible to create custom tables in the DB at all?


What I tried:

I created a class called Root, which extends Nylas' Model, I registered it in the DatabaseObjectRegistry, and I'm trying to persist it with:

let root = new Root({/* ... */})
DatabaseStore.inTransaction(t => t.persistModel(root))

But I get this error:

Error: DatabaseStore: Query REPLACE INTO Root [...] failed Error: Failed to construct SQL statement (no such table: Root)

Root.js

import { Attributes, Model } from 'nylas-exports'

export default class Root extends Model {
    static attributes = Object.assign({}, Model.attributes, {
        // define some attributes here...
    })
}

main.es6

import { ComponentRegistry, DatabaseObjectRegistry } from 'nylas-exports'
import WunderlistToolbarButton from './containers/WunderlistToolbarButton'
import {Root} from './model'

module.exports = {
    activate: () => {
        DatabaseObjectRegistry.register('Root', () => Root)
        ComponentRegistry.register(WunderlistToolbarButton, {role: 'ThreadActionsToolbarButton'})
    },

    deactivate: () => {
        ComponentRegistry.unregister(WunderlistToolbarButton)
    },
}

I tried this stuff by exploring the code myself, so I have no idea if this is possible at all...
This might be related to #3297 where I can use the Task because it doesn't seem to be registered.