https://github.com/alenap93/kysely-crypto
Crypto plugin for kysely
https://github.com/alenap93/kysely-crypto
crypto database kysely query-builder sql
Last synced: about 1 year ago
JSON representation
Crypto plugin for kysely
- Host: GitHub
- URL: https://github.com/alenap93/kysely-crypto
- Owner: alenap93
- License: mit
- Created: 2025-02-01T15:31:41.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-05T06:42:06.000Z (about 1 year ago)
- Last Synced: 2025-04-05T07:27:21.523Z (about 1 year ago)
- Topics: crypto, database, kysely, query-builder, sql
- Language: TypeScript
- Homepage:
- Size: 277 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# kysely-crypto
[](https://github.com/alenap93/kysely-crypto/actions/workflows/test.yml)
[](https://www.npmjs.com/package/kysely-crypto)
[](https://www.npmjs.com/package/kysely-crypto)
[](https://prettier.io/)
Crypto plugin for kysely, fully typed, developed for Node.js but also runs on Deno and Bun; with this plugin you can encrypt or decrypt fields using crypto-js library.
## Install
```
npm i kysely kysely-crypto
```
## Usage
**Options**
* fieldsToDecrypt: *field to decrypt in result (use alias if it is used)*
* fieldsToEncrypt: *field to encrypt during insert and update, or in '=' and '!=' where condition (use alias if it is used)*
* cryptoParameters: *encryption options*
* cryptoAlgorithm: *cipher algorithm*
* secretKey: *custom secretKey*
* iv: *custom iv in hex to be converted in word array (using CryptoJS.enc.Hex.parse(iv))*
* mode: *block mode, default: CBC*
* padding: *block padding, default: Pkcs7*
**How to use**
***WARNING: fields must be of string types***
const kyselyInstance = new Kysely({
dialect: new SqliteDialect({
database: new Database(':memory:'),
}),
})
await kyselyInstance.schema
.createTable('person')
.ifNotExists()
.addColumn('id', 'integer', (col) => col.primaryKey())
.addColumn('first_name', 'varchar(255)')
.addColumn('last_name', 'varchar(255)')
.addColumn('gender', 'varchar(255)')
.execute()
await kyselyInstance
.insertInto('person')
.values([
{
first_name: 'Max',
last_name: 'Jack',
gender: 'man',
},
{
first_name: 'George',
last_name: 'Rossi',
gender: 'man',
},
])
.withPlugin(
new KyselyCryptoPlugin({
fieldsToEncrypt: ['last_name'],
fieldsToDecrypt: [],
cryptoParameters: {
cryptoAlgorithm: 'AES',
secretKey: '996bf1b118a02007ea2c7001d92e0f91',
iv: 'df77b550164054c9e671ebbf2f9976b0',
},
}),
)
.execute()
const selectDecrypted = await kyselyInstance
.selectFrom('person')
.selectAll()
.where('last_name', '=', 'Jack')
.withPlugin(
new KyselyCryptoPlugin({
fieldsToEncrypt: ['last_name'],
fieldsToDecrypt: ['last_name'],
cryptoParameters: {
cryptoAlgorithm: 'AES',
secretKey: '996bf1b118a02007ea2c7001d92e0f91',
iv: 'df77b550164054c9e671ebbf2f9976b0',
}
}),
)
.executeTakeFirst()
## License
Licensed under [MIT](./LICENSE).