Skip to content

AnyhowStep/tsql

Repository files navigation

Build Status codecov npm version HitCount Known Vulnerabilities dependencies

This library currently has the following adapter libraries,

You may test this library (and execute SQLite3 queries!) with this playground link, https://anyhowstep.github.io/tsql-sqlite3-browser/test-playground/public

Documentation

Work-in-progress Documentation may be found here

Deprecated Documentation may be found here


Goals

  • As much as possible, compile-time type-safety!

    • Run-time checks should also be included, as much as possible (without impacting performance too much)
    • Expressions, sub-queries, correlated sub-queries, etc. must be composable and should have compile-time checks
  • Provide unified query-building

    • Write query-building code once
    • Execute on server (MySQL/PostgreSQL) and browser client (using sql.js)
  • Provide query-building specific to a database and version

    • Unified query-building will have to sacrifice features not supported by some database systems
    • Tailoring code to just one database and version means no need to sacrifice features (in general)

Non-Goals

  • Efficiency

    • You won't catch me writing O(2n) algorithms but I won't lose sleep over wasted CPU cycles
  • Direct support for MySQL BIGINT UNSIGNED type.

    • PostgreSQL and SQLite do not support BIGINT UNSIGNED.
    • Trying to shoehorn support for it has proven too complex.
    • This may be supported by the MySQL-specific version.

Project Structure

This project will have multiple subprojects,

  • Database-unifying subproject

    • Provides all the composable components one needs to create a compile-time safe SQL query-building library
    • Unifies query-building for,
      • MySQL
        • 5.7.26
        • It's the version I use for work and personal projects at the moment
      • SQLite
      • PostgreSQL
        • Specific version undecided

        • Must not be a version that has been released too recently

        • An eye is kept on PostgreSQL to sanity-check the other two implementations

        • According to @webstrand ,

          9.4 still receives updates, ubuntu 16.04 expires in 2021 and only has 9.5. So at least 9.5.

          Preference is leaning towards 9.4 at the moment.

    • DOES NOT produce SQL strings; only builds an abstract syntax tree
    • DOES NOT execute SQL strings
    • Major version bumps may change which databases and versions are unified
      • A future version of this library may choose to unify MySQL 8.x, PostgreSQL 11.x

    Because it must support multiple databases, it will only support features that all three databases support. This means that many features will be excluded.

  • Subprojects specific to a database and version

    • Uses composable components to implement features specific to a database and version
      • This means we do not need to sacrifice features for the sake of compatibility
    • Implements abstract syntax tree to SQL string converter
    • Implements SQL execution

Running on Non-node Environments

This library requires BigInt support.

If your environment does not have them, you must polyfill them before this library is loaded.


The simplest BigInt polyfill that should work is,

(global as any).BigInt = ((value : string|number|bigint) => {
  return {
    toString : () => {
      return String(value);
    },
  };
}) as any;

Notes

  • typed-orm supported RIGHT JOINs. Support is removed in this rewrite.
    • All RIGHT JOINs can be rewritten as LEFT JOINs

    • Using RIGHT JOIN on a LATERAL derived table introduces problems, https://dev.mysql.com/doc/refman/8.0/en/lateral-derived-tables.html

      If the table is in the left operand and contains a reference to the right operand, the join operation must be an INNER JOIN, CROSS JOIN, or RIGHT [OUTER] JOIN.

      It is possible to use a column before it even exists in the query. This complicates compile-time type checking code.

    • Maybe keep support for RIGHT JOIN but not support RIGHT JOIN LATERAL?


TODO

Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case. For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other. (The folding of unquoted names to lower case in PostgreSQL is incompatible with the SQL standard, which says that unquoted names should be folded to upper case. Thus, foo should be equivalent to "FOO" not "foo" according to the standard. If you want to write portable applications you are advised to always quote a particular name or never quote it.)


TODO Feature Parity with typed-orm

  • Emulated FULL OUTER JOIN (MySQL does not have it)

More TODO

  • Application schema generation from DB schema?
    • Requires data type from information_schema

About

A SQL query-builder/ORM

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published