Initial commit
This commit is contained in:
94
shared/resources.js
Normal file
94
shared/resources.js
Normal file
@@ -0,0 +1,94 @@
|
||||
const path = require('path');
|
||||
const { getResourceDir, resourceTypeToDirName } = require('./paths');
|
||||
const { ensureDir, listJsonFiles, readJsonIfExists, writeJson } = require('./fs');
|
||||
|
||||
function resourcePath(resourceType, resourceId) {
|
||||
return path.join(getResourceDir(resourceType), `${resourceId}.json`);
|
||||
}
|
||||
|
||||
function nowIso() {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
function createResourceDocument({
|
||||
id,
|
||||
resource_type,
|
||||
desired_state,
|
||||
current_state,
|
||||
last_applied_state,
|
||||
metadata,
|
||||
created_at,
|
||||
updated_at,
|
||||
}) {
|
||||
const timestamp = nowIso();
|
||||
|
||||
return {
|
||||
id,
|
||||
resource_type,
|
||||
schema_version: 'v1',
|
||||
desired_state: desired_state || {},
|
||||
current_state: current_state || {},
|
||||
last_applied_state: last_applied_state || {},
|
||||
metadata: metadata || {},
|
||||
created_at: created_at || timestamp,
|
||||
updated_at: updated_at || timestamp,
|
||||
};
|
||||
}
|
||||
|
||||
async function getResource(resourceType, resourceId) {
|
||||
return readJsonIfExists(resourcePath(resourceType, resourceId));
|
||||
}
|
||||
|
||||
async function saveResource(document) {
|
||||
const existing = await getResource(document.resource_type, document.id);
|
||||
const next = createResourceDocument({
|
||||
...document,
|
||||
created_at: existing ? existing.created_at : document.created_at,
|
||||
updated_at: nowIso(),
|
||||
});
|
||||
|
||||
await writeJson(resourcePath(next.resource_type, next.id), next);
|
||||
return next;
|
||||
}
|
||||
|
||||
async function patchResourceState(resourceType, resourceId, patch) {
|
||||
const existing = await getResource(resourceType, resourceId);
|
||||
|
||||
if (!existing) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return saveResource({
|
||||
...existing,
|
||||
desired_state: patch.desired_state || existing.desired_state,
|
||||
current_state: patch.current_state || existing.current_state,
|
||||
last_applied_state: patch.last_applied_state || existing.last_applied_state,
|
||||
metadata: patch.metadata || existing.metadata,
|
||||
});
|
||||
}
|
||||
|
||||
async function listResources(resourceType) {
|
||||
const files = await listJsonFiles(getResourceDir(resourceType));
|
||||
return Promise.all(files.map((filePath) => readJsonIfExists(filePath)));
|
||||
}
|
||||
|
||||
async function listAllResources() {
|
||||
const result = {};
|
||||
|
||||
for (const resourceType of Object.keys(resourceTypeToDirName)) {
|
||||
await ensureDir(getResourceDir(resourceType));
|
||||
result[resourceType] = await listResources(resourceType);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createResourceDocument,
|
||||
getResource,
|
||||
saveResource,
|
||||
patchResourceState,
|
||||
listResources,
|
||||
listAllResources,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user