From b614360c8a316e0933122ac1e3a631c0d0773a80 Mon Sep 17 00:00:00 2001 From: Kevin J Hoerr Date: Sat, 7 Dec 2019 19:50:16 -0500 Subject: Add Metadata core with MongoDB for persistence With the new process dependency, process handling has been added to ensure that the ExpressJS server and MongoDB client connections get closed up properly. As noted in the Metadata file above the Branch interface, the schema is definitely not finalized. Eventually metadata will be needed at the repo level anyways, so reorganizing the document schema is high on the priority list. --- src/metadata.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/metadata.ts (limited to 'src/metadata.ts') diff --git a/src/metadata.ts b/src/metadata.ts new file mode 100644 index 0000000..1eb7f1a --- /dev/null +++ b/src/metadata.ts @@ -0,0 +1,54 @@ +import { Db } from "mongodb"; + +/** //FIXME fix document schema + * Rather than using branches as the core, this should be adopted into the following document model: + * repo: + * - org + * - name + * - token + * - branches: { + * [branchname]: { + * head + * } + * } + */ +export interface Branch { + org: string; + repo: string; + name: string; + head: string; +} + +class Metadata { + database: Db; + + constructor(client: Db) { + this.database = client; + } + + async getHeadCommit( + org: string, + repo: string, + branch: string + ): Promise { + const result = await this.database + .collection("branch") + .findOne({ org, repo, name: branch }); + + if (result !== null) { + return result.head; + } else { + throw Error("Branch not found"); + } + } + + async updateBranch(branch: Branch): Promise { + const { head, ...matcher } = branch; + const { result } = await this.database + .collection("branch") + .replaceOne(matcher, branch, { upsert: true }); + return result.ok === 1; + } +} + +export default Metadata; -- cgit