This library currently has the following adapter librariies,
Documentation
Getting Started
-
Defining Tables
-
Data Types
- Null-Safe Equality
-
FROM
clause
- Correlated Subqueries
-
WHERE
clause
-
SELECT
clause
-
ORDER BY
clause
-
LIMIT
clause
-
Compound Query (UNION
/INTERSECT
/EXCEPT
)
-
Compound Query ORDER BY
clause
-
Compound Query LIMIT
clause
-
.map()
-
.fetchAllXxx()
-
.fetchOneXxx()
table.fetchOneXxx()
-
.fetchValueArray()
-
.fetchValueXxx()
table.fetchValueXxx()
-
.paginate()
-
.emulatedCursor()
-
query.count()
-
query.exists()/query.assertExists()
table.exists()/table.assertExists()
-
Derived Table
-
INSERT
-
DELETE
-
UPDATE
Schema Introspection and Validation
-
Schema Introspection
-
Schema Validation
Design Pattern
-
Log
-
Introduction
-
Set Up
-
INSERT
-
Table-per-type (TODO)
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
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 JOIN
s. Support is removed in this rewrite.
-
All RIGHT JOIN
s can be rewritten as LEFT JOIN
s
-
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.)
-
https://stackoverflow.com/questions/41936403/mysql-ieee-floating-point-nan-positiveinfinity-negativeinfinity
NaN
, +Infinity
, -Infinity
are not valid DOUBLE
values according to the SQL standard
-
Investigate and compare against,
-
This library will not handle migrations but it's good to keep an eye on promising migration libraries
-
Remove static use of potentially polyfilled functions like BigInt
, etc.
- Stick to lazy initialization
-
Investigate https://www.postgresql.org/docs/9.2/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-ESCAPE
- The escaping rules seem to have changed a lot over the different versions
- An escape string constant is specified by writing the letter
E
(upper or lower case) just before the opening single quote, e.g., E'foo'
. - If the configuration parameter
standard_conforming_strings
is off
, then PostgreSQL recognizes backslash escapes in both regular and escape string constants. However, as of PostgreSQL 9.1, the default is on
-
Add subqueries to certain callbacks (For example, the .where()
method)
- The
.select()
and .selectValue()
methods already have this
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