@morlay/rdbkit 0.0.0-stage → 0.0.1-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +146 -2
  3. package/dist/index.d.mts +518 -0
  4. package/dist/index.mjs +1422 -0
  5. package/dist/testing.d.mts +28 -0
  6. package/dist/testing.mjs +42 -0
  7. package/package.json +49 -3
  8. package/src/__tests__/database-introspect.spec.ts +71 -0
  9. package/src/__tests__/expr-functions.spec.ts +64 -0
  10. package/src/__tests__/migration.spec.ts +81 -0
  11. package/src/__tests__/pg.spec.ts +123 -0
  12. package/src/__tests__/query-join.spec.ts +114 -0
  13. package/src/__tests__/query-p1.spec.ts +108 -0
  14. package/src/__tests__/query-write.spec.ts +293 -0
  15. package/src/__tests__/query.spec.ts +88 -0
  16. package/src/__tests__/schema.spec.ts +144 -0
  17. package/src/__tests__/transaction.spec.ts +83 -0
  18. package/src/catalog.ts +24 -0
  19. package/src/compound.ts +64 -0
  20. package/src/database/capabilities.ts +70 -0
  21. package/src/database/create.ts +119 -0
  22. package/src/database/index.ts +9 -0
  23. package/src/database/postgres.ts +133 -0
  24. package/src/database/queue.ts +24 -0
  25. package/src/database/sqlite.ts +66 -0
  26. package/src/database/types.ts +87 -0
  27. package/src/database/wrap.ts +48 -0
  28. package/src/defs.ts +82 -0
  29. package/src/dialect.ts +219 -0
  30. package/src/error.ts +72 -0
  31. package/src/expr.ts +346 -0
  32. package/src/fragment.ts +96 -0
  33. package/src/index.ts +114 -0
  34. package/src/migrate.ts +133 -0
  35. package/src/migration.ts +82 -0
  36. package/src/table/handle.ts +142 -0
  37. package/src/table/index.ts +14 -0
  38. package/src/table/render.ts +140 -0
  39. package/src/table/select.ts +229 -0
  40. package/src/table/types.ts +53 -0
  41. package/src/testing.ts +56 -0
package/src/testing.ts ADDED
@@ -0,0 +1,56 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { DatabaseSync } from "node:sqlite";
3
+ import { Client } from "pg";
4
+
5
+ /**
6
+ * 测试专用句柄:造损坏数据、读 `PRAGMA` 这类只有同步驱动才做得到的事。
7
+ *
8
+ * 生产路径不经过这里——业务侧拿的是 `Database`(`openDatabase`),没有同步口。
9
+ * @param path - SQLite 库文件路径。
10
+ * @param options - 与打开库时一致的 busy timeout / journal mode。
11
+ * @returns 同步句柄(用完要 `close()`)。
12
+ */
13
+ export function openSqliteHandle(
14
+ path: string,
15
+ options: { journalMode?: string; busyTimeout?: number } = {},
16
+ ): DatabaseSync {
17
+ const client = new DatabaseSync(path);
18
+ client.exec(`PRAGMA busy_timeout = ${options.busyTimeout ?? 5000}`);
19
+ if (options.journalMode !== undefined) client.exec(`PRAGMA journal_mode = ${options.journalMode}`);
20
+ return client;
21
+ }
22
+
23
+ /** 一个临时的 PostgreSQL 测试库:用完必须 `drop()`。 */
24
+ export interface PostgresTestDatabase {
25
+ connectionString: string;
26
+ drop: () => Promise<void>;
27
+ }
28
+
29
+ /**
30
+ * 建一个随机名的 PostgreSQL 测试库(用完 drop)。
31
+ *
32
+ * 建库是管理面操作:业务侧的 `Database` 不提供它,测试要独立库时用这里。
33
+ * @param options - `adminUrl` 指向有建库权限的连接串;省略时读 `TEST_PG_URL`。
34
+ * @returns 指向新库的连接串与清理函数。
35
+ */
36
+ export async function createPostgresTestDatabase(
37
+ options: { adminUrl?: string } = {},
38
+ ): Promise<PostgresTestDatabase> {
39
+ const adminUrl =
40
+ options.adminUrl ??
41
+ process.env["TEST_PG_URL"] ??
42
+ "postgres://postgres:postgres@localhost:25433/postgres";
43
+ const name = `dsh_test_${randomUUID().replaceAll("-", "")}`;
44
+ const admin = new Client({ connectionString: adminUrl });
45
+ await admin.connect();
46
+ await admin.query(`CREATE DATABASE "${name}"`);
47
+ const url = new URL(adminUrl);
48
+ url.pathname = `/${name}`;
49
+ return {
50
+ connectionString: url.toString(),
51
+ drop: async () => {
52
+ await admin.query(`DROP DATABASE IF EXISTS "${name}" WITH (FORCE)`);
53
+ await admin.end();
54
+ },
55
+ };
56
+ }