In this article, we analyze Dexie usage in Lobechat.
If you check [database folder in lobechat, it has 2 folders:
client
server
In this Lobechat’s self-host docs, it is mentioned that LobeChat
defaults to using a client-side database (IndexedDB). That is why you have two folders, one for client and one for server.
Applications typically have one single Dexie instance declared as its own module. This is where you declare which tables you need and how each table shall be indexed. A Dexie instance is a singleton throughout the
application — you do not need to create it on demand. Export the resulting db instance from your module so that components or other modules can use it to query or write to the database.
Using this line shown below, Lobechat creates a singleton instance of BrowserDB.
export class BrowserDB extends Dexie { public files: BrowserDBTable<'files'>; public sessions: BrowserDBTable<'sessions'>; public messages: BrowserDBTable<'messages'>; public topics: BrowserDBTable<'topics'>; public plugins: BrowserDBTable<'plugins'>; public sessionGroups: BrowserDBTable<'sessionGroups'>; public users: BrowserDBTable<'users'>;constructor() { this.version(1).stores(dbSchemaV1); this.version(2).stores(dbSchemaV2); this.version(3).stores(dbSchemaV3); this.version(4) .stores(dbSchemaV4) .upgrade((trans) => this.upgradeToV4(trans)); // … more codeexport const browserDB = new BrowserDB();
versions written in constructor show how the client side database schema evolved over the time, Read more about Dexie.version() to understand the versions.
Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.