Handles SQLite connection.
Raw knex connection object.
Queries a single SQL.
const employees = await conn.query("SELECT * AS count FROM emp");
employees.forEach(({empno, deptno, name}) => console.log(name));
Queries execution plan for a single SQL.
// equivalent to:
// const plansForSelectEmployees = await conn.query("EXPLAIN QUERY PLAN SELECT * AS count FROM emp");
const plansForSelectEmployees = await conn.queryPlan("SELECT * AS count FROM emp");
Queries SQLs from file.
const {sql, records} = await conn.queryFromFile("emp.sql")[0];
console.log(sql); // SELECT * AS count FROM emp
records.forEach(({empno, deptno, name}) => console.log(name));
Note that return value is an array of {sql: string, records: array}
.
The given file may contain multiple queries.
Queries SQLs from file.
const {sql, records} = await conn.queryPlanFromFile("emp.sql")[0];
console.log(sql); // EXPLAIN QUERY PLAN SELECT * AS count FROM emp
Loads records from CSV and inserts them into the given table
Queries the table schema.
const columns = await conn.tableSchema("my_table");
columns.forEach(({order, name, type}) => console.log(name));
Utility for query result records
Normalizes each record of xs
as follows:
- Every keys are changed to lower case.
- Every values are changed their type to
string
s,boolean
s, ornull
.String
(object) ->string
(primitive type)number
->string
- ...
- Record itself is converted to
object
Map
->object
xs
is converted toarray
Set
, Iterators ->array
const input = [
{id: 1, NAME: 'NAME #1'},
{id: 'A', name: 'name #2'},
];
const normalized = records.normalize(input);
// [
// {id: '1', name: 'NAME #1'},
// {id: 'A', name: 'name #2'},
// ]
Diffs two records. Records will be normalized with records.normailze()
Result have a two Map
s for as
and bs
, which represents a lineno - columns pair.
const as = [
{id: 'a', name: 'name #1', flag: true},
{id: 'b', name: 'name #2', flag: false},
{id: 'c', name: 'name #3', flag: false},
];
const bs = [
{id: 'a', name: 'name #1', flag: true},
{id: 'b', name: 'name #0', flag: true},
{id: 'c', name: 'name #3', flag: false},
];
// {
// a: new Map([
// [1, ['name', 'flag']] // `name` and `flag` values ('name #2', false) on line 1 are present only in `as`
// ]),
// b: new Map([
// [1, ['name', 'flag']] // `name` and `flag` values ('name #0', true) on line 1 are present only in `bs`
// ]),
// }
Format records xs
as a table.
console.log(
records.format(
[{ empno: 1, deptno: 10, name: "Scott" }]
)
);
---------- ---------- ----------
empno deptno name
---------- ---------- ----------
1 10 Scott
---------- ---------- ----------
console.log(
records.format(
'Employee tables',
'SELECT * FROM emp',
[{ empno: 1, deptno: 10, name: "Scott" }]
)
);
Employee tables
SELECT * FROM emp
1 row(s) selected
---------- ---------- ----------
empno deptno name
---------- ---------- ----------
1 10 Scott
---------- ---------- ----------
Supports internationalization.
Returns the translation for the given string
.
It supports for tagged template string style.
locale/en.yml
"[基本実装] SELECT 文で適切なレコードを取得できる": "[Basic Case] Appropriate records can be fetched by SELECT statements"
const _ = require('track-db-test-library').i18n.text;
// [Basic Case] Appropriate records can be fetched by SELECT statements
console.log(_('[基本実装] SELECT 文で適切なレコードを取得できる'));
console.log(_`[基本実装] SELECT 文で適切なレコードを取得できる`);
Introduces a new assertion to chai.expect
.
To enable the assertions, use chai.use
as follows:
const chai = require('chai');
const dblib = require('track-db-test-library');
chai.use(dblib.assertions);
Deep and fuzzy equal
which ignores case of keys and ignores string
/ number
type of values.
expect([
{ EMPNO: "1", DEPTNO: "10", NAME: "Scott" }
]).to.recordEqual([
{ empno: 1, deptno: 10, name: "Scott" }
]);
Target will be loaded from CSV file.
expect([
{ EMPNO: "1", DEPTNO: "10", NAME: "Scott" }
]).to.recordEqualToCsv("emp.csv");
empno,deptno,name
1,10,Scott
Asserts if the records contain the given value.
expect([
{ name: "John", age: 24, sex: "male" },
{ name: "Karen", age: 21, sex: "female" },
]).to.recordContain(
{ name: "John", age: 24, sex: "male" },
);
recordContain
supports function matching.
expect([
{ name: "John", age: 24, sex: "male" },
{ name: "Karen", age: 21, sex: "female" },
]).to.recordContain(r => r.age === 24);
Retains given column(s) from actual records (no assertion)
expect([
{ empno: 1, deptno: 10, name: "Scott" }
])
.columns(['deptno', 'name'])
.to.recordEqual([
{ deptno: 10, name: "Scott" }
]);
Removes given column(s) from actual records (no assertion)
expect([
{ empno: 1, deptno: 10, name: "Scott" }
])
.without('empno') // `.without(['empno'])` will also be accepted
.to.recordEqual([
{ deptno: 10, name: "Scott" }
]);
Sort actual records by given columns. Records will be normalized.
expect([
...
])
.orderBy(['id', '-name']) // order by `id` (asc) and then `name` (desc)
.to.recordEqual([
...
]);
Checks if table full scan is planned to Connection#queryPlan()
results.
expect([
{id: 4, parent: 0, notused: 0, detail: 'SCAN TABLE rooms'},
{id: 10, parent: 0, notused: 0, detail: 'SEARCH TABLE hotels USING INTEGER PRIMARY KEY (rowid=?)'},
{id: 28, parent: 0, notused: 0, detail: 'USE TEMP B-TREE FOR ORDER BY'}
]).to.fullscan(['tables', 'rooms']) // by default, checks if 'any' from given tables will be fully scanned