Skip to content

Commit

Permalink
feat: kysely adapter initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallswain committed Apr 3, 2024
1 parent b991db0 commit b69c24c
Show file tree
Hide file tree
Showing 9 changed files with 734 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ npm run generate:adapter

# License

Copyright (c) 2023 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)
Copyright (c) 2024 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)

Licensed under the [MIT license](LICENSE).
34 changes: 31 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions packages/kysely/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2024 Wings Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

23 changes: 23 additions & 0 deletions packages/kysely/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# @wingshq/kysely

[![CI](https://github.com/wingshq/kysely/workflows/CI/badge.svg)](https://github.com/wingshq/wings/actions?query=workflow%3ACI)
[![Download Status](https://img.shields.io/npm/dm/@wingshq/kysely.svg?style=flat-square)](https://www.npmjs.com/package/@wingshq/kysely)
[![Discord](https://badgen.net/badge/icon/discord?icon=discord&label)](https://discord.gg/qa8kez8QBx)

> A Wings adapter for Kysely
## Installation

```bash
$ npm install --save @wingshq/kysely
```

## Documentation

See [Wings kysely Adapter API documentation](https://wings.codes/adapters/kysely.html) for more details.

## License

Copyright (c) 2024 [Wings contributors](https://github.com/wingshq/wings/graphs/contributors)

Licensed under the [MIT license](LICENSE).
64 changes: 64 additions & 0 deletions packages/kysely/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "@wingshq/kysely",
"description": "A Wings adapter for Kysely",
"version": "0.0.0",
"homepage": "https://wings.codes",
"keywords": [
"wings",
"wings-adapter"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/wingshq/wings.git",
"directory": "packages/kysely"
},
"author": {
"name": "Wings contributors",
"email": "[email protected]",
"url": "https://feathersjs.com"
},
"contributors": [],
"bugs": {
"url": "https://github.com/wingshq/wings/issues"
},
"engines": {
"node": ">= 20"
},
"files": [
"CHANGELOG.md",
"LICENSE",
"README.md",
"src/**",
"lib/**",
"esm/**"
],
"module": "./esm/index.js",
"main": "./lib/index.js",
"types": "./src/index.ts",
"exports": {
".": {
"import": "./esm/index.js",
"require": "./lib/index.js",
"types": "./src/index.ts"
}
},
"scripts": {
"prepublish": "npm run compile",
"compile:lib": "shx rm -rf lib/ && tsc --module commonjs",
"compile:esm": "shx rm -rf esm/ && tsc --module es2020 --outDir esm",
"compile": "npm run compile:lib && npm run compile:esm",
"test": "npm run compile && node --require ts-node/register --test test/**.test.ts"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@feathersjs/errors": "^5.0.24",
"@wingshq/adapter-commons": "^0.0.0",
"kysely": "^0.27.3"
},
"devDependencies": {
"@wingshq/adapter-tests": "^0.0.0"
}
}
96 changes: 96 additions & 0 deletions packages/kysely/src/error-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { errors } from '@feathersjs/errors'

export const ERROR = Symbol.for('feathers-kysely/error')

/**
* Returns the correct Feathers Error depending on the SQL error.
* @param error
* @returns a type of FeathersError
*/
export function errorHandler(error: any, params: any) {
const { message } = error
console.error(message)

let feathersError = error

if (error.sqlState && error.sqlState.length) {
// remove SQLSTATE marker (#) and pad/truncate SQLSTATE to 5 chars
const sqlState = `00000${error.sqlState.replace('#', '')}`.slice(-5)

switch (sqlState.slice(0, 2)) {
case '02':
feathersError = new errors.NotFound(message)
break
case '28':
feathersError = new errors.Forbidden(message)
break
case '08':
case '0A':
case '0K':
feathersError = new errors.Unavailable(message)
break
case '20':
case '21':
case '22':
case '23':
case '24':
case '25':
case '40':
case '42':
case '70':
feathersError = new errors.BadRequest(message)
break
default:
feathersError = new errors.GeneralError(message)
}
} else if (error.message.includes('SqliteError') || error.message.includes('D1_TYPE_ERROR')) {
let message = error.message

// remove SqliteError marker
message = message.replace('SqliteError: ', '')

const errorData = { query: JSON.stringify(params.query) }

// remove D1_ERROR marker
if (message.includes('D1_ERROR: ')) message = message.replace('D1_ERROR: ', '')

if (message.includes('UNIQUE constraint failed:'))
feathersError = new errors.BadRequest(message, errorData)

if (message.includes('not supported for value')) feathersError = new errors.BadRequest(message, errorData)
else feathersError = new errors.GeneralError(message, errorData)
} else if (typeof error.code === 'string' && error.severity && error.routine) {
// NOTE: Error codes taken from
// https://www.postgresql.org/docs/9.6/static/errcodes-appendix.html
// Omit query information
const messages = (error.message || '').split('-')

error.message = messages[messages.length - 1]

switch (error.code.slice(0, 2)) {
case '22':
feathersError = new errors.NotFound(message)
break
case '23':
feathersError = new errors.BadRequest(message)
break
case '28':
feathersError = new errors.Forbidden(message)
break
case '3D':
case '3F':
case '42':
feathersError = new errors.Unprocessable(message)
break
default:
feathersError = new errors.GeneralError(message)
break
}
} else if (!(error instanceof errors.FeathersError)) {
feathersError = new errors.GeneralError(message)
}

// feathersError[ERROR] = error

return feathersError
}
Loading

0 comments on commit b69c24c

Please sign in to comment.