@rwillians/qx 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -442,7 +442,7 @@ class BunSQLite {
442
442
  */
443
443
  async createTable(op) {
444
444
  const { sql } = ddl.createTable(op);
445
- this.loggers.forEach(logger => logger.debug(sql, []));
445
+ this.loggers.forEach(logger => logger.query.debug(sql, []));
446
446
  this.conn.run(sql);
447
447
  }
448
448
  /**
@@ -456,7 +456,7 @@ class BunSQLite {
456
456
  records: op.records.map(createEncoder(op.insertShape)),
457
457
  insertShape: u.mapKeys(op.insertShape, u.snakeCase),
458
458
  });
459
- this.loggers.forEach(logger => logger.debug(sql, params));
459
+ this.loggers.forEach(logger => logger.query.debug(sql, params));
460
460
  const stmt = this.conn.prepare(sql);
461
461
  const rows = stmt.all(...params);
462
462
  return rows.map(createDecoder(op.returnShape));
@@ -468,7 +468,7 @@ class BunSQLite {
468
468
  */
469
469
  async query(op) {
470
470
  const { sql, params } = ddl.select(op);
471
- this.loggers.forEach(logger => logger.debug(sql, params));
471
+ this.loggers.forEach(logger => logger.query.debug(sql, params));
472
472
  const stmt = this.conn.prepare(sql);
473
473
  const rows = stmt.all(...params);
474
474
  return rows.map(createDecoder(op.select));
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defineMigrations = void 0;
4
+ const ora_1 = require("ora");
4
5
  const index_1 = require("./index");
5
6
  const migrations = (0, index_1.table)('migrations', t => ({
6
7
  id: t.string({ size: 36 }).primaryKey(),
@@ -12,6 +13,7 @@ const migrations = (0, index_1.table)('migrations', t => ({
12
13
  * @version 1
13
14
  */
14
15
  const defineMigrations = (migs) => async (db) => {
16
+ let spinner;
15
17
  await index_1.create.table(migrations, { ifNotExists: true }).onto(db);
16
18
  for (const [id, migration] of Object.entries(migs)) {
17
19
  const alreadyMigrated = await (0, index_1.from)(migrations.as('m'))
@@ -19,12 +21,14 @@ const defineMigrations = (migs) => async (db) => {
19
21
  .exists(db);
20
22
  if (alreadyMigrated)
21
23
  continue;
22
- console.log(`running migration ${id}...`);
24
+ spinner ??= (0, ora_1.default)('running migrations...').start();
25
+ spinner.text = `running migration ${id}...`;
26
+ // @TODO run in a transaction
23
27
  await migration(db);
24
- console.log('done');
25
28
  await (0, index_1.into)(migrations)
26
29
  .insert({ id, migratedAt: new Date() })
27
30
  .run(db);
28
31
  }
32
+ spinner?.stop();
29
33
  };
30
34
  exports.defineMigrations = defineMigrations;
package/dist/cjs/index.js CHANGED
@@ -440,7 +440,6 @@ const is = {
440
440
  };
441
441
  exports.is = is;
442
442
  ;
443
- ;
444
443
  // // // // // // // // // // // // // // // // // // // // // // // //
445
444
  // CREATE STATEMENTS //
446
445
  // // // // // // // // // // // // // // // // // // // // // // // //
@@ -9,6 +9,8 @@ const sql_highlight_1 = require("sql-highlight");
9
9
  * @version 1
10
10
  */
11
11
  const createPrettyLogger = () => ({
12
- debug: (sql, params) => { process.stdout.write((0, sql_highlight_1.highlight)(sql) + ' ' + (0, node_util_1.inspect)(params, false, null, true) + '\n'); },
12
+ query: {
13
+ debug: (sql, params) => { process.stdout.write((0, sql_highlight_1.highlight)(sql) + ' ' + (0, node_util_1.inspect)(params, false, null, true) + '\n'); },
14
+ },
13
15
  });
14
16
  exports.createPrettyLogger = createPrettyLogger;
@@ -439,7 +439,7 @@ class BunSQLite {
439
439
  */
440
440
  async createTable(op) {
441
441
  const { sql } = ddl.createTable(op);
442
- this.loggers.forEach(logger => logger.debug(sql, []));
442
+ this.loggers.forEach(logger => logger.query.debug(sql, []));
443
443
  this.conn.run(sql);
444
444
  }
445
445
  /**
@@ -453,7 +453,7 @@ class BunSQLite {
453
453
  records: op.records.map(createEncoder(op.insertShape)),
454
454
  insertShape: u.mapKeys(op.insertShape, u.snakeCase),
455
455
  });
456
- this.loggers.forEach(logger => logger.debug(sql, params));
456
+ this.loggers.forEach(logger => logger.query.debug(sql, params));
457
457
  const stmt = this.conn.prepare(sql);
458
458
  const rows = stmt.all(...params);
459
459
  return rows.map(createDecoder(op.returnShape));
@@ -465,7 +465,7 @@ class BunSQLite {
465
465
  */
466
466
  async query(op) {
467
467
  const { sql, params } = ddl.select(op);
468
- this.loggers.forEach(logger => logger.debug(sql, params));
468
+ this.loggers.forEach(logger => logger.query.debug(sql, params));
469
469
  const stmt = this.conn.prepare(sql);
470
470
  const rows = stmt.all(...params);
471
471
  return rows.map(createDecoder(op.select));
@@ -1,3 +1,4 @@
1
+ import ora from 'ora';
1
2
  import { create, expr, from, into, table } from './index';
2
3
  const migrations = table('migrations', t => ({
3
4
  id: t.string({ size: 36 }).primaryKey(),
@@ -9,6 +10,7 @@ const migrations = table('migrations', t => ({
9
10
  * @version 1
10
11
  */
11
12
  export const defineMigrations = (migs) => async (db) => {
13
+ let spinner;
12
14
  await create.table(migrations, { ifNotExists: true }).onto(db);
13
15
  for (const [id, migration] of Object.entries(migs)) {
14
16
  const alreadyMigrated = await from(migrations.as('m'))
@@ -16,11 +18,13 @@ export const defineMigrations = (migs) => async (db) => {
16
18
  .exists(db);
17
19
  if (alreadyMigrated)
18
20
  continue;
19
- console.log(`running migration ${id}...`);
21
+ spinner ??= ora('running migrations...').start();
22
+ spinner.text = `running migration ${id}...`;
23
+ // @TODO run in a transaction
20
24
  await migration(db);
21
- console.log('done');
22
25
  await into(migrations)
23
26
  .insert({ id, migratedAt: new Date() })
24
27
  .run(db);
25
28
  }
29
+ spinner?.stop();
26
30
  };
package/dist/esm/index.js CHANGED
@@ -433,7 +433,6 @@ const is = {
433
433
  column: (expr) => u.isPlainObject(expr) && 'type' in expr && 'schema' in expr,
434
434
  };
435
435
  ;
436
- ;
437
436
  // // // // // // // // // // // // // // // // // // // // // // // //
438
437
  // CREATE STATEMENTS //
439
438
  // // // // // // // // // // // // // // // // // // // // // // // //
@@ -7,5 +7,7 @@ import {} from './index';
7
7
  * @version 1
8
8
  */
9
9
  export const createPrettyLogger = () => ({
10
- debug: (sql, params) => { process.stdout.write(highlight(sql) + ' ' + inspect(params, false, null, true) + '\n'); },
10
+ query: {
11
+ debug: (sql, params) => { process.stdout.write(highlight(sql) + ' ' + inspect(params, false, null, true) + '\n'); },
12
+ },
11
13
  });
@@ -964,11 +964,19 @@ type DDL = {
964
964
  */
965
965
  interface ILogger {
966
966
  /**
967
- * @public Logs a query that has executed successfully.
967
+ * @public Query specific logging methods, so this interface plays
968
+ * nice with existing logging libraries (by extending them).
968
969
  * @since 0.1.0
969
970
  * @version 1
970
971
  */
971
- debug(sql: string, params: any[]): void;
972
+ query: {
973
+ /**
974
+ * @public Logs a query that has executed successfully.
975
+ * @since 0.1.0
976
+ * @version 1
977
+ */
978
+ debug(sql: string, params: any[]): void;
979
+ };
972
980
  }
973
981
  /**
974
982
  * @public The interface that all database adapters must implement.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rwillians/qx",
3
3
  "description": "A teeny tiny ORM for SQLite.",
4
- "version": "0.1.6",
4
+ "version": "0.1.7",
5
5
  "author": "Rafael Willians <me@rwillians.com>",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -56,6 +56,7 @@
56
56
  },
57
57
  "dependencies": {
58
58
  "@standard-schema/spec": "^1.0.0",
59
+ "ora": "^9.0.0",
59
60
  "sql-highlight": "^6.1.0"
60
61
  },
61
62
  "devDependencies": {