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

db-typegen

v1.1.2

Published

A typescript ORM code generator for database sources.

Downloads

313

Readme

DB Typegen

Typescript Database ORM code generator.

Features

  • Generates typescript ORM (Current supported below)
    • MongoDB
    • PostgreSQL

Prerequisite

  • Node.js version >= 18

Installation

Install the package globally.

npm install -g db-typegen

or

npm install db-typegen

Setup

Create a typegen.config.json file on your project root directory

root
    -- node_modules
    |
    -- src
    |
    -- package.json
    |
    -- package-lock.json
    |
    -- typegen.config.json

Example Configuration

{
  "$schema": "./node_modules/db-typegen/schema.json",
  "architecture": "functional",
  "format": "camelCase",
  "experimentalResolvers": true,
  "splitTypings": true,
  "prettier": true,
  "postgresql": {
    "dbConfig": {
      "user": "postgres",
      "host": "localhost",
      "database": "",
      "password": "password",
      "port": 5432
    },
    "schemas": ["table_one", "table_two"],
    "path": "src/__typegen__/postgresql",
    "experimentals": {
      "relationships": true
    }
  },
  "mongodb": {
    "dbConfig": {
      "host": "localhost",
      "database": "",
      "port": 27017
    },
    "path": "src/__typegen__/mongodb",
    "experimentals": {
      "strict": true
    }
  }
}

Typegen Configuration

This document outlines the configuration schema for the typegen.config.json file used in your project.

Configuration Schema

| Key | Type | Description | | -------------------------------------------- | ---------- | ---------------------------------------------------------------------------------- | | architecture | string | Programming style: 'class' or 'functional'. | | format | string | Naming Convention 'camelCase' or 'snake_case'. | | experimentalResolvers | boolean | Creates methods for every table columns: true or false | | prettier | boolean | Format generated code: true or false | | splitTypings | boolean | Abstract types and interfaces in the types.ts file: true or false | | postgresql | object | PostgreSQL database configuration. | | postgresql.schemas (New v1.1) | string[] | Specific schema to generate: ["table_one", "table_two"] | | postgresql.dbConfig | object | Configuration settings for PostgreSQL database connection: {} | | postgresql.dbConfig.user | string | Database user: 'postgres' | | postgresql.dbConfig.host | string | Database host: 'localhost' | | postgresql.dbConfig.database | string | Database name: '' | | postgresql.dbConfig.password | string | Database password: 'password' | | postgresql.dbConfig.port | integer | Database port: 5432 | | postgresql.path | string | Directory to save the generated files: './src/typegen/postgresql' | | postgresql.experimentals | object | Experimental features for PostgreSQL: {} | | postgresql.experimentals.relationships | boolean | Include tables constraints / relationships for the select methods: true or false | | mongodb | object | MongoDB database configuration. | | mongodb.dbConfig | object | Configuration settings for MongoDB database connection: {} | | mongodb.dbConfig.host | string | Database host: 'localhost' | | mongodb.dbConfig.database | string | Database name: '' | | mongodb.dbConfig.port | integer | Database port: 27017 | | mongodb.path | string | Directory to save the generated files: './src/typegen/mongodb' | | mongodb.experimentals | object | Experimental features for MongoDB: {} | | mongodb.experimentals.strict | boolean | Strict schema and typings: true or false |

Usage

Assuming you have completed the Setup. Enter the command on your terminal.

npx db-typegen

if installed globally, you may use

db-typegen

It should create a directory __typegen__ in the root. Custom path for the generated files can be also provided using the path on your typegen.config.json configuration.

Note It will also create a file .env.sample just copy the content if you don't have .env yet.

Limitations

When experimentalResolvers is set to true and format is camelCase there is a chance that the resolver methods will have a conflicting name e.g.

❌
// camelCase
Table: users
  column: id    -> selectUsersById
  column: __id  -> selectUsersById
  column: _id_  -> selectUsersById
  column: id__  -> selectUsersById

✅
// snake_case
Table: users
  column: id    -> select_users_by_id
  column: __id  -> select_users_by__id
  column: _id_  -> select_users_by_id_
  column: id__  -> select_users_by_id__

Changelog

1.1.2

  • Removed default query logging
    • Though still shows when FindOptionsPG.debug is true
  • Fixed experimentalResolver methods filter args not working
    • Currently supports = by default

1.1.0

  • Added support to explicit schema on typegen.config.json
    • Updated local schema table variables. Previously TABLE_NAME, now SCHEMA_TABLE_NAME
  • Fixed experimentalResolvers javascript/typescript reserved words for column names
  • Fixed experimentalResolvers clauses not working
  • db-typegen-utils
    • Fixed select method options type suggest not working

1.0.0

  • Initial release

Contributing

Raise issue or pull request on: https://github.com/chrischanF/db-typegen