Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/transactions #38

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions backend/lib/GitSheets.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,41 @@ module.exports = class GitSheets {
await this.git.branch({D: true}, dstRef); // force delete in case srcRef is not checked out
}

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 (data) {
const path = gitSheets._renderTemplate(pathTemplate, 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;
}
Expand Down
28 changes: 28 additions & 0 deletions backend/test/GitSheets.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,34 @@ describe('GitSheets lib', () => {
.toThrow(MergeError);
});
});

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(sampleData.initial[0]);
await txn.upsert(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) {
Expand Down