prisma-extension-kysely 1.0.0
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 +21 -0
- package/README.md +107 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +40 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Eoin O'Brien
|
|
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,107 @@
|
|
|
1
|
+
# Prisma Kysely Extension
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/prisma-extension-kysely)
|
|
4
|
+
[](https://www.npmjs.com/package/prisma-extension-kysely)
|
|
5
|
+
[](https://www.npmjs.com/package/prisma-extension-kysely)
|
|
6
|
+
[](https://github.com/eoin-obrien/prisma-extension-kysely/actions/workflows/node.js.yml)
|
|
7
|
+
[](https://github.com/eoin-obrien/prisma-extension-kysely/actions/workflows/npm-publish-github-packages.yml)
|
|
8
|
+
[](https://github.com/eoin-obrien/prisma-extension-kysely/actions/workflows/npm-publish.yml)
|
|
9
|
+
|
|
10
|
+
Writing and maintaining raw SQL queries for Prisma can be a tedious and error-prone task. The moment you need to write a query that is not supported out-of-the-box by Prisma, you lose all of that type-safety and autocompletion. This is where `prisma-extension-kysely` comes in! It allowa you to easily write raw SQL queries in a type-safe manner with [`kysely`](https://kysely.dev/) and integrate them seamlessly with Prisma.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- **Type-safe** — Write raw SQL queries in a type-safe manner with `kysely`
|
|
15
|
+
- **Seamless integration** — Use `kysely` queries with Prisma as if they were native
|
|
16
|
+
- **Autocompletion** — Get autocompletion for your queries in your IDE
|
|
17
|
+
- **Type inference** — Get type inference for your queries in your IDE
|
|
18
|
+
|
|
19
|
+
## Get started
|
|
20
|
+
|
|
21
|
+
Click the **Use this template** button and provide details for your Client extension
|
|
22
|
+
|
|
23
|
+
Install the dependencies:
|
|
24
|
+
|
|
25
|
+
```shell
|
|
26
|
+
npm install prisma-extension-kysely kysely
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Set up the excellent [`prisma-kysely`](https://www.npmjs.com/package/prisma-kysely) library to automatically generate types for your database:
|
|
30
|
+
|
|
31
|
+
```shell
|
|
32
|
+
npm install -D prisma-kysely
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Add `prisma-kysely` as a generator to your `schema.prisma`:
|
|
36
|
+
|
|
37
|
+
```prisma
|
|
38
|
+
generator kysely {
|
|
39
|
+
provider = "prisma-kysely"
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Generate the types:
|
|
44
|
+
|
|
45
|
+
```shell
|
|
46
|
+
npx prisma generate
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Extend your Prisma Client:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import kyselyExtension from "prisma-extension-kysely";
|
|
53
|
+
import type { DB } from "./prisma/generated/types";
|
|
54
|
+
|
|
55
|
+
// Don't forget to customize this to match your database!
|
|
56
|
+
const kysely = new Kysely<DB>({
|
|
57
|
+
dialect: {
|
|
58
|
+
createAdapter: () => new PostgresAdapter(),
|
|
59
|
+
createDriver: () => new DummyDriver(),
|
|
60
|
+
createIntrospector: (db) => new PostgresIntrospector(db),
|
|
61
|
+
createQueryCompiler: () => new PostgresQueryCompiler(),
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const prisma = new PrismaClient().$extends(kyselyExtension({ kysely }));
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
It's that simple! Now you can write raw SQL queries with `kysely` and use them with Prisma:
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
// Replace this...
|
|
72
|
+
const result = prisma.$queryRaw`SELECT * FROM User WHERE id = ${id}`;
|
|
73
|
+
|
|
74
|
+
// With this!
|
|
75
|
+
const query = prisma.$kysely
|
|
76
|
+
.selectFrom("User")
|
|
77
|
+
.selectAll()
|
|
78
|
+
.where("id", "=", id);
|
|
79
|
+
|
|
80
|
+
// Thanks to kysely's magic, everything is type-safe!
|
|
81
|
+
const result = await prisma.$kyselyQuery(query);
|
|
82
|
+
|
|
83
|
+
// You can also execute queries without fetching the results
|
|
84
|
+
await prisma.$kyselyExecute(
|
|
85
|
+
prisma.$kysely.deleteFrom("User").where("id", "=", id),
|
|
86
|
+
);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Examples
|
|
90
|
+
|
|
91
|
+
Check out the [example](example) directory for a sample project!
|
|
92
|
+
|
|
93
|
+
```shell
|
|
94
|
+
cd examples
|
|
95
|
+
npm install
|
|
96
|
+
npx prisma db push
|
|
97
|
+
npm run dev
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Learn more
|
|
101
|
+
|
|
102
|
+
- [Kysely](https://kysely.dev/)
|
|
103
|
+
- [`prisma-kysely`](https://www.npmjs.com/package/prisma-kysely)
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { PrismaPromise } from "@prisma/client/runtime/library";
|
|
2
|
+
import type { Compilable, Kysely, Simplify } from "kysely";
|
|
3
|
+
/**
|
|
4
|
+
* The configuration object for the Prisma Kysely extension
|
|
5
|
+
*/
|
|
6
|
+
export type PrismaKyselyExtensionArgs<Database> = {
|
|
7
|
+
/**
|
|
8
|
+
* The Kysely instance to provide to the Prisma client
|
|
9
|
+
*/
|
|
10
|
+
kysely: Kysely<Database>;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Define a Prisma extension that adds Kysely query builder methods to the Prisma client
|
|
14
|
+
* @param extensionArgs The extension configuration object
|
|
15
|
+
*/
|
|
16
|
+
declare const _default: <Database>(extensionArgs: PrismaKyselyExtensionArgs<Database>) => (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/library").InternalArgs<{}, {}, {}, {
|
|
17
|
+
/**
|
|
18
|
+
* The Kysely instance used by the Prisma client
|
|
19
|
+
*/
|
|
20
|
+
$kysely: Kysely<Database>;
|
|
21
|
+
/**
|
|
22
|
+
* Execute a Kysely query and return the result
|
|
23
|
+
* @param query A Kysely select, insert, delete or update query builder
|
|
24
|
+
* @returns The result of the query
|
|
25
|
+
*/
|
|
26
|
+
$kyselyQuery<T>(query: Compilable<T>): Promise<import("kysely/dist/cjs/util/type-utils").DrainOuterGeneric<{ [K in keyof T]: T[K]; }>[]>;
|
|
27
|
+
/**
|
|
28
|
+
* Execute a Kysely query and return the number of rows affected
|
|
29
|
+
* @param query A Kysely select, insert, delete or update query builder
|
|
30
|
+
* @returns The number of rows affected
|
|
31
|
+
*/
|
|
32
|
+
$kyselyExecute(query: Compilable): PrismaPromise<number>;
|
|
33
|
+
}> & import("@prisma/client/runtime/library").DefaultArgs>;
|
|
34
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const extension_1 = require("@prisma/client/extension");
|
|
4
|
+
/**
|
|
5
|
+
* Define a Prisma extension that adds Kysely query builder methods to the Prisma client
|
|
6
|
+
* @param extensionArgs The extension configuration object
|
|
7
|
+
*/
|
|
8
|
+
exports.default = (extensionArgs) => extension_1.Prisma.defineExtension((client) => {
|
|
9
|
+
return client.$extends({
|
|
10
|
+
name: "prisma-extension-kysely",
|
|
11
|
+
client: {
|
|
12
|
+
/**
|
|
13
|
+
* The Kysely instance used by the Prisma client
|
|
14
|
+
*/
|
|
15
|
+
$kysely: extensionArgs.kysely,
|
|
16
|
+
/**
|
|
17
|
+
* Execute a Kysely query and return the result
|
|
18
|
+
* @param query A Kysely select, insert, delete or update query builder
|
|
19
|
+
* @returns The result of the query
|
|
20
|
+
*/
|
|
21
|
+
$kyselyQuery(query) {
|
|
22
|
+
const { sql, parameters } = query.compile();
|
|
23
|
+
const ctx = extension_1.Prisma.getExtensionContext(this);
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
|
+
return ctx.$queryRawUnsafe(sql, ...parameters);
|
|
26
|
+
},
|
|
27
|
+
/**
|
|
28
|
+
* Execute a Kysely query and return the number of rows affected
|
|
29
|
+
* @param query A Kysely select, insert, delete or update query builder
|
|
30
|
+
* @returns The number of rows affected
|
|
31
|
+
*/
|
|
32
|
+
$kyselyExecute(query) {
|
|
33
|
+
const { sql, parameters } = query.compile();
|
|
34
|
+
const ctx = extension_1.Prisma.getExtensionContext(this);
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
+
return ctx.$executeRawUnsafe(sql, ...parameters);
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "prisma-extension-kysely",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "Eoin O'Brien",
|
|
6
|
+
"url": "https://eoin.ai",
|
|
7
|
+
"email": "eoin@tracworx.ai"
|
|
8
|
+
},
|
|
9
|
+
"description": "Prisma extension for Kysely",
|
|
10
|
+
"keywords": [
|
|
11
|
+
"prisma",
|
|
12
|
+
"@prisma/client",
|
|
13
|
+
"extension",
|
|
14
|
+
"kysely",
|
|
15
|
+
"sql",
|
|
16
|
+
"query builder",
|
|
17
|
+
"orm",
|
|
18
|
+
"database",
|
|
19
|
+
"postgresql",
|
|
20
|
+
"mysql",
|
|
21
|
+
"sqlite"
|
|
22
|
+
],
|
|
23
|
+
"main": "dist/index.js",
|
|
24
|
+
"types": "dist/index.d.ts",
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "prisma generate && prisma db push && jest",
|
|
30
|
+
"build": "tsc",
|
|
31
|
+
"prepare": "husky install",
|
|
32
|
+
"commit": "git-cz",
|
|
33
|
+
"lint": "eslint .",
|
|
34
|
+
"format": "prettier --write .",
|
|
35
|
+
"format:check": "prettier --check ."
|
|
36
|
+
},
|
|
37
|
+
"config": {
|
|
38
|
+
"commitizen": {
|
|
39
|
+
"path": "@commitlint/cz-commitlint"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@prisma/client": "latest"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@commitlint/cli": "^18.4.3",
|
|
47
|
+
"@commitlint/config-conventional": "^18.4.3",
|
|
48
|
+
"@commitlint/cz-commitlint": "^18.4.3",
|
|
49
|
+
"@prisma/client": "latest",
|
|
50
|
+
"@types/jest": "^29.5.10",
|
|
51
|
+
"@typescript-eslint/eslint-plugin": "^6.13.0",
|
|
52
|
+
"@typescript-eslint/parser": "^6.13.0",
|
|
53
|
+
"commitizen": "^4.3.0",
|
|
54
|
+
"eslint": "^8.54.0",
|
|
55
|
+
"eslint-config-prettier": "^9.0.0",
|
|
56
|
+
"husky": "^8.0.3",
|
|
57
|
+
"inquirer": "^8.2.6",
|
|
58
|
+
"jest": "^29.7.0",
|
|
59
|
+
"jest-mock-extended": "^3.0.5",
|
|
60
|
+
"kysely": "^0.26.3",
|
|
61
|
+
"prettier": "3.1.0",
|
|
62
|
+
"prisma": "latest",
|
|
63
|
+
"prisma-kysely": "^1.7.1",
|
|
64
|
+
"ts-jest": "^29.1.1",
|
|
65
|
+
"typescript": "^4.9.5"
|
|
66
|
+
}
|
|
67
|
+
}
|