npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mishguru/make

v233.0.0

Published

Easily make rows for any Sequelize model, that are filled with mock data.

Downloads

33

Readme

Make

Easily create rows for any table

Built for Sequelize v3 and AVA.

Install

npm install --save-dev @mishguru/make

Usage

Manual

import { make } from '@mishguru/make'

import db from './db'

const context = {}

const task = await make({
  context,
  table: db.Task,
  attributes: {
    // optionally set custom attributes
    name: 'task with this specific name'
  }
})

Transaction support

If the test context has a Sequalize transaction object under the transaction key, make will automatically use it when creating entities in the database. While inside an active transaction, created entities will only be accessible under that transaction as well.

With AVA Context

If you are using AVA, then you can use withMake -- which automatically injects itself into the test context.

import anyTest, { TestInterface } from 'ava'
import { withMake, WithMakeFn } from '@mishguru/make'

const test = anyTest as TestInterface<{ make: WithMakeFn }>

withMake({ test })

test('my test', async (t) => {
  const { make } = t.context

  const task = make(db.Task)

  // create multiple tasks
  const tasks = [
    await make(db.Task),
    await make(db.Task),
    await make(db.Task),
  ])

  // custom attributes
  const specialTask = make(db.Task, { name: 'special' })
})

Mock Data

When creating a row with make, it will fill it with appropriate fake data.

If your test requires a specific value to be a in a column, for example, that url = 'https://mish.guru', then you can pass custom fields you want to use as second argument.

// make
const content = await make({
  context: {},
  table: db.Content,
  attributes: { url: 'https://mish.guru' }
})

// withMake
const content = await t.context.make(db.Content, { url: 'https://mish.guru' })

Relations

This is where make shines. It automatically detects foreign keys and will recursively create related tables for you.

For example, imagine you had a database with the following entities:

[Task] >---| [Project] >---| [User]

Task.belongsTo(Project)
Project.belongsTo(User)

With make, you can create a Task in one line, and don't need to worry about setting up a User and a Project.

const context = {}
const task = await make({ context, table: db.Task })

console.log(context)
// { task: {...}, project: {...}, user: {...} }

Full Example

import Sequelize from 'sequelize'
import { make } from '@mishguru/make'

const sequelize = new Sequelize(...)

const Project = sequelize.define('project', {
  title: Sequelize.STRING,
  description: Sequelize.TEXT
})

const Task = sequelize.define('task', {
  title: Sequelize.STRING,
  description: Sequelize.TEXT,
  deadline: Sequelize.DATE,
})

Task.belongsTo(Project)

const start = async () => {
  const context = {}

  const task = await make({
    context,
    table: Task,
    attributes: {
      title: 'a custom title'
    }
  })

  console.log(context)
  /*
  {
    project: {
      id: 1,
      title: 'District Granite Wooden',
      description: 'e-markets Bedfordshire'
    },
    task: {
      id: 1,
      title: 'a custom title',
      description: 'Massachusetts',
      deadline: 2019-05-05T17:08:06.435Z,
      projectId: 1
    }
  }*/

  console.log(task === context.task)
  // true
}

start().catch(console.error)