mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
Merge pull request #99 from bknd-io/feat/data-api-bulk-update
feat/data-api-bulk-update
This commit is contained in:
@@ -3,8 +3,7 @@ import { tbValidator as tb } from "core";
|
||||
import { Type, TypeInvalidError, parse, transformObject } from "core/utils";
|
||||
import { DataPermissions } from "data";
|
||||
import type { Hono } from "hono";
|
||||
import { Controller } from "modules/Controller";
|
||||
import type { ServerEnv } from "modules/Module";
|
||||
import { Controller, type ServerEnv } from "modules/Controller";
|
||||
|
||||
export type AuthActionResponse = {
|
||||
success: boolean;
|
||||
|
||||
@@ -13,7 +13,7 @@ import type { Context, Hono } from "hono";
|
||||
import { deleteCookie, getSignedCookie, setSignedCookie } from "hono/cookie";
|
||||
import { sign, verify } from "hono/jwt";
|
||||
import type { CookieOptions } from "hono/utils/cookie";
|
||||
import type { ServerEnv } from "modules/Module";
|
||||
import type { ServerEnv } from "modules/Controller";
|
||||
|
||||
type Input = any; // workaround
|
||||
export type JWTPayload = Parameters<typeof sign>[0];
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Exception, Permission } from "core";
|
||||
import { objectTransform } from "core/utils";
|
||||
import type { Context } from "hono";
|
||||
import type { ServerEnv } from "modules/Module";
|
||||
import type { ServerEnv } from "modules/Controller";
|
||||
import { Role } from "./Role";
|
||||
|
||||
export type GuardUserContext = {
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { Permission } from "core";
|
||||
import { patternMatch } from "core/utils";
|
||||
import type { Context } from "hono";
|
||||
import { createMiddleware } from "hono/factory";
|
||||
import type { ServerEnv } from "modules/Module";
|
||||
import type { ServerEnv } from "modules/Controller";
|
||||
|
||||
function getPath(reqOrCtx: Request | Context) {
|
||||
const req = reqOrCtx instanceof Request ? reqOrCtx : reqOrCtx.req.raw;
|
||||
|
||||
@@ -111,9 +111,8 @@ export class DataController extends Controller {
|
||||
/**
|
||||
* Schema endpoints
|
||||
*/
|
||||
hono
|
||||
// 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 schemas = Object.fromEntries(
|
||||
this.em.entities.map((e) => [
|
||||
@@ -128,9 +127,10 @@ export class DataController extends Controller {
|
||||
$id,
|
||||
properties: schemas
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
// read schema
|
||||
.get(
|
||||
hono.get(
|
||||
"/schemas/:entity/:context?",
|
||||
permission(DataPermissions.entityRead),
|
||||
tb(
|
||||
@@ -145,7 +145,7 @@ export class DataController extends Controller {
|
||||
const { entity, context } = c.req.param();
|
||||
if (!this.entityExists(entity)) {
|
||||
console.warn("not found:", entity, definedEntities);
|
||||
return c.notFound();
|
||||
return this.notFound(c);
|
||||
}
|
||||
const _entity = this.em.entity(entity);
|
||||
const schema = _entity.toSchema({ context } as any);
|
||||
@@ -171,7 +171,7 @@ export class DataController extends Controller {
|
||||
hono.get("/info/:entity", async (c) => {
|
||||
const { entity } = c.req.param();
|
||||
if (!this.entityExists(entity)) {
|
||||
return c.notFound();
|
||||
return this.notFound(c);
|
||||
}
|
||||
const _entity = this.em.entity(entity);
|
||||
const fields = _entity.fields.map((f) => f.name);
|
||||
@@ -208,32 +208,32 @@ export class DataController extends Controller {
|
||||
/**
|
||||
* Function endpoints
|
||||
*/
|
||||
hono
|
||||
// fn: count
|
||||
.post(
|
||||
hono.post(
|
||||
"/:entity/fn/count",
|
||||
permission(DataPermissions.entityRead),
|
||||
tb("param", Type.Object({ entity: Type.String() })),
|
||||
async (c) => {
|
||||
const { entity } = c.req.valid("param");
|
||||
if (!this.entityExists(entity)) {
|
||||
return c.notFound();
|
||||
return this.notFound(c);
|
||||
}
|
||||
|
||||
const where = (await c.req.json()) as any;
|
||||
const result = await this.em.repository(entity).count(where);
|
||||
return c.json({ entity, count: result.count });
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// fn: exists
|
||||
.post(
|
||||
hono.post(
|
||||
"/:entity/fn/exists",
|
||||
permission(DataPermissions.entityRead),
|
||||
tb("param", Type.Object({ entity: Type.String() })),
|
||||
async (c) => {
|
||||
const { entity } = c.req.valid("param");
|
||||
if (!this.entityExists(entity)) {
|
||||
return c.notFound();
|
||||
return this.notFound(c);
|
||||
}
|
||||
|
||||
const where = c.req.json() as any;
|
||||
@@ -245,9 +245,8 @@ export class DataController extends Controller {
|
||||
/**
|
||||
* Read endpoints
|
||||
*/
|
||||
hono
|
||||
// read many
|
||||
.get(
|
||||
hono.get(
|
||||
"/:entity",
|
||||
permission(DataPermissions.entityRead),
|
||||
tb("param", Type.Object({ entity: Type.String() })),
|
||||
@@ -257,7 +256,7 @@ export class DataController extends Controller {
|
||||
const { entity } = c.req.param();
|
||||
if (!this.entityExists(entity)) {
|
||||
console.warn("not found:", entity, definedEntities);
|
||||
return c.notFound();
|
||||
return this.notFound(c);
|
||||
}
|
||||
const options = c.req.valid("query") as RepoQuery;
|
||||
//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 });
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// read one
|
||||
.get(
|
||||
hono.get(
|
||||
"/:entity/:id",
|
||||
permission(DataPermissions.entityRead),
|
||||
tb(
|
||||
@@ -282,16 +281,17 @@ export class DataController extends Controller {
|
||||
async (c) => {
|
||||
const { entity, id } = c.req.param();
|
||||
if (!this.entityExists(entity)) {
|
||||
return c.notFound();
|
||||
return this.notFound(c);
|
||||
}
|
||||
const options = c.req.valid("query") as RepoQuery;
|
||||
const result = await this.em.repository(entity).findId(Number(id), options);
|
||||
|
||||
return c.json(this.repoResult(result), { status: result.data ? 200 : 404 });
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// read many by reference
|
||||
.get(
|
||||
hono.get(
|
||||
"/:entity/:id/:reference",
|
||||
permission(DataPermissions.entityRead),
|
||||
tb(
|
||||
@@ -306,7 +306,7 @@ export class DataController extends Controller {
|
||||
async (c) => {
|
||||
const { entity, id, reference } = c.req.param();
|
||||
if (!this.entityExists(entity)) {
|
||||
return c.notFound();
|
||||
return this.notFound(c);
|
||||
}
|
||||
|
||||
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 });
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// func query
|
||||
.post(
|
||||
hono.post(
|
||||
"/:entity/query",
|
||||
permission(DataPermissions.entityRead),
|
||||
tb("param", Type.Object({ entity: Type.String() })),
|
||||
@@ -326,7 +327,7 @@ export class DataController extends Controller {
|
||||
async (c) => {
|
||||
const { entity } = c.req.param();
|
||||
if (!this.entityExists(entity)) {
|
||||
return c.notFound();
|
||||
return this.notFound(c);
|
||||
}
|
||||
const options = (await c.req.valid("json")) as RepoQuery;
|
||||
//console.log("options", options);
|
||||
@@ -340,56 +341,84 @@ export class DataController extends Controller {
|
||||
* Mutation endpoints
|
||||
*/
|
||||
// insert one
|
||||
hono
|
||||
.post(
|
||||
hono.post(
|
||||
"/:entity",
|
||||
permission(DataPermissions.entityCreate),
|
||||
tb("param", Type.Object({ entity: Type.String() })),
|
||||
async (c) => {
|
||||
const { entity } = c.req.param();
|
||||
if (!this.entityExists(entity)) {
|
||||
return c.notFound();
|
||||
return this.notFound(c);
|
||||
}
|
||||
const body = (await c.req.json()) as EntityData;
|
||||
const result = await this.em.mutator(entity).insertOne(body);
|
||||
|
||||
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
|
||||
.patch(
|
||||
hono.patch(
|
||||
"/:entity/:id",
|
||||
permission(DataPermissions.entityUpdate),
|
||||
tb("param", Type.Object({ entity: Type.String(), id: tbNumber })),
|
||||
async (c) => {
|
||||
const { entity, id } = c.req.param();
|
||||
if (!this.entityExists(entity)) {
|
||||
return c.notFound();
|
||||
return this.notFound(c);
|
||||
}
|
||||
const body = (await c.req.json()) as EntityData;
|
||||
const result = await this.em.mutator(entity).updateOne(Number(id), body);
|
||||
|
||||
return c.json(this.mutatorResult(result));
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// delete one
|
||||
.delete(
|
||||
hono.delete(
|
||||
"/:entity/:id",
|
||||
permission(DataPermissions.entityDelete),
|
||||
tb("param", Type.Object({ entity: Type.String(), id: tbNumber })),
|
||||
async (c) => {
|
||||
const { entity, id } = c.req.param();
|
||||
if (!this.entityExists(entity)) {
|
||||
return c.notFound();
|
||||
return this.notFound(c);
|
||||
}
|
||||
const result = await this.em.mutator(entity).deleteOne(Number(id));
|
||||
|
||||
return c.json(this.mutatorResult(result));
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// delete many
|
||||
.delete(
|
||||
hono.delete(
|
||||
"/:entity",
|
||||
permission(DataPermissions.entityDelete),
|
||||
tb("param", Type.Object({ entity: Type.String() })),
|
||||
@@ -397,7 +426,7 @@ export class DataController extends Controller {
|
||||
async (c) => {
|
||||
const { entity } = c.req.param();
|
||||
if (!this.entityExists(entity)) {
|
||||
return c.notFound();
|
||||
return this.notFound(c);
|
||||
}
|
||||
const where = c.req.valid("json") as RepoQuery["where"];
|
||||
const result = await this.em.mutator(entity).deleteWhere(where);
|
||||
|
||||
@@ -1,7 +1,21 @@
|
||||
import { Hono } from "hono";
|
||||
import type { ServerEnv } from "modules/Module";
|
||||
import type { App } from "App";
|
||||
import { type Context, Hono } from "hono";
|
||||
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 {
|
||||
protected middlewares = middlewares;
|
||||
|
||||
@@ -16,4 +30,19 @@ export class Controller {
|
||||
getController(): Hono<ServerEnv> {
|
||||
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 DebugLogger, SchemaObject } from "core";
|
||||
import type { EventManager } from "core/events";
|
||||
@@ -15,20 +14,7 @@ import {
|
||||
import { Entity } from "data";
|
||||
import type { Hono } from "hono";
|
||||
import { isEqual } from "lodash-es";
|
||||
|
||||
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;
|
||||
};
|
||||
};
|
||||
import type { ServerEnv } from "modules/Controller";
|
||||
|
||||
export type ModuleBuildContext = {
|
||||
connection: Connection;
|
||||
|
||||
@@ -32,7 +32,8 @@ import { AppAuth } from "../auth/AppAuth";
|
||||
import { AppData } from "../data/AppData";
|
||||
import { AppFlows } from "../flows/AppFlows";
|
||||
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 };
|
||||
|
||||
|
||||
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";
|
||||
@@ -1,4 +1,4 @@
|
||||
---
|
||||
title: 'Create Entity'
|
||||
openapi: 'POST /api/data/{entity}'
|
||||
openapi: 'POST /api/data/entity/{entity}'
|
||||
---
|
||||
@@ -1,4 +1,4 @@
|
||||
---
|
||||
title: 'Delete Entity'
|
||||
openapi: 'DELETE /api/data/{entity}/{id}'
|
||||
openapi: 'DELETE /api/data/entity/{entity}/{id}'
|
||||
---
|
||||
@@ -1,4 +1,4 @@
|
||||
---
|
||||
title: 'Get Entity'
|
||||
openapi: 'GET /api/data/{entity}/{id}'
|
||||
openapi: 'GET /api/data/entity/{entity}/{id}'
|
||||
---
|
||||
@@ -1,4 +1,4 @@
|
||||
---
|
||||
title: 'List Entity'
|
||||
openapi: 'GET /api/data/{entity}'
|
||||
openapi: 'GET /api/data/entity/{entity}'
|
||||
---
|
||||
@@ -1,4 +1,4 @@
|
||||
---
|
||||
title: 'Update Entity'
|
||||
openapi: 'PATCH /api/data/{entity}/{id}'
|
||||
openapi: 'PATCH /api/data/entity/{entity}/{id}'
|
||||
---
|
||||
@@ -13,7 +13,7 @@ run on your toaster (probably).
|
||||
</Note>
|
||||
|
||||
## Preview
|
||||
**bknd** is so lightweight that it fully runs inside StackBlitz. Take a look at the preview below:
|
||||
Here is a preview of **bknd** in StackBlitz:
|
||||
<Stackblitz {...examples.adminRich} />
|
||||
|
||||
<Accordion title="What's going on?" icon="lightbulb">
|
||||
|
||||
Reference in New Issue
Block a user