npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lb4-soft-delete

v1.0.0-beta.0.0.2

Published

Adding soft delete functionality to Loopback 4 models.

Downloads

2

Readme

lb4-soft-delete

Add Soft Delete Funcionality to your LB4 Repositories.

  • [x] Default Repoistory
  • [ ] TODO: Soft Deleted Relations

Install

npm install --save lb4-soft-delete

Add Soft Delete Column to your Model

import {property} from '@loopback/repository';
import {SoftDeleteEntity} from 'lb4-soft-delete'

Product extends SoftDeleteEntity {
  @property({
    type: number,
    id: true
  })
  id?: number
}

or if you wanna change the definition of the deletedAt property: e.g. it should be null instead of undefined for not deleted entities.

import {Entity, property} from '@loopback/repository';
import {softDelete} from 'lb4-soft-delete';

Product extends Entity { // Not working like that
  @property({
    type: number,
    id: true
  })
  id?: number

  @softDeleteProperty({
    default: null,
  })
  deletedAt?: string | null;
}

Add Soft Delete Methods to your Repository

import {Product, ProductRelations} from '../models';
import {DbDataSource} from '../datasources';
import {SoftDeleteCrudRepository} from 'lb4-soft-delete';
import {inject} from '@loopback/core';

export class ProductRepository extends SoftDeleteCrudRepository<Product, Product.prototype.id, ProductRelations>
{
  constructor(
    @inject('datasources.db') dataSource: DbDataSource,
  ) {
    super(Product, dataSource);
  }
}

API:

SoftDeleteFilter

interface SoftDeleteFilter<T extends SoftDeleteEntity> extends Filter<T> {
  withDeleted?: boolean;
  onlyDeleted?: boolean;
}
  • withDeleted includes soft deleted Entities
  • onlyDeleted only shows soft deleted Entities
  • onlyDeleted > withDeleted

repository.save()

save(entity: T, options?: Options): Promise<T>;
  • Safe only if not deleted already // TODO
  • Create a new, is unchanged from DefaultCrudRepository

repository.create()

create(entity: DataObject<T>, options?: Options): Promise<T>;
  • unchanged from DefaultCrudRepository

repository.createAll()

createAll(entities: DataObject<T>[], options?: Options): Promise<T[]>;
  • unchanged from DefaultCrudRepository

repository.find()

find(filter?: SoftDeleteFilter<T>, options?: Options): Promise<(T & Relations)[]>;
  • does not find soft deleted Entities
  • filter includes withDeleted, onlyDeleted to show soft deleted Entities again

repository.findOne()

findOne(filter?: SoftDeleteFilter<T>, options?: Options): Promise<(T & Relations) | null>;
  • does not find soft deleted Entities
  • filter includes withDeleted, onlyDeleted to show soft deleted Entities again

repository.findById()

findById(id: ID, filter?: SoftDeleteFilter<T>, options?: Options): Promise<T & Relations>;
  • does not find soft deleted Entities
  • filter includes withDeleted, onlyDeleted to show soft deleted Entities again
  • throws when soft delete state does not fit filter options
  • throws when not found

repository.update()

update (entity: T, options?: Options): Promise<void>;
  • update entity
  • throws if entity is soft deleted already // TODO TEST
  • throws if entity is not found

repository.delete()

delete(entity: T, options?: Options): Promise<void>;
  • soft delete an entity
  • throws when already soft deleted
  • throws when not found

repository.deleteHard()

deleteHard(entity: T, options?: Options): Promise<void>;
  • soft delete an entity
  • throws when already soft deleted
  • throws when not found

repository.restore()

restore(entity: T, options?: Options): Promise<void>;
  • restore a soft deleted entity
  • sets the deletedAt column to the default value of the property definition
  • throws when not soft deleted
  • throws when not found

repository.updateAll()

updateAll(data: DataObject<T>, where?: Where<T>, options?: Options): Promise<Count>;
  • update entities (only not soft deleted)

repository.updateById()

updateById(id: ID, data: DataObject<T>, options?: Options): Promise<void>;
  • update entity
  • throws if entity is soft deleted already
  • throws if entity is not found

repository.replaceById()

replaceById(id: ID, data: DataObject<T>, options?: Options): Promise<void>;
  • replace entity
  • throws if entity is soft deleted already
  • throws if entity is not found

repository.deleteAll()

deleteAll(where?: Where<T>, options?: Options): Promise<Count>;
  • soft delete entities

repository.deleteAllHard()

deleteHardAll(where?: Where<T>, options?: Options): Promise<Count>;
  • delete already soft deleted entities

repository.restoreAll()

restoreAll(where?: Where<T>, options?: Options): Promise<Count>;
  • restore soft deleted entities

repository.deleteById()

deleteById(id: ID, options?: Options): Promise<void>;
  • soft delete an entity
  • throws when already soft deleted
  • throws when not found

repository.deleteHardById()

deleteHardById(id: ID, options?: Options): Promise<void>;
  • delete an entity
  • throws when not soft deleted
  • throws when not found

repository.restoreById()

restoreById(id: ID, options?: Options): Promise<void>;
  • restore a soft deleted entity
  • sets the deletedAt column to the default value of the property definition
  • throws when not soft deleted
  • throws when not found

repository.count()

count(where?: Where<T>, options?: Options): Promise<Count>;
  • counts only the not deleted entities

repository.countWithDeleted()

countWithDeleted(where?: Where<T>, options?: Options): Promise<Count>;
  • counts all entities

repository.countOnlyDeleted()

countOnlyDeleted(where?: Where<T>, options?: Options): Promise<Count>;
  • counts only the deleted entities

repository.exists()

exists(id: ID, options?: Options): Promise<boolean>;
  • unchanged from DefaultCrudRepository

LoopBack

https://help.github.com/en/articles/basic-writing-and-formatting-syntax