Basie
A TypeScript Node package that provides a fluent semi-typesafe SQL query builder akin to the one shipped with Laravel. Supports sqlite (using sqlite3), MySQL (using mysql2) and PostgreSQL (using pg). Find the documentation here.
Example
; ;; ;; // Fluent querying on objects.; // Complex querying supported:;// 'SELECT * FROM users WHERE age > ? OR (name LIKE ? OR name LIKE ?) ORDER BY age DESC LIMIT ?' // Methods directly on the object.;user.name = "Thijs";user.age = 17;await user.save; // insertsuser.age = 18;await user.save; // updatesawait user.delete; // deletes // Lazy relationships.;console.assertphones === await user.phones, "Not lazy"; // Allows relationships to be further narrowed down.;// 'SELECT LEN(number) as number_length FROM phones WHERE user_id = ? AND number_length >= ?'
Features
- Fluent Querying Interface: QueryBuilder is a fluent querying builder interface that attempts to be as type-safe as possible. This means that
where(nonexistentKey, 10)
will fail, and thatwhere("is_admin", "true")
won't compile ifis_admin
is supposed to be a boolean. - Easily Managable Objects: Simply call
save()
anddelete()
on objects to either insert, update or delete the specific model.
Limitations
- Only string, boolean and number fields are supported. Basie does not actually query your database for the scheme, so it doesn't know the types of your database columns. Only strings, numbers and booleans will be converted properly. For all other values, you can use custom getters and setters to wrap a value.
- Does not create tables. Basie only queries tables and simply assumes that your model matches the database layout.
- Tables are required to have an id column. Basie tracks documents by their id, and it does not support changing the name or type of the ID column.
- Relations are read-only. You have to manually fiddle with id columns to change relationships.
Usage
For a complete overhead view of all methods available, it is recommended you check out the documentation. The next few sections describe some common usecases and gotchas.
Configuring the Database
Basie supports SQLite, MySQL and PostgreSQL. Simply import the static Basie instance and call the appropriate method to configure Basie to use that database:
; // SQLite.;; // MySQL;;Basie.mysqlconnection; // PostgreSQL;;pg.types.setTypeParser20, parseInt; // convert strings to numbers (int8)pg.types.setTypeParser1700, parseFloat; // convert decimal strings to numbers (numeric)Basie.postgrespool;
Models, Methods and Computed Properties
Your models are simple TypeScript classes, which means that they support methods and computed properties. Basie will ensure that whenever a model instance gets loaded from the database, it will have the appropriate prototype.
;;
Relations
For a full overview of all relations, please check the documentation. Relations work similar to how Laravel's Eloquent handles relations. Do note that foreign keys will need to be present on objects.
; ;; ;;
Relations are lazy and return a promise. Calling a relation without ()
will instead return a query builder that can be refined further:
; // Phone.where("user_id", user.id).all(); // Phone.where("user_id", user.id). Can be further refined.
Development
Begin by running npm install
or yarn install
. Use npm run watch
(or yarn watch
) to start the TypeScript compilation service. Tests can be ran using npm test
or yarn test
. Pull requests are welcome! ;)