refactor: ts on pinia store

This commit is contained in:
Fabio Di Stasio 2022-05-28 18:43:56 +02:00
parent e97401e27d
commit 2007305ff0
9 changed files with 264 additions and 183 deletions

View File

@ -337,6 +337,15 @@ export interface SchemaInfos {
schedulers: EventInfos[]; schedulers: EventInfos[];
} }
export interface CollationInfos {
charset: string;
collation: string;
compiled: boolean;
default: boolean;
id: string | number;
sortLen: number;
}
// Query // Query
export interface QueryBuilderObject { export interface QueryBuilderObject {
schema: string; schema: string;

View File

@ -1,12 +1,13 @@
import { defineStore, acceptHMRUpdate } from 'pinia'; import { defineStore } from 'pinia';
import Store from 'electron-store'; import * as Store from 'electron-store';
import { Ace } from 'ace-builds';
const persistentStore = new Store({ name: 'settings' }); const persistentStore = new Store({ name: 'settings' });
export const useApplicationStore = defineStore('application', { export const useApplicationStore = defineStore('application', {
state: () => ({ state: () => ({
appName: 'Antares - SQL Client', appName: 'Antares - SQL Client',
appVersion: process.env.PACKAGE_VERSION || 0, appVersion: process.env.PACKAGE_VERSION || '0',
cachedVersion: persistentStore.get('cached_version', 0), cachedVersion: persistentStore.get('cached_version', '0') as string,
isLoading: false, isLoading: false,
isNewModal: false, isNewModal: false,
isSettingModal: false, isSettingModal: false,
@ -15,7 +16,7 @@ export const useApplicationStore = defineStore('application', {
selectedConection: {}, selectedConection: {},
updateStatus: 'noupdate', // 'noupdate' | 'available' | 'checking' | 'nocheck' | 'downloading' | 'downloaded' | 'disabled' updateStatus: 'noupdate', // 'noupdate' | 'available' | 'checking' | 'nocheck' | 'downloading' | 'downloaded' | 'disabled'
downloadProgress: 0, downloadProgress: 0,
baseCompleter: [] // Needed to reset ace editor, due global-only ace completer baseCompleter: [] as Ace.Completer[] // Needed to reset ace editor, due global-only ace completer
}), }),
getters: { getters: {
getBaseCompleter: state => state.baseCompleter, getBaseCompleter: state => state.baseCompleter,
@ -30,10 +31,10 @@ export const useApplicationStore = defineStore('application', {
persistentStore.set('cached_version', this.cachedVersion); persistentStore.set('cached_version', this.cachedVersion);
} }
}, },
setLoadingStatus (payload) { setLoadingStatus (payload: boolean) {
this.isLoading = payload; this.isLoading = payload;
}, },
setBaseCompleters (payload) { setBaseCompleters (payload: boolean) {
this.baseCompleter = payload; this.baseCompleter = payload;
}, },
// Modals // Modals
@ -43,7 +44,7 @@ export const useApplicationStore = defineStore('application', {
hideNewConnModal () { hideNewConnModal () {
this.isNewModal = false; this.isNewModal = false;
}, },
showSettingModal (tab) { showSettingModal (tab: string) {
this.selectedSettingTab = tab; this.selectedSettingTab = tab;
this.isSettingModal = true; this.isSettingModal = true;
}, },
@ -58,6 +59,3 @@ export const useApplicationStore = defineStore('application', {
} }
} }
}); });
if (import.meta.webpackHot)
import.meta.webpackHot.accept(acceptHMRUpdate(useApplicationStore, import.meta.webpackHot));

View File

@ -1,6 +1,7 @@
import { defineStore, acceptHMRUpdate } from 'pinia'; import { defineStore } from 'pinia';
import Store from 'electron-store'; import * as Store from 'electron-store';
import crypto from 'crypto'; import * as crypto from 'crypto';
import { ConnectionParams } from 'common/interfaces/antares';
const key = localStorage.getItem('key'); const key = localStorage.getItem('key');
if (!key) if (!key)
@ -16,10 +17,10 @@ const persistentStore = new Store({
export const useConnectionsStore = defineStore('connections', { export const useConnectionsStore = defineStore('connections', {
state: () => ({ state: () => ({
connections: persistentStore.get('connections', []) connections: persistentStore.get('connections', []) as ConnectionParams[]
}), }),
getters: { getters: {
getConnectionName: state => uid => { getConnectionName: state => (uid: string) => {
const connection = state.connections.filter(connection => connection.uid === uid)[0]; const connection = state.connections.filter(connection => connection.uid === uid)[0];
let connectionName = ''; let connectionName = '';
@ -42,16 +43,16 @@ export const useConnectionsStore = defineStore('connections', {
} }
}, },
actions: { actions: {
addConnection (connection) { addConnection (connection: ConnectionParams) {
this.connections.push(connection); this.connections.push(connection);
persistentStore.set('connections', this.connections); persistentStore.set('connections', this.connections);
}, },
deleteConnection (connection) { deleteConnection (connection: ConnectionParams) {
this.connections = this.connections.filter(el => el.uid !== connection.uid); this.connections = (this.connections as ConnectionParams[]).filter(el => el.uid !== connection.uid);
persistentStore.set('connections', this.connections); persistentStore.set('connections', this.connections);
}, },
editConnection (connection) { editConnection (connection: ConnectionParams) {
const editedConnections = this.connections.map(conn => { const editedConnections = (this.connections as ConnectionParams[]).map(conn => {
if (conn.uid === connection.uid) return connection; if (conn.uid === connection.uid) return connection;
return conn; return conn;
}); });
@ -59,12 +60,9 @@ export const useConnectionsStore = defineStore('connections', {
this.selected_conection = {}; this.selected_conection = {};
persistentStore.set('connections', this.connections); persistentStore.set('connections', this.connections);
}, },
updateConnections (connections) { updateConnections (connections: ConnectionParams) {
this.connections = connections; this.connections = connections;
persistentStore.set('connections', this.connections); persistentStore.set('connections', this.connections);
} }
} }
}); });
if (import.meta.webpackHot)
import.meta.webpackHot.accept(acceptHMRUpdate(useConnectionsStore, import.meta.webpackHot));

View File

@ -1,19 +1,26 @@
import { defineStore, acceptHMRUpdate } from 'pinia'; import { defineStore } from 'pinia';
import Store from 'electron-store'; import * as Store from 'electron-store';
import { uidGen } from 'common/libs/uidGen'; import { uidGen } from 'common/libs/uidGen';
const persistentStore = new Store({ name: 'history' }); const persistentStore = new Store({ name: 'history' });
const historySize = 1000; const historySize = 1000;
export interface HistoryRecord {
uid: string;
sql: string;
date: Date;
schema?: string;
}
export const useHistoryStore = defineStore('history', { export const useHistoryStore = defineStore('history', {
state: () => ({ state: () => ({
history: persistentStore.get('history', {}), history: persistentStore.get('history', {}) as {[key: string]: HistoryRecord[]},
favorites: persistentStore.get('favorites', {}) favorites: persistentStore.get('favorites', {})
}), }),
getters: { getters: {
getHistoryByWorkspace: state => uid => state.history[uid] getHistoryByWorkspace: state => (uid: string) => state.history[uid]
}, },
actions: { actions: {
saveHistory (args) { saveHistory (args: { uid: string; query: string; schema: string; tabUid: string }) {
if (this.getHistoryByWorkspace(args.uid) && if (this.getHistoryByWorkspace(args.uid) &&
this.getHistoryByWorkspace(args.uid).length && this.getHistoryByWorkspace(args.uid).length &&
this.getHistoryByWorkspace(args.uid)[0].sql === args.query this.getHistoryByWorkspace(args.uid)[0].sql === args.query
@ -37,12 +44,9 @@ export const useHistoryStore = defineStore('history', {
persistentStore.set('history', this.history); persistentStore.set('history', this.history);
}, },
deleteQueryFromHistory (query) { deleteQueryFromHistory (query: Partial<HistoryRecord> & { workspace: string}) {
this.history[query.workspace] = this.history[query.workspace].filter(q => q.uid !== query.uid); this.history[query.workspace] = (this.history[query.workspace] as HistoryRecord[]).filter(q => q.uid !== query.uid);
persistentStore.set('history', this.history); persistentStore.set('history', this.history);
} }
} }
}); });
if (import.meta.webpackHot)
import.meta.webpackHot.accept(acceptHMRUpdate(useHistoryStore, import.meta.webpackHot));

View File

@ -1,20 +0,0 @@
import { defineStore, acceptHMRUpdate } from 'pinia';
import { uidGen } from 'common/libs/uidGen';
export const useNotificationsStore = defineStore('notifications', {
state: () => ({
notifications: []
}),
actions: {
addNotification (payload) {
payload.uid = uidGen('N');
this.notifications.unshift(payload);
},
removeNotification (uid) {
this.notifications = this.notifications.filter(item => item.uid !== uid);
}
}
});
if (import.meta.webpackHot)
import.meta.webpackHot.accept(acceptHMRUpdate(useNotificationsStore, import.meta.webpackHot));

View File

@ -0,0 +1,23 @@
import { defineStore } from 'pinia';
import { uidGen } from 'common/libs/uidGen';
export interface Notification {
uid: string;
status: string;
message: string;
}
export const useNotificationsStore = defineStore('notifications', {
state: () => ({
notifications: [] as Notification[]
}),
actions: {
addNotification (payload: { status: string; message: string }) {
const notification: Notification = { uid: uidGen('N'), ...payload };
this.notifications.unshift(notification);
},
removeNotification (uid: string) {
this.notifications = (this.notifications as Notification[]).filter(item => item.uid !== uid);
}
}
});

View File

@ -1,10 +1,10 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import Store from 'electron-store'; import * as Store from 'electron-store';
const persistentStore = new Store({ name: 'notes' }); const persistentStore = new Store({ name: 'notes' });
export const useScratchpadStore = defineStore('scratchpad', { export const useScratchpadStore = defineStore('scratchpad', {
state: () => ({ state: () => ({
notes: persistentStore.get('notes', '# HOW TO SUPPORT ANTARES\n\n- [ ] Leave a star to Antares [GitHub repo](https://github.com/antares-sql/antares)\n- [ ] Send feedbacks and advices\n- [ ] Report for bugs\n- [ ] If you enjoy, share Antares with friends\n\n# ABOUT SCRATCHPAD\n\nThis is a scratchpad where you can save your **personal notes**. It supports `markdown` format, but you are free to use plain text.\nThis content is just a placeholder, feel free to clear it to make space for your notes.\n') notes: persistentStore.get('notes', '# HOW TO SUPPORT ANTARES\n\n- [ ] Leave a star to Antares [GitHub repo](https://github.com/antares-sql/antares)\n- [ ] Send feedbacks and advices\n- [ ] Report for bugs\n- [ ] If you enjoy, share Antares with friends\n\n# ABOUT SCRATCHPAD\n\nThis is a scratchpad where you can save your **personal notes**. It supports `markdown` format, but you are free to use plain text.\nThis content is just a placeholder, feel free to clear it to make space for your notes.\n') as string
}), }),
actions: { actions: {
changeNotes (notes: string) { changeNotes (notes: string) {

View File

@ -1,6 +1,6 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import i18n from '@/i18n'; import i18n from '@/i18n';
import Store from 'electron-store'; import * as Store from 'electron-store';
const persistentStore = new Store({ name: 'settings' }); const persistentStore = new Store({ name: 'settings' });
const isDarkTheme = window.matchMedia('(prefers-color-scheme: dark)'); const isDarkTheme = window.matchMedia('(prefers-color-scheme: dark)');
const defaultAppTheme = isDarkTheme.matches ? 'dark' : 'light'; const defaultAppTheme = isDarkTheme.matches ? 'dark' : 'light';

View File

@ -1,5 +1,5 @@
import { defineStore, acceptHMRUpdate } from 'pinia'; import { defineStore } from 'pinia';
import Store from 'electron-store'; import * as Store from 'electron-store';
import Connection from '@/ipc-api/Connection'; import Connection from '@/ipc-api/Connection';
import Schema from '@/ipc-api/Schema'; import Schema from '@/ipc-api/Schema';
import Users from '@/ipc-api/Users'; import Users from '@/ipc-api/Users';
@ -10,14 +10,88 @@ import customizations from 'common/customizations';
import { useConnectionsStore } from '@/stores/connections'; import { useConnectionsStore } from '@/stores/connections';
import { useNotificationsStore } from '@/stores/notifications'; import { useNotificationsStore } from '@/stores/notifications';
import { useSettingsStore } from '@/stores/settings'; import { useSettingsStore } from '@/stores/settings';
import {
CollationInfos,
ConnectionParams,
EventInfos,
FunctionInfos,
RoutineInfos,
TableInfos,
TriggerInfos,
TypesGroup
} from 'common/interfaces/antares';
import { Customizations } from 'common/interfaces/customizations';
export interface WorkspaceTab {
uid: string;
tab?: string;
index: number;
selected: boolean;
type: string;
schema?: string;
elementName?: string;
elementNewName?: string;
elementType?: string;
isChanged?: boolean;
content?: string;
autorun: boolean;
}
export interface WorkspaceStructure {
name: string;
functions: FunctionInfos[];
procedures: RoutineInfos[];
schedulers: EventInfos[];
tables: TableInfos[];
triggers: TriggerInfos[];
size: number;
}
export interface Breadcrumb {
function?: string;
procedure?: string;
query?: string;
scheduler?: string;
schema?: string;
table?: string;
trigger?: string;
triggerFunction?: string;
view?: string;
}
export interface Workspace {
uid: string;
client?: string;
connectionStatus: string;
selectedTab: string | number;
searchTerm: string;
tabs: WorkspaceTab[];
structure: WorkspaceStructure[];
variables: { name: string; value: string }[];
collations: CollationInfos[];
users: { host: string; name: string; password: string }[];
breadcrumbs: Breadcrumb[];
loadingElements: { name: string; schema: string; type: string }[];
loadedSchemas: Set<string>;
dataTypes?: { [key: string]: TypesGroup[] };
indexTypes?: string[];
customizations?: Customizations;
version?: {
number: string;
name: string;
arch: string;
os: string;
};
engines?: {[key: string]: string | boolean | number}[];
}
const persistentStore = new Store({ name: 'tabs' }); const persistentStore = new Store({ name: 'tabs' });
const tabIndex = []; const tabIndex: {[key: string]: number} = {};
export const useWorkspacesStore = defineStore('workspaces', { export const useWorkspacesStore = defineStore('workspaces', {
state: () => ({ state: () => ({
workspaces: [], workspaces: [] as Workspace[],
selectedWorkspace: null selectedWorkspace: null as string
}), }),
getters: { getters: {
getSelected: state => { getSelected: state => {
@ -25,14 +99,14 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (state.selectedWorkspace) return state.selectedWorkspace; if (state.selectedWorkspace) return state.selectedWorkspace;
return state.workspaces[0].uid; return state.workspaces[0].uid;
}, },
getWorkspace: state => (uid) => { getWorkspace: state => (uid: string) => {
return state.workspaces.find(workspace => workspace.uid === uid); return state.workspaces.find(workspace => workspace.uid === uid);
}, },
getDatabaseVariable: state => (uid, name) => { getDatabaseVariable: state => (uid: string, name: string) => {
return state.workspaces.find(workspace => workspace.uid === uid).variables.find(variable => variable.name === name); return state.workspaces.find(workspace => workspace.uid === uid).variables.find(variable => variable.name === name);
}, },
getWorkspaceTab (state) { getWorkspaceTab (state) {
return (tUid) => { return (tUid: string) => {
if (!this.getSelected) return; if (!this.getSelected) return;
const workspace = state.workspaces.find(workspace => workspace.uid === this.getSelected); const workspace = state.workspaces.find(workspace => workspace.uid === this.getSelected);
if ('tabs' in workspace) if ('tabs' in workspace)
@ -45,22 +119,22 @@ export const useWorkspacesStore = defineStore('workspaces', {
.filter(workspace => workspace.connectionStatus === 'connected') .filter(workspace => workspace.connectionStatus === 'connected')
.map(workspace => workspace.uid); .map(workspace => workspace.uid);
}, },
getLoadedSchemas: state => uid => { getLoadedSchemas: state => (uid: string) => {
return state.workspaces.find(workspace => workspace.uid === uid).loadedSchemas; return state.workspaces.find(workspace => workspace.uid === uid).loadedSchemas;
}, },
getSearchTerm: state => uid => { getSearchTerm: state => (uid: string) => {
return state.workspaces.find(workspace => workspace.uid === uid).searchTerm; return state.workspaces.find(workspace => workspace.uid === uid).searchTerm;
} }
}, },
actions: { actions: {
selectWorkspace (uid) { selectWorkspace (uid: string) {
if (!uid) if (!uid)
this.selectedWorkspace = this.workspaces.length ? this.workspaces[0].uid : 'NEW'; this.selectedWorkspace = this.workspaces.length ? this.workspaces[0].uid : 'NEW';
else else
this.selectedWorkspace = uid; this.selectedWorkspace = uid;
}, },
async connectWorkspace (connection) { async connectWorkspace (connection: ConnectionParams) {
this.workspaces = this.workspaces.map(workspace => workspace.uid === connection.uid this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === connection.uid
? { ? {
...workspace, ...workspace,
structure: {}, structure: {},
@ -79,7 +153,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error') { if (status === 'error') {
notificationsStore.addNotification({ status, message: response }); notificationsStore.addNotification({ status, message: response });
this.workspaces = this.workspaces.map(workspace => workspace.uid === connection.uid this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === connection.uid
? { ? {
...workspace, ...workspace,
structure: {}, structure: {},
@ -90,25 +164,25 @@ export const useWorkspacesStore = defineStore('workspaces', {
: workspace); : workspace);
} }
else { else {
let dataTypes = []; let dataTypes: TypesGroup[] = [];
let indexTypes = []; let indexTypes: string[] = [];
let clientCustomizations; let clientCustomizations: Customizations;
switch (connection.client) { switch (connection.client) {
case 'mysql': case 'mysql':
case 'maria': case 'maria':
dataTypes = require('common/data-types/mysql').default; dataTypes = require('common/data-types/mysql');
indexTypes = require('common/index-types/mysql').default; indexTypes = require('common/index-types/mysql');
clientCustomizations = customizations.mysql; clientCustomizations = customizations.mysql;
break; break;
case 'pg': case 'pg':
dataTypes = require('common/data-types/postgresql').default; dataTypes = require('common/data-types/postgresql');
indexTypes = require('common/index-types/postgresql').default; indexTypes = require('common/index-types/postgresql');
clientCustomizations = customizations.pg; clientCustomizations = customizations.pg;
break; break;
case 'sqlite': case 'sqlite':
dataTypes = require('common/data-types/sqlite').default; dataTypes = require('common/data-types/sqlite');
indexTypes = require('common/index-types/sqlite').default; indexTypes = require('common/index-types/sqlite');
clientCustomizations = customizations.sqlite; clientCustomizations = customizations.sqlite;
break; break;
} }
@ -132,16 +206,16 @@ export const useWorkspacesStore = defineStore('workspaces', {
connectionsStore.editConnection(connProxy); connectionsStore.editConnection(connProxy);
} }
const cachedTabs = settingsStore.restoreTabs ? persistentStore.get(connection.uid, []) : []; const cachedTabs: WorkspaceTab[] = settingsStore.restoreTabs ? persistentStore.get(connection.uid, []) as WorkspaceTab[] : [];
if (cachedTabs.length) { if (cachedTabs.length) {
tabIndex[connection.uid] = cachedTabs.reduce((acc, curr) => { tabIndex[connection.uid] = cachedTabs.reduce((acc: number, curr) => {
if (curr.index > acc) acc = curr.index; if (curr.index > acc) acc = curr.index;
return acc; return acc;
}, null); }, null);
} }
this.workspaces = this.workspaces.map(workspace => workspace.uid === connection.uid this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === connection.uid
? { ? {
...workspace, ...workspace,
client: connection.client, client: connection.client,
@ -166,7 +240,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack }); notificationsStore.addNotification({ status: 'error', message: err.stack });
} }
}, },
async refreshStructure (uid) { async refreshStructure (uid: string) {
const notificationsStore = useNotificationsStore(); const notificationsStore = useNotificationsStore();
try { try {
@ -175,7 +249,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error') if (status === 'error')
notificationsStore.addNotification({ status, message: response }); notificationsStore.addNotification({ status, message: response });
else { else {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? { ? {
...workspace, ...workspace,
structure: response structure: response
@ -187,7 +261,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack }); notificationsStore.addNotification({ status: 'error', message: err.stack });
} }
}, },
async refreshSchema ({ uid, schema }) { async refreshSchema ({ uid, schema }: {uid: string; schema: string}) {
const notificationsStore = useNotificationsStore(); const notificationsStore = useNotificationsStore();
try { try {
@ -195,8 +269,8 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error') if (status === 'error')
notificationsStore.addNotification({ status, message: response }); notificationsStore.addNotification({ status, message: response });
else { else {
const schemaElements =response.find(_schema => _schema.name === schema); const schemaElements = (response as WorkspaceStructure[]).find(_schema => _schema.name === schema);
this.workspaces = this.workspaces.map(workspace => { this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) { if (workspace.uid === uid) {
const schemaIndex = workspace.structure.findIndex(s => s.name === schema); const schemaIndex = workspace.structure.findIndex(s => s.name === schema);
@ -213,7 +287,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack }); notificationsStore.addNotification({ status: 'error', message: err.stack });
} }
}, },
async refreshCollations (uid) { async refreshCollations (uid: string) {
const notificationsStore = useNotificationsStore(); const notificationsStore = useNotificationsStore();
try { try {
@ -221,7 +295,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error') if (status === 'error')
notificationsStore.addNotification({ status, message: response }); notificationsStore.addNotification({ status, message: response });
else { else {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? { ? {
...workspace, ...workspace,
collations: response collations: response
@ -233,7 +307,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack }); notificationsStore.addNotification({ status: 'error', message: err.stack });
} }
}, },
async refreshVariables (uid) { async refreshVariables (uid: string) {
const notificationsStore = useNotificationsStore(); const notificationsStore = useNotificationsStore();
try { try {
@ -241,7 +315,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error') if (status === 'error')
notificationsStore.addNotification({ status, message: response }); notificationsStore.addNotification({ status, message: response });
else { else {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? { ? {
...workspace, ...workspace,
variables: response variables: response
@ -253,7 +327,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack }); notificationsStore.addNotification({ status: 'error', message: err.stack });
} }
}, },
async refreshEngines (uid) { async refreshEngines (uid: string) {
const notificationsStore = useNotificationsStore(); const notificationsStore = useNotificationsStore();
try { try {
@ -261,7 +335,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error') if (status === 'error')
notificationsStore.addNotification({ status, message: response }); notificationsStore.addNotification({ status, message: response });
else { else {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? { ? {
...workspace, ...workspace,
engines: response engines: response
@ -273,7 +347,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack }); notificationsStore.addNotification({ status: 'error', message: err.stack });
} }
}, },
async refreshUsers (uid) { async refreshUsers (uid: string) {
const notificationsStore = useNotificationsStore(); const notificationsStore = useNotificationsStore();
try { try {
@ -281,7 +355,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error') if (status === 'error')
notificationsStore.addNotification({ status, message: response }); notificationsStore.addNotification({ status, message: response });
else { else {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? { ? {
...workspace, ...workspace,
users: response users: response
@ -293,9 +367,9 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack }); notificationsStore.addNotification({ status: 'error', message: err.stack });
} }
}, },
removeConnected (uid) { removeConnected (uid: string) {
Connection.disconnect(uid); Connection.disconnect(uid);
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? { ? {
...workspace, ...workspace,
structure: {}, structure: {},
@ -307,26 +381,26 @@ export const useWorkspacesStore = defineStore('workspaces', {
this.selectTab({ uid, tab: 0 }); this.selectTab({ uid, tab: 0 });
}, },
addWorkspace (uid) { addWorkspace (uid: string) {
const workspace = { const workspace: Workspace = {
uid, uid,
connectionStatus: 'disconnected', connectionStatus: 'disconnected',
selectedTab: 0, selectedTab: 0,
searchTerm: '', searchTerm: '',
tabs: [], tabs: [],
structure: {}, structure: [],
variables: [], variables: [],
collations: [], collations: [],
users: [], users: [],
breadcrumbs: {}, breadcrumbs: [],
loadingElements: [], loadingElements: [],
loadedSchemas: new Set() loadedSchemas: new Set()
}; };
this.workspaces.push(workspace); this.workspaces.push(workspace);
}, },
changeBreadcrumbs (payload) { changeBreadcrumbs (payload: Breadcrumb) {
const breadcrumbsObj = { const breadcrumbsObj: Breadcrumb = {
schema: null, schema: null,
table: null, table: null,
trigger: null, trigger: null,
@ -338,29 +412,29 @@ export const useWorkspacesStore = defineStore('workspaces', {
query: null query: null
}; };
this.workspaces = this.workspaces.map(workspace => workspace.uid === this.getSelected this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === this.getSelected
? { ? {
...workspace, ...workspace,
breadcrumbs: { ...breadcrumbsObj, ...payload } breadcrumbs: { ...breadcrumbsObj, ...payload }
} }
: workspace); : workspace);
}, },
addLoadedSchema (schema) { addLoadedSchema (schema: string) {
this.workspaces = this.workspaces.map(workspace => { this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === this.getSelected) if (workspace.uid === this.getSelected)
workspace.loadedSchemas.add(schema); workspace.loadedSchemas.add(schema);
return workspace; return workspace;
}); });
}, },
addLoadingElement (element) { addLoadingElement (element: { name: string; schema: string; type: string }) {
this.workspaces = this.workspaces.map(workspace => { this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === this.getSelected) if (workspace.uid === this.getSelected)
workspace.loadingElements.push(element); workspace.loadingElements.push(element);
return workspace; return workspace;
}); });
}, },
removeLoadingElement (element) { removeLoadingElement (element: { name: string; schema: string; type: string }) {
this.workspaces = this.workspaces.map(workspace => { this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === this.getSelected) { if (workspace.uid === this.getSelected) {
const loadingElements = workspace.loadingElements.filter(el => const loadingElements = workspace.loadingElements.filter(el =>
el.schema !== element.schema && el.schema !== element.schema &&
@ -373,19 +447,19 @@ export const useWorkspacesStore = defineStore('workspaces', {
return workspace; return workspace;
}); });
}, },
setSearchTerm (term) { setSearchTerm (term: string) {
this.workspaces = this.workspaces.map(workspace => workspace.uid === this.getSelected this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === this.getSelected
? { ? {
...workspace, ...workspace,
searchTerm: term searchTerm: term
} }
: workspace); : workspace);
}, },
_addTab ({ uid, tab, content, type, autorun, schema, elementName, elementType }) { _addTab ({ uid, tab, content, type, autorun, schema, elementName, elementType }: WorkspaceTab) {
if (type === 'query') if (type === 'query')
tabIndex[uid] = tabIndex[uid] ? ++tabIndex[uid] : 1; tabIndex[uid] = tabIndex[uid] ? ++tabIndex[uid] : 1;
const newTab = { const newTab: WorkspaceTab = {
uid: tab, uid: tab,
index: type === 'query' ? tabIndex[uid] : null, index: type === 'query' ? tabIndex[uid] : null,
selected: false, selected: false,
@ -393,13 +467,11 @@ export const useWorkspacesStore = defineStore('workspaces', {
schema, schema,
elementName, elementName,
elementType, elementType,
fields: [],
keyUsage: [],
content: content || '', content: content || '',
autorun: !!autorun autorun: !!autorun
}; };
this.workspaces = this.workspaces.map(workspace => { this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) { if (workspace.uid === uid) {
return { return {
...workspace, ...workspace,
@ -410,10 +482,10 @@ export const useWorkspacesStore = defineStore('workspaces', {
return workspace; return workspace;
}); });
persistentStore.set(uid, this.workspaces.find(workspace => workspace.uid === uid).tabs); persistentStore.set(uid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid).tabs);
}, },
_replaceTab ({ uid, tab: tUid, type, schema, content, elementName, elementType }) { _replaceTab ({ uid, tab: tUid, type, schema, content, elementName, elementType }: WorkspaceTab) {
this.workspaces = this.workspaces.map(workspace => { this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) { if (workspace.uid === uid) {
return { return {
...workspace, ...workspace,
@ -429,11 +501,11 @@ export const useWorkspacesStore = defineStore('workspaces', {
return workspace; return workspace;
}); });
persistentStore.set(uid, this.workspaces.find(workspace => workspace.uid === uid).tabs); persistentStore.set(uid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid).tabs);
}, },
newTab ({ uid, content, type, autorun, schema, elementName, elementType }) { newTab ({ uid, content, type, autorun, schema, elementName, elementType }: WorkspaceTab) {
let tabUid; let tabUid;
const workspaceTabs = this.workspaces.find(workspace => workspace.uid === uid); const workspaceTabs = (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid);
switch (type) { switch (type) {
case 'new-table': case 'new-table':
@ -535,8 +607,8 @@ export const useWorkspacesStore = defineStore('workspaces', {
this.selectTab({ uid, tab: tabUid }); this.selectTab({ uid, tab: tabUid });
}, },
checkSelectedTabExists (uid) { checkSelectedTabExists (uid: string) {
const workspace = this.workspaces.find(workspace => workspace.uid === uid); const workspace = (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid);
const isSelectedExistent = workspace const isSelectedExistent = workspace
? workspace.tabs.some(tab => tab.uid === workspace.selectedTab) ? workspace.tabs.some(tab => tab.uid === workspace.selectedTab)
: false; : false;
@ -544,11 +616,11 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (!isSelectedExistent && workspace.tabs.length) if (!isSelectedExistent && workspace.tabs.length)
this.selectTab({ uid, tab: workspace.tabs[workspace.tabs.length - 1].uid }); this.selectTab({ uid, tab: workspace.tabs[workspace.tabs.length - 1].uid });
}, },
updateTabContent ({ uid, tab, type, schema, content }) { updateTabContent ({ uid, tab, type, schema, content }: WorkspaceTab) {
this._replaceTab({ uid, tab, type, schema, content }); this._replaceTab({ uid, tab, type, schema, content });
}, },
renameTabs ({ uid, schema, elementName, elementNewName }) { renameTabs ({ uid, schema, elementName, elementNewName }: WorkspaceTab) {
this.workspaces = this.workspaces.map(workspace => { this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) { if (workspace.uid === uid) {
return { return {
...workspace, ...workspace,
@ -568,10 +640,10 @@ export const useWorkspacesStore = defineStore('workspaces', {
return workspace; return workspace;
}); });
persistentStore.set(uid, this.workspaces.find(workspace => workspace.uid === uid).tabs); persistentStore.set(uid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid).tabs);
}, },
removeTab ({ uid, tab: tUid }) { removeTab ({ uid, tab: tUid }: {uid: string; tab: string}) {
this.workspaces = this.workspaces.map(workspace => { this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) { if (workspace.uid === uid) {
return { return {
...workspace, ...workspace,
@ -582,13 +654,13 @@ export const useWorkspacesStore = defineStore('workspaces', {
return workspace; return workspace;
}); });
persistentStore.set(uid, this.workspaces.find(workspace => workspace.uid === uid).tabs); persistentStore.set(uid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid).tabs);
this.checkSelectedTabExists(uid); this.checkSelectedTabExists(uid);
}, },
removeTabs ({ uid, schema, elementName, elementType }) { // Multiple tabs based on schema and element name removeTabs ({ uid, schema, elementName, elementType }: WorkspaceTab) { // Multiple tabs based on schema and element name
if (elementType === 'procedure') elementType = 'routine'; // TODO: pass directly "routine" if (elementType === 'procedure') elementType = 'routine'; // TODO: pass directly "routine"
this.workspaces = this.workspaces.map(workspace => { this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) { if (workspace.uid === uid) {
return { return {
...workspace, ...workspace,
@ -603,62 +675,62 @@ export const useWorkspacesStore = defineStore('workspaces', {
return workspace; return workspace;
}); });
persistentStore.set(uid, this.workspaces.find(workspace => workspace.uid === uid).tabs); persistentStore.set(uid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid).tabs);
this.checkSelectedTabExists(uid); this.checkSelectedTabExists(uid);
}, },
selectTab ({ uid, tab }) { selectTab ({ uid, tab }: {uid: string; tab: string}) {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? { ...workspace, selectedTab: tab } ? { ...workspace, selectedTab: tab }
: workspace : workspace
); );
}, },
updateTabs ({ uid, tabs }) { updateTabs ({ uid, tabs }: {uid: string; tabs: string[]}) {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? { ...workspace, tabs } ? { ...workspace, tabs }
: workspace : workspace
); );
persistentStore.set(uid, this.workspaces.find(workspace => workspace.uid === uid).tabs); persistentStore.set(uid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid).tabs);
}, },
setTabFields ({ cUid, tUid, fields }) { // setTabFields ({ cUid, tUid, fields }: { cUid: string; tUid: string; fields: any }) {
this.workspaces = this.workspaces.map(workspace => { // this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === cUid) { // if (workspace.uid === cUid) {
return { // return {
...workspace, // ...workspace,
tabs: workspace.tabs.map(tab => { // tabs: workspace.tabs.map(tab => {
if (tab.uid === tUid) // if (tab.uid === tUid)
return { ...tab, fields }; // return { ...tab, fields };
else // else
return tab; // return tab;
}) // })
}; // };
} // }
else // else
return workspace; // return workspace;
}); // });
persistentStore.set(cUid, this.workspaces.find(workspace => workspace.uid === cUid).tabs); // persistentStore.set(cUid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === cUid).tabs);
}, // },
setTabKeyUsage ({ cUid, tUid, keyUsage }) { // setTabKeyUsage ({ cUid, tUid, keyUsage }: { cUid: string; tUid: string; keyUsage: any }) {
this.workspaces = this.workspaces.map(workspace => { // this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === cUid) { // if (workspace.uid === cUid) {
return { // return {
...workspace, // ...workspace,
tabs: workspace.tabs.map(tab => { // tabs: workspace.tabs.map(tab => {
if (tab.uid === tUid) // if (tab.uid === tUid)
return { ...tab, keyUsage }; // return { ...tab, keyUsage };
else // else
return tab; // return tab;
}) // })
}; // };
} // }
else // else
return workspace; // return workspace;
}); // });
persistentStore.set(cUid, this.workspaces.find(workspace => workspace.uid === cUid).tabs); // persistentStore.set(cUid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === cUid).tabs);
}, // },
setUnsavedChanges ({ uid, tUid, isChanged }) { setUnsavedChanges ({ uid, tUid, isChanged }: { uid: string; tUid: string; isChanged: boolean }) {
this.workspaces = this.workspaces.map(workspace => { this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) { if (workspace.uid === uid) {
return { return {
...workspace, ...workspace,
@ -676,6 +748,3 @@ export const useWorkspacesStore = defineStore('workspaces', {
} }
} }
}); });
if (import.meta.webpackHot)
import.meta.webpackHot.accept(acceptHMRUpdate(useWorkspacesStore, import.meta.webpackHot));