The best of an ORM with the flexibility of writing your own SQL.
npm install seaquel --save
At this moment only PostgreSQL is supported. You can either use a connection string or an object. For example:
var seaquel =var db = seaquel
var seaquel =var db = seaquel
You can use
addTable(tableName) to create new tables. Then you can use
addColumn(columnName, type) to add columns to each table. The type can be a string with the name of the native type, such as
character varying(255) or one of these:
timestamp without timezone
intand creates a sequence to let this field be autoincremental.
Columns are not nullable by default. You can make a column nullable with
You can define some constraints to the columns as well:
primaryKey()sets the primary constraint to the column
unique()creates a unique constraint on this column
index([[indexName], indexType])creates an index. The default index type is
You can create foreign keys very easily. Check the example to see all this functionality in action:
var users = dbusersusersusersusersusersindexnull 'btree'usersusersusersusersvar notifications = dbnotificationsnotificationsnotificationsdb
sync() method will return SQL to match the schema in your existing database. There is an optional parameter that can be one of the available safety lebels of dbdiff. In any case don't worry because this SQL is never executed automatically. You will need to execute it yourself.
As you can see you can define functions for generating default values for each column. You can use
onUpdate(). The given functions will receive the current value (which you can use or ignore).
addCreatedAtColumn(name) is a shorthand for:
addUpdatedAtColumn(name) is a short hand for:
CRUD operations are super easy. All operations return promises. Some examples:
var users = db// ...// Insert an objectusers// Update an objetusers// Querying one objectusers// Querying all records in a table// This method has two parameters, both optional. See belowusers// Querying all records specifying an ORDER BYusers// Query specifying order by, limit and offset. All are optionalusers// Query specifying WHERE constraints and other optionsusers// Update with WHERE. The first argument is the values to set// and the second one is the conditions to match. In this case:// SET banned=false to all users with banned=trueusers// Delete a recordusers// Delete with WHEREusers
Some methods accept objects "with operators". For example:
These methods are:
selectOne(where). This method accepts operators
selectAll(where). This method accepts operators
deleteWhere(where). This method accepts operators
updateWhere(obj, where). The second argument accepts operators
seaquel is easy to perform joins. For example:
In this case the result will include for each row a
user object with the columns of the joined table.
These are all the available options:
table. Required. The referenced table. It must be an object returned by
as. Required. It's the property name that will contain the referenced objetc
where. Optional. Additional constraints to apply to the JOIN
type. Optional. The type of join (left, right, inner)
filterOnly. Optional. If it's true the join will be performed but the columns of the referenced table won't be included in the result
through. Optional. If you have more than one foreign key to the same referenced table with this you specify the column or columns that need to be used in the JOIN. This must be a string or an array of strings (if the foreign key has multiple columns).
This ORM wants to be as simple as possible. For any operation not covered by the high level API you can use your custom SQL. But the ORM helps you with it!
// returns an objectdb// returns an array of objectsdb// returns an integer with the number of affected rowsdb
Transactions are super easy. Just wrap everything that needs to be ran in the same transaction inside a
The second parameter is optional and it is the isolation level of the transaction. If it is not provided the isolation level would be the default of the db you are using. For PostgreSQL it is
db.transaction() method returns itself a Promise too.
When you are running custom queries sometimes you need to put all the columns of one or many tables. You can use the
table.columns(alias) method. For example:
That query will result in:
SELECTu.id AS u_id, u.first_name AS u_first_name, u.last_name AS u_last_name, u.email AS u_email, u.banned AS u_banned, u.password AS u_passwordFROM users uJOIN ...
If no alias is passed to
columns() in this case it will return
id, first_name, last_name, email, banned, password.
If you pass an alias but
false as second parameter, this is the result:
u.id, u.first_name, u.last_name, u.email, u.banned, u.password