mysql-with-kysely
Advanced tools
Comparing version
{ | ||
"name": "mysql-with-kysely", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Use MySQL with kysely", | ||
"repository": { | ||
@@ -8,2 +9,10 @@ "type": "git", | ||
}, | ||
"homepage": "https://github.com/kanziw/mysql-with-kysely#readme", | ||
"keywords": [ | ||
"mysql", | ||
"kysely", | ||
"query builder", | ||
"Incorrect arguments to mysqld_stmt_execute", | ||
"kanziw" | ||
], | ||
"author": { | ||
@@ -25,2 +34,13 @@ "name": "Jiwoong Jung", | ||
}, | ||
"dependencies": { | ||
"@kanziw/error": "^0.0.4", | ||
"@kanziw/time": "^0.0.3", | ||
"jest-mock-extended": "^2.0.5", | ||
"kysely": "^0.18.1", | ||
"mysql2": "^2.3.3" | ||
}, | ||
"peerDependencies": { | ||
"kysely": "^0.18.1", | ||
"mysql2": "^2.3.3" | ||
}, | ||
"devDependencies": { | ||
@@ -49,10 +69,3 @@ "@babel/core": "^7.17.9", | ||
"typescript": "^4.6.3" | ||
}, | ||
"dependencies": { | ||
"@kanziw/error": "^0.0.4", | ||
"@kanziw/time": "^0.0.3", | ||
"jest-mock-extended": "^2.0.5", | ||
"kysely": "^0.18.1", | ||
"mysql2": "^2.3.3" | ||
} | ||
} |
# mysql-with-kysely | ||
 [](https://codecov.io/gh/kanziw/mysql-with-kysely) | ||
- `mysql-with-kysely` use [mysql2](https://github.com/sidorares/node-mysql2) and [kysely](https://github.com/koskimas/kysely) | ||
- Handle MySQL BIGINT type as string | ||
- `Incorrect arguments to mysqld_stmt_execute` error safe! | ||
- Since MySQL 8.0.22, mysql2 driver has `Incorrect arguments to mysqld_stmt_execute` [issue](https://github.com/sidorares/node-mysql2/issues/1239) | ||
- [LimitCasingPlugin](./src/kyselyPlugins/limitCasingPlugin.ts) resolves it | ||
## Installation | ||
```zsh | ||
$ yarn add mysql-with-kysely | ||
``` | ||
## Usages | ||
```ts | ||
import { queryBuilder, WithSchema, SelectableSchema } from 'mysql-with-kysely' | ||
import type { User } from './model' | ||
// for query builder | ||
type Database = { | ||
user: WithSchema<User> | ||
} | ||
// for your code | ||
type Schema = SelectableSchema<Database> | ||
type InsertValueSchema = InsertableSchema<Database> | ||
const { db, close } = connect<Database>({ uri: 'mysql://root:root@localhost:3306/test' }); | ||
// collect your metrics | ||
db.subscribe(({ sql, normalizedSql, durationMs, occurredAt, parameters }) => { | ||
}) | ||
// your query builder | ||
const qb = queryBuilder<Database>() | ||
// write your codes type safely | ||
// type of users is `Array<Schema['user']>` | ||
const users = await db.query(qb | ||
.selectFrom('user') | ||
.selectAll() | ||
.orderBy('id', 'desc') | ||
.limit(1), | ||
) | ||
// type of value is `InsertValueSchema['user']` | ||
const value = { name: 'kanziw', email: 'kanziwoong@gmail.com' } | ||
const { insertId } = await db.execute(qb | ||
.insertInto('user') | ||
.values(value), | ||
) | ||
// close MySQL connection | ||
await close() | ||
``` | ||
### Database type | ||
1. `WithPkId`: for auto increment `id` column | ||
2. `WithDataLifecycleTracker` | ||
1. `created_at`: for `DATETIME DEFAULT CURRENT_TIMESTAMP` | ||
2. `updated_at`: for `DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP` | ||
3. `WithSchema`: `WithPkId` & `WithDataLifecycleTracker` | ||
## Recommended Usages Personally | ||
1. Write DDL | ||
1. See [ddl.sql](./ddl.sql) | ||
2. Prepare types | ||
1. Apply DDL to MySQL Server using [mysqldef](https://github.com/k0kubun/sqldef) | ||
2. Generate model using [sql-ts](https://github.com/rmp135/sql-ts) | ||
3. See `db:sync` script in [package.json](./package.json) | ||
3. Set up your own query builder | ||
1. See [mysql.ts](./src/__tests__/fixtures/mysql.ts) | ||
4. Write your own code! | ||
1. See [integration.spec.ts](./src/__tests__/integration.spec.ts) | ||
5. Write your test code using [createMockMySqlHelper](./src/test/mockMySql.ts) | ||
1. See [mockMySql.spec.ts](./src/__tests__/mockMySql.spec.ts) | ||
6. Subscribe MySQL Metrics | ||
1. See [subscribe.spec.ts](./src/__tests__/subscribe.spec.ts) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
1
-50%89
4350%0
-100%26696
-97.4%7
40%24
-11.11%473
-97.67%