Starting post load
This commit is contained in:
Vendored
+5
-1
@@ -1,9 +1,13 @@
|
||||
// See https://kit.svelte.dev/docs/types#app
|
||||
// for information about these interfaces
|
||||
import type { Sql } from "postgres";
|
||||
|
||||
declare global {
|
||||
namespace App {
|
||||
// interface Error {}
|
||||
// interface Locals {}
|
||||
interface Locals {
|
||||
sql: Sql
|
||||
}
|
||||
// interface PageData {}
|
||||
// interface PageState {}
|
||||
// interface Platform {}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
<script>
|
||||
/**
|
||||
* @type {import('$types/base').Post}
|
||||
*/
|
||||
export let post;
|
||||
</script>
|
||||
|
||||
<div>
|
||||
<h3>{post.name}</h3>
|
||||
<!-- <p>{post.author.name}</p> -->
|
||||
<p>{post.content}</p>
|
||||
</div>
|
||||
@@ -0,0 +1,14 @@
|
||||
<script>
|
||||
import Post from './post.svelte';
|
||||
|
||||
/**
|
||||
* @type {import("$types/base").Post[]}
|
||||
*/
|
||||
export let posts = [];
|
||||
</script>
|
||||
|
||||
<div>
|
||||
{#each posts as post}
|
||||
<Post post={post}></Post>
|
||||
{/each}
|
||||
</div>
|
||||
@@ -0,0 +1,41 @@
|
||||
// @ts-nocheck
|
||||
import { env } from '$lib/env';
|
||||
import postgres from 'postgres';
|
||||
import ssh2 from 'ssh2';
|
||||
|
||||
export const handle = async ({event, resolve}) => {
|
||||
const sql = postgres(
|
||||
// 'postgres://doki8902:[email protected]:5432/studentu',
|
||||
{
|
||||
host: env.PG_HOST,
|
||||
port: parseInt(env.PG_PORT),
|
||||
database: env.PG_DATABASE,
|
||||
username: env.PG_USERNAME,
|
||||
password: env.PG_PASSWORD,
|
||||
socket: ({ host: [host], port: [port] }) => new Promise((resolve, reject) => {
|
||||
const ssh = new ssh2.Client();
|
||||
ssh
|
||||
.on('error', reject)
|
||||
.on('ready', () =>
|
||||
ssh.forwardOut('127.0.0.1', 12345, host, port,
|
||||
(err, socket) => err ? reject(err) : resolve(socket)
|
||||
)
|
||||
)
|
||||
.connect({
|
||||
host: env.SSH_HOST,
|
||||
port: parseInt(env.SSH_PORT),
|
||||
username: env.SSH_USERNAME,
|
||||
password: env.SSH_PASSWORD
|
||||
})
|
||||
})
|
||||
}
|
||||
);
|
||||
|
||||
// https://github.com/porsager/postgres/issues/762
|
||||
|
||||
event.locals = {
|
||||
sql: sql
|
||||
};
|
||||
const response = await resolve(event);
|
||||
return response;
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @param {import('postgres').Sql} sql
|
||||
* @param {number | undefined} category
|
||||
* @param {number} limit
|
||||
* @param {number} offset
|
||||
* @returns {Promise<import('$types/base').Post[]>}
|
||||
*/
|
||||
export async function getPosts(sql, category = undefined, limit = 10, offset = 0) {
|
||||
let query;
|
||||
|
||||
if (category === undefined) {
|
||||
query = sql`
|
||||
SELECT name, latest_content
|
||||
FROM doki8902.message_post
|
||||
FETCH FIRST ${limit} ROWS ONLY
|
||||
OFFSET ${offset};`;
|
||||
} else {
|
||||
query = sql`
|
||||
SELECT name, latest_content
|
||||
FROM doki8902.message_post
|
||||
WHERE category_id = ${category}
|
||||
FETCH FIRST ${limit} ROWS ONLY
|
||||
OFFSET ${offset};`;
|
||||
}
|
||||
|
||||
let posts = await query;
|
||||
|
||||
/**
|
||||
* @type {import('$types/base').Post[]}
|
||||
*/
|
||||
let result = [];
|
||||
|
||||
posts.forEach(row => {
|
||||
result.push({
|
||||
name: row['name'],
|
||||
content: row['latest_content']
|
||||
});
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// export function runQuery(
|
||||
// /** @type {import('postgres').Sql} */ sql,
|
||||
// /** @type {string} */ query,
|
||||
// /** @type {any[]} */ args = []) {
|
||||
// sql`${query}`
|
||||
// }
|
||||
@@ -0,0 +1,8 @@
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
/**
|
||||
* @type {import('$types/env').Env}
|
||||
*/
|
||||
export const env = process.env;
|
||||
@@ -0,0 +1 @@
|
||||
<slot />
|
||||
@@ -0,0 +1,15 @@
|
||||
import { getPosts } from '$lib/db/post';
|
||||
import { env } from '$lib/env';
|
||||
|
||||
/** @type {import('./$types').PageServerLoad} */
|
||||
export async function load({ locals }) {
|
||||
let result = await getPosts(locals.sql);
|
||||
|
||||
// console.log();
|
||||
|
||||
console.log(result);
|
||||
|
||||
return {
|
||||
posts: result
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<script>
|
||||
import Postlist from "$comp/postlist.svelte";
|
||||
|
||||
/**
|
||||
* @type {{posts: import("$types/base").Post[]}}
|
||||
*/
|
||||
export let data;
|
||||
</script>
|
||||
|
||||
<h1>Posts</h1>
|
||||
<Postlist posts={data.posts}></Postlist>
|
||||
@@ -0,0 +1,8 @@
|
||||
/** @type {import('./$types').PageServerLoad} */
|
||||
export function load({ cookies }) {
|
||||
// coo
|
||||
|
||||
return {
|
||||
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
export type User = {
|
||||
name: string
|
||||
};
|
||||
|
||||
export type Rating = {
|
||||
likes: number,
|
||||
dislikes: number
|
||||
}
|
||||
|
||||
export type Category = {
|
||||
name: string
|
||||
}
|
||||
|
||||
export type Post = {
|
||||
// author: User,
|
||||
name: string,
|
||||
// category: Category,
|
||||
content: string,
|
||||
// post_date: Date,
|
||||
// rating: Rating
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
export type Env = {
|
||||
PG_USERNAME: string,
|
||||
PG_PASSWORD: string,
|
||||
PG_HOST: string,
|
||||
PG_PORT: string,
|
||||
PG_DATABASE: string,
|
||||
|
||||
SSH_HOST: string,
|
||||
SSH_PORT: string,
|
||||
SSH_USERNAME: string,
|
||||
SSH_PASSWORD: string
|
||||
};
|
||||
Reference in New Issue
Block a user