UNPKG

wowok

Version:

Wowok Blockchain TypeScript API

1 lines 4.54 kB
import a176a from'libsql';import*as a176b from'path';import*as a176c from'fs';import{getWowDir}from'./util.js';const WOW_DIR=getWowDir(),CACHE_DB_PATH=a176b['join'](WOW_DIR,'cache.db'),ACCOUNT_DB_PATH=a176b['join'](WOW_DIR,'account.db');function ensureWowDirExists(){!a176c['existsSync'](WOW_DIR)&&a176c['mkdirSync'](WOW_DIR,{'recursive':!![]});}ensureWowDirExists();function getCacheDatabase(){ensureWowDirExists();const a=new a176a(CACHE_DB_PATH);return a['pragma']('journal_mode\x20=\x20WAL'),a['pragma']('synchronous\x20=\x20NORMAL'),a['exec']('\x0a\x09\x09CREATE\x20TABLE\x20IF\x20NOT\x20EXISTS\x20cache\x20(\x0a\x09\x09\x09key\x20TEXT\x20PRIMARY\x20KEY,\x0a\x09\x09\x09value\x20TEXT\x20NOT\x20NULL,\x0a\x09\x09\x09updated_at\x20INTEGER\x20DEFAULT\x20(strftime(\x27%s\x27,\x20\x27now\x27))\x0a\x09\x09);\x0a\x09'),a;}let cacheDbInstance=null;function getCacheDb(){return!cacheDbInstance&&(cacheDbInstance=getCacheDatabase()),cacheDbInstance;}function getAccountDatabase(){ensureWowDirExists();const a=new a176a(ACCOUNT_DB_PATH);a['pragma']('journal_mode\x20=\x20WAL'),a['pragma']('synchronous\x20=\x20NORMAL');try{a176c['chmodSync'](ACCOUNT_DB_PATH,0x180);}catch{}return a['exec']('\x0a\x09\x09CREATE\x20TABLE\x20IF\x20NOT\x20EXISTS\x20accounts\x20(\x0a\x09\x09\x09id\x20INTEGER\x20PRIMARY\x20KEY\x20CHECK\x20(id\x20=\x201),\x0a\x09\x09\x09data\x20TEXT\x20NOT\x20NULL,\x0a\x09\x09\x09updated_at\x20INTEGER\x20DEFAULT\x20(strftime(\x27%s\x27,\x20\x27now\x27))\x0a\x09\x09);\x0a\x09'),a;}let accountDbInstance=null;function getAccountDb(){return!accountDbInstance&&(accountDbInstance=getAccountDatabase()),accountDbInstance;}export class SQLiteStorage{['_db'];['_dbSource'];constructor(a){this['_dbSource']=a;}get['db'](){if(this['_dbSource'])return this['_dbSource'];return!this['_db']&&(this['_db']=getCacheDb()),this['_db'];}async['get'](a){const b=this['db']['prepare']('SELECT\x20value\x20FROM\x20cache\x20WHERE\x20key\x20=\x20?')['get'](a);return b?.['value'];}async['getMany'](a){const b=a['map'](()=>'?')['join'](','),c=this['db']['prepare']('SELECT\x20key,\x20value\x20FROM\x20cache\x20WHERE\x20key\x20IN\x20('+b+')')['all'](...a),d=new Map(c['map'](e=>[e['key'],e['value']]));return a['map'](e=>d['get'](e));}async['put'](a,b){this['db']['prepare']('\x0a\x09\x09\x09INSERT\x20INTO\x20cache\x20(key,\x20value,\x20updated_at)\x20\x0a\x09\x09\x09VALUES\x20(?,\x20?,\x20strftime(\x27%s\x27,\x20\x27now\x27))\x0a\x09\x09\x09ON\x20CONFLICT(key)\x20DO\x20UPDATE\x20SET\x20value\x20=\x20excluded.value,\x20updated_at\x20=\x20excluded.updated_at\x0a\x09\x09')['run'](a,b);}async['putMany'](a){const b=this['db']['prepare']('\x0a\x09\x09\x09INSERT\x20INTO\x20cache\x20(key,\x20value,\x20updated_at)\x20\x0a\x09\x09\x09VALUES\x20(?,\x20?,\x20strftime(\x27%s\x27,\x20\x27now\x27))\x0a\x09\x09\x09ON\x20CONFLICT(key)\x20DO\x20UPDATE\x20SET\x20value\x20=\x20excluded.value,\x20updated_at\x20=\x20excluded.updated_at\x0a\x09\x09'),c=this['db']['transaction'](d=>{for(const e of d)b['run'](e['key'],e['value']);});c(a);}async['del'](a){this['db']['prepare']('DELETE\x20FROM\x20cache\x20WHERE\x20key\x20=\x20?')['run'](a);}async['clear'](){this['db']['exec']('DELETE\x20FROM\x20cache');}}export class SQLiteAccountStorage{['_db'];['_dbSource'];constructor(a){this['_dbSource']=a;}get['db'](){if(this['_dbSource'])return this['_dbSource'];return!this['_db']&&(this['_db']=getAccountDb()),this['_db'];}async['get'](){const a=this['db']['prepare']('SELECT\x20data\x20FROM\x20accounts\x20WHERE\x20id\x20=\x201')['get']();return a?.['data'];}async['put'](a){this['db']['prepare']('\x0a\x09\x09\x09INSERT\x20INTO\x20accounts\x20(id,\x20data,\x20updated_at)\x20\x0a\x09\x09\x09VALUES\x20(1,\x20?,\x20strftime(\x27%s\x27,\x20\x27now\x27))\x0a\x09\x09\x09ON\x20CONFLICT(id)\x20DO\x20UPDATE\x20SET\x20data\x20=\x20excluded.data,\x20updated_at\x20=\x20excluded.updated_at\x0a\x09\x09')['run'](a);}async['clear'](){this['db']['exec']('DELETE\x20FROM\x20accounts');}}export function resetAllDbConnections(){if(cacheDbInstance){try{cacheDbInstance['close']();}catch(a){}cacheDbInstance=null;}if(accountDbInstance){try{accountDbInstance['close']();}catch(b){}accountDbInstance=null;}}process['on']('exit',()=>{resetAllDbConnections();}),process['on']('SIGINT',()=>{resetAllDbConnections(),process['exit'](0x0);}),process['on']('SIGTERM',()=>{resetAllDbConnections(),process['exit'](0x0);});export function resetAllStorage(){resetAllDbConnections();}export const defaultStorage=new SQLiteStorage();export const defaultAccountStorage=new SQLiteAccountStorage();