Skip to content

Commit 9540493

Browse files
Initial commit
0 parents  commit 9540493

File tree

13 files changed

+488
-0
lines changed

13 files changed

+488
-0
lines changed

.github/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: github-actions
4+
directory: /
5+
schedule:
6+
interval: daily

.github/workflows/ci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: CI
2+
on: push
3+
4+
jobs:
5+
build:
6+
name: Build
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
11+
- uses: pnpm/action-setup@v2
12+
with:
13+
version: 7.x.x
14+
15+
- uses: actions/setup-node@v3
16+
with:
17+
node-version: '16'
18+
cache: pnpm
19+
20+
- run: pnpm install --frozen-lockfile
21+
- run: pnpm lint
22+
- run: pnpm type-check
23+
- run: pnpm build

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
coverage/
2+
dist/
3+
node_modules/
4+
*.log
5+
.env*

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
pnpm-lock.yaml

.prettierrc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
printWidth: 120,
3+
semi: false,
4+
singleQuote: true,
5+
trailingComma: 'all',
6+
bracketSpacing: false,
7+
plugins: [require('prettier-plugin-organize-imports'), require('prettier-plugin-pkg')],
8+
}

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"prettier.prettierPath": "node_modules/prettier/index.js",
3+
"typescript.tsdk": "node_modules/typescript/lib",
4+
"typescript.enablePromptUseWorkspaceTsdk": true
5+
}

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## v0.1.0 - 2022-09-01
4+
5+
- Initial release

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Jacob Gillespie <[email protected]>
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.

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# kysely-planetscale
2+
3+
[![CI](https://github.com/depot/kysely-planetscale/actions/workflows/ci.yml/badge.svg)](https://github.com/depot/kysely-planetscale/actions/workflows/ci.yml)
4+
[![npm](https://img.shields.io/npm/dm/kysely-planetscale.svg)](https://www.npmjs.com/package/kysely-planetscale)
5+
[![npm](https://img.shields.io/npm/v/kysely-planetscale.svg)](https://www.npmjs.com/package/kysely-planetscale)
6+
![Powered by TypeScript](https://img.shields.io/badge/powered%20by-typescript-blue.svg)
7+
8+
A Kysely dialect for [PlanetScale](https://planetscale.com/), using the [PlanetScale serverless driver for JavaScript](https://planetscale.com/blog/introducing-the-planetscale-serverless-driver-for-javascript).
9+
10+
## Installation
11+
12+
You should install both `kysely` and `@planetscale/database` with `kysely-planetscale`, as they are both required peer dependencies. You can install them with your favorite package manager:
13+
14+
```bash
15+
# with pnpm
16+
pnpm add kysely-planetscale kysely @planetscale/database
17+
18+
# with yarn
19+
yarn add kysely-planetscale kysely @planetscale/database
20+
21+
# with npm
22+
npm install kysely-planetscale kysely @planetscale/database
23+
```
24+
25+
## Usage
26+
27+
You can pass a new instance of `PlanetScaleDialect` as the `dialect` option when creating a new `Kysely` instance:
28+
29+
```typescript
30+
import {Kysely} from 'kysely'
31+
import {PlanetScaleDialect} from 'keysely-planetscale'
32+
33+
const db = new Kysely<Database>({
34+
dialect: new PlanetScaleDialect({
35+
host: '<host>',
36+
username: '<user>',
37+
password: '<password>',
38+
}),
39+
})
40+
```
41+
42+
`PlanetScaleDialect` accepts the same options as `connect({...})` from `@planetscale/database`, so for instance if you are using Node.js and need to provide a `fetch` implementation:
43+
44+
```typescript
45+
import {Kysely} from 'kysely'
46+
import {PlanetScaleDialect} from 'keysely-planetscale'
47+
import {fetch} from 'undici'
48+
49+
// Connect using a DATABASE_URL, provide a fetch implementation
50+
const db = new Kysely<Database>({
51+
dialect: new PlanetScaleDialect({
52+
url: process.env.DATABASE_URL,
53+
fetch,
54+
}),
55+
})
56+
```
57+
58+
### Type Conversion
59+
60+
`PlanetScaleDialect` provides built-in support for converting JavaScript Dates to and from `DATETIME` and `TIMESTAMP` columns, as Kysely's generated types expect. However, you can override or extend this behavior by providing a custom `format` or `cast` function to override the defaults.
61+
62+
#### Custom `format` function
63+
64+
`PlanetScaleDialect` passes all parameters to `@planetscale/database` unmodified, expect for JavaScript Dates, which are converted to MySQL strings. If you [set a `format` function](https://github.com/planetscale/database-js#custom-query-parameter-format-function), you can override this behavior:
65+
66+
```typescript
67+
import {Kysely} from 'kysely'
68+
import {PlanetScaleDialect} from 'keysely-planetscale'
69+
import SqlString from 'sqlstring'
70+
71+
const db = new Kysely<Database>({
72+
dialect: new PlanetScaleDialect({
73+
url: process.env.DATABASE_URL,
74+
format: SqlString.format,
75+
}),
76+
})
77+
```
78+
79+
#### Custom `cast` function
80+
81+
`PlanetScaleDialect` automatically type-casts `DATETIME` and `TIMESTAMP` to JavaScript Dates. If you'd prefer to customize this behavior, you can [pass a custom `cast` function](https://github.com/planetscale/database-js#custom-type-casting-function):
82+
83+
```typescript
84+
import {cast} from '@planetscale/database'
85+
import {Kysely} from 'kysely'
86+
import {PlanetScaleDialect} from 'keysely-planetscale'
87+
import SqlString from 'sqlstring'
88+
89+
const db = new Kysely<Database>({
90+
dialect: new PlanetScaleDialect({
91+
url: process.env.DATABASE_URL,
92+
cast: inflate,
93+
}),
94+
})
95+
96+
function inflate(field, value) {
97+
if (field.type === 'INT64' || field.type === 'UINT64') {
98+
return BigInt(value)
99+
}
100+
return cast(field, value)
101+
}
102+
```
103+
104+
## License
105+
106+
MIT License, see `LICENSE`.

package.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "kysely-planetscale",
3+
"version": "0.0.0",
4+
"description": "Kysely dialect for PlanetScale Serverless",
5+
"repository": "https://github.com/depot/kysely-planetscale.git",
6+
"homepage": "https://github.com/depot/kysely-planetscale",
7+
"author": "Jacob Gillespie <[email protected]>",
8+
"license": "MIT",
9+
"main": "./dist/index.js",
10+
"types": "./dist/index.d.ts",
11+
"files": [
12+
"dist"
13+
],
14+
"keywords": [
15+
"kysely",
16+
"planetscale",
17+
"mysql",
18+
"vitess",
19+
"serverless"
20+
],
21+
"scripts": {
22+
"build": "tsc",
23+
"clean": "rm -rf dist",
24+
"fmt": "prettier --write .",
25+
"fmt:check": "prettier --check .",
26+
"prepack": "pnpm run build",
27+
"type-check": "tsc --noEmit"
28+
},
29+
"peerDependencies": {
30+
"@planetscale/database": "*",
31+
"kysely": "*"
32+
},
33+
"dependencies": {
34+
"date-fns": "^2.29.2"
35+
},
36+
"devDependencies": {
37+
"@planetscale/database": "^1.3.0",
38+
"@tsconfig/node14": "^1.0.3",
39+
"@types/node": "^16",
40+
"kysely": "^0.21.5",
41+
"prettier": "^2.7.1",
42+
"prettier-plugin-organize-imports": "^3.1.0",
43+
"prettier-plugin-pkg": "^0.17.1",
44+
"typescript": "^4.8.2"
45+
},
46+
"sideEffects": false
47+
}

0 commit comments

Comments
 (0)