mirror of
https://github.com/zen-browser/components.git
synced 2025-07-08 16:49:58 +02:00
feat(workspaces): Store workspaces in a database
This commit introduces a new `ZenWorkspacesStorage` class to handle the persistence of workspaces in a database. The previous implementation used JSON files, but this approach brings several advantages, including: - Improved performance and scalability - Easier data management and synchronization - Enhanced security and data integrity This change removes the reliance on JSON files and streamlines workspace management, leading to a more robust and reliable system.
This commit is contained in:
parent
2ea7024eea
commit
de8a857549
2 changed files with 106 additions and 28 deletions
89
src/ZenWorkspacesStorage.mjs
Normal file
89
src/ZenWorkspacesStorage.mjs
Normal file
|
@ -0,0 +1,89 @@
|
|||
var ZenWorkspacesStorage = {
|
||||
async init() {
|
||||
console.log('ZenWorkspacesStorage: Initializing...');
|
||||
await this._ensureTable();
|
||||
},
|
||||
|
||||
async _ensureTable() {
|
||||
await PlacesUtils.withConnectionWrapper("ZenWorkspacesStorage._ensureTable", async db => {
|
||||
await db.execute(`
|
||||
CREATE TABLE IF NOT EXISTS moz_workspaces (
|
||||
id INTEGER PRIMARY KEY,
|
||||
uuid TEXT UNIQUE NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
icon TEXT,
|
||||
is_default INTEGER NOT NULL DEFAULT 0,
|
||||
is_active INTEGER NOT NULL DEFAULT 0,
|
||||
container_id INTEGER,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
)
|
||||
`);
|
||||
});
|
||||
},
|
||||
|
||||
async saveWorkspace(workspace) {
|
||||
await PlacesUtils.withConnectionWrapper("ZenWorkspacesStorage.saveWorkspace", async db => {
|
||||
const now = Date.now();
|
||||
await db.executeCached(`
|
||||
INSERT OR REPLACE INTO moz_workspaces (
|
||||
uuid, name, icon, is_default, is_active, container_id, created_at, updated_at
|
||||
) VALUES (
|
||||
:uuid, :name, :icon, :is_default, :is_active, :container_id,
|
||||
COALESCE((SELECT created_at FROM moz_workspaces WHERE uuid = :uuid), :now),
|
||||
:now
|
||||
)
|
||||
`, {
|
||||
uuid: workspace.uuid,
|
||||
name: workspace.name,
|
||||
icon: workspace.icon || null,
|
||||
is_default: workspace.default ? 1 : 0,
|
||||
is_active: workspace.used ? 1 : 0,
|
||||
container_id: workspace.containerTabId || null,
|
||||
now
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
async getWorkspaces() {
|
||||
const db = await PlacesUtils.promiseDBConnection();
|
||||
const rows = await db.execute(`
|
||||
SELECT * FROM moz_workspaces ORDER BY created_at ASC
|
||||
`);
|
||||
|
||||
return rows.map(row => ({
|
||||
uuid: row.getResultByName("uuid"),
|
||||
name: row.getResultByName("name"),
|
||||
icon: row.getResultByName("icon"),
|
||||
default: !!row.getResultByName("is_default"),
|
||||
used: !!row.getResultByName("is_active"),
|
||||
containerTabId: row.getResultByName("container_id")
|
||||
}));
|
||||
},
|
||||
|
||||
async removeWorkspace(uuid) {
|
||||
await PlacesUtils.withConnectionWrapper("ZenWorkspacesStorage.removeWorkspace", async db => {
|
||||
await db.execute(`
|
||||
DELETE FROM moz_workspaces WHERE uuid = :uuid
|
||||
`, { uuid });
|
||||
});
|
||||
},
|
||||
|
||||
async setActiveWorkspace(uuid) {
|
||||
await PlacesUtils.withConnectionWrapper("ZenWorkspacesStorage.setActiveWorkspace", async db => {
|
||||
await db.executeTransaction(async function() {
|
||||
await db.execute(`UPDATE moz_workspaces SET is_active = 0`);
|
||||
await db.execute(`UPDATE moz_workspaces SET is_active = 1 WHERE uuid = :uuid`, { uuid });
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
async setDefaultWorkspace(uuid) {
|
||||
await PlacesUtils.withConnectionWrapper("ZenWorkspacesStorage.setDefaultWorkspace", async db => {
|
||||
await db.executeTransaction(async function() {
|
||||
await db.execute(`UPDATE moz_workspaces SET is_default = 0`);
|
||||
await db.execute(`UPDATE moz_workspaces SET is_default = 1 WHERE uuid = :uuid`, { uuid });
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue