* Delete multiple items by primary key.
(keys: PrimaryKey[], opts: MutationOptions = {})
| 1108 | * Delete multiple items by primary key. |
| 1109 | */ |
| 1110 | async deleteMany(keys: PrimaryKey[], opts: MutationOptions = {}): Promise<PrimaryKey[]> { |
| 1111 | if (!opts.mutationTracker) opts.mutationTracker = this.createMutationTracker(); |
| 1112 | |
| 1113 | if (!opts.bypassLimits) { |
| 1114 | opts.mutationTracker.trackMutations(keys.length); |
| 1115 | } |
| 1116 | |
| 1117 | const primaryKeyField = this.schema.collections[this.collection]!.primary; |
| 1118 | validateKeys(this.schema, this.collection, primaryKeyField, keys); |
| 1119 | |
| 1120 | const keysAfterHooks = |
| 1121 | opts.emitEvents !== false |
| 1122 | ? await emitter.emitFilter( |
| 1123 | this.eventScope === 'items' |
| 1124 | ? ['items.delete', `${this.collection}.items.delete`] |
| 1125 | : `${this.eventScope}.delete`, |
| 1126 | keys, |
| 1127 | { |
| 1128 | collection: this.collection, |
| 1129 | }, |
| 1130 | { |
| 1131 | database: this.knex, |
| 1132 | schema: this.schema, |
| 1133 | accountability: this.accountability, |
| 1134 | }, |
| 1135 | ) |
| 1136 | : keys; |
| 1137 | |
| 1138 | if (this.accountability) { |
| 1139 | await validateAccess( |
| 1140 | { |
| 1141 | accountability: this.accountability, |
| 1142 | action: 'delete', |
| 1143 | collection: this.collection, |
| 1144 | primaryKeys: keysAfterHooks, |
| 1145 | }, |
| 1146 | { |
| 1147 | knex: this.knex, |
| 1148 | schema: this.schema, |
| 1149 | }, |
| 1150 | ); |
| 1151 | } |
| 1152 | |
| 1153 | if (opts.preMutationError) { |
| 1154 | throw opts.preMutationError; |
| 1155 | } |
| 1156 | |
| 1157 | await transaction(this.knex, async (trx) => { |
| 1158 | const previousSeatCount = await captureSeatCount(trx, opts.userIntegrityCheckFlags); |
| 1159 | |
| 1160 | await trx(this.collection).whereIn(primaryKeyField, keysAfterHooks).delete(); |
| 1161 | |
| 1162 | if (opts.userIntegrityCheckFlags) { |
| 1163 | if (opts.onRequireUserIntegrityCheck) { |
| 1164 | opts.onRequireUserIntegrityCheck(opts.userIntegrityCheckFlags); |
| 1165 | } else { |
| 1166 | await validateUserCountIntegrity({ |
| 1167 | flags: opts.userIntegrityCheckFlags, |
no test coverage detected