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

(sdk) Add alias to file instead of overwriting #60

Merged
merged 1 commit into from
Oct 24, 2023
Merged
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
3 changes: 2 additions & 1 deletion packages/sdk/src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ export function jsonFileAliases(filepath: string): AliasesNameMap {
},
write: async function (nameMap: NameMapping) {
const fs = await getFsModule();
fs.writeFileSync(filepath, JSON.stringify(nameMap));
const current = await this.read();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is really just this simple change. We should have done it this way from the start.
One note is that if you create two tables with the same name the new table will replace the old table, which seems correct to me.

fs.writeFileSync(filepath, JSON.stringify({ ...current, ...nameMap }));
},
};
}
Expand Down
37 changes: 32 additions & 5 deletions packages/sdk/test/aliases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,21 @@ describe("aliases", function () {
try {
fs.mkdirSync(aliasesDir);
} catch (err) {}
// reset the aliases file, and ensure the helper
// creates the file if it doesn't exist
try {
fs.unlinkSync(aliasesFile);
} catch (err) {}

const db = new Database({
signer,
// use the built-in SDK helper to setup and manage json aliases files
aliases: jsonFileAliases(aliasesFile),
});

beforeEach(function () {
// reset the aliases file, and ensure the helper
// creates the file if it doesn't exist
try {
fs.unlinkSync(aliasesFile);
} catch (err) {}
});

this.afterAll(function () {
try {
fs.unlinkSync(aliasesFile);
Expand All @@ -231,5 +234,29 @@ describe("aliases", function () {

strictEqual(nameMap[tablePrefix], uuTableName);
});

test("running create statement updates existing aliases", async function () {
const tablePrefix1 = "json_aliases_table1";
const tablePrefix2 = "json_aliases_table2";
const { meta: meta1 } = await db
.prepare(`CREATE TABLE ${tablePrefix1} (counter int, info text);`)
.all();

const uuTableName1 = meta1.txn?.name ?? "";
const nameMap1 = (await db.config.aliases?.read()) ?? {};

strictEqual(nameMap1[tablePrefix1], uuTableName1);
strictEqual(nameMap1[tablePrefix2], undefined);

const { meta: meta2 } = await db
.prepare(`CREATE TABLE ${tablePrefix2} (counter int, info text);`)
.all();

const uuTableName2 = meta2.txn?.name ?? "";
const nameMap2 = (await db.config.aliases?.read()) ?? {};

strictEqual(nameMap2[tablePrefix1], uuTableName1);
strictEqual(nameMap2[tablePrefix2], uuTableName2);
});
});
});
Loading