Giter Site home page Giter Site logo

kriasoft / knex-types Goto Github PK

View Code? Open in Web Editor NEW
63.0 5.0 21.0 3.77 MB

Generate TypeScript definitions (types) from a PostgreSQL database schema.

License: MIT License

JavaScript 9.83% TypeScript 90.17%
knex knexjs database schema database-schema postgresql postgres typescript types definitions

knex-types's Introduction

Knex.js types generator

NPM Version NPM Downloads TypeScript Donate Discord

An utility module for Knex.js that generates TypeScript definitions (types) from a PostgreSQL database schema.

$ npm install knex
$ npm install knex-types --dev

Usage Example

const { knex } = require("knex");
const { updateTypes } = require("knex-types");

const db = knex(require("./knexfile"));

updateTypes(db, { output: "./types.ts" }).catch((err) => {
  console.error(err);
  process.exit(1);
});

Find an example of generated types in ./main.test.ts.

Related Projects

How to Contribute

Please create a PR or send me a message on Discord.

License

Copyright © 2021-present Kriasoft. This source code is licensed under the MIT license found in the LICENSE file.


Made with ♥ by Konstantin Tarkus (@koistya, blog) and contributors.

knex-types's People

Contributors

bhenderson avatar cskeppstedt avatar d7ark avatar koistya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

knex-types's Issues

Support for formatters (that could fix few problems at once)

Hey,

I'm just migrating from using schemats (https://www.npmjs.com/package/schemats) and would like to work with knex-types, but there's few features missing. I thought rather than forking and just adding it for myself I'll make this project better(?) for everyone who uses it. I hope it's fine.

First thing is an idea that sprouted from this comment. I've looked into suggested approach (asterisk argument for overrides being function taking type and name) and found there are two problems with this:

  1. technical - typescript doesnt allow inconsistent types, so you cannot have object Record<string, string> with special string (*) that takes function. As explained here.
  2. user experience - there's no way to say which types are overwrite with asterisk so we need to turn off any default formatting and ask user to provide formatting for all of the types.

But working on that I came up with (i think) better solution: another option - formatters - it's a set of optional functions that each formats different type (right now I have column, enum, enumEl(ement) and table, but im not 100% sold on these).

It would look like this:

  /**
   * Custom formatters for specific type of data - enum, enumEl, table and column.
   *
   * If any type is null/not specified default formatter for this type will be used.
   *
   * @example
   *   formatters: {
   *      column: (name) => kebabCase(name),
   *      table: (name) => upperFirst(name),
   *   }
   */
  formatters?: {
    column?: (name: string) => string;
    enum?: (name: string) => string;
    enumEl?: (name: string) => string;
    table?: (name: string) => string;
  };

I think it could solve multiple problems: columns with space formatters = { column: name => "${name}"}, obviously camelCasing columns, but also escaping column namesformatters={ column: name => quoteColumnName(name). I think It would add a lot of freedom for users.

I rarely contribute to open source*, so I just wrote code thinking I'll go straight to pushing Pull requests... So I have few things already written. And It seems I cant push to this repo 😅

What should I do next? 🙇

* I'd love to write more for open source projects, but Im not sure where to start and to be honest it's a bit scary.

add support for comments

We recently started putting comments into our database and we thought it would be awesome if this lib could add the comments as documentation in the output. This might be specific to postgres so I'm not sure how that would work. Potentially this lib could just offer a hook so we could add our own documentation before the typescript definition.

We add comments like this:

-- table
comment on table my_table is 'This is my table';

-- column
comment on column my_column.id is 'PK of my_table';

actually, the more I think about it, general hooks is probably the way to go. This would also solve my problems for #5 and #4.

great library by the way, it has been super helpful!

How to use this package with the official documentation?

Thank you so much for your effort for writing this package, it is awesome! But it would be even better if it can support the official typing usage for Knex.

This is the official way for defining table types for Knex:

https://knexjs.org/#typescript-support

But right now knex-types would exports an enum of Table, which we will still need to import the whole file to get the type. Can it generate a .d.ts instead, and define everything under module declare module 'knex/types/tables'?

instead of enum Tables allow an object with table.value for easy access

Maybe instead of heaving an Enum of Tables create objects with all the possible keys for cases of duplicate column names on joins and selects.

some code that does that:
https://gist.github.com/zadokdaniel/ea4dd31e13957c717ffb26e238f25173

look at the comments to see the results.

If done probably would be preferred to find another way to state the table name by property (or symbol)
unfortunately using typescript .toString() must explicitly be called

possible use for a suffix option?

I wanted to wrap the types in a namespace, so I set the prefix to be namespace DB { but I needed a closing }. I ended up just using fs to write it to the file, but I'm wondering if this would be a good use case for adding a suffix option.

By the way, I'm new to typings in javascript, but having the namespace made it so I didn't have to import the file to use any of the table definitions.

Release doesn't contain latest code

An earlier PR #14 is pretty much required to use this correctly with Knex. It looks like 0.4.0 was intended to have this feature, but main.js didn't get rebuilt so it doesn't contain the new export type Tables = code.

Super confusing btw =) Would be awesome if this can be fixed!

support for column names with spaces

Hey, I have a view where column names have spaces. I know this isn't ideal, but is there a way to support this in the generated type definition?

How to use it properly?

Hi! In the readme is shown clearly how this library can generate types, but how do we use them to incorporate them into our app?

Long support?

For the 64bit types can there be optional usage of long support instead of just string?

MySQL support

What do you think of adding MySQL support? or more abstraction to be able to add other database sources?

Support for domains

In my database I use custom domain like:

Create Domain email As text;
Create Domain phone_number As varChar(16);

Currently when I generate the types, it seams to work in general but fails for arrays.

In this example all columns are from type email or email[]

SQL
Create Table private.email (
  email_from                    email           not null,
  email_reply_to               email               null,
  email_to                      email[]         not null,
  email_cc                      email[]             null,
  email_bcc                     email[]             null,

TS
export type PrivateEmail = {
  from: string; 
  reply_to: string | null;
  to: unknown[];
  cc: unknown[] | null;

In my opinion, it would be a nice addition if the domain got its own type.
e.g.

SQL
Create Domain email As text;

TS
export type email = string;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.