import { getXataClient } from './Xata';
class KeyCache {
cache: Set<string>;
constructor() {
this.cache = new Set<string>();
this.init();
}
async init() {
const records = await getXataClient().db.uploads.select(['slug']).getAll();
records.forEach((rec) => {
if (rec && typeof rec.slug === 'string') {
this.cache.add(rec.slug);
}
});
}
getUniqueId(length: number) {
let string = this.generateRandomString(length);
while (this.cache.has(string)) string = this.generateRandomString(length);
this.cache.add(string);
return string;
}
private generateRandomString(length: number) {
let result = '';
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++)
result += characters.charAt(
Math.floor(Math.random() * characters.length),
);
return result;
}
}
export default new KeyCache();
import { getXataClient } from './Xata';
class KeyCache {
cache: Set<string>;
constructor() {
this.cache = new Set<string>();
this.init();
}
async init() {
const records = await getXataClient().db.uploads.select(['slug']).getAll();
records.forEach((rec) => {
if (rec && typeof rec.slug === 'string') {
this.cache.add(rec.slug);
}
});
}
getUniqueId(length: number) {
let string = this.generateRandomString(length);
while (this.cache.has(string)) string = this.generateRandomString(length);
this.cache.add(string);
return string;
}
private generateRandomString(length: number) {
let result = '';
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++)
result += characters.charAt(
Math.floor(Math.random() * characters.length),
);
return result;
}
}
export default new KeyCache();