0.7.9 • Published 4 months ago

@bleco/soft-delete v0.7.9

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

@bleco/soft-delete

LoopBack

A loopback-next extension for soft delete feature

This package is initial forked from loopback4-soft-delete

Install

npm install @bleco/soft-delete

NOTE - With latest version 3.0.0, you also need to install @bleco/authentication for using deleted_by feature added.

npm install @bleco/soft-delete

Quick Starter

For a quick starter guide, you can refer to our loopback 4 starter application which utilizes this package for soft-deletes in a multi-tenant application.

Transaction support

With version 3.0.0, transaction repository support has been added. In place of DefaultSoftCrudRepository, extend your repository with DefaultTransactionSoftCrudRepository. For further usage guidelines, refer below.

Usage

Right now, this extension exports three abstract classes which are actually helping with soft delete operations.

  • SoftDeleteEntity - An abstract base class for all models which require soft delete feature. This class is a wrapper over Entity class from @loopback/repository adding three attributes to the model class for handling soft-delete, namely, deleted, deletedAt, deletedBy. The column names needed to be there in DB within that table are - 'deleted', 'deleted_at', 'deleted_by'. If you are using auto-migration of loopback 4, then, you may not need to do anything specific to add this column. If not, then please add these columns to the DB table.
  • DefaultSoftCrudRepository - An abstract base class for all repositories which require soft delete feature. This class is going to be the one which handles soft delete operations and ensures soft deleted entries are not returned in responses, However if there is a need to query soft deleted entries as well,there is an options to achieve that and you can use findAll() in place of find() , findOneIncludeSoftDelete() in place of findOne() and findByIdIncludeSoftDelete() in place of findById(), these will give you the responses including soft deleted entries. This class is a wrapper over DefaultCrudRepository class from @loopback/repository.
  • DefaultTransactionSoftCrudRepository - An abstract base class for all repositories which require soft delete feature with transaction support. This class is going to be the one which handles soft delete operations and ensures soft deleted entries are not returned in responses, However if there is a need to query soft deleted entries as well,there is an options to achieve that and you can use findAll() in place of find() , findOneIncludeSoftDelete() in place of findOne() and findByIdIncludeSoftDelete() in place of findById(), these will give you the responses including soft deleted entries. This class is a wrapper over DefaultTransactionalRepository class from @loopback/repository.

In order to use this extension in your LB4 application, please follow below steps.

  1. Extend models with SoftDeleteEntity class replacing Entity. For example,
import {SoftDeleteEntity} from '@bleco/soft-delete';
import {model, property} from '@loopback/repository';

@model({
  name: 'users',
})
export class User extends SoftDeleteEntity {
  @property({
    type: 'number',
    id: true,
  })
  id?: number;

  // .... More properties
}
  1. Extend repositories with DefaultSoftCrudRepository class replacing DefaultCrudRepository. For example,
import {AuthenticationBindings, IAuthUser} from '@bleco/authentication';
import {DefaultSoftCrudRepository} from '@bleco/soft-delete';
import {Getter, inject} from '@loopback/core';

import {PgdbDataSource} from '../datasources';
import {User, UserRelations} from '../models';

export class UserRepository extends DefaultSoftCrudRepository<User, typeof User.prototype.id, UserRelations> {
  constructor(
    @inject('datasources.pgdb') dataSource: PgdbDataSource,
    @inject.getter(AuthenticationBindings.CURRENT_USER, {optional: true})
    protected readonly getCurrentUser: Getter<IAuthUser | undefined>,
  ) {
    super(User, dataSource, getCurrentUser);
  }
}
  1. For transaction support, extend repositories with DefaultTransactionSoftCrudRepository class replacing DefaultTransactionalRepository. For example,
import {AuthenticationBindings, IAuthUser} from '@bleco/authentication';
import {DefaultSoftCrudRepository} from '@bleco/soft-delete';
import {Getter, inject} from '@loopback/core';

import {PgdbDataSource} from '../datasources';
import {User, UserRelations} from '../models';

export class UserRepository extends DefaultTransactionSoftCrudRepository<
  User,
  typeof User.prototype.id,
  UserRelations
> {
  constructor(
    @inject('datasources.pgdb') dataSource: PgdbDataSource,
    @inject.getter(AuthenticationBindings.CURRENT_USER, {optional: true})
    protected readonly getCurrentUser: Getter<IAuthUser | undefined>,
  ) {
    super(User, dataSource, getCurrentUser);
  }
}

Feedback

If you've noticed a bug or have a question or have a feature request, search the issue tracker to see if someone else in the community has already created a ticket. If not, go ahead and make one! All feature requests are welcome. Implementation time may vary. Feel free to contribute the same, if you can. If you think this extension is useful, please star it. Appreciation really helps in keeping this project alive.

Contributing

Please read CONTRIBUTING.md for details on the process for submitting pull requests to us.

Code of conduct

Code of conduct guidelines here.

License

MIT

Credits

0.7.9

4 months ago

0.7.8

5 months ago

0.7.7

5 months ago

0.7.2

8 months ago

0.7.1

8 months ago

0.5.3

9 months ago

0.7.4

7 months ago

0.7.3

8 months ago

0.5.0

9 months ago

0.7.0

8 months ago

0.5.2

9 months ago

0.5.1

9 months ago

0.7.6

6 months ago

0.7.5

7 months ago

0.4.10

10 months ago

0.4.11

10 months ago

0.4.12

10 months ago

0.6.2

8 months ago

0.6.1

8 months ago

0.6.0

9 months ago

0.4.9

11 months ago

0.4.8

11 months ago

0.4.7

11 months ago

0.3.37

1 year ago

0.4.5

12 months ago

0.4.4

12 months ago

0.4.6

11 months ago

0.4.1

12 months ago

0.4.0

12 months ago

0.4.3

12 months ago

0.4.2

12 months ago

0.3.36

1 year ago

0.3.35

1 year ago

0.3.34

1 year ago

0.3.31

1 year ago

0.3.30

1 year ago

0.3.33

1 year ago

0.3.32

1 year ago

0.3.29

1 year ago

0.3.28

1 year ago

0.3.27

1 year ago

0.3.26

1 year ago

0.3.25

1 year ago

0.3.24

1 year ago

0.3.23

2 years ago

0.3.22

2 years ago

0.3.9

2 years ago

0.3.17

2 years ago

0.3.16

2 years ago

0.3.15

2 years ago

0.3.14

2 years ago

0.3.13

2 years ago

0.3.12

2 years ago

0.3.11

2 years ago

0.3.10

2 years ago

0.3.20

2 years ago

0.3.21

2 years ago

0.3.0

2 years ago

0.3.19

2 years ago

0.3.18

2 years ago

0.3.6

2 years ago

0.3.5

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.18

2 years ago

0.2.17

2 years ago

0.2.12

2 years ago

0.2.10

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.0

2 years ago