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

@hedhog/utils

v0.0.28

Published

<p align="center"> <img src="https://avatars.githubusercontent.com/u/177489127?s=200&v=4" alt="Hedhog Avatar" /> </p>

Downloads

680

Readme

@hedhog/utils

Welcome to the hedhog/utils repository! This package contains utility functions, mainly designed to simplify database migrations, particularly for adding common columns like id and timestamp in a TypeORM project.

Features

  • idColumn: Automatically generates an id column in your database via migration, making it easier to define primary keys.
  • timestampColumn: Automatically creates a timestamp column to track creation and update times for your records.
  • itemTranslations: Extracts translations from a specified translation key (e.g., group_translations, translations) within an item object. This function is particularly useful for handling translation data and integrating it smoothly into an object for display or processing.
  • foreignColumn: Creates a foreign key column for a database table using TypeORM. It's particularly useful for defining foreign key relationships in migrations or database schemas.

Installation

To install this package in your project, use the following command (npm):

npm install @hedhog/utils

or with Yarn:

yarn add @hedhog/utils

Usage

Here’s how you can use the utility functions in your TypeORM migrations:

Example using idColumn

import { MigrationInterface, QueryRunner, Table } from "typeorm";
import { idColumn } from "@hedhog/utils";

export class CreateYourTable1612345678901 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(
      new Table({
        name: "your_table",
        columns: [
          idColumn(), // generates a new ID column
          // other columns
        ],
      })
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable("your_table");
  }
}

Example using timestampColumn

import { MigrationInterface, QueryRunner, Table } from "typeorm";
import { timestampColumn } from "@hedhog/utils";

export class CreateYourTable1612345678901 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(
      new Table({
        name: "your_table",
        columns: [
          // your columns
          timestampColumn(), // generates a new timestamp column
        ],
      })
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable("your_table");
  }
}

Example using itemTranslations

const item = {
  id: 1,
  group_translations: [
    { name: "Group Name", description: "Group Description" },
  ],
  slug: "example-slug",
};

const translatedItem = itemTranslations("group_translations", item);

console.log(translatedItem);
/*
Output:
{
  id: 1,
  slug: "example-slug",
  name: "Group Name",
  description: "Group Description"
}
*/

Example using foreignColumn

const foreignKey = foreignColumn({
  name: "user_id",
  isPrimary: false,
  isNullable: false,
});

console.log(foreignKey);
/*
Output:
{
  name: 'user_id',
  type: 'int',
  unsigned: true,
  isPrimary: false,
  isNullable: false,
}
*/

Folder Structure

The following is the folder structure of the repository:

hedhog-utils/
├── src/                  # Source code for the utility functions
│   └── functions/        # Folder containing individual utility functions
├── .gitignore            # Specifies files and directories to be ignored by Git
├── node_modules/         # Directory containing project dependencies (generated automatically by running npm install)
├── package-lock.json     # Ensures consistent installs across different environments
├── package.json          # Contains project metadata and dependencies
├── tsconfig.json         # TypeScript configuration for the project
└── README.md             # Documentation for the project