Skip to content

createMany ​

Category
Tags
Export size
min 7.23 kB · gzip 2.95 kB
See also

Creates all given items with a single create([...]) call.

If the service does not allow multi creating (its multi option, or an explicit multi option), the items are created with one call per item instead, at most concurrency at a time - so it works regardless of the service's multi configuration. A single item is always created with a single call.

ts
  import { 
createMany
} from 'feathers-utils/utils';

Example ​

ts
import { createMany } from 'feathers-utils/utils'

// one `create([...])` call, or one `create({...})` call per item
const todos = await createMany(app, 'todos', [
  { title: 'Buy milk', userId: 1 },
  { title: 'Buy eggs', userId: 1 },
])

Options ​

multiMulti

Whether the service allows creating multiple items in a single call.

Defaults to the multi option of the service. If multi creating is not allowed, the items are created with one call per item.

concurrencynumberdefault10

How many create calls run at the same time when the items are created with one call per item, so a long list does not exhaust the database pool.

paramsP

The params of every create call - e.g. to create the items in the caller's transaction.

Example

Type declaration ​

Show Type Declarations
ts
export type CreateManyOptions<P extends Params = Params> = {
  /**
   * Whether the service allows creating multiple items in a single call.
   *
   * Defaults to the `multi` option of the service. If multi creating is not
   * allowed, the items are created with one call per item.
   */
  multi?: Multi
  /**
   * How many `create` calls run at the same time when the items are created
   * with one call per item, so a long list does not exhaust the database pool.
   *
   * @default 10
   */
  concurrency?: number
  /**
   * The params of every `create` call - e.g. to create the items in the
   * caller's transaction.
   *
   * @example { transaction: context.params.transaction }
   */
  params?: P
}
/**
 * Creates all given items with a single `create([...])` call.
 *
 * If the service does not allow multi creating (its `multi` option, or an
 * explicit `multi` option), the items are created with one call per item
 * instead, at most `concurrency` at a time - so it works regardless of the
 * service's `multi` configuration.
 * A single item is always created with a single call.
 *
 * @example
 * ```ts
 *
 *
 * // one `create([...])` call, or one `create({...})` call per item
 * const todos = await createMany(app, 'todos', [
 *   { title: 'Buy milk', userId: 1 },
 *   { title: 'Buy eggs', userId: 1 },
 * ])
 * ```
 *
 * @see https://utils.feathersjs.com/utils/create-many.html
 */
export declare function createMany<
  Services,
  Path extends KeyOf<Services>,
  Service extends Services[Path] = Services[Path],
  Item = InferCreateResultSingle<Service>,
>(
  app: Application<Services>,
  servicePath: Path,
  data: InferCreateDataSingle<Service>[],
  options?: CreateManyOptions<InferCreateParams<Service>>,
): Promise<Item[]>
ArgumentTypeDescription
appApplication<Services>
servicePathPath
dataInferCreateDataSingle<Service>[]
optionsCreateManyOptions<InferCreateParams<Service>>

Released under the MIT License.