alinea
Version:
Headless git-based CMS
105 lines (103 loc) • 2.87 kB
JavaScript
import {
pLimit
} from "../../chunks/chunk-C53YJRET.js";
import "../../chunks/chunk-NZLE2WMY.js";
// src/core/db/LocalDB.ts
import { Entry } from "../Entry.js";
import { MemorySource } from "../source/MemorySource.js";
import { syncWith } from "../source/Source.js";
import { sourceChanges } from "./CommitRequest.js";
import { EntryIndex } from "./EntryIndex.js";
import { EntryResolver } from "./EntryResolver.js";
import { EntryTransaction } from "./EntryTransaction.js";
import { WriteableGraph } from "./WriteableGraph.js";
var limit = pLimit(1);
var LocalDB = class extends WriteableGraph {
constructor(config, source = new MemorySource()) {
super();
this.config = config;
const index = new EntryIndex(config);
const resolver = new EntryResolver(config, index);
this.#resolver = resolver;
this.index = index;
this.source = source;
}
index;
source;
#resolver;
resolve(query) {
return this.#resolver.resolve(query);
}
get sha() {
return this.index.sha;
}
indexChanges(batch) {
return this.index.indexChanges(batch);
}
applyChanges(batch) {
return this.source.applyChanges(batch);
}
getTreeIfDifferent(sha) {
return this.source.getTreeIfDifferent(sha);
}
getBlobs(shas) {
return this.source.getBlobs(shas);
}
async sync() {
await this.index.syncWith(this.source);
await this.index.seed(this.source);
return this.sha;
}
syncWith(remote) {
return limit(async () => {
await syncWith(this.source, remote);
return this.sync();
});
}
async logEntries() {
const entries = await this.find({
select: {
id: Entry.id,
root: Entry.root,
workspace: Entry.workspace,
parentId: Entry.parentId,
locale: Entry.locale,
status: Entry.status,
path: Entry.path,
index: Entry.index,
title: Entry.title,
active: Entry.active
},
status: "all"
});
console.table(
entries.map(({ id, parentId, ...entry }) => {
return { id: id.slice(-7), parentId: parentId?.slice(-7), ...entry };
})
);
}
async request(mutations) {
await this.sync();
const from = await this.source.getTree();
const tx = new EntryTransaction(this.config, this.index, this.source, from);
tx.apply(mutations);
return tx.toRequest();
}
async mutate(mutations) {
const request = await this.request(mutations);
return this.write(request);
}
async write(request) {
if (this.sha === request.intoSha) return { sha: this.sha };
const contentChanges = sourceChanges(request);
await this.indexChanges(contentChanges);
await this.applyChanges(contentChanges);
return { sha: await this.sync() };
}
async prepareUpload(file) {
throw new Error("Uploads not supported on local DB");
}
};
export {
LocalDB
};