update
This commit is contained in:
34
bun/.gitignore
vendored
Normal file
34
bun/.gitignore
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
# dependencies (bun install)
|
||||
node_modules
|
||||
|
||||
# output
|
||||
out
|
||||
dist
|
||||
*.tgz
|
||||
|
||||
# code coverage
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# logs
|
||||
logs
|
||||
_.log
|
||||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
# dotenv environment variable files
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# caches
|
||||
.eslintcache
|
||||
.cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# IntelliJ based IDEs
|
||||
.idea
|
||||
|
||||
# Finder (MacOS) folder config
|
||||
.DS_Store
|
||||
15
bun/README.md
Normal file
15
bun/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# bun
|
||||
|
||||
To install dependencies:
|
||||
|
||||
```bash
|
||||
bun install
|
||||
```
|
||||
|
||||
To run:
|
||||
|
||||
```bash
|
||||
bun run index.ts
|
||||
```
|
||||
|
||||
This project was created using `bun init` in bun v1.2.21. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
|
||||
42
bun/bun.lock
Normal file
42
bun/bun.lock
Normal file
@@ -0,0 +1,42 @@
|
||||
{
|
||||
"lockfileVersion": 1,
|
||||
"workspaces": {
|
||||
"": {
|
||||
"name": "bun",
|
||||
"dependencies": {
|
||||
"@duckdb/node-api": "^1.4.4-r.1",
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest",
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5",
|
||||
},
|
||||
},
|
||||
},
|
||||
"packages": {
|
||||
"@duckdb/node-api": ["@duckdb/node-api@1.4.4-r.1", "", { "dependencies": { "@duckdb/node-bindings": "1.4.4-r.1" } }, "sha512-oqaH9DXTJNwyLkd2FgJwmSnWVqjB5irbESeTeNVMBnM03iRaNY545BhfBDumu1TnOV2koIdG1mNsmjgq/ZTIkA=="],
|
||||
|
||||
"@duckdb/node-bindings": ["@duckdb/node-bindings@1.4.4-r.1", "", { "optionalDependencies": { "@duckdb/node-bindings-darwin-arm64": "1.4.4-r.1", "@duckdb/node-bindings-darwin-x64": "1.4.4-r.1", "@duckdb/node-bindings-linux-arm64": "1.4.4-r.1", "@duckdb/node-bindings-linux-x64": "1.4.4-r.1", "@duckdb/node-bindings-win32-x64": "1.4.4-r.1" } }, "sha512-NFm0AMrK3kiVLQhgnGUEjX5c8Elm93dYePZ9BUCvvd0AVVTKEBeRhBp9afziuzP3Sl5+7XQ1TyaBLsZJKKBDBQ=="],
|
||||
|
||||
"@duckdb/node-bindings-darwin-arm64": ["@duckdb/node-bindings-darwin-arm64@1.4.4-r.1", "", { "os": "darwin", "cpu": "arm64" }, "sha512-/NtbkCgCAOJDxw41XvSGV/mxQAlsx+2xUvhIVUj6fxoOfTG4jTttRhuphwE3EXNoWzJOjZxCZ5LwhC/qb6ZwLg=="],
|
||||
|
||||
"@duckdb/node-bindings-darwin-x64": ["@duckdb/node-bindings-darwin-x64@1.4.4-r.1", "", { "os": "darwin", "cpu": "x64" }, "sha512-lzFRDrZwc1EoV513vmKufasiAQ2WlhEb0O6guRBarbvOKKVhRb8tQ5H7LPVTrIewjTI3XDgHrnK+vfh9L+xQcA=="],
|
||||
|
||||
"@duckdb/node-bindings-linux-arm64": ["@duckdb/node-bindings-linux-arm64@1.4.4-r.1", "", { "os": "linux", "cpu": "arm64" }, "sha512-wq92/EcTiOTRW1RSDOwjeLyMMXWwNVNwU21TQdfu3sgS86+Ih3raaK68leDgY5cWgf72We3J2W7HYz8GwxcMYw=="],
|
||||
|
||||
"@duckdb/node-bindings-linux-x64": ["@duckdb/node-bindings-linux-x64@1.4.4-r.1", "", { "os": "linux", "cpu": "x64" }, "sha512-fjYNc+t4/T7mhzZ57oJoIQaWvbYVvxhidcNNansQFiWnd6/JMLCULd4qnt8XI3Tt2BrZsraH690KSBIS3QPt0w=="],
|
||||
|
||||
"@duckdb/node-bindings-win32-x64": ["@duckdb/node-bindings-win32-x64@1.4.4-r.1", "", { "os": "win32", "cpu": "x64" }, "sha512-+J+MUYGvYWfX0balWToDIy3CBYg7hHI0KQUQ39+SniinXlMF8+puRW6ebyQ+AXrcrKkwuj4wzJuEBD0AdhHGtw=="],
|
||||
|
||||
"@types/bun": ["@types/bun@1.3.8", "", { "dependencies": { "bun-types": "1.3.8" } }, "sha512-3LvWJ2q5GerAXYxO2mffLTqOzEu5qnhEAlh48Vnu8WQfnmSwbgagjGZV6BoHKJztENYEDn6QmVd949W4uESRJA=="],
|
||||
|
||||
"@types/node": ["@types/node@25.2.2", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-BkmoP5/FhRYek5izySdkOneRyXYN35I860MFAGupTdebyE66uZaR+bXLHq8k4DirE5DwQi3NuhvRU1jqTVwUrQ=="],
|
||||
|
||||
"bun-types": ["bun-types@1.3.8", "", { "dependencies": { "@types/node": "*" } }, "sha512-fL99nxdOWvV4LqjmC+8Q9kW3M4QTtTR1eePs94v5ctGqU8OeceWrSUaRw3JYb7tU3FkMIAjkueehrHPPPGKi5Q=="],
|
||||
|
||||
"typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
|
||||
|
||||
"undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="],
|
||||
}
|
||||
}
|
||||
4
bun/database.ts
Normal file
4
bun/database.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { DuckDBConnection, DuckDBInstance } from '@duckdb/node-api';
|
||||
|
||||
const instance = await DuckDBInstance.create('./jkhsakha.duckdb');
|
||||
export const connection = await DuckDBConnection.create(instance)
|
||||
297
bun/index.ts
Normal file
297
bun/index.ts
Normal file
@@ -0,0 +1,297 @@
|
||||
import { connection } from "./database";
|
||||
|
||||
const server = Bun.serve({
|
||||
port: 3490,
|
||||
routes: {
|
||||
"/api/post-grids/:id": async req => {
|
||||
const gridId = Number(req.params.id);
|
||||
if (!gridId) {
|
||||
return Response.json({ error: "Invalid grid id" }, { status: 400 });
|
||||
}
|
||||
|
||||
// pagination params
|
||||
const url = new URL(req.url);
|
||||
const page = Math.max(1, Number(url.searchParams.get("page") ?? 1));
|
||||
|
||||
// 1. Load grid config post
|
||||
const gridPost = await connection.run(`
|
||||
SELECT ID, post_title
|
||||
FROM wp_posts
|
||||
WHERE ID = $id
|
||||
LIMIT 1;
|
||||
`, { id: gridId });
|
||||
|
||||
const [grid] = await gridPost.getRowObjectsJson();
|
||||
if (!grid) {
|
||||
return Response.json(null, { status: 404 });
|
||||
}
|
||||
|
||||
// 2. Load grid meta
|
||||
const metaRes = await connection.run(`
|
||||
SELECT meta_key, meta_value
|
||||
FROM wp_postmeta
|
||||
WHERE post_id = $id;
|
||||
`, { id: gridId });
|
||||
|
||||
const meta = Object.fromEntries(
|
||||
(await metaRes.getRowObjectsJson())
|
||||
.map(m => [m.meta_key, m.meta_value])
|
||||
);
|
||||
|
||||
const postType = meta._rt_tp_post_type ?? "post";
|
||||
const perPage = Math.max(1, Number(meta._rt_tp_posts_per_page ?? 6));
|
||||
const orderBy = meta._rt_tp_orderby ?? "post_date";
|
||||
const order = meta._rt_tp_order === "ASC" ? "ASC" : "DESC";
|
||||
|
||||
const offset = (page - 1) * perPage;
|
||||
|
||||
// 3. Total count (for pagination)
|
||||
const totalRes = await connection.run(`
|
||||
SELECT COUNT(*)::int AS total
|
||||
FROM wp_posts
|
||||
WHERE post_status = 'publish'
|
||||
AND post_type = $postType;
|
||||
`, { postType });
|
||||
|
||||
const [{ total }] = await totalRes.getRowObjectsJson();
|
||||
const totalPages = Math.max(1, Math.ceil(total / perPage));
|
||||
|
||||
// clamp page if out of range
|
||||
const safePage = Math.min(page, totalPages);
|
||||
const safeOffset = (safePage - 1) * perPage;
|
||||
|
||||
// 4. Fetch paginated posts
|
||||
const postsRes = await connection.run(`
|
||||
SELECT
|
||||
ID,
|
||||
post_title,
|
||||
post_name,
|
||||
post_excerpt,
|
||||
post_date
|
||||
FROM wp_posts
|
||||
WHERE post_status = 'publish'
|
||||
AND post_type = $postType
|
||||
ORDER BY ${orderBy} ${order}
|
||||
LIMIT $perPage OFFSET $offset;
|
||||
`, {
|
||||
postType,
|
||||
perPage,
|
||||
offset: safeOffset
|
||||
});
|
||||
|
||||
const posts = await postsRes.getRowObjectsJson();
|
||||
|
||||
return Response.json({
|
||||
id: grid.ID,
|
||||
title: grid.post_title,
|
||||
page: safePage,
|
||||
perPage,
|
||||
total,
|
||||
totalPages,
|
||||
posts
|
||||
});
|
||||
},
|
||||
"/api/posts/slugs": async req => {
|
||||
const res = await connection.run(`
|
||||
SELECT post_name, post_type
|
||||
FROM wp_posts
|
||||
WHERE post_status = 'publish'
|
||||
AND post_type IN ('post', 'page')
|
||||
ORDER BY post_date DESC;
|
||||
`);
|
||||
|
||||
const data = await res.getRowObjectsJson()
|
||||
|
||||
if (data.length === 0) {
|
||||
return Response.json(null, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json(data[0]);
|
||||
},
|
||||
"/api/posts/id/:id": async req => {
|
||||
const res = await connection.run(`
|
||||
SELECT p.*
|
||||
FROM wp_posts p
|
||||
WHERE p.ID = $id
|
||||
LIMIT 1;
|
||||
`, { slug: Number(req.params.id) });
|
||||
|
||||
const data = await res.getRowObjectsJson()
|
||||
|
||||
if (data.length === 0) {
|
||||
return Response.json(null, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json(data[0]);
|
||||
},
|
||||
"/api/posts/:slug": async req => {
|
||||
const res = await connection.run(`
|
||||
SELECT
|
||||
*
|
||||
FROM wp_posts
|
||||
WHERE post_name = $slug
|
||||
AND post_status = 'publish'
|
||||
AND post_type IN ('post', 'page')
|
||||
LIMIT 1;
|
||||
`, { slug: req.params.slug.toString() });
|
||||
|
||||
const data = await res.getRowObjectsJson()
|
||||
|
||||
if (data.length === 0) {
|
||||
return Response.json(null, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json(data[0], { status: 200 });
|
||||
},
|
||||
"/api/pages/:slug": async req => {
|
||||
const res = await connection.run(`
|
||||
SELECT
|
||||
*
|
||||
FROM wp_posts
|
||||
WHERE post_name = $slug
|
||||
AND post_status = 'publish'
|
||||
AND post_type IN ('post', 'page')
|
||||
LIMIT 1;
|
||||
`, { slug: req.params.slug.toString() });
|
||||
|
||||
const data = await res.getRowObjectsJson()
|
||||
|
||||
if (data.length === 0) {
|
||||
return Response.json(null, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json(data[0]);
|
||||
},
|
||||
"/api/pages/id/:id": async req => {
|
||||
const res = await connection.run(`
|
||||
SELECT p.*
|
||||
FROM wp_posts p
|
||||
WHERE p.ID = $id
|
||||
LIMIT 1;`, { id: Number(req.params.id) });
|
||||
|
||||
const data = await res.getRowObjectsJson()
|
||||
|
||||
if (data.length === 0) {
|
||||
return Response.json(null, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json(data[0]);
|
||||
},
|
||||
"/api/home": async req => {
|
||||
const res = await connection.run(`
|
||||
SELECT p.*
|
||||
FROM wp_posts p
|
||||
WHERE p.ID = (
|
||||
SELECT option_value
|
||||
FROM wp_options
|
||||
WHERE option_name = 'page_on_front'
|
||||
LIMIT 1
|
||||
)
|
||||
LIMIT 1;`);
|
||||
|
||||
const data = await res.getRowObjectsJson()
|
||||
|
||||
if (data.length === 0) {
|
||||
return Response.json(null, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json(data);
|
||||
},
|
||||
"/api/menu/footer": async req => {
|
||||
const res = await connection.run(`
|
||||
SELECT
|
||||
menu_item.ID AS menu_item_id,
|
||||
menu_item.post_title AS menu_title,
|
||||
menu_item.menu_order,
|
||||
menu_item.post_parent,
|
||||
-- Get the actual page/post content
|
||||
actual_post.ID AS actual_post_id,
|
||||
actual_post.post_title AS actual_post_title,
|
||||
actual_post.post_type,
|
||||
actual_post.post_status,
|
||||
actual_post.post_name AS slug,
|
||||
-- Get the URL (for custom links)
|
||||
url_meta.meta_value AS custom_url,
|
||||
-- Get the object type (post, page, category, custom, etc.)
|
||||
object_type_meta.meta_value AS object_type,
|
||||
-- Get parent menu item info
|
||||
menu_parent_meta.meta_value AS parent_menu_item_id,
|
||||
FROM wp_posts AS menu_item
|
||||
INNER JOIN wp_term_relationships r ON menu_item.ID = r.object_id
|
||||
-- Join to get the actual post ID
|
||||
INNER JOIN wp_postmeta AS object_id_meta ON menu_item.ID = object_id_meta.post_id
|
||||
AND object_id_meta.meta_key = '_menu_item_object_id'
|
||||
-- Join to get the actual post content
|
||||
INNER JOIN wp_posts AS actual_post ON object_id_meta.meta_value = actual_post.ID
|
||||
-- Left joins for additional info
|
||||
LEFT JOIN wp_postmeta AS url_meta ON menu_item.ID = url_meta.post_id
|
||||
AND url_meta.meta_key = '_menu_item_url'
|
||||
LEFT JOIN wp_postmeta AS object_type_meta ON menu_item.ID = object_type_meta.post_id
|
||||
AND object_type_meta.meta_key = '_menu_item_object'
|
||||
LEFT JOIN wp_postmeta AS menu_parent_meta ON menu_item.ID = menu_parent_meta.post_id
|
||||
AND menu_parent_meta.meta_key = '_menu_item_menu_item_parent'
|
||||
WHERE r.term_taxonomy_id = 10
|
||||
AND menu_item.post_type = 'nav_menu_item'
|
||||
AND menu_item.post_status = 'publish'
|
||||
ORDER BY menu_item.menu_order;
|
||||
`);
|
||||
|
||||
const data = await res.getRowObjectsJson()
|
||||
|
||||
if (data.length === 0) {
|
||||
return Response.json(null, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json(data);
|
||||
},
|
||||
"/api/menu/navbar": async req => {
|
||||
const res = await connection.run(`
|
||||
SELECT
|
||||
menu_item.ID AS menu_item_id,
|
||||
menu_item.post_title AS menu_title,
|
||||
menu_item.menu_order,
|
||||
menu_item.post_parent,
|
||||
-- Get the actual page/post content
|
||||
actual_post.ID AS actual_post_id,
|
||||
actual_post.post_title AS actual_post_title,
|
||||
actual_post.post_type,
|
||||
actual_post.post_status,
|
||||
actual_post.post_name AS slug,
|
||||
-- Get the URL (for custom links)
|
||||
url_meta.meta_value AS custom_url,
|
||||
-- Get the object type (post, page, category, custom, etc.)
|
||||
object_type_meta.meta_value AS object_type,
|
||||
-- Get parent menu item info
|
||||
menu_parent_meta.meta_value AS parent_menu_item_id,
|
||||
FROM wp_posts AS menu_item
|
||||
INNER JOIN wp_term_relationships r ON menu_item.ID = r.object_id
|
||||
-- Join to get the actual post ID
|
||||
INNER JOIN wp_postmeta AS object_id_meta ON menu_item.ID = object_id_meta.post_id
|
||||
AND object_id_meta.meta_key = '_menu_item_object_id'
|
||||
-- Join to get the actual post content
|
||||
INNER JOIN wp_posts AS actual_post ON object_id_meta.meta_value = actual_post.ID
|
||||
-- Left joins for additional info
|
||||
LEFT JOIN wp_postmeta AS url_meta ON menu_item.ID = url_meta.post_id
|
||||
AND url_meta.meta_key = '_menu_item_url'
|
||||
LEFT JOIN wp_postmeta AS object_type_meta ON menu_item.ID = object_type_meta.post_id
|
||||
AND object_type_meta.meta_key = '_menu_item_object'
|
||||
LEFT JOIN wp_postmeta AS menu_parent_meta ON menu_item.ID = menu_parent_meta.post_id
|
||||
AND menu_parent_meta.meta_key = '_menu_item_menu_item_parent'
|
||||
WHERE r.term_taxonomy_id = 24
|
||||
AND menu_item.post_type = 'nav_menu_item'
|
||||
AND menu_item.post_status = 'publish'
|
||||
ORDER BY menu_item.menu_order;
|
||||
`);
|
||||
|
||||
const data = await res.getRowObjectsJson()
|
||||
|
||||
if (data.length === 0) {
|
||||
return Response.json(null, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json(data);
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
console.log(`Server running at ${server.url}`);
|
||||
BIN
bun/jkhsakha.duckdb
Normal file
BIN
bun/jkhsakha.duckdb
Normal file
Binary file not shown.
15
bun/package.json
Normal file
15
bun/package.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "bun",
|
||||
"module": "index.ts",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@duckdb/node-api": "^1.4.4-r.1"
|
||||
}
|
||||
}
|
||||
29
bun/tsconfig.json
Normal file
29
bun/tsconfig.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
// Environment setup & latest features
|
||||
"lib": ["ESNext"],
|
||||
"target": "ESNext",
|
||||
"module": "Preserve",
|
||||
"moduleDetection": "force",
|
||||
"jsx": "react-jsx",
|
||||
"allowJs": true,
|
||||
|
||||
// Bundler mode
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"noEmit": true,
|
||||
|
||||
// Best practices
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"noImplicitOverride": true,
|
||||
|
||||
// Some stricter flags (disabled by default)
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": false,
|
||||
"noPropertyAccessFromIndexSignature": false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user