-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(repository): allow deletedBy id to be configurable using class p…
…rotected property BREAKING CHANGE: change approach of deletedById key provider gh-99
- Loading branch information
Samarpan Bhattacharya
authored and
Samarpan Bhattacharya
committed
Nov 6, 2022
1 parent
1be1dc4
commit 28de1cd
Showing
7 changed files
with
477 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import {expect} from '@loopback/testlab'; | |
|
||
import {Constructor, Getter} from '@loopback/context'; | ||
import { | ||
DefaultCrudRepository, | ||
DefaultTransactionalRepository, | ||
Entity, | ||
EntityNotFoundError, | ||
|
@@ -32,6 +33,16 @@ class Customer extends SoftDeleteEntity { | |
email: string; | ||
} | ||
|
||
@model() | ||
class Customer2 extends SoftDeleteEntity { | ||
@property({ | ||
id: true, | ||
}) | ||
id: number; | ||
@property() | ||
email: string; | ||
} | ||
|
||
class CustomerCrudRepo extends SoftCrudRepositoryMixin< | ||
Customer, | ||
typeof Customer.prototype.id, | ||
|
@@ -51,19 +62,45 @@ class CustomerCrudRepo extends SoftCrudRepositoryMixin< | |
} | ||
} | ||
|
||
class Customer2CrudRepo extends SoftCrudRepositoryMixin< | ||
Customer, | ||
typeof Customer.prototype.id, | ||
Constructor< | ||
DefaultCrudRepository<Customer, typeof Customer.prototype.id, {}> | ||
>, | ||
{} | ||
>(DefaultCrudRepository) { | ||
constructor( | ||
entityClass: typeof Entity & { | ||
prototype: Customer; | ||
}, | ||
dataSource: juggler.DataSource, | ||
readonly getCurrentUser: Getter<IAuthUser | undefined>, | ||
readonly deletedByIdKey: string = 'id', | ||
) { | ||
super(entityClass, dataSource, getCurrentUser); | ||
} | ||
} | ||
|
||
describe('SoftCrudRepositoryMixin', () => { | ||
let repo: CustomerCrudRepo; | ||
let repoWithCustomDeletedByKey: Customer2CrudRepo; | ||
const userData = { | ||
id: '1', | ||
username: 'test', | ||
}; | ||
|
||
before(() => { | ||
const ds: juggler.DataSource = new juggler.DataSource({ | ||
name: 'db', | ||
connector: 'memory', | ||
}); | ||
repo = new CustomerCrudRepo(Customer, ds, () => | ||
Promise.resolve({ | ||
id: '1', | ||
username: 'test', | ||
}), | ||
repo = new CustomerCrudRepo(Customer, ds, () => Promise.resolve(userData)); | ||
repoWithCustomDeletedByKey = new Customer2CrudRepo( | ||
Customer2, | ||
ds, | ||
() => Promise.resolve(userData), | ||
'username', | ||
); | ||
}); | ||
|
||
|
@@ -504,6 +541,56 @@ describe('SoftCrudRepositoryMixin', () => { | |
}); | ||
}); | ||
|
||
describe('deleteById', () => { | ||
beforeEach(setupTestData); | ||
afterEach(clearTestData); | ||
|
||
it('should soft delete entries', async () => { | ||
await repo.deleteById(1); | ||
try { | ||
await repo.findById(1); | ||
fail(); | ||
} catch (e) { | ||
expect(e.message).to.be.equal('EntityNotFound'); | ||
} | ||
const afterDeleteIncludeSoftDeleted = | ||
await repo.findByIdIncludeSoftDelete(1); | ||
expect(afterDeleteIncludeSoftDeleted) | ||
.to.have.property('email') | ||
.equal('[email protected]'); | ||
}); | ||
|
||
it('should soft delete entries with deletedBy set to id', async () => { | ||
await repo.deleteById(1); | ||
try { | ||
await repo.findById(1); | ||
fail(); | ||
} catch (e) { | ||
expect(e.message).to.be.equal('EntityNotFound'); | ||
} | ||
const afterDeleteIncludeSoftDeleted = | ||
await repo.findByIdIncludeSoftDelete(1); | ||
expect(afterDeleteIncludeSoftDeleted) | ||
.to.have.property('deletedBy') | ||
.equal(userData.id); | ||
}); | ||
|
||
it('should soft delete entries with deletedBy set to custom key provided', async () => { | ||
await repoWithCustomDeletedByKey.deleteById(1); | ||
try { | ||
await repoWithCustomDeletedByKey.findById(1); | ||
fail(); | ||
} catch (e) { | ||
expect(e.message).to.be.equal('EntityNotFound'); | ||
} | ||
const afterDeleteIncludeSoftDeleted = | ||
await repoWithCustomDeletedByKey.findByIdIncludeSoftDelete(1); | ||
expect(afterDeleteIncludeSoftDeleted) | ||
.to.have.property('deletedBy') | ||
.equal(userData.username); | ||
}); | ||
}); | ||
|
||
describe('delete', () => { | ||
beforeEach(setupTestData); | ||
afterEach(clearTestData); | ||
|
@@ -522,18 +609,73 @@ describe('SoftCrudRepositoryMixin', () => { | |
.to.have.property('email') | ||
.equal('[email protected]'); | ||
}); | ||
|
||
it('should soft delete entries with deletedBy set to id', async () => { | ||
const entity = await repo.findById(1); | ||
await repo.delete(entity); | ||
try { | ||
await repo.findById(1); | ||
fail(); | ||
} catch (e) { | ||
expect(e.message).to.be.equal('EntityNotFound'); | ||
} | ||
const afterDeleteIncludeSoftDeleted = | ||
await repo.findByIdIncludeSoftDelete(1); | ||
expect(afterDeleteIncludeSoftDeleted) | ||
.to.have.property('deletedBy') | ||
.equal(userData.id); | ||
}); | ||
|
||
it('should soft delete entries with deletedBy set to custom key provided', async () => { | ||
const entity = await repoWithCustomDeletedByKey.findById(1); | ||
await repoWithCustomDeletedByKey.delete(entity); | ||
try { | ||
await repoWithCustomDeletedByKey.findById(1); | ||
fail(); | ||
} catch (e) { | ||
expect(e.message).to.be.equal('EntityNotFound'); | ||
} | ||
const afterDeleteIncludeSoftDeleted = | ||
await repoWithCustomDeletedByKey.findByIdIncludeSoftDelete(1); | ||
expect(afterDeleteIncludeSoftDeleted) | ||
.to.have.property('deletedBy') | ||
.equal(userData.username); | ||
}); | ||
}); | ||
|
||
describe('deleteAll', () => { | ||
beforeEach(setupTestData); | ||
afterEach(clearTestData); | ||
|
||
it('should soft delete all entries', async () => { | ||
await repo.deleteAll(); | ||
const customers = await repo.find(); | ||
expect(customers).to.have.length(0); | ||
const afterDeleteAll = await repo.findAll(); | ||
expect(afterDeleteAll).to.have.length(4); | ||
}); | ||
|
||
it('should soft delete entries with deletedBy set to id', async () => { | ||
await repo.deleteAll(); | ||
const customers = await repo.find(); | ||
expect(customers).to.have.length(0); | ||
const afterDeleteAll = await repo.findAll(); | ||
expect(afterDeleteAll).to.have.length(4); | ||
afterDeleteAll.forEach((rec) => { | ||
expect(rec).to.have.property('deletedBy').equal(userData.id); | ||
}); | ||
}); | ||
|
||
it('should soft delete entries with deletedBy set to custom key provided', async () => { | ||
await repoWithCustomDeletedByKey.deleteAll(); | ||
const customers = await repoWithCustomDeletedByKey.find(); | ||
expect(customers).to.have.length(0); | ||
const afterDeleteAll = await repoWithCustomDeletedByKey.findAll(); | ||
expect(afterDeleteAll).to.have.length(4); | ||
afterDeleteAll.forEach((rec) => { | ||
expect(rec).to.have.property('deletedBy').equal(userData.username); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('deleteHard', () => { | ||
|
@@ -571,9 +713,19 @@ describe('SoftCrudRepositoryMixin', () => { | |
await repo.create({id: 3, email: '[email protected]'}); | ||
await repo.create({id: 4, email: '[email protected]'}); | ||
await repo.deleteById(3); | ||
|
||
await repoWithCustomDeletedByKey.create({id: 1, email: '[email protected]'}); | ||
await repoWithCustomDeletedByKey.create({id: 2, email: '[email protected]'}); | ||
await repoWithCustomDeletedByKey.create({ | ||
id: 3, | ||
email: '[email protected]', | ||
}); | ||
await repoWithCustomDeletedByKey.create({id: 4, email: '[email protected]'}); | ||
await repoWithCustomDeletedByKey.deleteById(3); | ||
} | ||
|
||
async function clearTestData() { | ||
await repo.deleteAllHard(); | ||
await repoWithCustomDeletedByKey.deleteAllHard(); | ||
} | ||
}); |
Oops, something went wrong.