Improve type safety
This commit is contained in:
@ -45,14 +45,14 @@ db.on('open', () => {
|
||||
console.log('Opened database');
|
||||
db.serialize();
|
||||
db.run('CREATE TABLE IF NOT EXISTS "migrations" ("id" integer,"name" TEXT, PRIMARY KEY (id))');
|
||||
db.all('SELECT id FROM migrations', (err, rows) => {
|
||||
db.all('SELECT id FROM migrations', (err, rows: Migration[]) => {
|
||||
if (err !== null) {
|
||||
console.error('Could not fetch existing migrations', err);
|
||||
databaseReady = true;
|
||||
return;
|
||||
}
|
||||
console.debug('Already applied migrations', rows);
|
||||
const appliedMigrations: Set<number> = new Set(rows.map((row: any) => row['id']));
|
||||
const appliedMigrations: Set<number> = new Set(rows.map((row) => row['id']));
|
||||
const toApply = getMigrations().filter((m) => !appliedMigrations.has(m.id));
|
||||
let remaining = toApply.length;
|
||||
if (remaining === 0) {
|
||||
@ -291,13 +291,20 @@ export async function savePost(post: Post): Promise<undefined> {
|
||||
});
|
||||
}
|
||||
|
||||
type FilterParameter = {
|
||||
$limit: number | undefined | null;
|
||||
$since?: string | undefined | null;
|
||||
$before?: string | undefined | null;
|
||||
[x: string]: string | number | undefined | null;
|
||||
};
|
||||
|
||||
export async function getPosts(since: string | null, before: string | null, limit: number) {
|
||||
if (!databaseReady) {
|
||||
await waitReady();
|
||||
}
|
||||
const promise = await new Promise<Post[]>((resolve, reject) => {
|
||||
let filter_query = '';
|
||||
const params: any = { $limit: limit };
|
||||
const params: FilterParameter = { $limit: limit };
|
||||
if (since === null && before === null) {
|
||||
filter_query = '';
|
||||
} else if (since !== null) {
|
||||
@ -319,6 +326,25 @@ export async function getPosts(since: string | null, before: string | null, limi
|
||||
params[usernameParam] = ignoredUser;
|
||||
});
|
||||
|
||||
type PostResult = {
|
||||
id: string;
|
||||
content: string;
|
||||
created_at: string;
|
||||
url: string;
|
||||
account_id: string;
|
||||
acct: string;
|
||||
username: string;
|
||||
display_name: string;
|
||||
account_url: string;
|
||||
avatar: string;
|
||||
};
|
||||
|
||||
type PostTagResult = {
|
||||
post_id: string;
|
||||
tag: string;
|
||||
url: string;
|
||||
};
|
||||
|
||||
const sql = `SELECT posts.id, posts.content, posts.created_at, posts.url,
|
||||
accounts.id AS account_id, accounts.acct, accounts.username, accounts.display_name,
|
||||
accounts.url AS account_url, accounts.avatar
|
||||
@ -327,7 +353,7 @@ export async function getPosts(since: string | null, before: string | null, limi
|
||||
${filter_query}
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $limit`;
|
||||
db.all(sql, params, (err, rows: any[]) => {
|
||||
db.all(sql, params, (err, rows: PostResult[]) => {
|
||||
if (err != null) {
|
||||
console.error('Error loading posts', err);
|
||||
reject(err);
|
||||
@ -344,8 +370,8 @@ export async function getPosts(since: string | null, before: string | null, limi
|
||||
FROM poststags
|
||||
JOIN tags ON poststags.tag_url = tags.url
|
||||
WHERE post_id IN (${postIdsParams});`,
|
||||
rows.map((r: any) => r.url),
|
||||
(tagErr, tagRows: any[]) => {
|
||||
rows.map((r: PostResult) => r.url),
|
||||
(tagErr, tagRows: PostTagResult[]) => {
|
||||
if (tagErr != null) {
|
||||
console.error('Error loading post tags', tagErr);
|
||||
reject(tagErr);
|
||||
|
Reference in New Issue
Block a user