@rwillians/qx 0.1.19 → 0.1.21

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.
@@ -3,12 +3,133 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createConsoleLogger = void 0;
4
4
  const node_util_1 = require("node:util");
5
5
  /**
6
- * @public Creates a basic console logger that logs all queries.
6
+ * @private Non-exhaustive table of ASCII codes for styled console
7
+ * output.
8
+ * @since 0.1.21
9
+ * @version 1
10
+ */
11
+ const ASCII_STYLE_CODES = {
12
+ blue: { open: 34, close: 39 },
13
+ bold: { open: 1, close: 22 },
14
+ brightRed: { open: 91, close: 39 },
15
+ dim: { open: 2, close: 22 },
16
+ green: { open: 32, close: 39 },
17
+ italic: { open: 3, close: 23 },
18
+ red: { open: 31, close: 39 },
19
+ yellow: { open: 33, close: 39 },
20
+ };
21
+ /**
22
+ * @private A registry of style functions for console output, one for
23
+ * each style in the ASCII codes table.
24
+ * @since 0.1.21
25
+ * @version 1
26
+ */
27
+ const s = Object.fromEntries(Object
28
+ .entries(ASCII_STYLE_CODES)
29
+ .map(([key, style]) => [key, (str) => `\u001b[${style.open}m${str.toString()}\u001b[${style.close}m`])
30
+ .concat([['default', (str) => str.toString()]]));
31
+ /**
32
+ * @private Split the given query into a tuple, where the first elment
33
+ * is the statement name (e.g. SELECT, INSERT, etc) and the
34
+ * second element is the rest of the query.
35
+ * @since 0.1.21
36
+ * @version 1
37
+ */
38
+ const splitAtStatementName = (str) => [str.split(' ')[0], str.slice(str.indexOf(' '))];
39
+ /**
40
+ * @private Paints the given SQL depending on what it does (e.g.
41
+ * DELETEs are red, SELECTs are green, etc).
42
+ * @since 0.1.21
43
+ * @version 1
44
+ */
45
+ const dye = (sql) => {
46
+ const [statement, rest] = splitAtStatementName(sql);
47
+ if (statement === 'INSERT')
48
+ return s.green(`${s.bold(statement)} ${rest}`);
49
+ if (statement === 'SELECT')
50
+ return s.blue(`${s.bold(statement)} ${rest}`);
51
+ if (statement === 'UPDATE')
52
+ return s.yellow(`${s.bold(statement)} ${rest}`);
53
+ if (statement === 'DELETE')
54
+ return s.brightRed(`${s.bold(statement)} ${rest}`);
55
+ return sql;
56
+ };
57
+ /**
58
+ * @private Renders a query parameter value for logging purposes.
59
+ * @since 0.1.21
60
+ * @version 1
61
+ */
62
+ const render = (value) => {
63
+ if (value === null)
64
+ return s.dim('null');
65
+ if (value === undefined)
66
+ return s.dim('null');
67
+ if (typeof value === 'boolean')
68
+ return value.toString();
69
+ if (typeof value === 'number')
70
+ return s.blue(value.toString());
71
+ if (typeof value === 'string')
72
+ return [s.dim('`'), value, s.dim('`')].join('');
73
+ if (value instanceof Date)
74
+ return s.blue(`${value.toISOString()}`);
75
+ if (Array.isArray(value))
76
+ return [s.dim('['), value.map(render).join(s.dim(', ')), s.dim(']')].join('');
77
+ throw new Error(`Unable to render value: ${(0, node_util_1.inspect)(value, true, null, true)}`);
78
+ };
79
+ /**
80
+ * @private A function that [p]retty-[p]rints the given SQL query, its
81
+ * parameters and its error.
82
+ * @since 0.1.21
83
+ * @version 1
84
+ */
85
+ const pp = (sql, params, error) => [
86
+ dye(sql),
87
+ ' ',
88
+ render(params),
89
+ error
90
+ ? ('\n\n' + s.red(`${s.bold(error.constructor.name)} ${error.message}\n${error.stack}`.trim()) + '\n\n')
91
+ : '',
92
+ ].join('');
93
+ /**
94
+ * @private A function that prints the given SQL query, its parameters
95
+ * and its error as [p]lain [t]ext (no ASCII styling).
96
+ * @since 0.1.21
97
+ * @version 1
98
+ */
99
+ const pt = (sql, params, error) => [
100
+ sql,
101
+ ' ',
102
+ render(params),
103
+ error
104
+ ? ('\n\n' + (`${error.constructor.name} ${error.message}\n${error.stack}`.trim()) + '\n\n')
105
+ : '',
106
+ ].join('');
107
+ /**
108
+ * @private Returns a function that pretty-prints its given arguments
109
+ * to the specified stream.
110
+ * @since 0.1.21
111
+ * @version 1
112
+ */
113
+ const handler = (stream, fn) => (...args) => { stream.write(fn(...args)); };
114
+ /**
115
+ * @public Creates a basic console logger that pretty-prints queries.
116
+ *
117
+ * Pretty printing is enabled by default but you can disable
118
+ * it to print plain-text instead (no ASCII styling), just
119
+ * set the option `pretty` to `false`.
7
120
  * @since 0.1.0
8
- * @version 2
121
+ * @version 3
122
+ *
123
+ * @example
124
+ * ```ts
125
+ * import { createConsoleLogger } from '@rwillians/qx/console-logger';
126
+ *
127
+ * const prettyLogger = createConsoleLogger();
128
+ * const plainLogger = createConsoleLogger({ pretty: false });
129
+ * ```
9
130
  */
