mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
added bulk updates to data controller
This commit is contained in:
@@ -2,8 +2,7 @@ import { type AppAuth, AuthPermissions, type SafeUser, type Strategy } from "aut
|
|||||||
import { TypeInvalidError, parse } from "core/utils";
|
import { TypeInvalidError, parse } from "core/utils";
|
||||||
import { DataPermissions } from "data";
|
import { DataPermissions } from "data";
|
||||||
import type { Hono } from "hono";
|
import type { Hono } from "hono";
|
||||||
import { Controller } from "modules/Controller";
|
import { Controller, type ServerEnv } from "modules/Controller";
|
||||||
import type { ServerEnv } from "modules/Module";
|
|
||||||
|
|
||||||
export type AuthActionResponse = {
|
export type AuthActionResponse = {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import type { Context, Hono } from "hono";
|
|||||||
import { deleteCookie, getSignedCookie, setSignedCookie } from "hono/cookie";
|
import { deleteCookie, getSignedCookie, setSignedCookie } from "hono/cookie";
|
||||||
import { sign, verify } from "hono/jwt";
|
import { sign, verify } from "hono/jwt";
|
||||||
import type { CookieOptions } from "hono/utils/cookie";
|
import type { CookieOptions } from "hono/utils/cookie";
|
||||||
import type { ServerEnv } from "modules/Module";
|
import type { ServerEnv } from "modules/Controller";
|
||||||
|
|
||||||
type Input = any; // workaround
|
type Input = any; // workaround
|
||||||
export type JWTPayload = Parameters<typeof sign>[0];
|
export type JWTPayload = Parameters<typeof sign>[0];
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Exception, Permission } from "core";
|
import { Exception, Permission } from "core";
|
||||||
import { objectTransform } from "core/utils";
|
import { objectTransform } from "core/utils";
|
||||||
import type { Context } from "hono";
|
import type { Context } from "hono";
|
||||||
import type { ServerEnv } from "modules/Module";
|
import type { ServerEnv } from "modules/Controller";
|
||||||
import { Role } from "./Role";
|
import { Role } from "./Role";
|
||||||
|
|
||||||
export type GuardUserContext = {
|
export type GuardUserContext = {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import type { Permission } from "core";
|
|||||||
import { patternMatch } from "core/utils";
|
import { patternMatch } from "core/utils";
|
||||||
import type { Context } from "hono";
|
import type { Context } from "hono";
|
||||||
import { createMiddleware } from "hono/factory";
|
import { createMiddleware } from "hono/factory";
|
||||||
import type { ServerEnv } from "modules/Module";
|
import type { ServerEnv } from "modules/Controller";
|
||||||
|
|
||||||
function getPath(reqOrCtx: Request | Context) {
|
function getPath(reqOrCtx: Request | Context) {
|
||||||
const req = reqOrCtx instanceof Request ? reqOrCtx : reqOrCtx.req.raw;
|
const req = reqOrCtx instanceof Request ? reqOrCtx : reqOrCtx.req.raw;
|
||||||
|
|||||||
@@ -111,9 +111,8 @@ export class DataController extends Controller {
|
|||||||
/**
|
/**
|
||||||
* Schema endpoints
|
* Schema endpoints
|
||||||
*/
|
*/
|
||||||
hono
|
|
||||||
// read entity schema
|
// read entity schema
|
||||||
.get("/schema.json", permission(DataPermissions.entityRead), async (c) => {
|
hono.get("/schema.json", permission(DataPermissions.entityRead), async (c) => {
|
||||||
const $id = `${this.config.basepath}/schema.json`;
|
const $id = `${this.config.basepath}/schema.json`;
|
||||||
const schemas = Object.fromEntries(
|
const schemas = Object.fromEntries(
|
||||||
this.em.entities.map((e) => [
|
this.em.entities.map((e) => [
|
||||||
@@ -128,9 +127,10 @@ export class DataController extends Controller {
|
|||||||
$id,
|
$id,
|
||||||
properties: schemas
|
properties: schemas
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
// read schema
|
// read schema
|
||||||
.get(
|
hono.get(
|
||||||
"/schemas/:entity/:context?",
|
"/schemas/:entity/:context?",
|
||||||
permission(DataPermissions.entityRead),
|
permission(DataPermissions.entityRead),
|
||||||
tb(
|
tb(
|
||||||
@@ -145,7 +145,7 @@ export class DataController extends Controller {
|
|||||||
const { entity, context } = c.req.param();
|
const { entity, context } = c.req.param();
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
console.warn("not found:", entity, definedEntities);
|
console.warn("not found:", entity, definedEntities);
|
||||||
return c.notFound();
|
return this.notFound(c);
|
||||||
}
|
}
|
||||||
const _entity = this.em.entity(entity);
|
const _entity = this.em.entity(entity);
|
||||||
const schema = _entity.toSchema({ context } as any);
|
const schema = _entity.toSchema({ context } as any);
|
||||||
@@ -171,7 +171,7 @@ export class DataController extends Controller {
|
|||||||
hono.get("/info/:entity", async (c) => {
|
hono.get("/info/:entity", async (c) => {
|
||||||
const { entity } = c.req.param();
|
const { entity } = c.req.param();
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
return c.notFound();
|
return this.notFound(c);
|
||||||
}
|
}
|
||||||
const _entity = this.em.entity(entity);
|
const _entity = this.em.entity(entity);
|
||||||
const fields = _entity.fields.map((f) => f.name);
|
const fields = _entity.fields.map((f) => f.name);
|
||||||
@@ -208,32 +208,32 @@ export class DataController extends Controller {
|
|||||||
/**
|
/**
|
||||||
* Function endpoints
|
* Function endpoints
|
||||||
*/
|
*/
|
||||||
hono
|
|
||||||
// fn: count
|
// fn: count
|
||||||
.post(
|
hono.post(
|
||||||
"/:entity/fn/count",
|
"/:entity/fn/count",
|
||||||
permission(DataPermissions.entityRead),
|
permission(DataPermissions.entityRead),
|
||||||
tb("param", Type.Object({ entity: Type.String() })),
|
tb("param", Type.Object({ entity: Type.String() })),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const { entity } = c.req.valid("param");
|
const { entity } = c.req.valid("param");
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
return c.notFound();
|
return this.notFound(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
const where = (await c.req.json()) as any;
|
const where = (await c.req.json()) as any;
|
||||||
const result = await this.em.repository(entity).count(where);
|
const result = await this.em.repository(entity).count(where);
|
||||||
return c.json({ entity, count: result.count });
|
return c.json({ entity, count: result.count });
|
||||||
}
|
}
|
||||||
)
|
);
|
||||||
|
|
||||||
// fn: exists
|
// fn: exists
|
||||||
.post(
|
hono.post(
|
||||||
"/:entity/fn/exists",
|
"/:entity/fn/exists",
|
||||||
permission(DataPermissions.entityRead),
|
permission(DataPermissions.entityRead),
|
||||||
tb("param", Type.Object({ entity: Type.String() })),
|
tb("param", Type.Object({ entity: Type.String() })),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const { entity } = c.req.valid("param");
|
const { entity } = c.req.valid("param");
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
return c.notFound();
|
return this.notFound(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
const where = c.req.json() as any;
|
const where = c.req.json() as any;
|
||||||
@@ -245,9 +245,8 @@ export class DataController extends Controller {
|
|||||||
/**
|
/**
|
||||||
* Read endpoints
|
* Read endpoints
|
||||||
*/
|
*/
|
||||||
hono
|
|
||||||
// read many
|
// read many
|
||||||
.get(
|
hono.get(
|
||||||
"/:entity",
|
"/:entity",
|
||||||
permission(DataPermissions.entityRead),
|
permission(DataPermissions.entityRead),
|
||||||
tb("param", Type.Object({ entity: Type.String() })),
|
tb("param", Type.Object({ entity: Type.String() })),
|
||||||
@@ -257,7 +256,7 @@ export class DataController extends Controller {
|
|||||||
const { entity } = c.req.param();
|
const { entity } = c.req.param();
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
console.warn("not found:", entity, definedEntities);
|
console.warn("not found:", entity, definedEntities);
|
||||||
return c.notFound();
|
return this.notFound(c);
|
||||||
}
|
}
|
||||||
const options = c.req.valid("query") as RepoQuery;
|
const options = c.req.valid("query") as RepoQuery;
|
||||||
//console.log("before", this.ctx.emgr.Events);
|
//console.log("before", this.ctx.emgr.Events);
|
||||||
@@ -265,10 +264,10 @@ export class DataController extends Controller {
|
|||||||
|
|
||||||
return c.json(this.repoResult(result), { status: result.data ? 200 : 404 });
|
return c.json(this.repoResult(result), { status: result.data ? 200 : 404 });
|
||||||
}
|
}
|
||||||
)
|
);
|
||||||
|
|
||||||
// read one
|
// read one
|
||||||
.get(
|
hono.get(
|
||||||
"/:entity/:id",
|
"/:entity/:id",
|
||||||
permission(DataPermissions.entityRead),
|
permission(DataPermissions.entityRead),
|
||||||
tb(
|
tb(
|
||||||
@@ -282,16 +281,17 @@ export class DataController extends Controller {
|
|||||||
async (c) => {
|
async (c) => {
|
||||||
const { entity, id } = c.req.param();
|
const { entity, id } = c.req.param();
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
return c.notFound();
|
return this.notFound(c);
|
||||||
}
|
}
|
||||||
const options = c.req.valid("query") as RepoQuery;
|
const options = c.req.valid("query") as RepoQuery;
|
||||||
const result = await this.em.repository(entity).findId(Number(id), options);
|
const result = await this.em.repository(entity).findId(Number(id), options);
|
||||||
|
|
||||||
return c.json(this.repoResult(result), { status: result.data ? 200 : 404 });
|
return c.json(this.repoResult(result), { status: result.data ? 200 : 404 });
|
||||||
}
|
}
|
||||||
)
|
);
|
||||||
|
|
||||||
// read many by reference
|
// read many by reference
|
||||||
.get(
|
hono.get(
|
||||||
"/:entity/:id/:reference",
|
"/:entity/:id/:reference",
|
||||||
permission(DataPermissions.entityRead),
|
permission(DataPermissions.entityRead),
|
||||||
tb(
|
tb(
|
||||||
@@ -306,7 +306,7 @@ export class DataController extends Controller {
|
|||||||
async (c) => {
|
async (c) => {
|
||||||
const { entity, id, reference } = c.req.param();
|
const { entity, id, reference } = c.req.param();
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
return c.notFound();
|
return this.notFound(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
const options = c.req.valid("query") as RepoQuery;
|
const options = c.req.valid("query") as RepoQuery;
|
||||||
@@ -316,9 +316,10 @@ export class DataController extends Controller {
|
|||||||
|
|
||||||
return c.json(this.repoResult(result), { status: result.data ? 200 : 404 });
|
return c.json(this.repoResult(result), { status: result.data ? 200 : 404 });
|
||||||
}
|
}
|
||||||
)
|
);
|
||||||
|
|
||||||
// func query
|
// func query
|
||||||
.post(
|
hono.post(
|
||||||
"/:entity/query",
|
"/:entity/query",
|
||||||
permission(DataPermissions.entityRead),
|
permission(DataPermissions.entityRead),
|
||||||
tb("param", Type.Object({ entity: Type.String() })),
|
tb("param", Type.Object({ entity: Type.String() })),
|
||||||
@@ -326,7 +327,7 @@ export class DataController extends Controller {
|
|||||||
async (c) => {
|
async (c) => {
|
||||||
const { entity } = c.req.param();
|
const { entity } = c.req.param();
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
return c.notFound();
|
return this.notFound(c);
|
||||||
}
|
}
|
||||||
const options = (await c.req.valid("json")) as RepoQuery;
|
const options = (await c.req.valid("json")) as RepoQuery;
|
||||||
//console.log("options", options);
|
//console.log("options", options);
|
||||||
@@ -340,56 +341,84 @@ export class DataController extends Controller {
|
|||||||
* Mutation endpoints
|
* Mutation endpoints
|
||||||
*/
|
*/
|
||||||
// insert one
|
// insert one
|
||||||
hono
|
hono.post(
|
||||||
.post(
|
|
||||||
"/:entity",
|
"/:entity",
|
||||||
permission(DataPermissions.entityCreate),
|
permission(DataPermissions.entityCreate),
|
||||||
tb("param", Type.Object({ entity: Type.String() })),
|
tb("param", Type.Object({ entity: Type.String() })),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const { entity } = c.req.param();
|
const { entity } = c.req.param();
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
return c.notFound();
|
return this.notFound(c);
|
||||||
}
|
}
|
||||||
const body = (await c.req.json()) as EntityData;
|
const body = (await c.req.json()) as EntityData;
|
||||||
const result = await this.em.mutator(entity).insertOne(body);
|
const result = await this.em.mutator(entity).insertOne(body);
|
||||||
|
|
||||||
return c.json(this.mutatorResult(result), 201);
|
return c.json(this.mutatorResult(result), 201);
|
||||||
}
|
}
|
||||||
)
|
);
|
||||||
|
|
||||||
|
// update many
|
||||||
|
hono.patch(
|
||||||
|
"/:entity",
|
||||||
|
permission(DataPermissions.entityUpdate),
|
||||||
|
tb("param", Type.Object({ entity: Type.String() })),
|
||||||
|
tb(
|
||||||
|
"json",
|
||||||
|
Type.Object({
|
||||||
|
update: Type.Object({}),
|
||||||
|
where: querySchema.properties.where
|
||||||
|
})
|
||||||
|
),
|
||||||
|
async (c) => {
|
||||||
|
const { entity } = c.req.param();
|
||||||
|
if (!this.entityExists(entity)) {
|
||||||
|
return this.notFound(c);
|
||||||
|
}
|
||||||
|
const { update, where } = (await c.req.json()) as {
|
||||||
|
update: EntityData;
|
||||||
|
where: RepoQuery["where"];
|
||||||
|
};
|
||||||
|
const result = await this.em.mutator(entity).updateWhere(update, where);
|
||||||
|
|
||||||
|
return c.json(this.mutatorResult(result));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
// update one
|
// update one
|
||||||
.patch(
|
hono.patch(
|
||||||
"/:entity/:id",
|
"/:entity/:id",
|
||||||
permission(DataPermissions.entityUpdate),
|
permission(DataPermissions.entityUpdate),
|
||||||
tb("param", Type.Object({ entity: Type.String(), id: tbNumber })),
|
tb("param", Type.Object({ entity: Type.String(), id: tbNumber })),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const { entity, id } = c.req.param();
|
const { entity, id } = c.req.param();
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
return c.notFound();
|
return this.notFound(c);
|
||||||
}
|
}
|
||||||
const body = (await c.req.json()) as EntityData;
|
const body = (await c.req.json()) as EntityData;
|
||||||
const result = await this.em.mutator(entity).updateOne(Number(id), body);
|
const result = await this.em.mutator(entity).updateOne(Number(id), body);
|
||||||
|
|
||||||
return c.json(this.mutatorResult(result));
|
return c.json(this.mutatorResult(result));
|
||||||
}
|
}
|
||||||
)
|
);
|
||||||
|
|
||||||
// delete one
|
// delete one
|
||||||
.delete(
|
hono.delete(
|
||||||
"/:entity/:id",
|
"/:entity/:id",
|
||||||
permission(DataPermissions.entityDelete),
|
permission(DataPermissions.entityDelete),
|
||||||
tb("param", Type.Object({ entity: Type.String(), id: tbNumber })),
|
tb("param", Type.Object({ entity: Type.String(), id: tbNumber })),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const { entity, id } = c.req.param();
|
const { entity, id } = c.req.param();
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
return c.notFound();
|
return this.notFound(c);
|
||||||
}
|
}
|
||||||
const result = await this.em.mutator(entity).deleteOne(Number(id));
|
const result = await this.em.mutator(entity).deleteOne(Number(id));
|
||||||
|
|
||||||
return c.json(this.mutatorResult(result));
|
return c.json(this.mutatorResult(result));
|
||||||
}
|
}
|
||||||
)
|
);
|
||||||
|
|
||||||
// delete many
|
// delete many
|
||||||
.delete(
|
hono.delete(
|
||||||
"/:entity",
|
"/:entity",
|
||||||
permission(DataPermissions.entityDelete),
|
permission(DataPermissions.entityDelete),
|
||||||
tb("param", Type.Object({ entity: Type.String() })),
|
tb("param", Type.Object({ entity: Type.String() })),
|
||||||
@@ -397,7 +426,7 @@ export class DataController extends Controller {
|
|||||||
async (c) => {
|
async (c) => {
|
||||||
const { entity } = c.req.param();
|
const { entity } = c.req.param();
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
return c.notFound();
|
return this.notFound(c);
|
||||||
}
|
}
|
||||||
const where = c.req.valid("json") as RepoQuery["where"];
|
const where = c.req.valid("json") as RepoQuery["where"];
|
||||||
const result = await this.em.mutator(entity).deleteWhere(where);
|
const result = await this.em.mutator(entity).deleteWhere(where);
|
||||||
|
|||||||
@@ -1,7 +1,21 @@
|
|||||||
import { Hono } from "hono";
|
import type { App } from "App";
|
||||||
import type { ServerEnv } from "modules/Module";
|
import { type Context, Hono } from "hono";
|
||||||
import * as middlewares from "modules/middlewares";
|
import * as middlewares from "modules/middlewares";
|
||||||
|
|
||||||
|
export type ServerEnv = {
|
||||||
|
Variables: {
|
||||||
|
app: App;
|
||||||
|
// to prevent resolving auth multiple times
|
||||||
|
auth?: {
|
||||||
|
resolved: boolean;
|
||||||
|
registered: boolean;
|
||||||
|
skip: boolean;
|
||||||
|
user?: { id: any; role?: string; [key: string]: any };
|
||||||
|
};
|
||||||
|
html?: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export class Controller {
|
export class Controller {
|
||||||
protected middlewares = middlewares;
|
protected middlewares = middlewares;
|
||||||
|
|
||||||
@@ -16,4 +30,19 @@ export class Controller {
|
|||||||
getController(): Hono<ServerEnv> {
|
getController(): Hono<ServerEnv> {
|
||||||
return this.create();
|
return this.create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected isJsonRequest(c: Context<ServerEnv>) {
|
||||||
|
return (
|
||||||
|
c.req.header("Content-Type") === "application/json" ||
|
||||||
|
c.req.header("Accept") === "application/json"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected notFound(c: Context<ServerEnv>) {
|
||||||
|
if (this.isJsonRequest(c)) {
|
||||||
|
return c.json({ error: "Not found" }, 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.notFound();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import type { App } from "App";
|
|
||||||
import type { Guard } from "auth";
|
import type { Guard } from "auth";
|
||||||
import { type DebugLogger, SchemaObject } from "core";
|
import { type DebugLogger, SchemaObject } from "core";
|
||||||
import type { EventManager } from "core/events";
|
import type { EventManager } from "core/events";
|
||||||
@@ -15,20 +14,7 @@ import {
|
|||||||
import { Entity } from "data";
|
import { Entity } from "data";
|
||||||
import type { Hono } from "hono";
|
import type { Hono } from "hono";
|
||||||
import { isEqual } from "lodash-es";
|
import { isEqual } from "lodash-es";
|
||||||
|
import type { ServerEnv } from "modules/Controller";
|
||||||
export type ServerEnv = {
|
|
||||||
Variables: {
|
|
||||||
app: App;
|
|
||||||
// to prevent resolving auth multiple times
|
|
||||||
auth?: {
|
|
||||||
resolved: boolean;
|
|
||||||
registered: boolean;
|
|
||||||
skip: boolean;
|
|
||||||
user?: { id: any; role?: string; [key: string]: any };
|
|
||||||
};
|
|
||||||
html?: string;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export type ModuleBuildContext = {
|
export type ModuleBuildContext = {
|
||||||
connection: Connection;
|
connection: Connection;
|
||||||
|
|||||||
@@ -32,7 +32,8 @@ import { AppAuth } from "../auth/AppAuth";
|
|||||||
import { AppData } from "../data/AppData";
|
import { AppData } from "../data/AppData";
|
||||||
import { AppFlows } from "../flows/AppFlows";
|
import { AppFlows } from "../flows/AppFlows";
|
||||||
import { AppMedia } from "../media/AppMedia";
|
import { AppMedia } from "../media/AppMedia";
|
||||||
import { Module, type ModuleBuildContext, type ServerEnv } from "./Module";
|
import type { ServerEnv } from "./Controller";
|
||||||
|
import { Module, type ModuleBuildContext } from "./Module";
|
||||||
|
|
||||||
export type { ModuleBuildContext };
|
export type { ModuleBuildContext };
|
||||||
|
|
||||||
|
|||||||
1
app/src/modules/middlewares/index.ts
Normal file
1
app/src/modules/middlewares/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export { auth, permission } from "auth/middlewares";
|
||||||
Reference in New Issue
Block a user