@flowblade/sql-tag-format 0.0.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-current Sébastien Vanvelthem
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,142 @@
1
+ # @flowblade/sql-tag-format
2
+
3
+ Sql formatter utilities for [@flowblade/sql-tag](https://github.com/belgattitude/flowblade/tree/main/packages/sql-tag#readme)
4
+
5
+ [![npm](https://img.shields.io/npm/v/@flowblade/sql-tag-format?style=for-the-badge&label=Npm&labelColor=444&color=informational)](https://www.npmjs.com/package/@flowblade/sql-tag-format)
6
+ [![changelog](https://img.shields.io/static/v1?label=&message=changelog&logo=github&style=for-the-badge&labelColor=444&color=informational)](https://github.com/belgattitude/flowblade/blob/main/packages/sql-tag-format/CHANGELOG.md)
7
+ [![codecov](https://img.shields.io/codecov/c/github/belgattitude/flowblade?logo=codecov&label=Unit&flag=flowblade-sql-tag-format-unit&style=for-the-badge&labelColor=444)](https://app.codecov.io/gh/belgattitude/flowblade/tree/main/packages%2Fsql-tag-format)
8
+ [![bundles](https://img.shields.io/static/v1?label=&message=cjs|esm@treeshake&logo=webpack&style=for-the-badge&labelColor=444&color=informational)](https://github.com/belgattitude/flowblade/blob/main/packages/sql-tag-format/.size-limit.cjs)
9
+ [![node](https://img.shields.io/static/v1?label=Node&message=18%2b&logo=node.js&style=for-the-badge&labelColor=444&color=informational)](#compatibility)
10
+ [![browserslist](https://img.shields.io/static/v1?label=Browser&message=%3E96%25&logo=googlechrome&style=for-the-badge&labelColor=444&color=informational)](#compatibility)
11
+ [![size](https://img.shields.io/bundlephobia/minzip/@flowblade/sql-tag-format@latest?label=Max&style=for-the-badge&labelColor=444&color=informational)](https://bundlephobia.com/package/@flowblade/sql-tag-format@latest)
12
+ [![downloads](https://img.shields.io/npm/dm/@flowblade/sql-tag-format?style=for-the-badge&labelColor=444)](https://www.npmjs.com/package/@flowblade/sql-tag-format)
13
+ [![license](https://img.shields.io/npm/l/@flowblade/sql-tag-format?style=for-the-badge&labelColor=444)](https://github.com/belgattitude/flowblade/blob/main/LICENSE)
14
+
15
+ ## Features
16
+
17
+ - 🛡️  Tested on [node 18-22, browser, cloudflare workers and runtime/edge](#compatibility).
18
+ - 🗝️  Available in ESM and CJS formats.
19
+
20
+ ## Install
21
+
22
+ ```bash
23
+ yarn add @flowblade/sql-tag-format @flowblade/sql-tag
24
+ ```
25
+ ## Usage
26
+
27
+ ```typescript
28
+ import {sql} from '@flowblade/sql-tag';
29
+ import {SqlFormatter} from "@flowblade/sql-tag-format";
30
+
31
+ // 👈 Unvalidated parameters
32
+ const params = {
33
+ country: 'BE',
34
+ users: ['John', 'Doe'],
35
+ ids: [1],
36
+ };
37
+
38
+ const query = sql<{
39
+ id: number;
40
+ username: string;
41
+ }>`
42
+ SELECT id, username FROM users
43
+ WHERE country = ${params.country} -- 👈 simple
44
+ AND username IN (${sql.join(params.users)}) -- 👈 sql.join
45
+
46
+ -- 👇 conditional clause with sql.empty
47
+ ${params.ids.length > 0 ? sql`AND id IN (${sql.join(params.ids)})` : sql.empty}
48
+ `;
49
+
50
+ const pgsqlFormatter = new SqlFormatter('postgresql');
51
+
52
+ const formatted = pgsqlFormatter.formatOrNull(query);
53
+
54
+ try {
55
+ const formatted = pgsqlFormatter.formatOrThrow(query);
56
+ } catch (e) {
57
+ console.error(e);
58
+ }
59
+ ```
60
+
61
+ ## Supported dialects
62
+
63
+ | Dialect | Description |
64
+ |---------------|------------------------------|
65
+ | bigquery | Google SQL |
66
+ | db2 | DB2 SQL |
67
+ | db2i | DB2 iSeries SQL |
68
+ | hive | HiveQL |
69
+ | mariadb | MariaDB SQL |
70
+ | mysql | MySQL SQL |
71
+ | n1ql | N1QL |
72
+ | plsql | PL/SQL |
73
+ | postgresql | PostgreSQL SQL |
74
+ | redshift | Redshift SQL |
75
+ | singlestoredb | SingleStore SQL |
76
+ | snowflake | Snowflake SQL |
77
+ | spark | Spark SQL |
78
+ | sql | ANSI SQL |
79
+ | sqlite | SQLite SQL |
80
+ | tidb | TiDB SQL |
81
+ | transactsql | Mssql / Transact-SQL (T-SQL) |
82
+ | trino | Trino SQL |
83
+ | tsql | Transact-SQL |
84
+
85
+ ## Bundle size
86
+
87
+ Bundle size is tracked by a [size-limit configuration](https://github.com/belgattitude/flowblade/blob/main/packages/sql-tag/.size-limit.ts)
88
+
89
+ | Scenario (esm) | Size (compressed) |
90
+ |-----------------------------------------------------------|------------------:|
91
+ | `import { SqlFormatter } from '@flowblade/sql-tag-format` | ~ 47Kb |
92
+
93
+ ## Compatibility
94
+
95
+ | Level | CI | Description |
96
+ |------------|----|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
97
+ | Node | ✅ | CI for 18.x, 20.x & 22.x. |
98
+ | Browser | ✅ | Tested with latest chrome (vitest/playwright) |
99
+ | Browsers | ✅ | [> 96%](https://browserslist.dev/?q=ZGVmYXVsdHMsIGNocm9tZSA%2BPSA5NixmaXJlZm94ID49IDkwLGVkZ2UgPj0gMTksc2FmYXJpID49IDEyLGlvcyA%2BPSAxMixvcGVyYSA%2BPSA3Nw%3D%3D) on 07/2024. Mins to [Chrome 96+, Firefox 90+, Edge 19+, iOS 12+, Safari 12+, Opera 77+](https://github.com/belgattitude/flowblade/blob/main/packages/sql-tag/.browserslistrc) |
100
+ | Edge | ✅ | Ensured on CI with [@vercel/edge-runtime](https://github.com/vercel/edge-runtime). |
101
+ | Cloudflare | ✅ | Ensured with @cloudflare/vitest-pool-workers (see [wrangler.toml](https://github.com/belgattitude/flowblade/blob/main/devtools/vitest/wrangler.toml) |
102
+ | Typescript | ✅ | TS 5.0 + / [are-the-type-wrong](https://github.com/arethetypeswrong/arethetypeswrong.github.io) checks on CI. |
103
+ | ES2022 | ✅ | Dist files checked with [es-check](https://github.com/yowainwright/es-check) |
104
+ | Performance| ✅ | Monitored with [codspeed.io](https://codspeed.io/belgattitude/flowblade) |
105
+
106
+ ## Contributors
107
+
108
+ Contributions are welcome. Have a look to the [CONTRIBUTING](https://github.com/belgattitude/flowblade/blob/main/CONTRIBUTING.md) document.
109
+
110
+ ## Sponsors
111
+
112
+ [Sponsor](<[sponsorship](https://github.com/sponsors/belgattitude)>), [coffee](<(https://ko-fi.com/belgattitude)>),
113
+ or star – All is spent for quality time with loved ones. Thanks ! 🙏❤️
114
+
115
+ ### Special thanks to
116
+
117
+ <table>
118
+ <tr>
119
+ <td>
120
+ <a href="https://www.jetbrains.com/?ref=belgattitude" target="_blank">
121
+ <img width="65" src="https://asset.brandfetch.io/idarKiKkI-/id53SttZhi.jpeg" alt="Jetbrains logo" />
122
+ </a>
123
+ </td>
124
+ <td>
125
+ <a href="https://www.embie.be/?ref=belgattitude" target="_blank">
126
+ <img width="65" src="https://avatars.githubusercontent.com/u/98402122?s=200&v=4" alt="Jetbrains logo" />
127
+ </a>
128
+ </td>
129
+ </tr>
130
+ <tr>
131
+ <td align="center">
132
+ <a href="https://www.jetbrains.com/?ref=belgattitude" target="_blank">JetBrains</a>
133
+ </td>
134
+ <td align="center">
135
+ <a href="https://www.embie.be/?ref=belgattitude" target="_blank">Embie.be</a>
136
+ </td>
137
+ </tr>
138
+ </table>
139
+
140
+ ## License
141
+
142
+ MIT © [Sébastien Vanvelthem](https://github.com/belgattitude) and contributors.
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ 'use strict';var sqlFormatter=require('sql-formatter');var m=Object.defineProperty;var s=(o,r,t)=>r in o?m(o,r,{enumerable:true,configurable:true,writable:true,value:t}):o[r]=t;var e=(o,r,t)=>s(o,typeof r!="symbol"?r+"":r,t);var l=class{constructor(r,t){this.dialect=r;this.formatterOptions=t;e(this,"formatOrNull",(r,t)=>{try{return this.formatOrThrow(r,t)}catch{return null}});e(this,"formatOrThrow",(r,t)=>{var a,i;let n=(a=t==null?undefined:t.options)!=null?a:this.formatterOptions;return sqlFormatter.format(r.sql,{language:(i=t==null?undefined:t.dialect)!=null?i:this.dialect,...n})});}};exports.SqlFormatter=l;//# sourceMappingURL=index.cjs.map
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/sql-formatter.ts"],"names":["SqlFormatter","dialect","formatterOptions","__publicField","sql","params","_a","_b","options","sqlFormat"],"mappings":"uDACA,IAAA,CAAA,CAAA,MAAA,CAAA,cAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,UAAA,CAAA,IAAA,CAAA,YAAA,CAAA,IAAA,CAAA,QAAA,CAAA,IAAA,CAAA,KAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA,OAAA,CAAA,EAAA,QAAA,CAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAoBO,IAAMA,EAAN,KAAmB,CAmCxB,WACUC,CAAAA,CAAAA,CACAC,EACR,CAFQ,IAAA,CAAA,OAAA,CAAAD,CACA,CAAA,IAAA,CAAA,gBAAA,CAAAC,EAiBVC,CAAA,CAAA,IAAA,CAAA,cAAA,CAAe,CACbC,CAAAA,CACAC,IACkB,CAClB,GAAI,CACF,OAAO,KAAK,aAAcD,CAAAA,CAAAA,CAAKC,CAAM,CACvC,MAAQ,CACN,OAAO,IACT,CACF,GAqBAF,CAAA,CAAA,IAAA,CAAA,eAAA,CAAgB,CAACC,CAAAA,CAAsBC,IAAkC,CAzG3E,IAAAC,CAAAC,CAAAA,CAAAA,CA0GI,IAAMC,CAAUF,CAAAA,CAAAA,CAAAA,CAAAD,CAAA,EAAA,IAAA,CAAA,SAAA,CAAAA,EAAQ,OAAR,GAAA,IAAA,CAAAC,CAAmB,CAAA,IAAA,CAAK,iBACxC,OAAOG,mBAAAA,CAAUL,CAAI,CAAA,GAAA,CAAK,CACxB,QAAUG,CAAAA,CAAAA,CAAAA,CAAAF,CAAA,EAAA,IAAA,CAAA,SAAA,CAAAA,EAAQ,OAAR,GAAA,IAAA,CAAAE,CAAmB,CAAA,IAAA,CAAK,QAClC,GAAGC,CACL,CAAC,CACH,GApDG,CAqDL","file":"index.cjs","sourcesContent":["import type { SqlTag } from '@flowblade/sql-tag';\nimport {\n format as sqlFormat,\n type FormatOptions,\n type SqlLanguage,\n} from 'sql-formatter';\n\nexport type SqlFormatterOptions = Omit<FormatOptions, 'indentStyle'>;\nexport type SqlFormatterDialect = SqlLanguage;\n\ntype FormatParams = {\n /**\n * If not provided will default to the dialect provided in the constructor\n */\n dialect?: SqlFormatterDialect;\n /**\n * If not provided will default to the formatter options provided in the constructor\n */\n options?: Partial<SqlFormatterOptions>;\n};\n\nexport class SqlFormatter {\n /**\n * SqlFormatter constructor\n *\n * @example\n * ```typescript\n * const sqlFormatter = new SqlFormatter('postgresql');\n *\n * // Alternatively, you can pass in options\n * // @see https://github.com/sql-formatter-org/sql-formatter/tree/master?tab=readme-ov-file#configuration-options\n *\n * const pgsqlFormatter = new SqlFormatter('postgresql', {\n * keywordCase: 'preserve',\n * identifierCase: 'preserve',\n * dataTypeCase: 'preserve',\n * functionCase: 'preserve',\n * logicalOperatorNewline: 'before',\n * expressionWidth: 50,\n * linesBetweenQueries: 1,\n * denseOperators: false,\n * newlineBeforeSemicolon: false,\n * useTabs: false,\n * tabWidth: 2,\n * });\n *\n * try {\n * const formatted = pgsqlFormatter.formatOrThrow(\n * 'SELECT * FROM table WHERE id = 1'\n * );\n * } catch (e) {\n * // Might throw something similar to: Parse error: Unexpected \"[col] from\" at line 1 column 8\n * console.log('Error:', e);\n * }\n * ```\n */\n constructor(\n private dialect: SqlFormatterDialect,\n private formatterOptions?: SqlFormatterOptions\n ) {}\n\n /**\n * Format sql to string or return null if sql cannot be parsed\n *\n * @example\n * ```typescript\n * const sqlFormatter = new SqlFormatter('postgresql');\n *\n * const formatted = sqlFormatter.formatOrNull(\n * 'SELECT * FROM table WHERE id = 1'\n * );\n * ```\n *\n * @return string if sql can be parsed, null otherwise\n */\n formatOrNull = (\n sql: SqlTag<unknown>,\n params?: FormatParams\n ): string | null => {\n try {\n return this.formatOrThrow(sql, params);\n } catch {\n return null;\n }\n };\n\n /**\n * Format sql to string or throw an error if sql cannot be parsed\n *\n * @example\n * ```typescript\n * const sqlFormatter = new SqlFormatter('postgresql');\n *\n * try {\n * const formatted = sqlFormatter.formatOrThrow(\n * 'SELECT * FROM table WHERE id = 1'\n * );\n * } catch (e) {\n * // Might throw something similat to: Parse error: Unexpected \"[col] from\" at line 1 column 8\n * console.log('Error:', e);\n * }\n * ```\n *\n * @throws Error is sql cannot be parsed\n */\n formatOrThrow = (sql: SqlTag<unknown>, params?: FormatParams): string => {\n const options = params?.options ?? this.formatterOptions;\n return sqlFormat(sql.sql, {\n language: params?.dialect ?? this.dialect,\n ...options,\n });\n };\n}\n"]}
@@ -0,0 +1,91 @@
1
+ import { SqlTag } from '@flowblade/sql-tag';
2
+ import { FormatOptions, SqlLanguage } from 'sql-formatter';
3
+
4
+ type SqlFormatterOptions = Omit<FormatOptions, 'indentStyle'>;
5
+ type SqlFormatterDialect = SqlLanguage;
6
+ type FormatParams = {
7
+ /**
8
+ * If not provided will default to the dialect provided in the constructor
9
+ */
10
+ dialect?: SqlFormatterDialect;
11
+ /**
12
+ * If not provided will default to the formatter options provided in the constructor
13
+ */
14
+ options?: Partial<SqlFormatterOptions>;
15
+ };
16
+ declare class SqlFormatter {
17
+ private dialect;
18
+ private formatterOptions?;
19
+ /**
20
+ * SqlFormatter constructor
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * const sqlFormatter = new SqlFormatter('postgresql');
25
+ *
26
+ * // Alternatively, you can pass in options
27
+ * // @see https://github.com/sql-formatter-org/sql-formatter/tree/master?tab=readme-ov-file#configuration-options
28
+ *
29
+ * const pgsqlFormatter = new SqlFormatter('postgresql', {
30
+ * keywordCase: 'preserve',
31
+ * identifierCase: 'preserve',
32
+ * dataTypeCase: 'preserve',
33
+ * functionCase: 'preserve',
34
+ * logicalOperatorNewline: 'before',
35
+ * expressionWidth: 50,
36
+ * linesBetweenQueries: 1,
37
+ * denseOperators: false,
38
+ * newlineBeforeSemicolon: false,
39
+ * useTabs: false,
40
+ * tabWidth: 2,
41
+ * });
42
+ *
43
+ * try {
44
+ * const formatted = pgsqlFormatter.formatOrThrow(
45
+ * 'SELECT * FROM table WHERE id = 1'
46
+ * );
47
+ * } catch (e) {
48
+ * // Might throw something similar to: Parse error: Unexpected "[col] from" at line 1 column 8
49
+ * console.log('Error:', e);
50
+ * }
51
+ * ```
52
+ */
53
+ constructor(dialect: SqlFormatterDialect, formatterOptions?: SqlFormatterOptions | undefined);
54
+ /**
55
+ * Format sql to string or return null if sql cannot be parsed
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * const sqlFormatter = new SqlFormatter('postgresql');
60
+ *
61
+ * const formatted = sqlFormatter.formatOrNull(
62
+ * 'SELECT * FROM table WHERE id = 1'
63
+ * );
64
+ * ```
65
+ *
66
+ * @return string if sql can be parsed, null otherwise
67
+ */
68
+ formatOrNull: (sql: SqlTag<unknown>, params?: FormatParams) => string | null;
69
+ /**
70
+ * Format sql to string or throw an error if sql cannot be parsed
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * const sqlFormatter = new SqlFormatter('postgresql');
75
+ *
76
+ * try {
77
+ * const formatted = sqlFormatter.formatOrThrow(
78
+ * 'SELECT * FROM table WHERE id = 1'
79
+ * );
80
+ * } catch (e) {
81
+ * // Might throw something similat to: Parse error: Unexpected "[col] from" at line 1 column 8
82
+ * console.log('Error:', e);
83
+ * }
84
+ * ```
85
+ *
86
+ * @throws Error is sql cannot be parsed
87
+ */
88
+ formatOrThrow: (sql: SqlTag<unknown>, params?: FormatParams) => string;
89
+ }
90
+
91
+ export { SqlFormatter, type SqlFormatterDialect, type SqlFormatterOptions };
@@ -0,0 +1,91 @@
1
+ import { SqlTag } from '@flowblade/sql-tag';
2
+ import { FormatOptions, SqlLanguage } from 'sql-formatter';
3
+
4
+ type SqlFormatterOptions = Omit<FormatOptions, 'indentStyle'>;
5
+ type SqlFormatterDialect = SqlLanguage;
6
+ type FormatParams = {
7
+ /**
8
+ * If not provided will default to the dialect provided in the constructor
9
+ */
10
+ dialect?: SqlFormatterDialect;
11
+ /**
12
+ * If not provided will default to the formatter options provided in the constructor
13
+ */
14
+ options?: Partial<SqlFormatterOptions>;
15
+ };
16
+ declare class SqlFormatter {
17
+ private dialect;
18
+ private formatterOptions?;
19
+ /**
20
+ * SqlFormatter constructor
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * const sqlFormatter = new SqlFormatter('postgresql');
25
+ *
26
+ * // Alternatively, you can pass in options
27
+ * // @see https://github.com/sql-formatter-org/sql-formatter/tree/master?tab=readme-ov-file#configuration-options
28
+ *
29
+ * const pgsqlFormatter = new SqlFormatter('postgresql', {
30
+ * keywordCase: 'preserve',
31
+ * identifierCase: 'preserve',
32
+ * dataTypeCase: 'preserve',
33
+ * functionCase: 'preserve',
34
+ * logicalOperatorNewline: 'before',
35
+ * expressionWidth: 50,
36
+ * linesBetweenQueries: 1,
37
+ * denseOperators: false,
38
+ * newlineBeforeSemicolon: false,
39
+ * useTabs: false,
40
+ * tabWidth: 2,
41
+ * });
42
+ *
43
+ * try {
44
+ * const formatted = pgsqlFormatter.formatOrThrow(
45
+ * 'SELECT * FROM table WHERE id = 1'
46
+ * );
47
+ * } catch (e) {
48
+ * // Might throw something similar to: Parse error: Unexpected "[col] from" at line 1 column 8
49
+ * console.log('Error:', e);
50
+ * }
51
+ * ```
52
+ */
53
+ constructor(dialect: SqlFormatterDialect, formatterOptions?: SqlFormatterOptions | undefined);
54
+ /**
55
+ * Format sql to string or return null if sql cannot be parsed
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * const sqlFormatter = new SqlFormatter('postgresql');
60
+ *
61
+ * const formatted = sqlFormatter.formatOrNull(
62
+ * 'SELECT * FROM table WHERE id = 1'
63
+ * );
64
+ * ```
65
+ *
66
+ * @return string if sql can be parsed, null otherwise
67
+ */
68
+ formatOrNull: (sql: SqlTag<unknown>, params?: FormatParams) => string | null;
69
+ /**
70
+ * Format sql to string or throw an error if sql cannot be parsed
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * const sqlFormatter = new SqlFormatter('postgresql');
75
+ *
76
+ * try {
77
+ * const formatted = sqlFormatter.formatOrThrow(
78
+ * 'SELECT * FROM table WHERE id = 1'
79
+ * );
80
+ * } catch (e) {
81
+ * // Might throw something similat to: Parse error: Unexpected "[col] from" at line 1 column 8
82
+ * console.log('Error:', e);
83
+ * }
84
+ * ```
85
+ *
86
+ * @throws Error is sql cannot be parsed
87
+ */
88
+ formatOrThrow: (sql: SqlTag<unknown>, params?: FormatParams) => string;
89
+ }
90
+
91
+ export { SqlFormatter, type SqlFormatterDialect, type SqlFormatterOptions };
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import {format}from'sql-formatter';var m=Object.defineProperty;var s=(o,r,t)=>r in o?m(o,r,{enumerable:true,configurable:true,writable:true,value:t}):o[r]=t;var e=(o,r,t)=>s(o,typeof r!="symbol"?r+"":r,t);var l=class{constructor(r,t){this.dialect=r;this.formatterOptions=t;e(this,"formatOrNull",(r,t)=>{try{return this.formatOrThrow(r,t)}catch{return null}});e(this,"formatOrThrow",(r,t)=>{var a,i;let n=(a=t==null?undefined:t.options)!=null?a:this.formatterOptions;return format(r.sql,{language:(i=t==null?undefined:t.dialect)!=null?i:this.dialect,...n})});}};export{l as SqlFormatter};//# sourceMappingURL=index.mjs.map
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/sql-formatter.ts"],"names":["SqlFormatter","dialect","formatterOptions","__publicField","sql","params","_a","_b","options","sqlFormat"],"mappings":"mCACA,IAAA,CAAA,CAAA,MAAA,CAAA,cAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,UAAA,CAAA,IAAA,CAAA,YAAA,CAAA,IAAA,CAAA,QAAA,CAAA,IAAA,CAAA,KAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA,OAAA,CAAA,EAAA,QAAA,CAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAoBO,IAAMA,EAAN,KAAmB,CAmCxB,WACUC,CAAAA,CAAAA,CACAC,EACR,CAFQ,IAAA,CAAA,OAAA,CAAAD,CACA,CAAA,IAAA,CAAA,gBAAA,CAAAC,EAiBVC,CAAA,CAAA,IAAA,CAAA,cAAA,CAAe,CACbC,CAAAA,CACAC,IACkB,CAClB,GAAI,CACF,OAAO,KAAK,aAAcD,CAAAA,CAAAA,CAAKC,CAAM,CACvC,MAAQ,CACN,OAAO,IACT,CACF,GAqBAF,CAAA,CAAA,IAAA,CAAA,eAAA,CAAgB,CAACC,CAAAA,CAAsBC,IAAkC,CAzG3E,IAAAC,CAAAC,CAAAA,CAAAA,CA0GI,IAAMC,CAAUF,CAAAA,CAAAA,CAAAA,CAAAD,CAAA,EAAA,IAAA,CAAA,SAAA,CAAAA,EAAQ,OAAR,GAAA,IAAA,CAAAC,CAAmB,CAAA,IAAA,CAAK,iBACxC,OAAOG,MAAAA,CAAUL,CAAI,CAAA,GAAA,CAAK,CACxB,QAAUG,CAAAA,CAAAA,CAAAA,CAAAF,CAAA,EAAA,IAAA,CAAA,SAAA,CAAAA,EAAQ,OAAR,GAAA,IAAA,CAAAE,CAAmB,CAAA,IAAA,CAAK,QAClC,GAAGC,CACL,CAAC,CACH,GApDG,CAqDL","file":"index.mjs","sourcesContent":["import type { SqlTag } from '@flowblade/sql-tag';\nimport {\n format as sqlFormat,\n type FormatOptions,\n type SqlLanguage,\n} from 'sql-formatter';\n\nexport type SqlFormatterOptions = Omit<FormatOptions, 'indentStyle'>;\nexport type SqlFormatterDialect = SqlLanguage;\n\ntype FormatParams = {\n /**\n * If not provided will default to the dialect provided in the constructor\n */\n dialect?: SqlFormatterDialect;\n /**\n * If not provided will default to the formatter options provided in the constructor\n */\n options?: Partial<SqlFormatterOptions>;\n};\n\nexport class SqlFormatter {\n /**\n * SqlFormatter constructor\n *\n * @example\n * ```typescript\n * const sqlFormatter = new SqlFormatter('postgresql');\n *\n * // Alternatively, you can pass in options\n * // @see https://github.com/sql-formatter-org/sql-formatter/tree/master?tab=readme-ov-file#configuration-options\n *\n * const pgsqlFormatter = new SqlFormatter('postgresql', {\n * keywordCase: 'preserve',\n * identifierCase: 'preserve',\n * dataTypeCase: 'preserve',\n * functionCase: 'preserve',\n * logicalOperatorNewline: 'before',\n * expressionWidth: 50,\n * linesBetweenQueries: 1,\n * denseOperators: false,\n * newlineBeforeSemicolon: false,\n * useTabs: false,\n * tabWidth: 2,\n * });\n *\n * try {\n * const formatted = pgsqlFormatter.formatOrThrow(\n * 'SELECT * FROM table WHERE id = 1'\n * );\n * } catch (e) {\n * // Might throw something similar to: Parse error: Unexpected \"[col] from\" at line 1 column 8\n * console.log('Error:', e);\n * }\n * ```\n */\n constructor(\n private dialect: SqlFormatterDialect,\n private formatterOptions?: SqlFormatterOptions\n ) {}\n\n /**\n * Format sql to string or return null if sql cannot be parsed\n *\n * @example\n * ```typescript\n * const sqlFormatter = new SqlFormatter('postgresql');\n *\n * const formatted = sqlFormatter.formatOrNull(\n * 'SELECT * FROM table WHERE id = 1'\n * );\n * ```\n *\n * @return string if sql can be parsed, null otherwise\n */\n formatOrNull = (\n sql: SqlTag<unknown>,\n params?: FormatParams\n ): string | null => {\n try {\n return this.formatOrThrow(sql, params);\n } catch {\n return null;\n }\n };\n\n /**\n * Format sql to string or throw an error if sql cannot be parsed\n *\n * @example\n * ```typescript\n * const sqlFormatter = new SqlFormatter('postgresql');\n *\n * try {\n * const formatted = sqlFormatter.formatOrThrow(\n * 'SELECT * FROM table WHERE id = 1'\n * );\n * } catch (e) {\n * // Might throw something similat to: Parse error: Unexpected \"[col] from\" at line 1 column 8\n * console.log('Error:', e);\n * }\n * ```\n *\n * @throws Error is sql cannot be parsed\n */\n formatOrThrow = (sql: SqlTag<unknown>, params?: FormatParams): string => {\n const options = params?.options ?? this.formatterOptions;\n return sqlFormat(sql.sql, {\n language: params?.dialect ?? this.dialect,\n ...options,\n });\n };\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,106 @@
1
+ {
2
+ "name": "@flowblade/sql-tag-format",
3
+ "description": "Formatter utilities for @flowblade/sql-tag",
4
+ "version": "0.0.2",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "Vanvelthem Sébastien",
8
+ "url": "https://github.com/belgattitude"
9
+ },
10
+ "homepage": "https://belgattitude.github.io/flowblade",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/belgattitude/flowblade.git",
14
+ "directory": "packages/sql-tag-format"
15
+ },
16
+ "keywords": [],
17
+ "sideEffects": false,
18
+ "type": "module",
19
+ "main": "./dist/index.cjs",
20
+ "module": "./dist/index.mjs",
21
+ "types": "./dist/index.d.cts",
22
+ "exports": {
23
+ ".": {
24
+ "import": {
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.mjs"
27
+ },
28
+ "require": {
29
+ "types": "./dist/index.d.cts",
30
+ "default": "./dist/index.cjs"
31
+ }
32
+ },
33
+ "./package.json": "./package.json"
34
+ },
35
+ "files": [
36
+ "dist"
37
+ ],
38
+ "scripts": {
39
+ "?build-release": "When https://github.com/atlassian/changesets/issues/432 has a solution we can remove this trick",
40
+ "build": "run clean && yarn run tsup",
41
+ "build-release": "run build",
42
+ "bench-disabled": "vitest bench --run",
43
+ "bench-codspeed-disabled": "cross-env CODSPEED=1 vitest bench --run",
44
+ "bench-watch-disabled": "vitest bench",
45
+ "docgen": "run-s docgen-typedoc",
46
+ "docgen-typedoc": "rimraf ./docs/api && typedoc --plugin typedoc-plugin-markdown --out ./docs/api",
47
+ "check-dist": "run-s check-dist-esm check-dist-cjs",
48
+ "check-dist-cjs": "es-check --not './dist/*.map.js' -v es2022 './dist/**/*.cjs'",
49
+ "check-dist-esm": "es-check --not './dist/*.map.js' -v es2022 --module './dist/**/*.mjs'",
50
+ "check-pub": "attw --pack && publint",
51
+ "check-size": "size-limit",
52
+ "clean": "rimraf --glob ./dist ./build ./coverage ./_release './tsconfig*.tsbuildinfo'",
53
+ "dev": "tsup --watch",
54
+ "fix-staged": "lint-staged --allow-empty",
55
+ "lint": "eslint . --ext .ts,.tsx,.js,.jsx,.mjs,.cjs,.mts,.cts --cache --cache-location ../../.cache/eslint/sql-tag-format.eslintcache",
56
+ "test": "vitest run",
57
+ "test-unit": "vitest run",
58
+ "test-e2e": "vitest --config=vitest.e2e.config.ts run",
59
+ "test-unit-coverage": "vitest run --coverage",
60
+ "test-unit-edge": "vitest run --environment edge-runtime",
61
+ "test-unit-watch": "vitest --ui --api 4515",
62
+ "typecheck": "tsc --project tsconfig.json --noEmit",
63
+ "typecheck-build": "tsc --project tsconfig.build.json --noEmit",
64
+ "ci-coverage-upload": "../../.github/scripts/download/codecov -F flowblade-sql-tag-format --dir ./coverage"
65
+ },
66
+ "dependencies": {
67
+ "sql-formatter": "^15.4.9"
68
+ },
69
+ "peerDependencies": {
70
+ "@flowblade/sql-tag": "workspace:^"
71
+ },
72
+ "devDependencies": {
73
+ "@arethetypeswrong/cli": "0.17.2",
74
+ "@belgattitude/eslint-config-bases": "6.18.0",
75
+ "@codspeed/vitest-plugin": "4.0.0",
76
+ "@edge-runtime/vm": "5.0.0",
77
+ "@size-limit/file": "11.1.6",
78
+ "@size-limit/webpack": "11.1.6",
79
+ "@vitest/coverage-istanbul": "2.1.8",
80
+ "@vitest/ui": "2.1.8",
81
+ "browserslist": "4.24.3",
82
+ "browserslist-to-esbuild": "2.1.1",
83
+ "cross-env": "7.0.3",
84
+ "es-check": "8.0.1",
85
+ "esbuild": "0.24.2",
86
+ "eslint": "8.57.1",
87
+ "execa": "9.5.2",
88
+ "npm-run-all2": "7.0.2",
89
+ "prettier": "3.4.2",
90
+ "publint": "0.3.0",
91
+ "rimraf": "6.0.1",
92
+ "rollup": "4.30.1",
93
+ "size-limit": "11.1.6",
94
+ "tsup": "8.3.5",
95
+ "tsx": "4.19.2",
96
+ "typedoc": "0.27.6",
97
+ "typedoc-plugin-markdown": "4.4.1",
98
+ "typescript": "5.7.2",
99
+ "vite-tsconfig-paths": "5.1.4",
100
+ "vitest": "2.1.8",
101
+ "webpack": "5.97.1"
102
+ },
103
+ "engines": {
104
+ "node": ">=18.17.0"
105
+ }
106
+ }