mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
initialized postgres support
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { afterAll, describe, expect, test } from "bun:test";
|
||||
import { afterAll, afterEach, describe, expect, test } from "bun:test";
|
||||
import { App } from "../src";
|
||||
import { getDummyConnection } from "./helper";
|
||||
|
||||
const { dummyConnection, afterAllCleanup } = getDummyConnection();
|
||||
afterAll(afterAllCleanup);
|
||||
afterEach(afterAllCleanup);
|
||||
|
||||
describe("App tests", async () => {
|
||||
test("boots and pongs", async () => {
|
||||
@@ -12,4 +12,16 @@ describe("App tests", async () => {
|
||||
|
||||
//expect(await app.data?.em.ping()).toBeTrue();
|
||||
});
|
||||
|
||||
/*test.only("what", async () => {
|
||||
const app = new App(dummyConnection, {
|
||||
auth: {
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
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());
|
||||
});*/
|
||||
});
|
||||
|
||||
53
app/__test__/data/pg.spec.ts
Normal file
53
app/__test__/data/pg.spec.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { createApp } from "App";
|
||||
import { PostgresConnection } from "data/connection/postgres/PostgresConnection";
|
||||
import * as proto from "data/prototype";
|
||||
import { PostgresIntrospector } from "data/connection/postgres/PostgresIntrospector";
|
||||
import { ParseJSONResultsPlugin } from "kysely";
|
||||
|
||||
const connection = new PostgresConnection({
|
||||
database: "test",
|
||||
host: "localhost",
|
||||
user: "root",
|
||||
password: "1234",
|
||||
port: 5433,
|
||||
});
|
||||
|
||||
describe("postgres", () => {
|
||||
test.skip("introspector", async () => {
|
||||
const introspector = new PostgresIntrospector(connection.kysely, {
|
||||
plugins: [new ParseJSONResultsPlugin()],
|
||||
});
|
||||
|
||||
console.log(await introspector.getSchema());
|
||||
});
|
||||
|
||||
test("builds", async () => {
|
||||
const schema = proto.em(
|
||||
{
|
||||
posts: proto.entity("posts", {
|
||||
title: proto.text().required(),
|
||||
}),
|
||||
comments: proto.entity("comments", {
|
||||
text: proto.text(),
|
||||
}),
|
||||
},
|
||||
(ctx, s) => {
|
||||
ctx.relation(s.comments).manyToOne(s.posts);
|
||||
ctx.index(s.posts).on(["title"], true);
|
||||
ctx.index(s.comments).on(["text"]);
|
||||
},
|
||||
);
|
||||
|
||||
const app = createApp({
|
||||
initialConfig: {
|
||||
data: schema.toJSON(),
|
||||
},
|
||||
connection,
|
||||
});
|
||||
|
||||
await app.build({ sync: true });
|
||||
|
||||
expect(app.version()).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -27,7 +27,7 @@ describe("Relations", async () => {
|
||||
|
||||
const sql1 = schema
|
||||
.createTable("posts")
|
||||
.addColumn(...r1.schema()!)
|
||||
.addColumn(...em.connection.getFieldSchema(r1.schema())!)
|
||||
.compile().sql;
|
||||
|
||||
expect(sql1).toBe(
|
||||
@@ -43,7 +43,7 @@ describe("Relations", async () => {
|
||||
|
||||
const sql2 = schema
|
||||
.createTable("posts")
|
||||
.addColumn(...r2.schema()!)
|
||||
.addColumn(...em.connection.getFieldSchema(r2.schema())!)
|
||||
.compile().sql;
|
||||
|
||||
expect(sql2).toBe(
|
||||
|
||||
@@ -15,7 +15,7 @@ describe("SchemaManager tests", async () => {
|
||||
const em = new EntityManager([entity], dummyConnection, [], [index]);
|
||||
const schema = new SchemaManager(em);
|
||||
|
||||
const introspection = schema.getIntrospectionFromEntity(em.entities[0]);
|
||||
const introspection = schema.getIntrospectionFromEntity(em.entities[0]!);
|
||||
expect(introspection).toEqual({
|
||||
name: "test",
|
||||
isView: false,
|
||||
@@ -109,7 +109,7 @@ describe("SchemaManager tests", async () => {
|
||||
await schema.sync({ force: true, drop: true });
|
||||
const diffAfter = await schema.getDiff();
|
||||
|
||||
console.log("diffAfter", diffAfter);
|
||||
//console.log("diffAfter", diffAfter);
|
||||
expect(diffAfter.length).toBe(0);
|
||||
|
||||
await kysely.schema.dropTable(table).execute();
|
||||
|
||||
108
app/__test__/data/specs/connection/SqliteIntrospector.spec.ts
Normal file
108
app/__test__/data/specs/connection/SqliteIntrospector.spec.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { beforeEach, describe, test, expect } from "bun:test";
|
||||
import { SqliteIntrospector } from "data/connection/SqliteIntrospector";
|
||||
import { getDummyConnection, getDummyDatabase } from "../../helper";
|
||||
import { Kysely, ParseJSONResultsPlugin, SqliteDialect } from "kysely";
|
||||
import { _jsonp } from "core/utils";
|
||||
|
||||
function create() {
|
||||
const database = getDummyDatabase().dummyDb;
|
||||
return new Kysely({
|
||||
dialect: new SqliteDialect({ database }),
|
||||
});
|
||||
}
|
||||
|
||||
function createLibsql() {
|
||||
const database = getDummyDatabase().dummyDb;
|
||||
return new Kysely({
|
||||
dialect: new SqliteDialect({ database }),
|
||||
});
|
||||
}
|
||||
|
||||
describe("SqliteIntrospector", () => {
|
||||
test("asdf", async () => {
|
||||
const kysely = create();
|
||||
|
||||
await kysely.schema
|
||||
.createTable("test")
|
||||
.addColumn("id", "integer", (col) => col.primaryKey().autoIncrement().notNull())
|
||||
.addColumn("string", "text", (col) => col.notNull())
|
||||
.addColumn("number", "integer")
|
||||
.execute();
|
||||
|
||||
await kysely.schema
|
||||
.createIndex("idx_test_string")
|
||||
.on("test")
|
||||
.columns(["string"])
|
||||
.unique()
|
||||
.execute();
|
||||
|
||||
await kysely.schema
|
||||
.createTable("test2")
|
||||
.addColumn("id", "integer", (col) => col.primaryKey().autoIncrement().notNull())
|
||||
.addColumn("number", "integer")
|
||||
.execute();
|
||||
|
||||
await kysely.schema.createIndex("idx_test2_number").on("test2").columns(["number"]).execute();
|
||||
|
||||
const introspector = new SqliteIntrospector(kysely, {});
|
||||
|
||||
const result = await introspector.getTables();
|
||||
|
||||
//console.log(_jsonp(result));
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
name: "test",
|
||||
isView: false,
|
||||
columns: [
|
||||
{
|
||||
name: "id",
|
||||
dataType: "INTEGER",
|
||||
isNullable: false,
|
||||
isAutoIncrementing: true,
|
||||
hasDefaultValue: false,
|
||||
comment: undefined,
|
||||
},
|
||||
{
|
||||
name: "string",
|
||||
dataType: "TEXT",
|
||||
isNullable: false,
|
||||
isAutoIncrementing: false,
|
||||
hasDefaultValue: false,
|
||||
comment: undefined,
|
||||
},
|
||||
{
|
||||
comment: undefined,
|
||||
dataType: "INTEGER",
|
||||
hasDefaultValue: false,
|
||||
isAutoIncrementing: false,
|
||||
isNullable: true,
|
||||
name: "number",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "test2",
|
||||
isView: false,
|
||||
columns: [
|
||||
{
|
||||
name: "id",
|
||||
dataType: "INTEGER",
|
||||
isNullable: false,
|
||||
isAutoIncrementing: true,
|
||||
hasDefaultValue: false,
|
||||
comment: undefined,
|
||||
},
|
||||
{
|
||||
name: "number",
|
||||
dataType: "INTEGER",
|
||||
isNullable: true,
|
||||
isAutoIncrementing: false,
|
||||
hasDefaultValue: false,
|
||||
comment: undefined,
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -1,23 +1,29 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { Default, parse, stripMark } from "../../../../src/core/utils";
|
||||
import { Field, type SchemaResponse, TextField, baseFieldConfigSchema } from "../../../../src/data";
|
||||
import { runBaseFieldTests, transformPersist } from "./inc";
|
||||
import { Default, stripMark } from "../../../../src/core/utils";
|
||||
import { baseFieldConfigSchema, Field } from "../../../../src/data/fields/Field";
|
||||
import { runBaseFieldTests } from "./inc";
|
||||
|
||||
describe("[data] Field", async () => {
|
||||
class FieldSpec extends Field {
|
||||
schema(): SchemaResponse {
|
||||
return this.useSchemaHelper("text");
|
||||
}
|
||||
getSchema() {
|
||||
return baseFieldConfigSchema;
|
||||
}
|
||||
}
|
||||
|
||||
test("fieldSpec", () => {
|
||||
expect(new FieldSpec("test").schema()).toEqual({
|
||||
name: "test",
|
||||
type: "text",
|
||||
nullable: true, // always true
|
||||
dflt: undefined, // never using default value
|
||||
});
|
||||
});
|
||||
|
||||
runBaseFieldTests(FieldSpec, { defaultValue: "test", schemaType: "text" });
|
||||
|
||||
test("default config", async () => {
|
||||
const config = Default(baseFieldConfigSchema, {});
|
||||
expect(stripMark(new FieldSpec("test").config)).toEqual(config);
|
||||
expect(stripMark(new FieldSpec("test").config)).toEqual(config as any);
|
||||
});
|
||||
|
||||
test("transformPersist (specific)", async () => {
|
||||
|
||||
@@ -10,7 +10,12 @@ describe("[data] PrimaryField", async () => {
|
||||
|
||||
test("schema", () => {
|
||||
expect(field.name).toBe("primary");
|
||||
expect(field.schema()).toEqual(["primary", "integer", expect.any(Function)]);
|
||||
expect(field.schema()).toEqual({
|
||||
name: "primary",
|
||||
type: "integer" as const,
|
||||
nullable: false,
|
||||
primary: true,
|
||||
});
|
||||
});
|
||||
|
||||
test("hasDefault", async () => {
|
||||
|
||||
@@ -34,11 +34,14 @@ export function runBaseFieldTests(
|
||||
|
||||
test("schema", () => {
|
||||
expect(noConfigField.name).toBe("no_config");
|
||||
expect(noConfigField.schema(null as any)).toEqual([
|
||||
"no_config",
|
||||
config.schemaType,
|
||||
expect.any(Function),
|
||||
]);
|
||||
|
||||
const { type, name, nullable, dflt } = noConfigField.schema()!;
|
||||
expect({ type, name, nullable, dflt }).toEqual({
|
||||
type: config.schemaType as any,
|
||||
name: "no_config",
|
||||
nullable: true, // always true
|
||||
dflt: undefined, // never using default value
|
||||
});
|
||||
});
|
||||
|
||||
test("hasDefault", async () => {
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it } from "bun:test";
|
||||
import { App, createApp } from "../../src";
|
||||
import type { AuthResponse } from "../../src/auth";
|
||||
import { auth } from "../../src/auth/middlewares";
|
||||
import { randomString, secureRandomString, withDisabledConsole } from "../../src/core/utils";
|
||||
import { disableConsoleLog, enableConsoleLog } from "../helper";
|
||||
import { disableConsoleLog, enableConsoleLog, getDummyConnection } from "../helper";
|
||||
|
||||
const { dummyConnection, afterAllCleanup } = getDummyConnection();
|
||||
afterEach(afterAllCleanup);
|
||||
|
||||
beforeAll(disableConsoleLog);
|
||||
afterAll(enableConsoleLog);
|
||||
@@ -64,6 +67,7 @@ const configs = {
|
||||
|
||||
function createAuthApp() {
|
||||
const app = createApp({
|
||||
connection: dummyConnection,
|
||||
initialConfig: {
|
||||
auth: configs.auth,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user