Skip to main content

Basic Usage

Getting Started

For this example we have three types Users, Tags and Elements

  • Tags and Elements are both created_by Users
  • Elements include a list of Tags
type User = {
alias: string
first_name: string
last_name: string
};

type Tag = {
id: string
name: string
created_by: string
};

type Element = {
id: string
name: string
tags: string[]
created_by: string
};

Generating Data

Here are our base data generators

/generators.js
export const generateMockUser = (i) => ({
alias: `alias_${i}`,
first_name: `first_name_${i}`,
last_name: `last_name_${i}`
});

export const generateMockTag = (i) => ({
id: `t${`0000000000000${i}`.slice(-9)}`,
name: `tag_name_${i}`,
created_by: 'unknown'
});

export const generateMockElement = (i) => ({
id: `e${`0000000000000${i}`.slice(-9)}`,
name: `element_name_${i}`,
tags: [],
created_by: 'unknown'
});

Connecting Data

We don't really care how our data is connected just that it is in some way

we will use this helper functions to grab a random element out of an array

/utils.js
export const getRandomArrElement = (data) => {
return data[Math.floor(Math.random() * data.length)]
};

export const getRandomArrElements = (data, maxCount) => {
return [...new Set(...new Array(maxCount).map(() => getRandomArrElement(data)))]
};

Configuration

/index.js
import { mocq } from "mocq";
import { generateMockUser, generateMockTag, generateMockElement } from './generators';
import { getRandomArrElement, getRandomArrElements } from './utils';

const { generate } = mocq({
users: {
generator: generateMockUser,
count: 25
},
tags: {
generator: generateMockTag,
count: 25,
connections: {
users: (users)=>({ created_by: getRandomArrElement(users).alias }),
}
},
elements: {
generator: generateMockElement,
count: 100,
connections: {
users: (users)=>({ created_by: getRandomArrElement(users).alias }),
tags: (tags)=>({ tags: getRandomArrElements(tags, 2).map(x => x.id) }),
}
},
});

const { data: { users, tags, elements} } = generate();

Sample Resolved Data

users:

[
{
alias: "alias_0",
first_name: "first_name_0",
last_name: "last_name_0"
},
// ... 24 more entries
]

tags:

[
{
id: "t000000000",
name: "tag_name_0",
created_by: "alias_1"
},
// ... 24 more entries
]

elements:

[
{
id: "e000000099",
name: "element_name_99",
tags: [ "t000000001", "t000000000" ],
created_by: "alias_21"
},
// ... 99 more entries
]