|
| 1 | +import { |
| 2 | + AndClause, |
| 3 | + Condition, |
| 4 | + DataObject, |
| 5 | + Filter, |
| 6 | + juggler, |
| 7 | + OrClause, |
| 8 | + DefaultTransactionalRepository, |
| 9 | + Where, |
| 10 | +} from '@loopback/repository'; |
| 11 | +import {Count} from '@loopback/repository/src/common-types'; |
| 12 | +import {Options} from 'loopback-datasource-juggler'; |
| 13 | +import {SoftDeleteEntity} from '../models'; |
| 14 | + |
| 15 | +export abstract class DefaultTransactionSoftCrudRepository< |
| 16 | + T extends SoftDeleteEntity, |
| 17 | + ID, |
| 18 | + Relations extends object = {} |
| 19 | +> extends DefaultTransactionalRepository<T, ID, Relations> { |
| 20 | + constructor( |
| 21 | + entityClass: typeof SoftDeleteEntity & { |
| 22 | + prototype: T; |
| 23 | + }, |
| 24 | + dataSource: juggler.DataSource, |
| 25 | + ) { |
| 26 | + super(entityClass, dataSource); |
| 27 | + } |
| 28 | + |
| 29 | + find(filter?: Filter<T>, options?: Options): Promise<(T & Relations)[]> { |
| 30 | + // Filter out soft deleted entries |
| 31 | + if ( |
| 32 | + filter?.where && |
| 33 | + (filter.where as AndClause<T>).and && |
| 34 | + (filter.where as AndClause<T>).and.length > 0 |
| 35 | + ) { |
| 36 | + (filter.where as AndClause<T>).and.push({ |
| 37 | + deleted: false, |
| 38 | + } as Condition<T>); |
| 39 | + } else if ( |
| 40 | + filter?.where && |
| 41 | + (filter.where as OrClause<T>).or && |
| 42 | + (filter.where as OrClause<T>).or.length > 0 |
| 43 | + ) { |
| 44 | + (filter.where as AndClause<T>).and = []; |
| 45 | + (filter.where as AndClause<T>).and.push( |
| 46 | + { |
| 47 | + deleted: false, |
| 48 | + } as Condition<T>, |
| 49 | + { |
| 50 | + or: (filter.where as OrClause<T>).or, |
| 51 | + }, |
| 52 | + ); |
| 53 | + } else { |
| 54 | + filter = filter ?? {}; |
| 55 | + filter.where = filter.where ?? {}; |
| 56 | + (filter.where as Condition<T>).deleted = false; |
| 57 | + } |
| 58 | + |
| 59 | + // Now call super |
| 60 | + return super.find(filter, options); |
| 61 | + } |
| 62 | + |
| 63 | + findOne( |
| 64 | + filter?: Filter<T>, |
| 65 | + options?: Options, |
| 66 | + ): Promise<(T & Relations) | null> { |
| 67 | + // Filter out soft deleted entries |
| 68 | + if ( |
| 69 | + filter?.where && |
| 70 | + (filter.where as AndClause<T>).and && |
| 71 | + (filter.where as AndClause<T>).and.length > 0 |
| 72 | + ) { |
| 73 | + (filter.where as AndClause<T>).and.push({ |
| 74 | + deleted: false, |
| 75 | + } as Condition<T>); |
| 76 | + } else if ( |
| 77 | + filter?.where && |
| 78 | + (filter.where as OrClause<T>).or && |
| 79 | + (filter.where as OrClause<T>).or.length > 0 |
| 80 | + ) { |
| 81 | + (filter.where as AndClause<T>).and = []; |
| 82 | + (filter.where as AndClause<T>).and.push( |
| 83 | + { |
| 84 | + deleted: false, |
| 85 | + } as Condition<T>, |
| 86 | + { |
| 87 | + or: (filter.where as OrClause<T>).or, |
| 88 | + }, |
| 89 | + ); |
| 90 | + } else { |
| 91 | + filter = filter ?? {}; |
| 92 | + filter.where = filter.where ?? {}; |
| 93 | + (filter.where as Condition<T>).deleted = false; |
| 94 | + } |
| 95 | + |
| 96 | + // Now call super |
| 97 | + return super.findOne(filter, options); |
| 98 | + } |
| 99 | + |
| 100 | + findById( |
| 101 | + id: ID, |
| 102 | + filter?: Filter<T>, |
| 103 | + options?: Options, |
| 104 | + ): Promise<T & Relations> { |
| 105 | + // Filter out soft deleted entries |
| 106 | + if ( |
| 107 | + filter?.where && |
| 108 | + (filter.where as AndClause<T>).and && |
| 109 | + (filter.where as AndClause<T>).and.length > 0 |
| 110 | + ) { |
| 111 | + (filter.where as AndClause<T>).and.push({ |
| 112 | + deleted: false, |
| 113 | + } as Condition<T>); |
| 114 | + } else if ( |
| 115 | + filter?.where && |
| 116 | + (filter.where as OrClause<T>).or && |
| 117 | + (filter.where as OrClause<T>).or.length > 0 |
| 118 | + ) { |
| 119 | + (filter.where as AndClause<T>).and = []; |
| 120 | + (filter.where as AndClause<T>).and.push( |
| 121 | + { |
| 122 | + deleted: false, |
| 123 | + } as Condition<T>, |
| 124 | + { |
| 125 | + or: (filter.where as OrClause<T>).or, |
| 126 | + }, |
| 127 | + ); |
| 128 | + } else { |
| 129 | + filter = filter ?? {}; |
| 130 | + filter.where = filter.where ?? {}; |
| 131 | + (filter.where as Condition<T>).deleted = false; |
| 132 | + } |
| 133 | + |
| 134 | + // Now call super |
| 135 | + return super.findById(id, filter, options); |
| 136 | + } |
| 137 | + |
| 138 | + updateAll( |
| 139 | + data: DataObject<T>, |
| 140 | + where?: Where<T>, |
| 141 | + options?: Options, |
| 142 | + ): Promise<Count> { |
| 143 | + // Filter out soft deleted entries |
| 144 | + if ( |
| 145 | + where && |
| 146 | + (where as AndClause<T>).and && |
| 147 | + (where as AndClause<T>).and.length > 0 |
| 148 | + ) { |
| 149 | + (where as AndClause<T>).and.push({ |
| 150 | + deleted: false, |
| 151 | + } as Condition<T>); |
| 152 | + } else if ( |
| 153 | + where && |
| 154 | + (where as OrClause<T>).or && |
| 155 | + (where as OrClause<T>).or.length > 0 |
| 156 | + ) { |
| 157 | + (where as AndClause<T>).and = []; |
| 158 | + (where as AndClause<T>).and.push( |
| 159 | + { |
| 160 | + deleted: false, |
| 161 | + } as Condition<T>, |
| 162 | + { |
| 163 | + or: (where as OrClause<T>).or, |
| 164 | + }, |
| 165 | + ); |
| 166 | + } else { |
| 167 | + where = where ?? {}; |
| 168 | + (where as Condition<T>).deleted = false; |
| 169 | + } |
| 170 | + |
| 171 | + // Now call super |
| 172 | + return super.updateAll(data, where, options); |
| 173 | + } |
| 174 | + |
| 175 | + count(where?: Where<T>, options?: Options): Promise<Count> { |
| 176 | + // Filter out soft deleted entries |
| 177 | + if ( |
| 178 | + where && |
| 179 | + (where as AndClause<T>).and && |
| 180 | + (where as AndClause<T>).and.length > 0 |
| 181 | + ) { |
| 182 | + (where as AndClause<T>).and.push({ |
| 183 | + deleted: false, |
| 184 | + } as Condition<T>); |
| 185 | + } else if ( |
| 186 | + where && |
| 187 | + (where as OrClause<T>).or && |
| 188 | + (where as OrClause<T>).or.length > 0 |
| 189 | + ) { |
| 190 | + (where as AndClause<T>).and = []; |
| 191 | + (where as AndClause<T>).and.push( |
| 192 | + { |
| 193 | + deleted: false, |
| 194 | + } as Condition<T>, |
| 195 | + { |
| 196 | + or: (where as OrClause<T>).or, |
| 197 | + }, |
| 198 | + ); |
| 199 | + } else { |
| 200 | + where = where ?? {}; |
| 201 | + (where as Condition<T>).deleted = false; |
| 202 | + } |
| 203 | + |
| 204 | + // Now call super |
| 205 | + return super.count(where, options); |
| 206 | + } |
| 207 | + |
| 208 | + delete(entity: T, options?: Options): Promise<void> { |
| 209 | + // Do soft delete, no hard delete allowed |
| 210 | + (entity as SoftDeleteEntity).deleted = true; |
| 211 | + return super.update(entity, options); |
| 212 | + } |
| 213 | + |
| 214 | + deleteAll(where?: Where<T>, options?: Options): Promise<Count> { |
| 215 | + // Do soft delete, no hard delete allowed |
| 216 | + return this.updateAll( |
| 217 | + { |
| 218 | + deleted: true, |
| 219 | + } as DataObject<T>, |
| 220 | + where, |
| 221 | + options, |
| 222 | + ); |
| 223 | + } |
| 224 | + |
| 225 | + deleteById(id: ID, options?: Options): Promise<void> { |
| 226 | + // Do soft delete, no hard delete allowed |
| 227 | + return super.updateById( |
| 228 | + id, |
| 229 | + { |
| 230 | + deleted: true, |
| 231 | + } as DataObject<T>, |
| 232 | + options, |
| 233 | + ); |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * Method to perform hard delete of entries. Take caution. |
| 238 | + * @param entity |
| 239 | + * @param options |
| 240 | + */ |
| 241 | + deleteHard(entity: T, options?: Options): Promise<void> { |
| 242 | + // Do hard delete |
| 243 | + return super.delete(entity, options); |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Method to perform hard delete of entries. Take caution. |
| 248 | + * @param entity |
| 249 | + * @param options |
| 250 | + */ |
| 251 | + deleteAllHard(where?: Where<T>, options?: Options): Promise<Count> { |
| 252 | + // Do hard delete |
| 253 | + return super.deleteAll(where, options); |
| 254 | + } |
| 255 | + |
| 256 | + /** |
| 257 | + * Method to perform hard delete of entries. Take caution. |
| 258 | + * @param entity |
| 259 | + * @param options |
| 260 | + */ |
| 261 | + deleteByIdHard(id: ID, options?: Options): Promise<void> { |
| 262 | + // Do hard delete |
| 263 | + return super.deleteById(id, options); |
| 264 | + } |
| 265 | +} |
0 commit comments