Skip to content

Commit

Permalink
Merge pull request #381 from ujjwalguptaofficial/ug-fix-primarykey-wi…
Browse files Browse the repository at this point in the history
…th-keypath

Ug fix primarykey with keypath
  • Loading branch information
ujjwalguptaofficial authored Nov 1, 2024
2 parents 55f7119 + 52014a1 commit 4cf3d69
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/worker/idbutil/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ export class IDBUtil {
const storeNames = upgradeConnection.objectStoreNames;
const createObjectStore = (table: TableMeta) => {
const option: IDBObjectStoreParameters = table.primaryKey ? {
keyPath: table.primaryKey
keyPath: table.keypath,
} : {
autoIncrement: true
}
autoIncrement: true
}

const store = upgradeConnection.createObjectStore(table.name, option);
table.columns.forEach(column => {
Expand Down Expand Up @@ -183,4 +183,4 @@ export class IDBUtil {
}
return promise<InitDbResult>(initLogic)
}
}
}
4 changes: 3 additions & 1 deletion src/worker/model/table_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class TableMeta {
primaryKey: string;
autoIncColumnValue = {};
alter?: IAlterQuery;
keypath: string | string[];

constructor(table: ITable) {
this.columns = this.setColumn(table.columns);
Expand All @@ -23,6 +24,7 @@ export class TableMeta {
}
if (column.primaryKey) {
this.primaryKey = columnName;
this.keypath = column.keyPath || columnName;
}
column.enableSearch = column.enableSearch == null ? true : column.enableSearch;
const existingColumnIndex = this.columns.indexOf(q => q.name === columnName);
Expand All @@ -38,4 +40,4 @@ export class TableMeta {
}


}
}
79 changes: 77 additions & 2 deletions test/cases/column_option/key_path.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,58 @@ describe('keyPath test', function () {
});
})

it('insert metingen v2', function (done) {
con.insert({
into: 'metingenV2',
values: metingenV2Values
}).then(function (results) {
expect(results).to.be.an('number').to.equal(1);
done();
}).
catch(function (err) {
done(err);
});
})

it('check metingenv2 unique', function (done) {
con.insert({
into: 'metingen',
values: metingenValues
}).then(function (results) {
expect(results).to.be.an('number').to.equal(1);
done();
}).catch(function (err) {
var error = { "message": "Key already exists in the object store.", "type": "ConstraintError" };
expect(err).to.be.an('object').to.haveOwnProperty('type').equal('ConstraintError')
done();
});
});

it('select all value from metigenv2', function (done) {
con.select({
from: 'metingenV2'
}).then(function (results) {
expect(results).to.be.an('array').length(1);
done();
}).catch(function (err) {
done(err);
});
});

it('select all value from metigenv2 with where keypath', function (done) {
const value = metingenV2Values[0];
con.select({
from: 'metingenV2',
where: {
unique: [value.userID, value.date, value.time]
}
}).then(function (results) {
expect(results).to.be.an('array').length(1);
done();
}).catch(function (err) {
done(err);
});
});

it('drop db pincodes', function (done) {
con.dropDb().then(function () {
Expand Down Expand Up @@ -147,9 +199,27 @@ describe('keyPath test', function () {

}
};
// for testing keypath and primary key together
var metingenV2 = {
name: 'metingenV2',
columns: {
id: { autoIncrement: true },
userID: { notNull: true, dataType: 'number' },
date: { notNull: true, dataType: 'string' },
time: { notNull: true, dataType: 'string' },
unique: {
keyPath: ['userID', 'date', 'time'],
// unique: true,
primaryKey: true,
}
},
alter: {

}
};
var database = {
name: 'pinCodeDetails',
tables: [table, metingen]
tables: [table, metingen, metingenV2]
}
return database;
}
Expand All @@ -158,6 +228,11 @@ describe('keyPath test', function () {
userID: 1,
date: new Date().toString(),
time: new Date().getTime().toString()
}]
}];
var metingenV2Values = [{
userID: 1,
date: new Date().toString(),
time: new Date().getTime().toString()
}];
})

0 comments on commit 4cf3d69

Please sign in to comment.