MCPcopy
hub / github.com/directus/directus / createOne

Method createOne

api/src/services/items.ts:127–437  ·  view source on GitHub ↗

* Create a single new item.

(data: Partial<Item>, opts: MutationOptions = {})

Source from the content-addressed store, hash-verified

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,

Callers 11

handleVersionFunction · 0.95
createOneMethod · 0.95
updateOneMethod · 0.95
upsertOneMethod · 0.95
upsertSingletonMethod · 0.95
createOneMethod · 0.95
updateOneMethod · 0.95
promoteMethod · 0.95
createFieldMethod · 0.95
createMethod · 0.95
onMessageMethod · 0.95

Calls 15

createMutationTrackerMethod · 0.95
processM2OMethod · 0.95
processA2OMethod · 0.95
processValuesMethod · 0.95
processO2MMethod · 0.95
prepareDeltaMethod · 0.95
createOneMethod · 0.95
updateManyMethod · 0.95
isDirectusErrorFunction · 0.90
transactionFunction · 0.85
captureSeatCountFunction · 0.85

Tested by

no test coverage detected