mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
feat: improved abilities of plugins, moved schema fns to ctx
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { afterAll, afterEach, describe, expect, test } from "bun:test";
|
||||
import { App } from "../src";
|
||||
import { getDummyConnection } from "./helper";
|
||||
import { Hono } from "hono";
|
||||
import * as proto from "../src/data/prototype";
|
||||
import { pick } from "lodash-es";
|
||||
|
||||
const { dummyConnection, afterAllCleanup } = getDummyConnection();
|
||||
afterEach(afterAllCleanup);
|
||||
@@ -10,18 +13,91 @@ describe("App tests", async () => {
|
||||
const app = new App(dummyConnection);
|
||||
await app.build();
|
||||
|
||||
//expect(await app.data?.em.ping()).toBeTrue();
|
||||
expect(await app.em.ping()).toBeTrue();
|
||||
});
|
||||
|
||||
/*test.only("what", async () => {
|
||||
const app = new App(dummyConnection, {
|
||||
auth: {
|
||||
enabled: true,
|
||||
test("plugins", async () => {
|
||||
const called: string[] = [];
|
||||
const app = App.create({
|
||||
initialConfig: {
|
||||
auth: {
|
||||
enabled: true,
|
||||
},
|
||||
},
|
||||
options: {
|
||||
plugins: [
|
||||
(app) => {
|
||||
expect(app).toBeDefined();
|
||||
expect(app).toBeInstanceOf(App);
|
||||
return {
|
||||
name: "test",
|
||||
schema: () => {
|
||||
called.push("schema");
|
||||
return proto.em(
|
||||
{
|
||||
posts: proto.entity("posts", {
|
||||
title: proto.text(),
|
||||
}),
|
||||
comments: proto.entity("comments", {
|
||||
content: proto.text(),
|
||||
}),
|
||||
users: proto.entity("users", {
|
||||
email_verified: proto.boolean(),
|
||||
}),
|
||||
},
|
||||
(fn, s) => {
|
||||
fn.relation(s.comments).manyToOne(s.posts);
|
||||
fn.index(s.posts).on(["title"]);
|
||||
},
|
||||
);
|
||||
},
|
||||
beforeBuild: async () => {
|
||||
called.push("beforeBuild");
|
||||
},
|
||||
onBuilt: async () => {
|
||||
called.push("onBuilt");
|
||||
},
|
||||
onServerInit: async (server) => {
|
||||
called.push("onServerInit");
|
||||
expect(server).toBeDefined();
|
||||
expect(server).toBeInstanceOf(Hono);
|
||||
},
|
||||
onFirstBoot: async () => {
|
||||
called.push("onFirstBoot");
|
||||
},
|
||||
};
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
await app.module.auth.build();
|
||||
await app.module.data.build();
|
||||
console.log(app.em.entities.map((e) => e.name));
|
||||
console.log(await app.em.schema().getDiff());
|
||||
});*/
|
||||
|
||||
await app.build();
|
||||
|
||||
expect(app.em.entities.map((e) => e.name)).toEqual(["users", "posts", "comments"]);
|
||||
expect(app.em.indices.map((i) => i.name)).toEqual([
|
||||
"idx_unique_users_email",
|
||||
"idx_users_strategy",
|
||||
"idx_users_strategy_value",
|
||||
"idx_posts_title",
|
||||
]);
|
||||
expect(
|
||||
app.em.relations.all.map((r) => pick(r.toJSON(), ["type", "source", "target"])),
|
||||
).toEqual([
|
||||
{
|
||||
type: "n:1",
|
||||
source: "comments",
|
||||
target: "posts",
|
||||
},
|
||||
]);
|
||||
expect(called).toEqual([
|
||||
"onServerInit",
|
||||
"beforeBuild",
|
||||
"onServerInit",
|
||||
"schema",
|
||||
"onFirstBoot",
|
||||
"onBuilt",
|
||||
]);
|
||||
expect(app.plugins).toHaveLength(1);
|
||||
expect(app.plugins.map((p) => p.name)).toEqual(["test"]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,6 +20,7 @@ describe("App", () => {
|
||||
"guard",
|
||||
"flags",
|
||||
"logger",
|
||||
"helper",
|
||||
]);
|
||||
},
|
||||
},
|
||||
|
||||
@@ -4,6 +4,7 @@ import { type TSchema, Type } from "@sinclair/typebox";
|
||||
import { EntityManager, em, entity, index, text } from "../../src/data";
|
||||
import { DummyConnection } from "../../src/data/connection/DummyConnection";
|
||||
import { Module } from "../../src/modules/Module";
|
||||
import { ModuleHelper } from "modules/ModuleHelper";
|
||||
|
||||
function createModule<Schema extends TSchema>(schema: Schema) {
|
||||
class TestModule extends Module<typeof schema> {
|
||||
@@ -46,9 +47,9 @@ describe("Module", async () => {
|
||||
}
|
||||
|
||||
prt = {
|
||||
ensureEntity: this.ensureEntity.bind(this),
|
||||
ensureIndex: this.ensureIndex.bind(this),
|
||||
ensureSchema: this.ensureSchema.bind(this),
|
||||
ensureEntity: this.ctx.helper.ensureEntity.bind(this.ctx.helper),
|
||||
ensureIndex: this.ctx.helper.ensureIndex.bind(this.ctx.helper),
|
||||
ensureSchema: this.ctx.helper.ensureSchema.bind(this.ctx.helper),
|
||||
};
|
||||
|
||||
get em() {
|
||||
@@ -63,7 +64,11 @@ describe("Module", async () => {
|
||||
_em.relations,
|
||||
_em.indices,
|
||||
);
|
||||
return new M({} as any, { em, flags: Module.ctx_flags } as any);
|
||||
const ctx = {
|
||||
em,
|
||||
flags: Module.ctx_flags,
|
||||
};
|
||||
return new M({} as any, { ...ctx, helper: new ModuleHelper(ctx as any) } as any);
|
||||
}
|
||||
function flat(_em: EntityManager) {
|
||||
return {
|
||||
@@ -143,14 +148,9 @@ describe("Module", async () => {
|
||||
|
||||
// this should only add the field "important"
|
||||
m.prt.ensureEntity(
|
||||
entity(
|
||||
"u",
|
||||
{
|
||||
important: text(),
|
||||
},
|
||||
undefined,
|
||||
"system",
|
||||
),
|
||||
entity("u", {
|
||||
important: text(),
|
||||
}),
|
||||
);
|
||||
|
||||
expect(m.ctx.flags.sync_required).toBe(true);
|
||||
@@ -159,8 +159,7 @@ describe("Module", async () => {
|
||||
{
|
||||
name: "u",
|
||||
fields: ["id", "name", "important"],
|
||||
// ensured type must be present
|
||||
type: "system",
|
||||
type: "regular",
|
||||
},
|
||||
{
|
||||
name: "p",
|
||||
|
||||
@@ -8,10 +8,11 @@ import { Default, stripMark } from "../../src/core/utils";
|
||||
import { EntityManager } from "../../src/data";
|
||||
import { Module, type ModuleBuildContext } from "../../src/modules/Module";
|
||||
import { getDummyConnection } from "../helper";
|
||||
import { ModuleHelper } from "modules/ModuleHelper";
|
||||
|
||||
export function makeCtx(overrides?: Partial<ModuleBuildContext>): ModuleBuildContext {
|
||||
const { dummyConnection } = getDummyConnection();
|
||||
return {
|
||||
const ctx = {
|
||||
connection: dummyConnection,
|
||||
server: new Hono(),
|
||||
em: new EntityManager([], dummyConnection),
|
||||
@@ -21,6 +22,10 @@ export function makeCtx(overrides?: Partial<ModuleBuildContext>): ModuleBuildCon
|
||||
logger: new DebugLogger(false),
|
||||
...overrides,
|
||||
};
|
||||
return {
|
||||
...ctx,
|
||||
helper: new ModuleHelper(ctx as any),
|
||||
} as any;
|
||||
}
|
||||
|
||||
export function moduleTestSuite(module: { new (): Module }) {
|
||||
|
||||
Reference in New Issue
Block a user