Basic Usage
Getting Started
For this example we have three types Users
, Tags
and Elements
Tags
andElements
are both created_byUsers
Elements
include a list ofTags
- JavaScript
- TypeScript
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
};
/types.ts
export type User = {
alias: string
first_name: string
last_name: string
};
export type Tag = {
id: string
name: string
created_by: string
};
export type Element = {
id: string
name: string
tags: string[]
created_by: string
};
Generating Data
Here are our base data generators
- JavaScript
- TypeScript
/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'
});
/generators.ts
import { User, Tag, Element } from './types';
export function generateMockUser(i: number): User {
return {
alias: `alias_${i}`,
first_name: `first_name_${i}`,
last_name: `last_name_${i}`,
};
};
export function generateMockTag(i: number): Tag {
return {
id: `t${`0000000000000${i}`.slice(-9)}`,
name: `tag_name_${i}`,
created_by: 'unknown',
};
};
export function generateMockElement(i: number): Element {
return {
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
- JavaScript
- TypeScript
/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)))]
};
/utils.ts
export function getRandomArrElement<T>(data: T[]) {
return data[Math.floor(Math.random() * data.length)]
};
export function getRandomArrElements<T>(data: T[], maxCount: number): T[] {
return [...new Set(...new Array(maxCount).map(() => getRandomArrElement(data)))] as T[]
};
Configuration
- JavaScript
- TypeScript
/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();
/index.ts
import { mocq, MocQ } from "mocq";
import { User, Tag, Element } from './types';
import { generateMockUser, generateMockTag, generateMockElement } from './generators';
import { getRandomArrElement, getRandomArrElements } from './utils';
const { generate } = mocq({
users: {
generator: generateMockUser,
count: 25
} as MocQ<User>,
tags: {
generator: generateMockTag,
count: 25,
connections: {
users: (data: User[])=>({ created_by: getRandomArrElement(data).alias }),
},
} as MocQ<Tag>,
elements: {
generator: generateMockElement,
count: 100,
connections: {
users: (data: User[])=>({ created_by: getRandomArrElement(data).alias }),
tags: (data: Tag[])=>({ tags: getRandomArrElements(data, 2).map(x => x.id) }),
}
} as MocQ<Element>,
});
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
]