Skip to content

A Deno library to keep everything small.

License

Notifications You must be signed in to change notification settings

txthinking/denolib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Denolib

A Deno library to keep everything small.


HTTP Server


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

httpserver.staticdir = '/path/to/static';

Static + DenoBundle

import readFileSync from './bundle.js';

httpserver.readfile = (path) => 
    readFileSync('static' + path);

SPA

httpserver.spa = true;

CORS

httpserver.cors = '*';

404

httpserver.default = (request) => {...}


Crypto

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);
// Only allow token to be valid for 30 days

const uid = await kv.decrypt('uid',token,30 * 24 * 60 * 60);



MySQL


Connect

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

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

// table name and row object, keys must match table fields or less

const row = await database.c('user',{ 
    email : 'hi@httpserver.com'
});
// object keys must match table fields or less and must contain id

const row = await database.u('user',{ 
    email : 'hey@httpserver.com' ,
    id : 1 
});
// pass in id

const row = await database.r('user',1);
// pass in id

await database.d('user',1);

SQL

const rows = await database.query(
    'select * from user where id=?',[1]);

await database.execute(
    'update user set email=? where id=?',
    [ 'hi@httpserver.com' , 1 ]);

Transaction

const request = await database.transaction(async (database) => {
    
    const request = await database.c('user',{
        email : 'hey@httpserver.com'
    });
    
    // throw new Error('rollback');
    
    await database.execute('update user set email=? where id=?',
        [ 'hi@httpserver.com' , 1 ]);
    
    const rows = await database.query(
        'select * from user where id=?',[1]);
    
    return rows;
});



Redis


Connect

import redis from 'https://raw.githubusercontent.com/txthinking/denolib/master/redis.js';

const rds = await redis({
    hostname : '127.0.0.1' , 
    port : 6379
});

command

const request = await rds.exec( 'set' , 'hi' , 'httpserver' );
const request = await rds.exec( 'get' , 'hi' );

Pipeline

await rds.pipeline((rds) => {
    rds.exec( 'set' , 'hi' , 'httpserver' );
    rds.exec( 'set' , 'hey' , 'httpserver' );
});

Transaction

Guarantee atomicity

await rds.transaction((rds) => {
    rds.exec( 'set' , 'hi' , 'httpserver1' );
    rds.exec( 'set' , 'hey' , 'httpserver2' );
});

Subscribe

const channel = await rds.subscribe('channel');

for await (const event of channel.receive())
    console.log(event);