add database crud operation function to plugin system

This commit is contained in:
zhongqin
2026-05-19 02:20:48 +08:00
parent 493be8f7de
commit 15c6094de7
22 changed files with 2201 additions and 1002 deletions
+13 -28
View File
@@ -8,17 +8,10 @@ local sdk = require("sdk")
Plugin = {}
Plugin.stats_overview = function(input)
local posts = sdk.dbQuery("SELECT CAST(COUNT(*) AS TEXT) as total FROM posts")
if not posts then return sdk.fail(500, "query failed") end
local comments = sdk.dbQuery("SELECT CAST(COUNT(*) AS TEXT) as total FROM comments")
local users = sdk.dbQuery("SELECT CAST(COUNT(*) AS TEXT) as total FROM users")
local published = sdk.dbQuery("SELECT CAST(COUNT(*) AS TEXT) as total FROM posts WHERE status = 'published'")
local total_posts = posts[1] and tonumber(posts[1].total) or 0
local total_comments = comments and comments[1] and tonumber(comments[1].total) or 0
local total_users = users and users[1] and tonumber(users[1].total) or 0
local total_published = published and published[1] and tonumber(published[1].total) or 0
local total_posts = sdk.dbCount("posts")
local total_comments = sdk.dbCount("comments")
local total_users = sdk.dbCount("users")
local total_published = sdk.dbCount("posts", { status = "published" })
sdk.logInfo("[blog-stats] GET overview")
@@ -31,27 +24,19 @@ Plugin.stats_overview = function(input)
end
Plugin.stats_posts = function(input)
local result = sdk.dbQuery("SELECT status, CAST(COUNT(*) AS TEXT) as count FROM posts GROUP BY status")
if not result then return sdk.fail(500, "query failed") end
return sdk.ok(result)
local result = sdk.dbGroupBy("posts", {
group_by = "status",
count = true
})
return sdk.ok(result.data or {})
end
Plugin.stats_recent = function(input)
local result = sdk.dbQuery(
"SELECT title, slug, view_count, published_at "
.. "FROM posts WHERE status = 'published' "
.. "ORDER BY published_at DESC LIMIT 10"
)
if not result then return sdk.fail(500, "query failed") end
return sdk.ok(result)
local result = sdk.dbFetchAll("posts", { status = "published" }, { order_by = "published_at DESC", limit = 10 })
return sdk.ok(result.data or {})
end
Plugin.stats_top = function(input)
local result = sdk.dbQuery(
"SELECT title, slug, view_count "
.. "FROM posts WHERE status = 'published' "
.. "ORDER BY view_count DESC LIMIT 10"
)
if not result then return sdk.fail(500, "query failed") end
return sdk.ok(result)
local result = sdk.dbFetchAll("posts", { status = "published" }, { order_by = "view_count DESC", limit = 10 })
return sdk.ok(result.data or {})
end
+20
View File
@@ -2,6 +2,17 @@ export interface DbExecResult {
error?: string;
rows_affected?: number;
}
export interface DbCrudResult {
rows_affected?: number;
}
export interface DbFetchAllResult {
data: Record<string, unknown>[];
total: number;
}
export interface DbGroupByResult {
data: Record<string, unknown>[];
total: number;
}
export interface PluginError {
__plugin_error: boolean;
__status: number;
@@ -10,6 +21,15 @@ export interface PluginError {
export declare const SDK_VERSION: string;
export declare function dbQuery(sql: string, params?: unknown[]): Record<string, unknown>[];
export declare function dbExec(sql: string, params?: unknown[]): DbExecResult;
export declare function dbInsert(table: string, data: Record<string, unknown> | string, options?: Record<string, unknown>): DbCrudResult;
export declare function dbFetchOne(table: string, where: Record<string, unknown> | string | unknown[], options?: Record<string, unknown>): Record<string, unknown> | null;
export declare function dbFetchAll(table: string, where?: Record<string, unknown> | string | unknown[] | null, options?: Record<string, unknown>): DbFetchAllResult;
export declare function dbUpdate(table: string, data: Record<string, unknown> | string, where: Record<string, unknown> | string | unknown[], options?: Record<string, unknown>): DbCrudResult;
export declare function dbDelete(table: string, where: Record<string, unknown> | string | unknown[], options?: Record<string, unknown>): DbCrudResult;
export declare function dbCount(table: string, where?: Record<string, unknown> | string | unknown[] | null, options?: Record<string, unknown>): number;
export declare function dbIncrement(table: string, columns: Record<string, number> | string, where?: Record<string, unknown> | string | unknown[] | null, options?: Record<string, unknown>): DbCrudResult;
export declare function dbSum(table: string, column: string, where?: Record<string, unknown> | string | unknown[] | null, options?: Record<string, unknown>): number;
export declare function dbGroupBy(table: string, options: Record<string, unknown> | string): DbGroupByResult;
export declare function dbBegin(): {
ok: boolean;
};