-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmemory-loader.ts
More file actions
116 lines (101 loc) · 2.61 KB
/
memory-loader.ts
File metadata and controls
116 lines (101 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* Memory Metadata Loader
*
* Stores metadata in memory only. Changes are lost when process restarts.
* Useful for testing, temporary overrides, or "dirty" edits.
*/
import type {
MetadataLoadOptions,
MetadataLoadResult,
MetadataStats,
MetadataLoaderContract,
MetadataSaveOptions,
MetadataSaveResult,
} from '@objectstack/spec/system';
import type { MetadataLoader } from './loader-interface.js';
export class MemoryLoader implements MetadataLoader {
readonly contract: MetadataLoaderContract = {
name: 'memory',
protocol: 'memory:',
capabilities: {
read: true,
write: true,
watch: false,
list: true,
},
};
// Storage: Type -> Name -> Data
private storage = new Map<string, Map<string, any>>();
async load(
type: string,
name: string,
_options?: MetadataLoadOptions
): Promise<MetadataLoadResult> {
const typeStore = this.storage.get(type);
const data = typeStore?.get(name);
if (data) {
return {
data,
source: 'memory',
format: 'json',
loadTime: 0,
};
}
return { data: null };
}
async loadMany<T = any>(
type: string,
_options?: MetadataLoadOptions
): Promise<T[]> {
const typeStore = this.storage.get(type);
if (!typeStore) return [];
return Array.from(typeStore.values()) as T[];
}
async exists(type: string, name: string): Promise<boolean> {
return this.storage.get(type)?.has(name) ?? false;
}
async stat(type: string, name: string): Promise<MetadataStats | null> {
if (await this.exists(type, name)) {
return {
size: 0, // In-memory
mtime: new Date().toISOString(),
format: 'json',
};
}
return null;
}
async list(type: string): Promise<string[]> {
const typeStore = this.storage.get(type);
if (!typeStore) return [];
return Array.from(typeStore.keys());
}
async save(
type: string,
name: string,
data: any,
_options?: MetadataSaveOptions
): Promise<MetadataSaveResult> {
if (!this.storage.has(type)) {
this.storage.set(type, new Map());
}
this.storage.get(type)!.set(name, data);
return {
success: true,
path: `memory://${type}/${name}`,
saveTime: 0,
};
}
/**
* Delete a metadata item from memory storage
*/
async delete(type: string, name: string): Promise<void> {
const typeStore = this.storage.get(type);
if (typeStore) {
typeStore.delete(name);
if (typeStore.size === 0) {
this.storage.delete(type);
}
}
}
}