aboutsummaryrefslogtreecommitdiff
path: root/src/metadata.ts
diff options
context:
space:
mode:
authorKevin J Hoerr <kjhoerr@protonmail.com>2019-12-07 19:50:16 -0500
committerKevin J Hoerr <kjhoerr@protonmail.com>2019-12-07 19:50:16 -0500
commitb614360c8a316e0933122ac1e3a631c0d0773a80 (patch)
tree71ac2c0064d02cafcf53c3d63f3d02220275c63e /src/metadata.ts
parentbd041baf0f3c9af7b331becc4c982ce5e835c054 (diff)
downloadao-coverage-b614360c8a316e0933122ac1e3a631c0d0773a80.tar.gz
ao-coverage-b614360c8a316e0933122ac1e3a631c0d0773a80.tar.bz2
ao-coverage-b614360c8a316e0933122ac1e3a631c0d0773a80.zip
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.
Diffstat (limited to 'src/metadata.ts')
-rw-r--r--src/metadata.ts54
1 files changed, 54 insertions, 0 deletions
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<string> {
+ const result = await this.database
+ .collection<Branch>("branch")
+ .findOne({ org, repo, name: branch });
+
+ if (result !== null) {
+ return result.head;
+ } else {
+ throw Error("Branch not found");
+ }
+ }
+
+ async updateBranch(branch: Branch): Promise<boolean> {
+ const { head, ...matcher } = branch;
+ const { result } = await this.database
+ .collection<Branch>("branch")
+ .replaceOne(matcher, branch, { upsert: true });
+ return result.ok === 1;
+ }
+}
+
+export default Metadata;