Socket
Socket
Sign inDemoInstall

@squill/squill

Package Overview
Dependencies
1
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @squill/squill

A SQL query-builder/ORM


Version published
Maintainers
1
Created

Readme

Source

Build Status codecov HitCount Known Vulnerabilities dependencies

This library currently has the following adapter librariies,

  • mysql-5.7

Documentation

Getting Started
  1. Defining Tables

  2. Data Types

    1. Null-Safe Equality
  3. FROM clause

    1. Correlated Subqueries
  4. WHERE clause

  5. SELECT clause

  6. ORDER BY clause

  7. LIMIT clause

  8. Compound Query (UNION/INTERSECT/EXCEPT)

  9. Compound Query ORDER BY clause

  10. Compound Query LIMIT clause

  11. .map()

  12. .fetchAllXxx()

  13. .fetchOneXxx()

    1. table.fetchOneXxx()
  14. .fetchValueArray()

  15. .fetchValueXxx()

    1. table.fetchValueXxx()
  16. .paginate()

  17. .emulatedCursor()

  18. query.count()

  19. query.exists()/query.assertExists()

    1. table.exists()/table.assertExists()
  20. Derived Table

  21. INSERT

  22. DELETE

  23. UPDATE

Schema Introspection and Validation
  1. Schema Introspection

  2. Schema Validation

Design Pattern
  1. Log

    1. Introduction

    2. Set Up

    3. INSERT

  2. 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

  • 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

FAQs

Last updated on 10 Mar 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc