@commercetools-test-data/core

中文文档
10.18.2发布于 2025/05/08MITTypeScriptESM + CJS近 30 天下载 1.1万

This package provides the core functions to define the data models

原始使用文档
文档目录7 节
  1. Install
  2. Creating a new data model
  3. Builder
  4. Generator
  5. Transformers
  6. Presets
  7. Types

@commercetools-test-data/core

This package provides the core functions to define the data models.

Install

$ pnpm add -D @commercetools-test-data/core

Creating a new data model

Models are defined in the models/* workspace and are grouped by domain in different packages.

To create a new model, add a new package in the models workspace with the following structure:

models/<package_name>
├── LICENSE
├── README.md
├── index.ts
├── package.json
├── src
│   └── <model_name>
│       ├── builder.ts
│       ├── generator.ts
│       ├── index.ts
│       ├── presets
│       │   └── index.ts
│       ├── transformers.ts
│       └── types.ts
├── tsconfig.declarations.json
└── tsconfig.json

To define and build new models, you need to use the core package @commercetools-test-data/core.

Builder

A builder is the result of the data model. Builders allow compositions with other models and to transform the data into different shapes, for example rest or graphql.

When you want to get the generated data, you would build the model (builder).

Builders are essentially composed by a generator and (optionally) transformers.

Generator

A generator is what describes the initial model shape and data. Most of the fields can and should be initialized with some values. You can define random values using fake or static values.

import type { TAuthor } from './types';
import { Generator, fake } from '@commercetools-test-data/core';

const generator = Generator<TAuthor>({
  fields: {
    firstName: fake((f) => f.person.firstName()),
    locale: 'en',
  },
});

The shape of the generator model is what usually is used in the UI and does not need transformation.

Models can have nested fields, like object or arrays. Most of the time those are also defined as models. In this case, the generator can define the value of the nested field as null. Those nested models are then built in the builder and or the transformers.

For example, the author model can have a list of books:

import type { TAuthor } from './types';
import { Generator, fake } from '@commercetools-test-data/core';

const generator = Generator<TAuthor>({
  fields: {
    firstName: fake((f) => f.person.firstName()),
    locale: 'en',
    books: null,
  },
});

Books are defined as another model:

import type { TBook } from './types';
import { Generator, fake } from '@commercetools-test-data/core';

const generator = Generator<TBook>({
  name: 'Book',
  fields: {
    title: fake((f) => f.lorem.words()),
  },
});

When defining the builder for the Author, we can then initialize the books field by referencing the Book model.

import type { TAuthor } from './types';
import { Builder } from '@commercetools-test-data/core';
import * as Book from '../book';

const Author = Builder<TAuthor>({
  generator,
  transformers,
}).books([Book.random()]);

At the same time, we also need to tell the transformers that the books field can be built. We do that by specifying the field in the buildFields option:

import type { TAuthor, TAuthorGraphql } from './types';
import { Transformer } from '@commercetools-test-data/core';

const transformers = {
  graphql: Transformer<TAuthor, TAuthorGraphql>('graphql', {
    buildFields: ['books'],
    // ...
  }),
};

Transformers

A transformer is used to transform the data defined in the generator (the initial model) to another shape.

There are 3 available transformer types:

  • default: this is the "identity" transformer. You would use the build() method to get the generated data according to the initial shape.
  • graphql: this is the transformer for GraphQL data shapes. You would use the buildGraphql() method to get the generated data transformed into a GraphQL shape.
  • rest: this is the transformer for REST data shapes. You would use the buildRest() method to get the generated data transformed into a REST shape.

Defining a graphql and/or a rest transformer is optional and depends on what the specific data requirements are.

For example, for the Author we can define a graphql transformer like this:

import type { TAuthor, TAuthorGraphql } from './types';
import { Transformer } from '@commercetools-test-data/core';

const transformers = {
  graphql: Transformer<TAuthor, TAuthorGraphql>('graphql', {
    buildFields: ['books'],
    addFields: () => ({
      __typename: 'Author',
    }),
  }),
};

Then calling Author.random().buildGraphql<TAuthorGraphql>() will generate the data with the __typename field.

A transformer can be configured with different options:

  • buildFields: a list of fields that are defined as other data models. This is necessary so that transformers of these nested models are also executed.
  • removeFields: a list of fields that should be removed from the model.
  • addFields: a function that should return an object with new fields that needs to be added to the model. The object is then merged with the initial data. The function gets an object argument with the initial fields object, in case it's needed to construct the new object.
  • replaceFields: a function that should return the fully transformed value. This is useful for example when transforming data into different data types, for example an object into an array of objects. The function gets an object argument with the initial fields object, in case it's needed to construct the value.

The replaceFields option is mutually exclusive in regards to the addFields and removeFields options.

Presets

Presets are pre-configured models (builders) that can be defined for specific use cases and re-used.

For example:

// presets/author-with-one-book.ts
import type { TAuthorBuilder } from './types';
import * as Book from '../../book';
import Author from './builder';

const preset = (): TAuthorBuilder => Author.random().books([Book.random()]);
export default preset;

// presets/index.ts
export { default as authorWithOneBook } from './author-with-one-book';

Note that the preset exports a function. The function is useful in case the preset requires to use some random data, using the faker package. This ensures that on every function invocation, the returned data is indeed random.

For example:

// presets/author-with-one-book.ts
import type { TAuthorBuilder } from './types';
import { faker } from '@faker-js/faker';
import * as Book from '../../book';
import Author from './builder';

const preset = (): TAuthorBuilder =>
  Author.random().books([Book.random().title(faker.helpers.words())]);
export default preset;

// presets/index.ts
export { default as authorWithOneBook } from './author-with-one-book';

Types

The types.ts file contains the TypeScript representations of the models and transformed models.

版本与发布记录

@commercetools-test-data/core 共发布 599 个版本,首次发布于 2020/09/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
10.18.2latest2025/05/081.5 年前
0.0.0-canary-20250508100830canary2025/05/081.5 年前
10.18.12025/05/071.5 年前
0.0.0-canary-202505071304522025/05/071.5 年前
10.18.02025/05/071.5 年前
0.0.0-canary-202505070711342025/05/071.5 年前

关于 @commercetools-test-data/core 的常见问题

@commercetools-test-data/core 是什么?

This package provides the core functions to define the data models

如何安装 @commercetools-test-data/core?

在项目目录运行 npm install @commercetools-test-data/core,或使用 pnpm add / yarn add / bun add。

@commercetools-test-data/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@commercetools-test-data/core 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@commercetools-test-data/core 可以商用吗?

@commercetools-test-data/core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@commercetools-test-data/core 的下载量有多少?

根据 npm 官方下载统计,@commercetools-test-data/core 在 2026/10/11 – 2026/11/09 这 30 天内被下载 10,854 次,日均约 362 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@commercetools-test-data/core 还在维护吗?

最新版本 10.18.2 发布于 2025/05/08(1.5 年前);至今共发布 599 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/09/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。