@sqb/builder 4.23.3 → 4.24.1

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sqb/builder",
3
3
  "description": "Extensible multi-dialect SQL query builder written with TypeScript",
4
- "version": "4.23.3",
4
+ "version": "4.24.1",
5
5
  "author": "Panates",
6
6
  "license": "Apache-2.0",
7
7
  "dependencies": {
package/query/query.d.ts CHANGED
@@ -4,6 +4,8 @@ import { GenerateOptions, GenerateResult } from '../types.js';
4
4
  export declare interface Query extends EventEmitter {
5
5
  }
6
6
  export declare abstract class Query extends Serializable {
7
+ protected _comment?: string;
8
+ protected _commentDialect?: string[];
7
9
  protected _params?: Record<string, any>;
8
10
  constructor();
9
11
  /**
@@ -11,4 +13,5 @@ export declare abstract class Query extends Serializable {
11
13
  */
12
14
  generate(options?: GenerateOptions): GenerateResult;
13
15
  values(obj: any): this;
16
+ comment(text: string, dialect?: string[]): this;
14
17
  }
package/query/query.js CHANGED
@@ -4,6 +4,8 @@ import merge from 'putil-merge';
4
4
  import { Serializable } from '../serializable.js';
5
5
  import { SerializeContext } from '../serialize-context.js';
6
6
  export class Query extends Serializable {
7
+ _comment;
8
+ _commentDialect;
7
9
  _params;
8
10
  constructor() {
9
11
  super();
@@ -13,14 +15,22 @@ export class Query extends Serializable {
13
15
  * Generates Sql script
14
16
  */
15
17
  generate(options) {
16
- const ctx = new SerializeContext(options);
18
+ const ctx = new SerializeContext(this, options);
17
19
  if (this._params)
18
20
  ctx.params = { ...ctx.params, ...this._params };
19
21
  ctx.serializeHooks = this.listeners('serialize');
20
22
  /* generate output */
21
- const sql = this._serialize(ctx);
23
+ let sql = this._serialize(ctx);
24
+ sql = flattenText(sql, { noWrap: !ctx.prettyPrint });
25
+ if (this._comment &&
26
+ (!ctx.dialect ||
27
+ !this._commentDialect ||
28
+ this._commentDialect.includes(ctx.dialect))) {
29
+ const lines = '-- ' + this._comment.split('\n').join('\n-- ') + '\n';
30
+ sql = lines + sql;
31
+ }
22
32
  return {
23
- sql: flattenText(sql, { noWrap: !ctx.prettyPrint }),
33
+ sql,
24
34
  params: ctx.preparedParams,
25
35
  paramOptions: ctx.paramOptions,
26
36
  returningFields: ctx.returningFields,
@@ -32,5 +42,10 @@ export class Query extends Serializable {
32
42
  this._params = obj;
33
43
  return this;
34
44
  }
45
+ comment(text, dialect) {
46
+ this._comment = text;
47
+ this._commentDialect = dialect;
48
+ return this;
49
+ }
35
50
  }
36
51
  merge(Query.prototype, EventEmitter.prototype, { descriptor: true });
@@ -1,5 +1,7 @@
1
+ import type { Query } from './query/query.js';
1
2
  import { DefaultSerializeFunction, GenerateOptions, ParamOptions } from './types.js';
2
3
  export declare class SerializeContext implements GenerateOptions {
4
+ readonly rootQuery: Query;
3
5
  readonly reservedWords: string[];
4
6
  dialect?: string;
5
7
  prettyPrint?: boolean;
@@ -14,7 +16,7 @@ export declare class SerializeContext implements GenerateOptions {
14
16
  alias?: string;
15
17
  }[];
16
18
  strictParamGenId?: number;
17
- constructor(opts?: GenerateOptions);
19
+ constructor(rootQuery: Query, opts?: GenerateOptions);
18
20
  /**
19
21
  * Performs a fallback mechanism, tries hook functions, extensions than default function to serialize
20
22
  */
@@ -3,6 +3,7 @@ import { SerializerRegistry } from './extensions.js';
3
3
  import { Serializable } from './serializable.js';
4
4
  import { isLogicalOperator, isQuery, isSerializable } from './typeguards.js';
5
5
  export class SerializeContext {
6
+ rootQuery;
6
7
  reservedWords = [
7
8
  'schema',
8
9
  'table',
@@ -71,7 +72,8 @@ export class SerializeContext {
71
72
  preparedParams;
72
73
  returningFields;
73
74
  strictParamGenId;
74
- constructor(opts) {
75
+ constructor(rootQuery, opts) {
76
+ this.rootQuery = rootQuery;
75
77
  if (opts)
76
78
  Object.assign(this, opts);
77
79
  }