Skip to content

Commit

Permalink
Sets the unique flag for primKey to true
Browse files Browse the repository at this point in the history
Additional changes and tests
  • Loading branch information
Stretsh authored and dfahlander committed Jan 18, 2024
1 parent 558b846 commit cc6e262
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/classes/version/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class Version implements IVersion {
if (stores[tableName] !== null) {
var indexes = parseIndexSyntax(stores[tableName]);
var primKey = indexes.shift();
primKey.unique = true;
if (primKey.multi) throw new exceptions.Schema("Primary key cannot be multi-valued");
indexes.forEach(idx => {
if (idx.auto) throw new exceptions.Schema("Only primary key can be marked as autoIncrement (++)");
Expand Down
28 changes: 28 additions & 0 deletions test/tests-open.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,31 @@ promisedTest("Should be possible to open a vip DB", async ()=>{
await db.vip.table('foobar').toArray();
ok(true, "Could query dynamically opened viped db");
});

promisedTest("#1842 - Should set the unique flag for primKey to true", async () => {
// First test if flag is correctly set when creating new store
// version.ts, _parseStoresSpec()
await Dexie.delete("PrimKey1842");
let db = new Dexie("PrimKey1842");
db.version(1).stores({ foo: "++id, name"});
await db.open();
ok(true, "Could open new db");
const primKey = db.foo.schema.primKey.unique;
ok(primKey, "primKey should be unique");

// Closing and reopening db, to test if flag is correctly set when opening existing store
// schema-helpers.ts, buildGlobalSchema()
await db.close();
db = new Dexie("PrimKey1842");
await db.open();
ok(true, "Could open db after close");
const fooTable = await db.table("foo");
const primKeyAfterCloseOpen = fooTable.schema.primKey.unique;
ok(primKeyAfterCloseOpen, "primKey should be unique after close and then open");

// Cleanup
db.close();
await Dexie.delete("PrimKey1842");
const databases = await Dexie.getDatabaseNames();
ok(!databases.includes("PrimKey1842"), "'PrimKey1842' should NOT be in the list of database names");
})

0 comments on commit cc6e262

Please sign in to comment.