2021.09 v1.11

There are quite a few releases from v1.9 to v1.11 in the past month, which includes features like SQLite connection pool, and bug fixes such as logical operators in object conditions. We’ve got several new contributors as well. Keep at this pace, https://github.com/alibaba organization awaits!

SQLite Connection Pool

Leoric hasn’t got connection pools baked in yet, it uses whatever the client provides instead. For example, the default Pool provided by mysql or mysql2 is used. However, the API provided by sqlite3 is more like bare metal, hence a Connection class for SQLite is encapsulated.

This month we added the Pool for SQLite as well, with the API designed similar to mysql. With the newly added Pool, we can handle SQLite clients like @journeyapps/sqlcipher more easily.

const Realm = require('leoric');
const realm = new Realm({
  dialect: 'sqlite',
  client: '@journeyapps/sqlcipher',
  database: '/tmp/foo.sqlite3',
}):

// configurations at session start
realm.driver.pool.on('connection', function(connection) {
  connection.query('PRAGMA key = "Riddikulus!"');
});

await realm.connect();

By the way, we’ve enhanced the stack trace of SQLite driver:

  1) => SQLite driver.query()
       should support async stack trace:
     Error: SQLITE_ERROR: no such table: missing
  --> in Database#all('SELECT * FROM missing', undefined, [Function: Leoric_all])
      at /Users/nil/Projects/cyjake/leoric/src/drivers/sqlite/connection.js:48:21
      at new Promise (<anonymous>)
      at Connection.all (src/drivers/sqlite/connection.js:47:12)
      at Connection.query (src/drivers/sqlite/connection.js:39:33)
      at SqliteDriver.query (src/drivers/sqlite/index.js:46:33)
      at async Context.<anonymous> (test/unit/drivers/sqlite/index.test.js:181:7)

​ Please read the documentation about Setup with SQLite for more information.

BINARY, VARBINARY, and BLOB

v1.11 adds three new data types that deal with binary data. Currently these data types can only be used in model definition, and type casting when the data packets come back from database. Querying columns with such data type is not supported yet, unless the condition is something like col IS NULL.

const { Bone, DataTypes } = require('leoric');
const { BLOB, STRING, BIGINT } = DataTypes;

class Attachment extends Bone {
  static attributes = {
    id: BIGINT,
    name: STRING,
    file: BLOB,
  }
}

Logical Operators in Object Conditions

Leoric supports object conditions like the BOSN grammar used in MongoDB, but in a not expressive way. For example, previously the logical operators in object conditions can only be carried out in following way:

await Post.where({
  $or: [
    { title: 'Nephalem' },
    { title: { $like: 'Angel%' },
  ],
});

which is equivalent to the SQL below:

SELECT * FROM `articles` WHERE `title` = 'Nephalem' OR `title` LIKE 'Angel%';

But for JavaScript programmers, sometimes querying in following way is preferred:

await Post.where({
  title: {
    $or: [
      'Nephalem',
      { $like: 'Angel%' },
    ],
  },
});

Hence we’ve refactored the implemetions in the past month, with both means supported and nestable. For more information about example queries, please take a look at our unit tests.

Better TypeScript Definition Types

Leoric has been providing d.ts file for a long time, but with v1 release, some of the definition types are obsolete. In the past month we refactored the d.ts file a bit, along with tests. A tribute to @nightink.

import * as assert from 'assert';

import Realm, { Bone, DataTypes } from '../../../types/index';

const { STRING, DATE } = DataTypes;

class User extends Bone {
  static attributes = {
    name: STRING,
    created_at: DATE,
    updated_at: DATE,
  }
}

async function main() {
  const userBone = await User.create({ name: 'Stranger' });
  assert.strictEqual(userBone.toJSON().name, userBone.toObject().name);

  const user = await User.first;
  await user.update({ name: 'Tyrael' });

  const realm = new Realm({
    dialect: 'sqlite',
    database: '/tmp/leoric.sqlite3',
  });
  await realm.query('SELECT * FROM sqlite_master');
}

main().catch(err => console.error(err))

More About Leoric

It is recommended starting with our Website, or the release history at Github.