mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
Merge remote-tracking branch 'origin/release/0.15' into feat/plugin-improvements
# Conflicts: # app/package.json # app/src/App.ts
This commit is contained in:
@@ -3,9 +3,7 @@ import {
|
||||
DataPermissions,
|
||||
type EntityData,
|
||||
type EntityManager,
|
||||
type MutatorResponse,
|
||||
type RepoQuery,
|
||||
type RepositoryResponse,
|
||||
repoQuery,
|
||||
} from "data";
|
||||
import type { Handler } from "hono/types";
|
||||
@@ -32,33 +30,6 @@ export class DataController extends Controller {
|
||||
return this.ctx.guard;
|
||||
}
|
||||
|
||||
repoResult<T extends RepositoryResponse<any> = RepositoryResponse>(
|
||||
res: T,
|
||||
): Pick<T, "meta" | "data"> {
|
||||
let meta: Partial<RepositoryResponse["meta"]> = {};
|
||||
|
||||
if ("meta" in res) {
|
||||
const { query, ...rest } = res.meta;
|
||||
meta = rest;
|
||||
if (isDebug()) meta.query = query;
|
||||
}
|
||||
|
||||
const template = { data: res.data, meta };
|
||||
|
||||
// @todo: this works but it breaks in FE (need to improve DataTable)
|
||||
// filter empty
|
||||
return Object.fromEntries(
|
||||
Object.entries(template).filter(([_, v]) => typeof v !== "undefined" && v !== null),
|
||||
) as any;
|
||||
}
|
||||
|
||||
mutatorResult(res: MutatorResponse | MutatorResponse<EntityData>) {
|
||||
const template = { data: res.data };
|
||||
|
||||
// filter empty
|
||||
return Object.fromEntries(Object.entries(template).filter(([_, v]) => v !== undefined));
|
||||
}
|
||||
|
||||
entityExists(entity: string) {
|
||||
try {
|
||||
return !!this.em.entity(entity);
|
||||
@@ -257,7 +228,7 @@ export class DataController extends Controller {
|
||||
|
||||
const where = c.req.valid("json") as any;
|
||||
const result = await this.em.repository(entity).count(where);
|
||||
return c.json({ entity, count: result.count });
|
||||
return c.json({ entity, ...result.data });
|
||||
},
|
||||
);
|
||||
|
||||
@@ -279,7 +250,7 @@ export class DataController extends Controller {
|
||||
|
||||
const where = c.req.valid("json") as any;
|
||||
const result = await this.em.repository(entity).exists(where);
|
||||
return c.json({ entity, exists: result.exists });
|
||||
return c.json({ entity, ...result.data });
|
||||
},
|
||||
);
|
||||
|
||||
@@ -318,7 +289,7 @@ export class DataController extends Controller {
|
||||
const options = c.req.valid("query") as RepoQuery;
|
||||
const result = await this.em.repository(entity).findMany(options);
|
||||
|
||||
return c.json(this.repoResult(result), { status: result.data ? 200 : 404 });
|
||||
return c.json(result, { status: result.data ? 200 : 404 });
|
||||
},
|
||||
);
|
||||
|
||||
@@ -347,7 +318,7 @@ export class DataController extends Controller {
|
||||
const options = c.req.valid("query") as RepoQuery;
|
||||
const result = await this.em.repository(entity).findId(id, options);
|
||||
|
||||
return c.json(this.repoResult(result), { status: result.data ? 200 : 404 });
|
||||
return c.json(result, { status: result.data ? 200 : 404 });
|
||||
},
|
||||
);
|
||||
|
||||
@@ -380,7 +351,7 @@ export class DataController extends Controller {
|
||||
.repository(entity)
|
||||
.findManyByReference(id, reference, options);
|
||||
|
||||
return c.json(this.repoResult(result), { status: result.data ? 200 : 404 });
|
||||
return c.json(result, { status: result.data ? 200 : 404 });
|
||||
},
|
||||
);
|
||||
|
||||
@@ -414,7 +385,7 @@ export class DataController extends Controller {
|
||||
const options = (await c.req.json()) as RepoQuery;
|
||||
const result = await this.em.repository(entity).findMany(options);
|
||||
|
||||
return c.json(this.repoResult(result), { status: result.data ? 200 : 404 });
|
||||
return c.json(result, { status: result.data ? 200 : 404 });
|
||||
},
|
||||
);
|
||||
|
||||
@@ -440,11 +411,11 @@ export class DataController extends Controller {
|
||||
|
||||
if (Array.isArray(body)) {
|
||||
const result = await this.em.mutator(entity).insertMany(body);
|
||||
return c.json(this.mutatorResult(result), 201);
|
||||
return c.json(result, 201);
|
||||
}
|
||||
|
||||
const result = await this.em.mutator(entity).insertOne(body);
|
||||
return c.json(this.mutatorResult(result), 201);
|
||||
return c.json(result, 201);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -475,7 +446,7 @@ export class DataController extends Controller {
|
||||
};
|
||||
const result = await this.em.mutator(entity).updateWhere(update, where);
|
||||
|
||||
return c.json(this.mutatorResult(result));
|
||||
return c.json(result);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -497,7 +468,7 @@ export class DataController extends Controller {
|
||||
const body = (await c.req.json()) as EntityData;
|
||||
const result = await this.em.mutator(entity).updateOne(id, body);
|
||||
|
||||
return c.json(this.mutatorResult(result));
|
||||
return c.json(result);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -517,7 +488,7 @@ export class DataController extends Controller {
|
||||
}
|
||||
const result = await this.em.mutator(entity).deleteOne(id);
|
||||
|
||||
return c.json(this.mutatorResult(result));
|
||||
return c.json(result);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -539,7 +510,7 @@ export class DataController extends Controller {
|
||||
const where = (await c.req.json()) as RepoQuery["where"];
|
||||
const result = await this.em.mutator(entity).deleteWhere(where);
|
||||
|
||||
return c.json(this.mutatorResult(result));
|
||||
return c.json(result);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user