Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/txthinking/denolib
A Deno library to keep everything small.
https://github.com/txthinking/denolib
crypto database database-migrations deno denojs httpserver kiss library migration redis
Last synced: 3 months ago
JSON representation
A Deno library to keep everything small.
- Host: GitHub
- URL: https://github.com/txthinking/denolib
- Owner: txthinking
- License: mit
- Created: 2020-05-21T11:52:31.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-03-16T12:40:48.000Z (almost 2 years ago)
- Last Synced: 2024-10-16T20:05:03.766Z (4 months ago)
- Topics: crypto, database, database-migrations, deno, denojs, httpserver, kiss, library, migration, redis
- Language: JavaScript
- Homepage:
- Size: 117 KB
- Stars: 11
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Denolib
*A **Deno** library to keep everything small.*
## HTTP Server
```javascript
import server from 'https://raw.githubusercontent.com/txthinking/denolib/master/httpserver.js';server.path('/hello',async (request) =>
new Response('Hello World',{ status : 200 }));server.run({ port : 2020 });
```
### Static
```javascript
httpserver.staticdir = '/path/to/static';
```### Static + **[DenoBundle]**
```javascript
import readFileSync from './bundle.js';httpserver.readfile = (path) =>
readFileSync('static' + path);
```
### SPA
```javascript
httpserver.spa = true;
```
### CORS
```javascript
httpserver.cors = '*';
```
### 404
```javascript
httpserver.default = (request) => {...}
```
## Crypto
```javascript
import crypto from 'https://raw.githubusercontent.com/txthinking/denolib/master/crypto.js';// Pass in a 32 length key
const kv = crypto('abcdefghijklmnopqrstuvwxyz012345');
const token = await kv.encrypt('uid',1);
const uid = await kv.decrypt('uid',token);
``````javascript
// Only allow token to be valid for 30 daysconst uid = await kv.decrypt('uid',token,30 * 24 * 60 * 60);
```
---
## MySQL
### Connect
```javascript
import mysql from 'https://raw.githubusercontent.com/txthinking/denolib/master/mysql.js';const database = await mysql({
hostname : '127.0.0.1' ,
password : '111111' ,
username : 'root' ,
poolSize : 3 ,
port : 3306 ,
db : 'dbname'
});
```
### Migrate
```javascript
import migrate from 'https://raw.githubusercontent.com/txthinking/denolib/master/migrate.js';const mg = await migrate(database);
// Each unique id execute at most once
await mg('A unique id string', `
CREATE TABLE user (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
email varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL default '',
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`);await mg('Another unique id string','another sql');
```
### Curd
> if you want to use this four methods, set auto increment primary key: id, set not null and default value for each field
```javascript
// table name and row object, keys must match table fields or lessconst row = await database.c('user',{
email : '[email protected]'
});
``````javascript
// object keys must match table fields or less and must contain idconst row = await database.u('user',{
email : '[email protected]' ,
id : 1
});
``````javascript
// pass in idconst row = await database.r('user',1);
``````javascript
// pass in idawait database.d('user',1);
```
### SQL
```javascript
const rows = await database.query(
'select * from user where id=?',[1]);await database.execute(
'update user set email=? where id=?',
[ '[email protected]' , 1 ]);
```
### Transaction
```javascript
const request = await database.transaction(async (database) => {
const request = await database.c('user',{
email : '[email protected]'
});
// throw new Error('rollback');
await database.execute('update user set email=? where id=?',
[ '[email protected]' , 1 ]);
const rows = await database.query(
'select * from user where id=?',[1]);
return rows;
});
```
---
## Redis
### Connect
```javascript
import redis from 'https://raw.githubusercontent.com/txthinking/denolib/master/redis.js';const rds = await redis({
hostname : '127.0.0.1' ,
port : 6379
});
```
### command
```javascript
const request = await rds.exec( 'set' , 'hi' , 'httpserver' );
``````javascript
const request = await rds.exec( 'get' , 'hi' );
```### Pipeline
```javascript
await rds.pipeline((rds) => {
rds.exec( 'set' , 'hi' , 'httpserver' );
rds.exec( 'set' , 'hey' , 'httpserver' );
});
```### Transaction
> Guarantee atomicity
```javascript
await rds.transaction((rds) => {
rds.exec( 'set' , 'hi' , 'httpserver1' );
rds.exec( 'set' , 'hey' , 'httpserver2' );
});
```### Subscribe
```javascript
const channel = await rds.subscribe('channel');for await (const event of channel.receive())
console.log(event);
```
[denobundle]: https://github.com/txthinking/denobundle