sqlx-ts 0.1.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/.eslintignore +1 -0
- package/.eslintrc.js +19 -0
- package/README.md +148 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +7 -0
- package/jest.config.js +5 -0
- package/package.json +29 -0
- package/postinstall.js +8 -0
- package/sqlx-ts +0 -0
- package/src/index.spec.ts +27 -0
- package/src/index.ts +3 -0
- package/tsconfig.json +10 -0
package/.eslintignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
node_modules/*
|
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
"env": {
|
|
3
|
+
"browser": true,
|
|
4
|
+
"es2021": true,
|
|
5
|
+
},
|
|
6
|
+
"extends": [
|
|
7
|
+
"eslint:recommended",
|
|
8
|
+
"plugin:@typescript-eslint/recommended",
|
|
9
|
+
],
|
|
10
|
+
"parser": "@typescript-eslint/parser",
|
|
11
|
+
"parserOptions": {
|
|
12
|
+
"ecmaVersion": "latest",
|
|
13
|
+
"sourceType": "module",
|
|
14
|
+
},
|
|
15
|
+
"plugins": [
|
|
16
|
+
"@typescript-eslint",
|
|
17
|
+
],
|
|
18
|
+
"rules": {},
|
|
19
|
+
};
|
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
<h1 align="center">SQLx-ts</h1>
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<a href='https://coveralls.io/github/JasonShin/sqlx-ts?branch=main'><img src='https://coveralls.io/repos/github/JasonShin/sqlx-ts/badge.svg?branch=main' alt='Coverage Status' /></a>
|
|
5
|
+
</div>
|
|
6
|
+
|
|
7
|
+
<div align="center">
|
|
8
|
+
<strong>
|
|
9
|
+
🧰 The Typescript/Javascript SQL Toolkit
|
|
10
|
+
</strong>
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
<br />
|
|
14
|
+
|
|
15
|
+
<div align="center">
|
|
16
|
+
Built to free Node developers from ORMs' unpredictably generated SQL queries
|
|
17
|
+
</div>
|
|
18
|
+
|
|
19
|
+
<br />
|
|
20
|
+
|
|
21
|
+
SQLx-ts is a CLI application featuring compile-time checked queries without a DSL and prevents broken SQL queries being run during runtime.
|
|
22
|
+
|
|
23
|
+
- **Compile time checked queries** - never ship a broken SQL query to production
|
|
24
|
+
- **TypeScript type generations** - generates type definitions based on the raw SQLs and you can use them with any MySQL or PostgreSQL driver
|
|
25
|
+
- **Database Agnostic** - support for [PostgreSQL](http://postgresql.org/) and [MySQL](https://www.mysql.com/)
|
|
26
|
+
- **TypeScript and JavaScript** - supports for both [TypeScript](https://www.typescriptlang.org/) and JavaScript
|
|
27
|
+
|
|
28
|
+
<br>
|
|
29
|
+
<div align="center">
|
|
30
|
+
<strong>
|
|
31
|
+
📔 <a href="https://jasonshin.github.io/sqlx-ts/">Official Documentation</a>
|
|
32
|
+
</strong>
|
|
33
|
+
</div>
|
|
34
|
+
<br>
|
|
35
|
+
|
|
36
|
+
### Installation
|
|
37
|
+
|
|
38
|
+
##### Install sqlx-ts npm module (recommended)
|
|
39
|
+
|
|
40
|
+
If you are using npm
|
|
41
|
+
```bash
|
|
42
|
+
$ npm install sqlx-ts
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
If you are using yarn
|
|
46
|
+
```bash
|
|
47
|
+
$ yarn add sqlx-ts
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
You can also install sqlx-ts globally
|
|
51
|
+
```bash
|
|
52
|
+
$ npm install -g sqlx-ts
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Installing sqlx-ts using npm also installed `sqlx-ts` binary of the same version as the npm module.
|
|
56
|
+
Verify the installation by running
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
$ npx sqlx-ts --version
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
And to use sqlx-ts in your code
|
|
63
|
+
|
|
64
|
+
In TypeScript based projects:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { sql } from 'sqlx-ts'
|
|
68
|
+
|
|
69
|
+
// ...
|
|
70
|
+
const query = sql`SELECT * FROM some_table;`
|
|
71
|
+
// ...
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
In Babel based projects:
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
import { sql } from 'sqlx-ts'
|
|
79
|
+
const query = sql`SELECT * FROM some_table;`
|
|
80
|
+
|
|
81
|
+
// ... or
|
|
82
|
+
|
|
83
|
+
const { sql } = require('sqlx-ts')
|
|
84
|
+
const query = sql`SELECT * FROM some_table;`
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
##### Installing binary separately
|
|
88
|
+
|
|
89
|
+
You may choose to install sqlx-ts separately instead of using `npm i`
|
|
90
|
+
|
|
91
|
+
###### Using install.sh
|
|
92
|
+
|
|
93
|
+
The binary name for sqlx-ts is `sqlx-ts`.
|
|
94
|
+
|
|
95
|
+
[Archives of precompiled binaries of sqlx-ts are available for windows, macOS and Linux](https://github.com/JasonShin/sqlx-ts/releases). Linux and Windows binaries are static executables. Users of platforms not explicitly mentioned below are advised to download one of these archives.
|
|
96
|
+
|
|
97
|
+
If you're a **macOS** user, then you can install sqlx-ts from via install.sh:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
# macos & ARM CPU
|
|
101
|
+
$ curl -LSfs https://jasonshin.github.io/sqlx-ts/install.sh | sh -s -- --os darwin --cpu arm64
|
|
102
|
+
# macos & X64 CPU
|
|
103
|
+
$ curl -LSfs https://jasonshin.github.io/sqlx-ts/install.sh | sh -s -- --os darwin --cpu x64
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
If you're a **Windows** user, then you can install sqlx-ts from via install.sh:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
# windows & x32
|
|
110
|
+
$ curl -LSfs https://jasonshin.github.io/sqlx-ts/install.sh | sh -s -- --os win32 --cpu x32
|
|
111
|
+
# windows & x64
|
|
112
|
+
$ curl -LSfs https://jasonshin.github.io/sqlx-ts/install.sh | sh -s -- --os win32 --cpu x32
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
If you're a **Linux** user, then you can install sqlx-ts from via install.sh:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
# linux & x32
|
|
119
|
+
$ curl -LSfs https://jasonshin.github.io/sqlx-ts/install.sh | sh -s -- --os linux --cpu x32
|
|
120
|
+
# linux & x64
|
|
121
|
+
$ curl -LSfs https://jasonshin.github.io/sqlx-ts/install.sh | sh -s -- --os linux --cpu x64
|
|
122
|
+
# linux & arm
|
|
123
|
+
$ curl -LSfs https://jasonshin.github.io/sqlx-ts/install.sh | sh -s -- --os linux --cpu arm64
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
To install a specific artifact, [go to the release page to find the exact name of the artifact](https://github.com/JasonShin/sqlx-ts/releases)
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
$ curl -LSfs https://jasonshin.github.io/sqlx-ts/install.sh | sh -s -- --artifact sqlx-ts-v0.1.0-macos-arm.zip
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Upgrading to a new version can be done by grabbing the next version of the sqlx-ts artifact and use `--force` command from install.sh
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
$ curl -LSfs https://jasonshin.github.io/sqlx-ts/install.sh | \
|
|
136
|
+
sh -s -- --artifact ssqlx-ts-v0.1.0-macos-arm.zip --force
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
For more advanced usage, please check `--help` command of install.sh
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
$ curl -LSfs https://jasonshin.github.io/sqlx-ts/install.sh | \
|
|
143
|
+
sh -s -- --help
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Motivation
|
|
147
|
+
|
|
148
|
+
I would like to bring the powerful compile-time safety ideas to Node.js. [sqlx](https://github.com/launchbadge/sqlx) is a great example of this, as it provides compile-time check of SQLs within your Rust code and Rust itself provides a great environment for tools like sqlx. sqlx-ts is greatly inspired by [sqlx](https://github.com/launchbadge/sqlx), but solves additional problems of generating TypeScript interfaces based on the SQL queries that are present in your code.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function sql<T extends TemplateStringsArray>(query: T): string;
|
package/dist/index.js
ADDED
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sqlx-ts",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "sqlx-ts ensures your raw SQLs are compile-time checked",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"maintainers": [
|
|
7
|
+
"visualbbasic@gmail.com"
|
|
8
|
+
],
|
|
9
|
+
"author": "Jason Shin <visualbbasic@gmail.com>",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"bin": "./sqlx-ts",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"postinstall": "node postinstall.js",
|
|
14
|
+
"compile": "npx tsc -p tsconfig.json",
|
|
15
|
+
"lint": "npx eslint --ext .ts src",
|
|
16
|
+
"lint:fix": "npx eslint --ext .ts src --fix",
|
|
17
|
+
"test": "npx jest",
|
|
18
|
+
"prepublishOnly": "cp ../README.md . && npm i && npm run compile"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/jest": "^27.4.1",
|
|
22
|
+
"@typescript-eslint/eslint-plugin": "^5.19.0",
|
|
23
|
+
"@typescript-eslint/parser": "^5.19.0",
|
|
24
|
+
"eslint": "^8.13.0",
|
|
25
|
+
"jest": "^27.5.1",
|
|
26
|
+
"ts-jest": "^27.1.4",
|
|
27
|
+
"typescript": "^4.6.3"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/postinstall.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
let execSync = require('child_process').execSync
|
|
2
|
+
let tag = require('./package.json').version
|
|
3
|
+
|
|
4
|
+
const os = process.platform
|
|
5
|
+
const cpu = process.arch
|
|
6
|
+
|
|
7
|
+
execSync(`curl -LSfs https://jasonshin.github.io/sqlx-ts/install.sh | bash -s -- --os ${os} --cpu ${cpu} --tag ${tag} -f`, { stdio: 'inherit' })
|
|
8
|
+
console.info('sqlx-ts installation successful')
|
package/sqlx-ts
ADDED
|
Binary file
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { sql } from "./index";
|
|
2
|
+
|
|
3
|
+
test("should return single line", () => {
|
|
4
|
+
const rawSql = sql`SELECT * FROM test;`;
|
|
5
|
+
|
|
6
|
+
expect(rawSql).toBe("SELECT * FROM test;");
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
test("should return multiple lines", () => {
|
|
10
|
+
const rawSql = sql`
|
|
11
|
+
SELECT *
|
|
12
|
+
FROM test
|
|
13
|
+
WHERE createdAt > 2019-1-1;
|
|
14
|
+
`;
|
|
15
|
+
|
|
16
|
+
expect(rawSql).toBe(`
|
|
17
|
+
SELECT *
|
|
18
|
+
FROM test
|
|
19
|
+
WHERE createdAt > 2019-1-1;
|
|
20
|
+
`);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("should return empty", () => {
|
|
24
|
+
const rawSql = sql``;
|
|
25
|
+
|
|
26
|
+
expect(rawSql).toBe("");
|
|
27
|
+
});
|
package/src/index.ts
ADDED