Skip to content

Commit

Permalink
fix api_key bugs cause server not started (#58)
Browse files Browse the repository at this point in the history
Signed-off-by: cbh778899 <[email protected]>
  • Loading branch information
cbh778899 authored Aug 19, 2024
1 parent afe69fc commit c01ff0d
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions tools/apiKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { API_KEY_TABLE } from "../database/types.js";
import { getTable } from "../database/index.js";

const MAX_USAGE = 10;
const tbl = await getTable(API_KEY_TABLE);

function queryApiKeyTbl(key) {
async function queryApiKeyTbl(key, table = null) {
const tbl = table || await getTable(API_KEY_TABLE);
const keyQuery = tbl.query()
.where('api_key = '+"'"+key+"'")
.limit(1)
Expand All @@ -28,7 +28,8 @@ function queryApiKeyTbl(key) {
}

export async function addKeytoTable(key){
const keyQuery = await queryApiKeyTbl(key);
const tbl = await getTable(API_KEY_TABLE);
const keyQuery = await queryApiKeyTbl(key, tbl);
if ( keyQuery.length == 0){
await tbl.add([{api_key: key, usage: MAX_USAGE}]);
}
Expand All @@ -38,7 +39,8 @@ export async function addKeytoTable(key){
}

export async function updateKeyUsage(key){
const keyQuery = await queryApiKeyTbl(key);
const tbl = await getTable(API_KEY_TABLE);
const keyQuery = await queryApiKeyTbl(key, tbl);
console.log(keyQuery)
if (keyQuery.length > 0){
let usage = keyQuery[0].usage
Expand Down

0 comments on commit c01ff0d

Please sign in to comment.