* Create a single new item.
(data: Partial<Item>, opts: MutationOptions = {})
| 125 | * Create a single new item. |
| 126 | */ |
| 127 | async createOne(data: Partial<Item>, opts: MutationOptions = {}): Promise<PrimaryKey> { |
| 128 | if (!opts.mutationTracker) opts.mutationTracker = this.createMutationTracker(); |
| 129 | |
| 130 | if (!opts.bypassLimits) { |
| 131 | opts.mutationTracker.trackMutations(1); |
| 132 | } |
| 133 | |
| 134 | if (this.collection === 'directus_users') { |
| 135 | opts.userIntegrityCheckFlags = |
| 136 | (opts.userIntegrityCheckFlags ?? UserIntegrityCheckFlag.None) | UserIntegrityCheckFlag.UserLimits; |
| 137 | } |
| 138 | |
| 139 | const primaryKeyField = this.schema.collections[this.collection]!.primary; |
| 140 | const fields = Object.keys(this.schema.collections[this.collection]!.fields); |
| 141 | |
| 142 | const aliases = Object.values(this.schema.collections[this.collection]!.fields) |
| 143 | .filter((field) => field.alias === true) |
| 144 | .map((field) => field.field); |
| 145 | |
| 146 | const payload: AnyItem = cloneDeep(data); |
| 147 | let actionHookPayload = payload; |
| 148 | const nestedActionEvents: ActionEventParams[] = []; |
| 149 | |
| 150 | /** |
| 151 | * By wrapping the logic in a transaction, we make sure we automatically roll back all the |
| 152 | * changes in the DB if any of the parts contained within throws an error. This also means |
| 153 | * that any errors thrown in any nested relational changes will bubble up and cancel the whole |
| 154 | * update tree |
| 155 | */ |
| 156 | const primaryKey: PrimaryKey = await transaction(this.knex, async (trx) => { |
| 157 | const previousSeatCount = await captureSeatCount(trx, opts.userIntegrityCheckFlags); |
| 158 | |
| 159 | // Run all hooks that are attached to this event so the end user has the chance to augment the |
| 160 | // item that is about to be saved |
| 161 | const payloadAfterHooks = |
| 162 | opts.emitEvents !== false |
| 163 | ? await emitter.emitFilter( |
| 164 | this.eventScope === 'items' |
| 165 | ? ['items.create', `${this.collection}.items.create`] |
| 166 | : `${this.eventScope}.create`, |
| 167 | payload, |
| 168 | { |
| 169 | collection: this.collection, |
| 170 | }, |
| 171 | { |
| 172 | database: trx, |
| 173 | schema: this.schema, |
| 174 | accountability: this.accountability, |
| 175 | }, |
| 176 | ) |
| 177 | : payload; |
| 178 | |
| 179 | const payloadWithPresets = this.accountability |
| 180 | ? await processPayload( |
| 181 | { |
| 182 | accountability: this.accountability, |
| 183 | action: 'create', |
| 184 | collection: this.collection, |
no test coverage detected