MCPcopy
hub / github.com/directus/directus / createMany

Method createMany

api/src/services/users.ts:240–280  ·  view source on GitHub ↗

* Create multiple new users

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

Source from the content-addressed store, hash-verified

238 * Create multiple new users
239 */
240 override async createMany(data: Partial<Item>[], opts: MutationOptions = {}): Promise<PrimaryKey[]> {
241 const emails = data.map((payload) => payload['email']).filter((email) => email);
242 const passwords = data.map((payload) => payload['password']).filter((password) => password);
243 const providers = data.map((payload) => payload['provider']).filter((provider) => provider);
244 const someActive = data.some((payload) => !('status' in payload) || payload['status'] === 'active');
245
246 try {
247 if (emails.length) {
248 this.validateEmail(emails);
249 await this.checkUniqueEmails(emails);
250 }
251
252 if (passwords.length) {
253 await this.checkPasswordPolicy(passwords);
254 }
255
256 if (providers.length) {
257 this.checkProviderEntitlement(providers);
258 }
259 } catch (err: any) {
260 opts.preMutationError = err;
261 }
262
263 if (someActive) {
264 // Creating users only requires checking user limits if the users are active, no need to care about the role
265 opts.userIntegrityCheckFlags =
266 (opts.userIntegrityCheckFlags ?? UserIntegrityCheckFlag.None) | UserIntegrityCheckFlag.UserLimits;
267
268 opts.onRequireUserIntegrityCheck?.(opts.userIntegrityCheckFlags);
269 }
270
271 // Use generic ItemsService to avoid calling `UserService.createOne` to avoid additional work of validating emails,
272 // as this requires one query per email if done in `createOne`
273 const itemsService = new ItemsService(this.collection, {
274 schema: this.schema,
275 accountability: this.accountability,
276 knex: this.knex,
277 });
278
279 return await itemsService.createMany(data, opts);
280 }
281
282 /**
283 * Update many users by primary key

Callers

nothing calls this directly

Calls 5

validateEmailMethod · 0.95
checkUniqueEmailsMethod · 0.95
checkPasswordPolicyMethod · 0.95
createManyMethod · 0.95

Tested by

no test coverage detected