10
- const createConsoleLogger = () => ({
11
- debug: (sql, params) => { process.stdout.write(sql + ' ' + (0, node_util_1.inspect)(params, false, null, true) + '\n'); },
12
- error: (sql, params) => { process.stderr.write(sql + ' ' + (0, node_util_1.inspect)(params, false, null, true) + '\n'); },
131
+ const createConsoleLogger = (opts = { pretty: true }) => ({
132
+ debug: handler(process.stdout, opts.pretty ? pp : pt),
133
+ error: handler(process.stderr, opts.pretty ? pp : pt),
13
134
  });
14
135
  exports.createConsoleLogger = createConsoleLogger;
package/dist/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.t = exports.transaction = exports.is = exports.into = exports.from = exports.expr = exports.table = void 0;
3
+ exports.t = exports.transaction = exports.is = exports.into = exports.from = exports.expr = exports.table = exports.column = void 0;
4
4
  const std = require("./standard-schema");
5
5
  const u = require("./utils");
6
6
  // // // // // // // // // // // // // // // // // // // // // // // //
@@ -89,6 +89,7 @@ class ColumnPropsBuilder {
89
89
  * @version 1
90
90
  */
91
91
  const defineColumn = (baseProps) => new ColumnPropsBuilder(baseProps);
92
+ exports.column = defineColumn;
92
93
  /**
93
94
  * @public Built-in column types.
94
95
  * @since 0.1.0
@@ -1,11 +1,132 @@
1
1
  import { inspect } from 'node:util';
2
2
  import {} from './index';
3
3
  /**
4
- * @public Creates a basic console logger that logs all queries.
4
+ * @private Non-exhaustive table of ASCII codes for styled console
5
+ * output.
6
+ * @since 0.1.21
7
+ * @version 1
8
+ */
9
+ const ASCII_STYLE_CODES = {
10
+ blue: { open: 34, close: 39 },
11
+ bold: { open: 1, close: 22 },
12
+ brightRed: { open: 91, close: 39 },
13
+ dim: { open: 2, close: 22 },
14
+ green: { open: 32, close: 39 },
15
+ italic: { open: 3, close: 23 },
16
+ red: { open: 31, close: 39 },
17
+ yellow: { open: 33, close: 39 },
18
+ };
19
+ /**
20
+ * @private A registry of style functions for console output, one for
21
+ * each style in the ASCII codes table.
22
+ * @since 0.1.21
23
+ * @version 1
24
+ */
25
+ const s = Object.fromEntries(Object
26
+ .entries(ASCII_STYLE_CODES)
27
+ .map(([key, style]) => [key, (str) => `\u001b[${style.open}m${str.toString()}\u001b[${style.close}m`])
28
+ .concat([['default', (str) => str.toString()]]));
29
+ /**
30
+ * @private Split the given query into a tuple, where the first elment
31
+ * is the statement name (e.g. SELECT, INSERT, etc) and the
32
+ * second element is the rest of the query.
33
+ * @since 0.1.21
34
+ * @version 1
35
+ */
36
+ const splitAtStatementName = (str) => [str.split(' ')[0], str.slice(str.indexOf(' '))];
37
+ /**
38
+ * @private Paints the given SQL depending on what it does (e.g.
39
+ * DELETEs are red, SELECTs are green, etc).
40
+ * @since 0.1.21
41
+ * @version 1
42
+ */
43
+ const dye = (sql) => {
44
+ const [statement, rest] = splitAtStatementName(sql);
45
+ if (statement === 'INSERT')
46
+ return s.green(`${s.bold(statement)} ${rest}`);
47
+ if (statement === 'SELECT')
48
+ return s.blue(`${s.bold(statement)} ${rest}`);
49
+ if (statement === 'UPDATE')
50
+ return s.yellow(`${s.bold(statement)} ${rest}`);
51
+ if (statement === 'DELETE')
52
+ return s.brightRed(`${s.bold(statement)} ${rest}`);
53
+ return sql;
54
+ };
55
+ /**
56
+ * @private Renders a query parameter value for logging purposes.
57
+ * @since 0.1.21
58
+ * @version 1
59
+ */
60
+ const render = (value) => {
61
+ if (value === null)
62
+ return s.dim('null');
63
+ if (value === undefined)
64
+ return s.dim('null');
65
+ if (typeof value === 'boolean')
66
+ return value.toString();
67
+ if (typeof value === 'number')
68
+ return s.blue(value.toString());
69
+ if (typeof value === 'string')
70
+ return [s.dim('`'), value, s.dim('`')].join('');
71
+ if (value instanceof Date)
72
+ return s.blue(`${value.toISOString()}`);
73
+ if (Array.isArray(value))
74
+ return [s.dim('['), value.map(render).join(s.dim(', ')), s.dim(']')].join('');
75
+ throw new Error(`Unable to render value: ${inspect(value, true, null, true)}`);
76
+ };
77
+ /**
78
+ * @private A function that [p]retty-[p]rints the given SQL query, its
79
+ * parameters and its error.
80
+ * @since 0.1.21
81
+ * @version 1
82
+ */
83
+ const pp = (sql, params, error) => [
84
+ dye(sql),
85
+ ' ',
86
+ render(params),
87
+ error
88
+ ? ('\n\n' + s.red(`${s.bold(error.constructor.name)} ${error.message}\n${error.stack}`.trim()) + '\n\n')
89
+ : '',
90
+ ].join('');
91
+ /**
92
+ * @private A function that prints the given SQL query, its parameters
93
+ * and its error as [p]lain [t]ext (no ASCII styling).
94
+ * @since 0.1.21
95
+ * @version 1
96
+ */
97
+ const pt = (sql, params, error) => [
98
+ sql,
99
+ ' ',
100
+ render(params),
101
+ error
102
+ ? ('\n\n' + (`${error.constructor.name} ${error.message}\n${error.stack}`.trim()) + '\n\n')
103
+ : '',
104
+ ].join('');
105
+ /**
106
+ * @private Returns a function that pretty-prints its given arguments
107
+ * to the specified stream.
108
+ * @since 0.1.21
109
+ * @version 1
110
+ */
111
+ const handler = (stream, fn) => (...args) => { stream.write(fn(...args)); };
112
+ /**
113
+ * @public Creates a basic console logger that pretty-prints queries.
114
+ *
115
+ * Pretty printing is enabled by default but you can disable
116
+ * it to print plain-text instead (no ASCII styling), just
117
+ * set the option `pretty` to `false`.
5
118
  * @since 0.1.0
6
- * @version 2
119
+ * @version 3
120
+ *
121
+ * @example
122
+ * ```ts
123
+ * import { createConsoleLogger } from '@rwillians/qx/console-logger';
124
+ *
125
+ * const prettyLogger = createConsoleLogger();
126
+ * const plainLogger = createConsoleLogger({ pretty: false });
127
+ * ```
7
128
  */
8
- export const createConsoleLogger = () => ({
9
- debug: (sql, params) => { process.stdout.write(sql + ' ' + inspect(params, false, null, true) + '\n'); },
10
- error: (sql, params) => { process.stderr.write(sql + ' ' + inspect(params, false, null, true) + '\n'); },
129
+ export const createConsoleLogger = (opts = { pretty: true }) => ({
130
+ debug: handler(process.stdout, opts.pretty ? pp : pt),
131
+ error: handler(process.stderr, opts.pretty ? pp : pt),
11
132
  });
package/dist/esm/index.js CHANGED
@@ -689,4 +689,4 @@ const from = (table) => new QueryBuilder({
689
689
  // // // // // // // // // // // // // // // // // // // // // // // //
690
690
  // EXPORTS //
691
691
  // // // // // // // // // // // // // // // // // // // // // // // //
692
- export { defineTable as table, expr, from, into, is, transaction, types as t, };
692
+ export { defineColumn as column, defineTable as table, expr, from, into, is, transaction, types as t, };
@@ -1,7 +1,21 @@
1
1
  import { type ILogger } from './index';
2
2
  /**
3
- * @public Creates a basic console logger that logs all queries.
3
+ * @public Creates a basic console logger that pretty-prints queries.
4
+ *
5
+ * Pretty printing is enabled by default but you can disable
6
+ * it to print plain-text instead (no ASCII styling), just
7
+ * set the option `pretty` to `false`.
4
8
  * @since 0.1.0
5
- * @version 2
9
+ * @version 3
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { createConsoleLogger } from '@rwillians/qx/console-logger';
14
+ *
15
+ * const prettyLogger = createConsoleLogger();
16
+ * const plainLogger = createConsoleLogger({ pretty: false });
17
+ * ```
6
18
  */
7
- export declare const createConsoleLogger: () => ILogger;
19
+ export declare const createConsoleLogger: (opts?: {
20
+ pretty: boolean;
21
+ }) => ILogger;
@@ -310,6 +310,12 @@ declare class ColumnPropsBuilder<T extends ColumnProps = ColumnProps> {
310
310
  unique: true;
311
311
  }>>;
312
312
  }
313
+ /**
314
+ * @public A way to define a column with a custom standard schema.
315
+ * @since 0.1.0
316
+ * @version 1
317
+ */
318
+ declare const defineColumn: <T extends ColumnProps>(baseProps: T) => ColumnPropsBuilder<T>;
313
319
  /**
314
320
  * @public Built-in column types.
315
321
  * @since 0.1.0
@@ -1224,4 +1230,4 @@ declare class QueryBuilder<T extends Record<string, Aliased<string, Table>>, S e
1224
1230
  * @version 1
1225
1231
  */
1226
1232
  declare const from: <S extends string, T extends Aliased<S, Table>>(table: T) => QueryBuilder<{ [K in T[typeof TABLE_ALIAS]]: T; }, T>;
1227
- export { type CodecsRegistry, type Column, type CreateTableStatement, type DDL, type Expr, type ExprAnd, type ExprBinaryOp, type ExprEq, type ExprGt, type ExprGte, type ExprIn, type ExprIs, type ExprIsNot, type ExprLike, type ExprLiteral, type ExprLt, type ExprLte, type ExprNe, type ExprNot, type ExprNotIn, type ExprNotLike, type ExprOr, type IDatabase, type ILogger, type Join, type InsertStatement, type OrderDirection, type Primitive, type PrimitiveToNativeTypeFactory, type SelectStatement, type Table, defineTable as table, expr, from, into, is, transaction, types as t, };
1233
+ export { type CodecsRegistry, type Column, type CreateTableStatement, type DDL, type Expr, type ExprAnd, type ExprBinaryOp, type ExprEq, type ExprGt, type ExprGte, type ExprIn, type ExprIs, type ExprIsNot, type ExprLike, type ExprLiteral, type ExprLt, type ExprLte, type ExprNe, type ExprNot, type ExprNotIn, type ExprNotLike, type ExprOr, type IDatabase, type ILogger, type Join, type InsertStatement, type OrderDirection, type Primitive, type PrimitiveToNativeTypeFactory, type SelectStatement, type Table, defineColumn as column, defineTable as table, expr, from, into, is, transaction, types as t, };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rwillians/qx",
3
3
  "description": "A zero-dependencies teeny tiny ORM for SQLite.",
4
- "version": "0.1.19",
4
+ "version": "0.1.21",
5
5
  "author": "Rafael Willians <me@rwillians.com>",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -62,7 +62,8 @@
62
62
  "build:esm": "bun run --bun tsc --project tsconfig-esm.json"
63
63
  },
64
64
  "devDependencies": {
65
- "@types/bun": "latest"
65
+ "@types/bun": "^1.3.9",
66
+ "@types/node": "^25.3.0"
66
67
  },
67
68
  "peerDependencies": {
68
69
  "typescript": "^5.9.3"