1.1.11 • Published 6 months ago

db-typegen v1.1.11

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

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

KeyTypeDescription
architecturestringProgramming style: 'class' or 'functional'.
formatstringNaming Convention 'camelCase' or 'snake_case'.
experimentalResolversbooleanCreates methods for every table columns: true or false
prettierbooleanFormat generated code: true or false
splitTypingsbooleanAbstract types and interfaces in the types.ts file: true or false
postgresqlobjectPostgreSQL database configuration.
postgresql.schemas (New v1.1)string[]Specific schema to generate: "table_one", "table_two"
postgresql.dbConfigobjectConfiguration settings for PostgreSQL database connection: {}
postgresql.dbConfig.userstringDatabase user: 'postgres'
postgresql.dbConfig.hoststringDatabase host: 'localhost'
postgresql.dbConfig.databasestringDatabase name: ''
postgresql.dbConfig.passwordstringDatabase password: 'password'
postgresql.dbConfig.portintegerDatabase port: 5432
postgresql.pathstringDirectory to save the generated files: './src/typegen/postgresql'
postgresql.experimentalsobjectExperimental features for PostgreSQL: {}
postgresql.experimentals.relationshipsbooleanInclude tables constraints / relationships for the select methods: true or false
mongodbobject or booleanMongoDB database configuration.
mongodb.dbConfigobjectConfiguration settings for MongoDB database connection: {}
mongodb.dbConfig.hoststringDatabase host: 'localhost'
mongodb.dbConfig.databasestringDatabase name: ''
mongodb.dbConfig.portintegerDatabase port: 27017
mongodb.pathstringDirectory to save the generated files: './src/typegen/mongodb'
mongodb.experimentalsobjectExperimental features for MongoDB: {}
mongodb.experimentals.strictbooleanStrict 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.10

  • Added double quote delimiter for tables to avoid clashing with postgresql reserved words
  • Removed insert, update and delete client schema check
  • Improved experimentalResolver typings
  • Improved relationships resolvers return types
  • Improved insert statement return type
    • Introduced PGInsertResult<T> - { insertedCount: number; data: T[] }
  • Improved update statement return type
    • Introduced PGUpdateResult<T> - { updatedCount: number; data: T[] }
  • Improved delete statement return type
    • Introduced PGDeleteResult<T> - { deletedCount: number }
  • Fixed db-typegen-utils import path
  • Fixed undefined error on generate

1.1.2

  • Removed default query logging
    • Though still shows when PGFindOptions.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

1.1.11

6 months ago

1.1.10

8 months ago

1.1.9

9 months ago

1.1.8

9 months ago

1.1.7

10 months ago

1.1.6

10 months ago

1.1.5

10 months ago

1.1.4

10 months ago

1.1.3

10 months ago

1.1.2

10 months ago

1.1.1

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago

1.1.0

10 months ago