brianc / node-sql

SQL generation for node.js
MIT License
1.05k stars 191 forks source link

Unmaintained Package Status #405

Open charsleysa opened 5 years ago

charsleysa commented 5 years ago

By the looks of it this package is no longer maintained by the package author so I have forked this repo and upgraded the code to TypeScript (which can be found at charsleysa/node-sql-ts).

The main reasons for not continuing to use this package is due the security vulnerabilities that have not been fixed (#395 and #389) but also includes a few other changes (such as #365).

If this package ever resumes being maintained I am more than happy to PR my changes back into this repo.

spion commented 5 years ago

We also forked node-sql to https://github.com/TokyoFarmer/node-sql-2 with fixes for the vulnerabilities. We haven't refactored to typescript yet.

How good are your types? Are query/table types automatically and fully derived from define() calls?

spion commented 5 years ago

This could be a really nice enhancement for node-sql-ts:

/**
 * Typed model factories. Using plain objects
 * is a pain, because Typescript can't always infer the correct
 * types of the model, especially when you're defining the models
 * outside of the db.define(model) function call.
 */

import { TableDefinition, ColumnDefinition } from 'node-sql-ts';

export function table<Name extends string, Row>(t: TableDefinition<Name, Row>): TableDefinition<Name, Row> {
  return t;
}

export type SpecializeColumn<TType> = (def?: ColumnDefinition<any, any>) => ColumnDefinition<any, TType>;

export function specializeColumn<TType>(dataType: string) {
  let columMaker: SpecializeColumn<TType> = (o?: any) => Object.assign({}, o, { dataType: dataType });
  return columMaker;
}

export let column = {
  text: specializeColumn<string>('text'),
  varchar: specializeColumn<string>('varchar'),
  uuid: specializeColumn<string>('uuid'),
  boolean: specializeColumn<boolean>('boolean'),
  timestamp: specializeColumn<Date>('timestamp'),
  json: <T>(def?: ColumnDefinition<any, any>) =>
    Object.assign({}, def, { dataType: 'json' }) as ColumnDefinition<any, T>,
  jsonb: <T>(def?: ColumnDefinition<any, any>) =>
    Object.assign({}, def, { dataType: 'jsonb' }) as ColumnDefinition<any, T>,
  bytea: specializeColumn<Buffer>('bytea'),
  integer: specializeColumn<number>('integer'),
  custom: <T>(def?: ColumnDefinition<any, any>) => Object.assign({}, def) as ColumnDefinition<any, T>,
};

Sample usage:

import { table, column } from 'node-sql-ts';

const table = table({
  name: 'myTable',
  columns: {
    id: column.uuid({ notNull: true }), // no need to specify "name"
    todoBody: column.text()
  }
})
charsleysa commented 5 years ago

@spion due to the fact that column definitions are able to be created in so many different ways I was unable to come up with a solution to have types fully inferred by TypeScript.

Currently you'll need to pass in a model type (which can be an interface, mapped type, or even just an inline definition).

I would like to get type inference working but I currently have other higher priority tasks. If you've got a solution I'm more than happy to accept a PR.

spion commented 5 years ago

@charsleysa in the definitions we wrote for node-sql originally we decided to drop support for array of string names - only this format of tabledefinition is supported.

https://github.com/brianc/node-sql/blob/master/lib/types.d.ts#L37

It might be worth dropping support for array of strings...

I'll continue the discussion in sql-ts and maybe send a few PRs your way :grinning: