diff --git a/packages/sdk/src/helpers/config.ts b/packages/sdk/src/helpers/config.ts index 4d1934b6..99f8ed28 100644 --- a/packages/sdk/src/helpers/config.ts +++ b/packages/sdk/src/helpers/config.ts @@ -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(); + fs.writeFileSync(filepath, JSON.stringify({ ...current, ...nameMap })); }, }; } diff --git a/packages/sdk/test/aliases.test.ts b/packages/sdk/test/aliases.test.ts index 1e4ae2d6..385e3237 100644 --- a/packages/sdk/test/aliases.test.ts +++ b/packages/sdk/test/aliases.test.ts @@ -202,11 +202,6 @@ 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, @@ -214,6 +209,14 @@ describe("aliases", function () { 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); @@ -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); + }); }); });