@ismartify/arkstore
基于 arktype + Map 实现的带类型校验和安全设置功能的 KV 存储管理类。
ISmartifyArkStore 是 arktype 的 wrapper 类,提供了便捷的 KV 存储、类型校验和安全默认值功能。
特性
- 🔒 类型安全: 基于 arktype 提供强类型校验
- 🚀 高性能: 使用 Map 作为底层存储,提供 O(1) 的读写性能
- 🛡️ 安全设置: 支持 defaultValue 安全机制,验证失败时自动使用默认值
- 📝 灵活配置: 支持动态 schema 配置和验证规则
- 🎯 智能推断: 自动推断数据类型并生成对应的 schema
- 🔧 扩展性: 支持批量定义、对象扁平化等高级功能
- 💡 开发友好: 提供丰富的调试和监控功能
安装
pnpm add @ismartify/arkstore
npm install @ismartify/arkstore
yarn add @ismartify/arkstore
快速开始
import { ISmartifyArkStore } from '@ismartify/arkstore';
import { type } from 'arktype';
const store = new ISmartifyArkStore(type);
store.defineByKey('user.name', 'string');
store.defineByKey('user.email', 'string.email');
store.defineByKey('user.age', 'number');
store.define('user.name', 'string');
store.define('user.email', 'string.email');
store.define('user.age', 'number');
store.defineByKey('app.version', 'string');
store.defineByKey('app.debug', 'boolean');
store.defineByKey('config.timeout', 'number');
const sampleConfig = {
database: {
host: 'localhost',
port: 5432,
username: 'admin@example.com',
ssl: true
},
cache: {
redis: {
url: 'redis://localhost:6379',
ttl: 3600
}
}
};
store.defineByObject(sampleConfig);
store.set('user.name', 'John Doe');
store.set('user.email', 'john@example.com');
store.set('user.age', 30);
store.set({
'app.version': '1.0.0',
'app.debug': false,
'config.timeout': 5000
});
console.log(store.get('user.name'));
console.log(store.get('database.host'));
console.log(store.get('nonexistent', 'default'));
store.set('user.email', 'invalid-email', 'fallback@example.com');
console.log(store.get('user.email'));
API 参考
构造函数
new ISmartifyArkStore(type: typeof import('arktype').type)
参数说明:
type: typeof import('arktype').type - 必需参数,arktype 的 type 函数,用于后续的类型定义和校验
Schema 定义方法
defineByKey(key: string, schema: object | string) | 键名、schema定义 | this | 精确的键值对定义,支持点路径如 'a.b.c' |
define(schemaOrKey?: object | string, schema?: object | string) | 字符串或对象、可选schema | this | 智能判断参数类型,自动调用 defineByKey 或 defineByObject |
defineByObject(obj: object, schemaDefinition?: object | string, prefix?: string) | 对象、可选schema、可选前缀 | this | 基于对象结构自动推断并定义 schema |
isArkType(value: any) | 任意值 | boolean | 检查值是否为 arktype 类型 |
数据操作方法
get<T>(key: string, defaultValue?: T) | 键名、可选默认值 | T | undefined | 获取指定键的值,支持点路径 |
set(keyOrObject: string | object, value?: any, defaultValue?: any) | 键或对象、值、安全默认值 | this | 设置数据,支持安全默认值机制 |
has(key: string) | 键名 | boolean | 检查键是否存在 |
delete(key: string) | 键名 | boolean | 删除指定键 |
clear() | - | this | 清空所有数据(保留命名空间) |
遍历和统计方法
size() | - | number | 获取存储项数量(排除命名空间) |
keys() | - | IterableIterator<string> | 获取所有键的迭代器 |
values() | - | IterableIterator<any> | 获取所有值的迭代器 |
entries() | - | IterableIterator<[string, any]> | 获取所有键值对的迭代器 |
工具方法
raw() | - | {store: Map, schema: Map} | 获取底层存储和 schema 的原始访问 |
exec<T>(fn: (store: this) => T) | 回调函数 | T | 执行自定义函数并返回结果 |
debug() | - | this | 输出调试信息到控制台 |
高级用法
Schema 定义的三种方式
import { ISmartifyArkStore } from '@ismartify/arkstore';
import { type } from 'arktype';
const store = new ISmartifyArkStore(type);
store.defineByKey('user.name', 'string');
store.defineByKey('user.email', 'string.email');
store.defineByKey('user.age', 'number');
store.defineByKey('settings.theme', '"light" | "dark"');
store.defineByKey('app.config.database.connection.timeout', 'number');
store.defineByKey('user.profile.social.links.github', 'string');
store.define('user.name', 'string');
store.define('user.email', 'string.email');
const configTemplate = {
server: {
host: 'localhost',
port: 3000,
ssl: true
},
database: {
url: 'postgres://...',
maxConnections: 10
},
admin: {
email: 'admin@example.com'
}
};
store.define(configTemplate);
store.defineByObject(configTemplate);
store.defineByObject(configTemplate, 'string');
安全设置机制
store.define({
'user.email': 'string.email',
'user.age': 'number',
'user.active': 'boolean'
});
store.set('user.email', 'invalid-email', 'fallback@example.com');
console.log(store.get('user.email'));
store.set('user.email', 'valid@example.com', 'fallback@example.com');
console.log(store.get('user.email'));
store.set({
'user.email': 'invalid-email',
'user.age': 'not-a-number',
'user.active': true
}, 'safe-default');
console.log(store.get('user.email'));
console.log(store.get('user.age'));
console.log(store.get('user.active'));
try {
store.set('user.email', 'invalid', 'also-invalid');
} catch (error) {
console.error(error.message);
}
对象扁平化和嵌套设置
store.set({
user: {
profile: {
name: 'John Doe',
avatar: 'avatar.jpg'
},
settings: {
theme: 'dark',
notifications: {
email: true,
push: false
}
}
}
});
store.set({
'user.profile.name': 'John Doe',
'user.profile.avatar': 'avatar.jpg',
'user.settings.theme': 'dark',
'user.settings.notifications.email': true,
'user.settings.notifications.push': false
});
console.log(store.get('user.profile.name'));
console.log(store.get('user.settings.theme'));
console.log(store.get('user.settings.notifications.email'));
高级查询和操作
const userPreferences = store.exec((store) => {
const preferences = {};
const keys = Array.from(store.keys());
keys.filter(key => key.startsWith('user.preferences.'))
.forEach(key => {
const shortKey = key.replace('user.preferences.', '');
preferences[shortKey] = store.get(key);
});
return preferences;
});
const databaseConfig = store.exec((store) => {
const config = {};
Array.from(store.keys())
.filter(key => key.startsWith('database.'))
.forEach(key => {
const configKey = key.replace('database.', '');
config[configKey] = store.get(key);
});
return config;
});
const cleanupResult = store.exec((store) => {
const keys = Array.from(store.keys());
let deletedCount = 0;
keys.forEach(key => {
const value = store.get(key);
if (value === null || value === undefined || value === '') {
store.delete(key);
deletedCount++;
}
});
return { deletedCount, remainingCount: store.size() };
});
调试和监控
store.debug();
const { store: rawStore, schema: rawSchema } = store.raw();
console.log('原始存储大小:', rawStore.size);
console.log('定义的 schema 数量:', rawSchema.size);
for (const [key, value] of store.entries()) {
console.log(`${key}: ${JSON.stringify(value)}`);
}
console.log('存储项数量:', store.size());
console.log('所有键:', Array.from(store.keys()));
console.log('所有值:', Array.from(store.values()));
类型定义
export class ISmartifyArkStore {
constructor(arktype: any);
defineByKey(key: string, schema: object | string): this;
define(schemaOrKey?: object | string, schema?: object | string): this;
defineByObject(obj: object, schemaDefinition?: object | string, prefix?: string): this;
isArkType(value: any): boolean;
get<T = any>(key: string, defaultValue?: T): T | undefined;
set(keyOrObject: string | object, value?: any, defaultValue?: any): this;
has(key: string): boolean;
delete(key: string): boolean;
clear(): this;
size(): number;
keys(): IterableIterator<string>;
values(): IterableIterator<any>;
entries(): IterableIterator<[string, any]>;
raw(): { store: Map<string, any>, schema: Map<string, any> };
exec<T>(fn: (store: this) => T): T;
debug(): this;
}
自动推断的类型映射
string | 'string' | 'hello' → 'string' |
string (包含@和.) | 'string.email' | 'user@example.com' → 'string.email' |
number | 'number' | 42 → 'number' |
boolean | 'boolean' | true → 'boolean' |
Array<T> | 'T[]' | [1, 2, 3] → 'number[]' |
Array<unknown> | 'unknown[]' | [] → 'unknown[]' |
其他 | 'unknown' | null → 'unknown' |
最佳实践
1. Schema 定义策略
store.defineByKey('user.email', 'string.email');
store.defineByKey('app.config.timeout', 'number');
store.defineByKey('deep.nested.path.value', 'string');
store.define('user.email', 'string.email');
store.define(configObject);
const template = { user: { name: 'John', age: 30 } };
store.defineByObject(template);
store.set('user.profile.complex', someComplexObject);
2. 安全设置使用
store.defineByKey('config.timeout', 'number');
store.set('config.timeout', userInput, 5000);
store.defineByKey('user.email', 'string.email');
store.set('user.email', userEmail, 'noreply@example.com');
store.defineByKey('user.age', 'number');
store.set('user.age', 'invalid', 18);
store.set('user.age', 'invalid', 'also-invalid');
3. 性能优化
store.defineByKey('user.name', 'string');
store.defineByKey('user.email', 'string.email');
store.defineByKey('user.age', 'number');
store.set({
'user.name': 'John',
'user.email': 'john@example.com',
'user.age': 30
});
store.set('user.name', 'John');
store.set('user.email', 'john@example.com');
store.set('user.age', 30);
许可证
ISC
作者
iSmartify Team - ben.zeng@keepdb.com