基于electron-store的最佳实践封装工具类

基于之前讨论的electron-store最佳实践,以下是一个更加完善和健壮的封装示例,包含了类型处理、默认值设定、以及一些实用方法,以适应更复杂的场景:

// storeUtil.js
const Store = require('electron-store');
const app = require('electron').app;

class StoreUtil {
  constructor(configName = 'appSettings', defaults = {}, encryptionKey = null) {
    this.store = new Store({ 
      name: configName,
      encryptionKey, // 可选,提供一个密钥用于加密存储
      defaults
    });

    // 初始化默认值,确保所有默认设置已写入存储
    for (const key in defaults) {
      if (!this.store.has(key)) {
        this.set(key, defaults[key]);
      }
    }
  }

  // 设置值,支持自动处理特殊类型如Date
  set(key, value) {
    if (value instanceof Date) {
      value = value.toISOString();
    }
    return this.store.set(key, value);
  }

  // 获取值,支持类型转换和默认值
  get(key, defaultValue = null) {
    const rawValue = this.store.get(key);
    if (rawValue === undefined && defaultValue !== null) {
      return defaultValue;
    }

    try {
      if (typeof defaultValue === 'string' && defaultValue.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z$/)) {
        return new Date(rawValue);
      }
    } catch (error) {
      console.error(`Error converting value for key ${key}:`, error);
    }

    return rawValue;
  }

  // 删除键值对
  delete(key) {
    return this.store.delete(key);
  }

  // 清空所有存储
  clear() {
    return this.store.clear();
  }

  // 检查是否存在某个键
  has(key) {
    return this.store.has(key);
  }
}

module.exports = StoreUtil;

这个封装类不仅提供了基础的读写操作,还额外处理了日期类型的序列化与反序列化,以及检查和设置默认值的功能。
此外,它还允许传入一个加密密钥(encryptionKey),以便在需要时对存储的内容进行加密处理(这要求使用支持加密的electron-store版本或其他加密插件)。
通过这样的封装,你的应用程序可以更加灵活和安全地管理持久化数据。

在 Electron 应用中,如果你使用 electron-store 并希望自定义配置文件的存放位置,标准的 electron-store API 并不直接支持自定义路径。

但是,可以通过环境变量或在创建 Store 实例时利用 path 属性间接实现这一点。
不过,要注意这种方法可能需要你使用 electron-store 的低级别API或者扩展其功能,因为默认情况下路径是由 electron-store 管理的。

自定义路径的变通方案

修改环境变量

在启动 Electron 应用前,可以通过设置环境变量来改变 Electron 应用识别的用户主目录,进而间接影响配置文件的存储位置。例如,在 Node.js 中启动 Electron 时,可以通过修改 process.env.HOMEprocess.env.USERPROFILE(取决于操作系统)来改变默认的用户目录指向。但这会影响到应用的其他部分,可能不是最理想的解决方案。

扩展 electron-store

另一个更直接但相对复杂的方法是扩展 electron-store 类,手动指定配置文件的路径。这通常涉及到直接使用底层的 conf 库(electron-store 依赖的库)或创建一个新的 Store 类,如下所示:

const { Store } = require('electron-store');
const path = require('path');
const os = require('os');

class CustomStore extends Store {
  constructor(options) {
    const customPath = options.path || path.join(os.homedir(), '.myApp', 'config.json');
    super({
      ...options,
      cwd: path.dirname(customPath), // 设置当前工作目录
      configName: path.basename(customPath), // 设置配置文件名
    });
  }
}

module.exports = CustomStore;

然后,你可以像这样使用自定义路径的 CustomStore

const CustomStore = require('./customStore');
const store = new CustomStore({ path: './customLocation/myConfig.json' });

在这个例子中,你可以通过构造函数的 path 参数自定义配置文件的存放位置。请确保指定的路径是合法的并且应用有权限写入该位置。

注意事项

  • 自定义配置文件路径可能影响跨平台兼容性和用户体验,需谨慎处理。
  • 确保应用在没有写入权限的路径下不会崩溃,适当处理异常。
  • 考虑到隐私和安全,不要将配置文件放在共享或公共目录下。

采用上述方法,你可以灵活地控制配置文件的存储位置,以满足特定的应用需求。

// storeUtil.js
import Store from 'electron-store'
import path from 'path'
import os from 'os'

const customPath = path.join(os.homedir(), '.HikEMAS', 'HikEMAS_AppSettings.json')
class StoreUtil {
	constructor(configName = 'appSettings', defaults = {}, encryptionKey = null) {
		this.store = new Store({
			name: configName,
			encryptionKey, // 可选,提供一个密钥用于加密存储
			defaults,
			cwd: path.dirname(customPath), // 设置当前工作目录
			configName: path.basename(customPath), // 设置配置文件名
		})

		// 初始化默认值,确保所有默认设置已写入存储
		for (const key in defaults) {
			if (!this.store.has(key)) {
				this.set(key, defaults[key])
			}
		}
	}

	// 设置值,支持自动处理特殊类型如Date
	set(key, value) {
		if (value instanceof Date) {
			value = value.toISOString()
		}
		return this.store.set(key, value)
	}

	// 获取值,支持类型转换和默认值
	get(key, defaultValue = null) {
		const rawValue = this.store.get(key)
		if (rawValue === undefined && defaultValue !== null) {
			return defaultValue
		}

		try {
			if (
				typeof defaultValue === 'string' &&
				defaultValue.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z$/)
			) {
				return new Date(rawValue)
			}
		} catch (error) {
			console.error(`Error converting value for key ${key}:`, error)
		}

		return rawValue
	}

	// 删除键值对
	delete(key) {
		return this.store.delete(key)
	}

	// 清空所有存储
	clear() {
		return this.store.clear()
	}

	// 检查是否存在某个键
	has(key) {
		return this.store.has(key)
	}
}

export {StoreUtil}
posted @ 2024-04-29 20:58  龙陌  阅读(1202)  评论(0)    收藏  举报