Skip to content

Commit 1ed9471

Browse files
authored
Merge pull request #105 from bknd-io/feat/add-postgres-and-prepare-others
support for postgres and others
2 parents e9f2da8 + 178838e commit 1ed9471

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1113
-330
lines changed

app/__test__/App.spec.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { afterAll, describe, expect, test } from "bun:test";
1+
import { afterAll, afterEach, describe, expect, test } from "bun:test";
22
import { App } from "../src";
33
import { getDummyConnection } from "./helper";
44

55
const { dummyConnection, afterAllCleanup } = getDummyConnection();
6-
afterAll(afterAllCleanup);
6+
afterEach(afterAllCleanup);
77

88
describe("App tests", async () => {
99
test("boots and pongs", async () => {
@@ -12,4 +12,16 @@ describe("App tests", async () => {
1212

1313
//expect(await app.data?.em.ping()).toBeTrue();
1414
});
15+
16+
/*test.only("what", async () => {
17+
const app = new App(dummyConnection, {
18+
auth: {
19+
enabled: true,
20+
},
21+
});
22+
await app.module.auth.build();
23+
await app.module.data.build();
24+
console.log(app.em.entities.map((e) => e.name));
25+
console.log(await app.em.schema().getDiff());
26+
});*/
1527
});

app/__test__/data/relations.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe("Relations", async () => {
2727

2828
const sql1 = schema
2929
.createTable("posts")
30-
.addColumn(...r1.schema()!)
30+
.addColumn(...em.connection.getFieldSchema(r1.schema())!)
3131
.compile().sql;
3232

3333
expect(sql1).toBe(
@@ -43,7 +43,7 @@ describe("Relations", async () => {
4343

4444
const sql2 = schema
4545
.createTable("posts")
46-
.addColumn(...r2.schema()!)
46+
.addColumn(...em.connection.getFieldSchema(r2.schema())!)
4747
.compile().sql;
4848

4949
expect(sql2).toBe(

app/__test__/data/specs/SchemaManager.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe("SchemaManager tests", async () => {
1515
const em = new EntityManager([entity], dummyConnection, [], [index]);
1616
const schema = new SchemaManager(em);
1717

18-
const introspection = schema.getIntrospectionFromEntity(em.entities[0]);
18+
const introspection = schema.getIntrospectionFromEntity(em.entities[0]!);
1919
expect(introspection).toEqual({
2020
name: "test",
2121
isView: false,
@@ -109,7 +109,7 @@ describe("SchemaManager tests", async () => {
109109
await schema.sync({ force: true, drop: true });
110110
const diffAfter = await schema.getDiff();
111111

112-
console.log("diffAfter", diffAfter);
112+
//console.log("diffAfter", diffAfter);
113113
expect(diffAfter.length).toBe(0);
114114

115115
await kysely.schema.dropTable(table).execute();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { SqliteIntrospector } from "data/connection";
3+
import { getDummyDatabase } from "../../helper";
4+
import { Kysely, SqliteDialect } from "kysely";
5+
6+
function create() {
7+
const database = getDummyDatabase().dummyDb;
8+
return new Kysely({
9+
dialect: new SqliteDialect({ database }),
10+
});
11+
}
12+
13+
function createLibsql() {
14+
const database = getDummyDatabase().dummyDb;
15+
return new Kysely({
16+
dialect: new SqliteDialect({ database }),
17+
});
18+
}
19+
20+
describe("SqliteIntrospector", () => {
21+
test("asdf", async () => {
22+
const kysely = create();
23+
24+
await kysely.schema
25+
.createTable("test")
26+
.addColumn("id", "integer", (col) => col.primaryKey().autoIncrement().notNull())
27+
.addColumn("string", "text", (col) => col.notNull())
28+
.addColumn("number", "integer")
29+
.execute();
30+
31+
await kysely.schema
32+
.createIndex("idx_test_string")
33+
.on("test")
34+
.columns(["string"])
35+
.unique()
36+
.execute();
37+
38+
await kysely.schema
39+
.createTable("test2")
40+
.addColumn("id", "integer", (col) => col.primaryKey().autoIncrement().notNull())
41+
.addColumn("number", "integer")
42+
.execute();
43+
44+
await kysely.schema.createIndex("idx_test2_number").on("test2").columns(["number"]).execute();
45+
46+
const introspector = new SqliteIntrospector(kysely, {});
47+
48+
const result = await introspector.getTables();
49+
50+
//console.log(_jsonp(result));
51+
52+
expect(result).toEqual([
53+
{
54+
name: "test",
55+
isView: false,
56+
columns: [
57+
{
58+
name: "id",
59+
dataType: "INTEGER",
60+
isNullable: false,
61+
isAutoIncrementing: true,
62+
hasDefaultValue: false,
63+
comment: undefined,
64+
},
65+
{
66+
name: "string",
67+
dataType: "TEXT",
68+
isNullable: false,
69+
isAutoIncrementing: false,
70+
hasDefaultValue: false,
71+
comment: undefined,
72+
},
73+
{
74+
comment: undefined,
75+
dataType: "INTEGER",
76+
hasDefaultValue: false,
77+
isAutoIncrementing: false,
78+
isNullable: true,
79+
name: "number",
80+
},
81+
],
82+
},
83+
{
84+
name: "test2",
85+
isView: false,
86+
columns: [
87+
{
88+
name: "id",
89+
dataType: "INTEGER",
90+
isNullable: false,
91+
isAutoIncrementing: true,
92+
hasDefaultValue: false,
93+
comment: undefined,
94+
},
95+
{
96+
name: "number",
97+
dataType: "INTEGER",
98+
isNullable: true,
99+
isAutoIncrementing: false,
100+
hasDefaultValue: false,
101+
comment: undefined,
102+
},
103+
],
104+
},
105+
]);
106+
});
107+
});

app/__test__/data/specs/fields/Field.spec.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import { describe, expect, test } from "bun:test";
2-
import { Default, parse, stripMark } from "../../../../src/core/utils";
3-
import { Field, type SchemaResponse, TextField, baseFieldConfigSchema } from "../../../../src/data";
4-
import { runBaseFieldTests, transformPersist } from "./inc";
2+
import { Default, stripMark } from "../../../../src/core/utils";
3+
import { baseFieldConfigSchema, Field } from "../../../../src/data/fields/Field";
4+
import { runBaseFieldTests } from "./inc";
55

66
describe("[data] Field", async () => {
77
class FieldSpec extends Field {
8-
schema(): SchemaResponse {
9-
return this.useSchemaHelper("text");
10-
}
118
getSchema() {
129
return baseFieldConfigSchema;
1310
}
1411
}
1512

13+
test("fieldSpec", () => {
14+
expect(new FieldSpec("test").schema()).toEqual({
15+
name: "test",
16+
type: "text",
17+
nullable: true, // always true
18+
dflt: undefined, // never using default value
19+
});
20+
});
21+
1622
runBaseFieldTests(FieldSpec, { defaultValue: "test", schemaType: "text" });
1723

1824
test("default config", async () => {
1925
const config = Default(baseFieldConfigSchema, {});
20-
expect(stripMark(new FieldSpec("test").config)).toEqual(config);
26+
expect(stripMark(new FieldSpec("test").config)).toEqual(config as any);
2127
});
2228

2329
test("transformPersist (specific)", async () => {

app/__test__/data/specs/fields/PrimaryField.spec.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ describe("[data] PrimaryField", async () => {
1010

1111
test("schema", () => {
1212
expect(field.name).toBe("primary");
13-
expect(field.schema()).toEqual(["primary", "integer", expect.any(Function)]);
13+
expect(field.schema()).toEqual({
14+
name: "primary",
15+
type: "integer" as const,
16+
nullable: false,
17+
primary: true,
18+
});
1419
});
1520

1621
test("hasDefault", async () => {

app/__test__/data/specs/fields/inc.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ export function runBaseFieldTests(
3434

3535
test("schema", () => {
3636
expect(noConfigField.name).toBe("no_config");
37-
expect(noConfigField.schema(null as any)).toEqual([
38-
"no_config",
39-
config.schemaType,
40-
expect.any(Function),
41-
]);
37+
38+
const { type, name, nullable, dflt } = noConfigField.schema()!;
39+
expect({ type, name, nullable, dflt }).toEqual({
40+
type: config.schemaType as any,
41+
name: "no_config",
42+
nullable: true, // always true
43+
dflt: undefined, // never using default value
44+
});
4245
});
4346

4447
test("hasDefault", async () => {

app/__test__/integration/auth.integration.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
1+
import { afterAll, afterEach, beforeAll, describe, expect, it } from "bun:test";
22
import { App, createApp } from "../../src";
33
import type { AuthResponse } from "../../src/auth";
44
import { auth } from "../../src/auth/middlewares";
55
import { randomString, secureRandomString, withDisabledConsole } from "../../src/core/utils";
6-
import { disableConsoleLog, enableConsoleLog } from "../helper";
6+
import { disableConsoleLog, enableConsoleLog, getDummyConnection } from "../helper";
7+
8+
const { dummyConnection, afterAllCleanup } = getDummyConnection();
9+
afterEach(afterAllCleanup);
710

811
beforeAll(disableConsoleLog);
912
afterAll(enableConsoleLog);
@@ -64,6 +67,7 @@ const configs = {
6467

6568
function createAuthApp() {
6669
const app = createApp({
70+
connection: dummyConnection,
6771
initialConfig: {
6872
auth: configs.auth,
6973
},

app/build.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ function banner(title: string) {
5353
console.log("-".repeat(40));
5454
}
5555

56+
// collection of always-external packages
57+
const external = ["bun:test", "@libsql/client"] as const;
58+
5659
/**
5760
* Building backend and general API
5861
*/
@@ -64,7 +67,7 @@ async function buildApi() {
6467
watch,
6568
entry: ["src/index.ts", "src/data/index.ts", "src/core/index.ts", "src/core/utils/index.ts"],
6669
outDir: "dist",
67-
external: ["bun:test", "@libsql/client"],
70+
external: [...external],
6871
metafile: true,
6972
platform: "browser",
7073
format: ["esm"],
@@ -93,7 +96,7 @@ async function buildUi() {
9396
sourcemap,
9497
watch,
9598
external: [
96-
"bun:test",
99+
...external,
97100
"react",
98101
"react-dom",
99102
"react/jsx-runtime",

app/src/adapter/cloudflare/D1Connection.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class CustomD1Dialect extends D1Dialect {
1818
}
1919

2020
export class D1Connection extends SqliteConnection {
21+
protected override readonly supported = {
22+
batching: true,
23+
};
24+
2125
constructor(private config: D1ConnectionConfig) {
2226
const plugins = [new ParseJSONResultsPlugin()];
2327

@@ -28,14 +32,6 @@ export class D1Connection extends SqliteConnection {
2832
super(kysely, {}, plugins);
2933
}
3034

31-
override supportsBatching(): boolean {
32-
return true;
33-
}
34-
35-
override supportsIndices(): boolean {
36-
return true;
37-
}
38-
3935
protected override async batch<Queries extends QB[]>(
4036
queries: [...Queries],
4137
): Promise<{

app/src/core/utils/numbers.ts

+8
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ export function clampNumber(value: number, min: number, max: number): number {
33
const upper = Math.max(min, max);
44
return Math.max(lower, Math.min(value, upper));
55
}
6+
7+
export function ensureInt(value?: string | number | null | undefined): number {
8+
if (value === undefined || value === null) {
9+
return 0;
10+
}
11+
12+
return typeof value === "number" ? value : Number.parseInt(value, 10);
13+
}

0 commit comments

Comments
 (0)