From 4dcc8b720d6e9cf676d9c6e3865c46eb9a783051 Mon Sep 17 00:00:00 2001 From: timwis Date: Sat, 11 Jan 2020 17:11:03 +0000 Subject: [PATCH 1/2] Add upsert and save transaction proof of concept Supports #33 --- backend/lib/GitSheets.js | 33 +++++++++++++++++++++++++++++++++ backend/test/GitSheets.spec.js | 26 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/backend/lib/GitSheets.js b/backend/lib/GitSheets.js index c9c42ae..cbb1920 100644 --- a/backend/lib/GitSheets.js +++ b/backend/lib/GitSheets.js @@ -265,6 +265,39 @@ module.exports = class GitSheets { await this.git.branch({D: true}, dstRef); // force delete in case srcRef is not checked out } + async createTransaction (parentRef) { + const treeObject = await this._createTreeFromRef(parentRef); + const gitSheets = this; + + // TODO: Use a class or something, rather than generating this code each transaction + return { + upsert: function upsert (path, data) { + const contents = gitSheets._serialize(data); + // TODO: Wrap errors + return treeObject.writeChild(`${path}.toml`, contents); + }, + async save (saveToBranch = parentRef) { + // TODO: Wrap errors + const treeHash = await treeObject.write(); + + if (saveToBranch && saveToBranch === parentRef) { // TODO: check if branch exists instead + await gitSheets._saveTreeToExistingBranch({ + treeHash, + branch: saveToBranch, + msg: 'import to existing branch', + }); + } else if (saveToBranch) { + await gitSheets._saveTreeToNewBranch({ + treeHash, + parentRef, + branch: saveToBranch, + msg: 'import to new branch', + }); + } + }, + }; + } + _isDataBlob ([key, blob]) { return !key.startsWith('.gitsheets/') && key.endsWith('.toml') && blob instanceof BlobObject; } diff --git a/backend/test/GitSheets.spec.js b/backend/test/GitSheets.spec.js index 0817cde..16797ed 100644 --- a/backend/test/GitSheets.spec.js +++ b/backend/test/GitSheets.spec.js @@ -357,6 +357,32 @@ describe('GitSheets lib', () => { .toThrow(MergeError); }); }); + + describe.only('Transaction', () => { + it('upserts on top of branch', async () => { + const txn = await gitSheets.createTransaction('master'); + await txn.upsert('1', sampleData.initial[0]); + await txn.upsert('2', sampleData.initial[1]); + await txn.save(); + + const response = await gitSheets.git.lsTree('master'); + const tree = parseTree(response); + + const blobs = tree.filter((item) => item.type === 'blob'); + expect(blobs.length).toBe(2); + + await Promise.all(blobs.map(verifyBlob)); + + async function verifyBlob ({ hash, file }) { + const sampleDataItem = sampleData.initial.find((item) => item.id === file.substr(0, file.length - 5)); + expect(sampleDataItem).toBeDefined(); + + const contents = await gitSheets.git.catFile('blob', hash); + const data = gitSheets._deserialize(contents); + expect(data).toEqual(sampleDataItem); + } + }) + }) }); function parseTree (treeOutput) { From 7b110bb71494c12ec54d55039e02a5b3194ca1cb Mon Sep 17 00:00:00 2001 From: timwis Date: Sat, 11 Jan 2020 17:17:02 +0000 Subject: [PATCH 2/2] Compute path from path template in transaction --- backend/lib/GitSheets.js | 4 +++- backend/test/GitSheets.spec.js | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/backend/lib/GitSheets.js b/backend/lib/GitSheets.js index cbb1920..3a6adf3 100644 --- a/backend/lib/GitSheets.js +++ b/backend/lib/GitSheets.js @@ -266,12 +266,14 @@ module.exports = class GitSheets { } async createTransaction (parentRef) { + const pathTemplate = await this.getConfigItem(parentRef, 'path'); const treeObject = await this._createTreeFromRef(parentRef); const gitSheets = this; // TODO: Use a class or something, rather than generating this code each transaction return { - upsert: function upsert (path, data) { + upsert: function upsert (data) { + const path = gitSheets._renderTemplate(pathTemplate, data); const contents = gitSheets._serialize(data); // TODO: Wrap errors return treeObject.writeChild(`${path}.toml`, contents); diff --git a/backend/test/GitSheets.spec.js b/backend/test/GitSheets.spec.js index 16797ed..12ec662 100644 --- a/backend/test/GitSheets.spec.js +++ b/backend/test/GitSheets.spec.js @@ -360,9 +360,11 @@ describe('GitSheets lib', () => { describe.only('Transaction', () => { it('upserts on top of branch', async () => { + await gitSheets.setConfigItem('master', 'path', '{{id}}'); + const txn = await gitSheets.createTransaction('master'); - await txn.upsert('1', sampleData.initial[0]); - await txn.upsert('2', sampleData.initial[1]); + await txn.upsert(sampleData.initial[0]); + await txn.upsert(sampleData.initial[1]); await txn.save(); const response = await gitSheets.git.lsTree('master');