symmetria-kit 1.0.0

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.
@@ -0,0 +1,39 @@
1
+ import { BindableSchema, PostgresDriver, SCHEMA_BIND } from "symmetria/driver";
2
+ import { SchemaSnapshot } from "symmetria";
3
+ //#region src/testing.d.ts
4
+ export type TestDatabaseOptions<Model> = {
5
+ /** PostgreSQL connection URL; the test schema is created inside this database. */
6
+ readonly url: string;
7
+ readonly schema: Model;
8
+ /** Prefix of the generated schema name (default "symmetria_test"). */
9
+ readonly prefix?: string;
10
+ /** Pool size for the test driver (default 4). */
11
+ readonly max?: number;
12
+ /** Adapter used by both the administration and isolated-schema pools. */
13
+ readonly driver?: (options: {
14
+ url: string;
15
+ max: number;
16
+ }) => PostgresDriver;
17
+ /**
18
+ * Schemas appended to the test schema on the search_path, for example `["public"]`
19
+ * when the tables use types from an extension installed there.
20
+ */
21
+ readonly searchPath?: readonly string[];
22
+ };
23
+ export type TestDatabase<Model extends BindableSchema> = {
24
+ readonly database: ReturnType<Model[typeof SCHEMA_BIND]>;
25
+ readonly driver: PostgresDriver;
26
+ /** The isolated PostgreSQL schema that holds the tables. */
27
+ readonly namespace: string;
28
+ /** Close the pool and drop the schema with everything in it. */
29
+ readonly close: () => Promise<void>;
30
+ };
31
+ /**
32
+ * An isolated PostgreSQL schema per call: created from the declaration, bound to a
33
+ * fresh pool whose search_path points at it, and dropped on close().
34
+ */
35
+ export declare const testDatabase: <Model extends BindableSchema & {
36
+ readonly $snapshot: () => SchemaSnapshot;
37
+ }>(options: TestDatabaseOptions<Model>) => Promise<TestDatabase<Model>>;
38
+ //#endregion
39
+ //# sourceMappingURL=testing.d.ts.map
@@ -0,0 +1,69 @@
1
+ import { t as createSchemaSQL } from "./diff-ConfLt63.js";
2
+ import { randomUUID } from "node:crypto";
3
+ import { postgres } from "symmetria/driver";
4
+ import { createDatabase } from "symmetria";
5
+ //#region src/testing.ts
6
+ /**
7
+ * An isolated PostgreSQL schema per call: created from the declaration, bound to a
8
+ * fresh pool whose search_path points at it, and dropped on close().
9
+ */
10
+ const testDatabase = async (options) => {
11
+ const namespace = `${options.prefix ?? "symmetria_test"}_${randomUUID().replaceAll("-", "")}`;
12
+ if (!/^[a-z_][a-z0-9_]*$/.test(namespace) || namespace.length > 63) throw new Error("symmetria: test schema prefix must be a short identifier");
13
+ const connect = options.driver ?? postgres;
14
+ const admin = connect({
15
+ url: options.url,
16
+ max: 1
17
+ });
18
+ const adminSQL = (sql) => admin.execute({
19
+ sql,
20
+ parameters: []
21
+ });
22
+ try {
23
+ await adminSQL(`CREATE SCHEMA "${namespace}"`);
24
+ } catch (error) {
25
+ await admin.close();
26
+ throw error;
27
+ }
28
+ for (const schemaName of options.searchPath ?? []) if (!/^[a-z_][a-z0-9_]*$/.test(schemaName)) throw new Error("symmetria: search_path entries must be identifiers");
29
+ const connection = new URL(options.url);
30
+ connection.searchParams.set("options", `-c search_path=${[namespace, ...options.searchPath ?? []].join(",")}`);
31
+ const driver = connect({
32
+ url: connection.toString(),
33
+ max: options.max ?? 4
34
+ });
35
+ let database;
36
+ try {
37
+ for (const statement of createSchemaSQL(options.schema.$snapshot())) await driver.execute(statement);
38
+ database = createDatabase({
39
+ schema: options.schema,
40
+ driver
41
+ });
42
+ } catch (error) {
43
+ await driver.close();
44
+ await adminSQL(`DROP SCHEMA IF EXISTS "${namespace}" CASCADE`);
45
+ await admin.close();
46
+ throw error;
47
+ }
48
+ let closing;
49
+ return Object.freeze({
50
+ database,
51
+ driver,
52
+ namespace,
53
+ close: () => closing ??= (async () => {
54
+ try {
55
+ await driver.close();
56
+ } finally {
57
+ try {
58
+ await adminSQL(`DROP SCHEMA IF EXISTS "${namespace}" CASCADE`);
59
+ } finally {
60
+ await admin.close();
61
+ }
62
+ }
63
+ })()
64
+ });
65
+ };
66
+ //#endregion
67
+ export { testDatabase };
68
+
69
+ //# sourceMappingURL=testing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testing.js","names":[],"sources":["../src/testing.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { createDatabase, type SchemaSnapshot } from \"symmetria\";\nimport {\n postgres,\n SCHEMA_BIND,\n type BindableSchema,\n type PostgresDriver,\n} from \"symmetria/driver\";\nimport { createSchemaSQL } from \"./migrations/diff\";\n\nexport type TestDatabaseOptions<Model> = {\n /** PostgreSQL connection URL; the test schema is created inside this database. */\n readonly url: string;\n readonly schema: Model;\n /** Prefix of the generated schema name (default \"symmetria_test\"). */\n readonly prefix?: string;\n /** Pool size for the test driver (default 4). */\n readonly max?: number;\n /** Adapter used by both the administration and isolated-schema pools. */\n readonly driver?: (options: { url: string; max: number }) => PostgresDriver;\n /**\n * Schemas appended to the test schema on the search_path, for example `[\"public\"]`\n * when the tables use types from an extension installed there.\n */\n readonly searchPath?: readonly string[];\n};\n\nexport type TestDatabase<Model extends BindableSchema> = {\n readonly database: ReturnType<Model[typeof SCHEMA_BIND]>;\n readonly driver: PostgresDriver;\n /** The isolated PostgreSQL schema that holds the tables. */\n readonly namespace: string;\n /** Close the pool and drop the schema with everything in it. */\n readonly close: () => Promise<void>;\n};\n\n/**\n * An isolated PostgreSQL schema per call: created from the declaration, bound to a\n * fresh pool whose search_path points at it, and dropped on close().\n */\nexport const testDatabase = async <\n Model extends BindableSchema & { readonly $snapshot: () => SchemaSnapshot },\n>(\n options: TestDatabaseOptions<Model>,\n): Promise<TestDatabase<Model>> => {\n const namespace = `${options.prefix ?? \"symmetria_test\"}_${randomUUID().replaceAll(\"-\", \"\")}`;\n\n if (!/^[a-z_][a-z0-9_]*$/.test(namespace) || namespace.length > 63) {\n throw new Error(\"symmetria: test schema prefix must be a short identifier\");\n }\n\n const connect = options.driver ?? postgres;\n const admin = connect({ url: options.url, max: 1 });\n\n const adminSQL = (sql: string) => admin.execute({ sql, parameters: [] });\n\n try {\n await adminSQL(`CREATE SCHEMA \"${namespace}\"`);\n } catch (error) {\n await admin.close();\n throw error;\n }\n\n for (const schemaName of options.searchPath ?? []) {\n if (!/^[a-z_][a-z0-9_]*$/.test(schemaName)) {\n throw new Error(\"symmetria: search_path entries must be identifiers\");\n }\n }\n\n const connection = new URL(options.url);\n connection.searchParams.set(\n \"options\",\n `-c search_path=${[namespace, ...(options.searchPath ?? [])].join(\",\")}`,\n );\n const driver = connect({\n url: connection.toString(),\n max: options.max ?? 4,\n });\n\n let database: TestDatabase<Model>[\"database\"];\n\n try {\n for (const statement of createSchemaSQL(options.schema.$snapshot())) {\n await driver.execute(statement);\n }\n\n database = createDatabase({ schema: options.schema, driver });\n } catch (error) {\n await driver.close();\n await adminSQL(`DROP SCHEMA IF EXISTS \"${namespace}\" CASCADE`);\n await admin.close();\n throw error;\n }\n\n let closing: Promise<void> | undefined;\n\n return Object.freeze({\n database,\n driver,\n namespace,\n close: () =>\n (closing ??= (async () => {\n try {\n await driver.close();\n } finally {\n try {\n await adminSQL(`DROP SCHEMA IF EXISTS \"${namespace}\" CASCADE`);\n } finally {\n await admin.close();\n }\n }\n })()),\n });\n};\n"],"mappings":";;;;;;;;;AAwCA,MAAa,eAAe,OAG1B,YACiC;CACjC,MAAM,YAAY,GAAG,QAAQ,UAAU,iBAAiB,GAAG,WAAW,CAAC,CAAC,WAAW,KAAK,EAAE;CAE1F,IAAI,CAAC,qBAAqB,KAAK,SAAS,KAAK,UAAU,SAAS,IAC9D,MAAM,IAAI,MAAM,0DAA0D;CAG5E,MAAM,UAAU,QAAQ,UAAU;CAClC,MAAM,QAAQ,QAAQ;EAAE,KAAK,QAAQ;EAAK,KAAK;CAAE,CAAC;CAElD,MAAM,YAAY,QAAgB,MAAM,QAAQ;EAAE;EAAK,YAAY,CAAC;CAAE,CAAC;CAEvE,IAAI;EACF,MAAM,SAAS,kBAAkB,UAAU,EAAE;CAC/C,SAAS,OAAO;EACd,MAAM,MAAM,MAAM;EAClB,MAAM;CACR;CAEA,KAAK,MAAM,cAAc,QAAQ,cAAc,CAAC,GAC9C,IAAI,CAAC,qBAAqB,KAAK,UAAU,GACvC,MAAM,IAAI,MAAM,oDAAoD;CAIxE,MAAM,aAAa,IAAI,IAAI,QAAQ,GAAG;CACtC,WAAW,aAAa,IACtB,WACA,kBAAkB,CAAC,WAAW,GAAI,QAAQ,cAAc,CAAC,CAAE,CAAC,CAAC,KAAK,GAAG,GACvE;CACA,MAAM,SAAS,QAAQ;EACrB,KAAK,WAAW,SAAS;EACzB,KAAK,QAAQ,OAAO;CACtB,CAAC;CAED,IAAI;CAEJ,IAAI;EACF,KAAK,MAAM,aAAa,gBAAgB,QAAQ,OAAO,UAAU,CAAC,GAChE,MAAM,OAAO,QAAQ,SAAS;EAGhC,WAAW,eAAe;GAAE,QAAQ,QAAQ;GAAQ;EAAO,CAAC;CAC9D,SAAS,OAAO;EACd,MAAM,OAAO,MAAM;EACnB,MAAM,SAAS,0BAA0B,UAAU,UAAU;EAC7D,MAAM,MAAM,MAAM;EAClB,MAAM;CACR;CAEA,IAAI;CAEJ,OAAO,OAAO,OAAO;EACnB;EACA;EACA;EACA,aACG,aAAa,YAAY;GACxB,IAAI;IACF,MAAM,OAAO,MAAM;GACrB,UAAU;IACR,IAAI;KACF,MAAM,SAAS,0BAA0B,UAAU,UAAU;IAC/D,UAAU;KACR,MAAM,MAAM,MAAM;IACpB;GACF;EACF,EAAA,CAAG;CACP,CAAC;AACH"}
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "symmetria-kit",
3
+ "version": "1.0.0",
4
+ "description": "PostgreSQL migrations, schema browser and live query profiler for Node.js and Bun.",
5
+ "homepage": "https://github.com/nokturnal3301/symmetria#readme",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/nokturnal3301/symmetria.git",
9
+ "directory": "packages/kit"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/nokturnal3301/symmetria/issues"
13
+ },
14
+ "type": "module",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
20
+ },
21
+ "./inspect": {
22
+ "types": "./dist/cli/query-inspect.d.ts",
23
+ "default": "./dist/cli/query-inspect.js"
24
+ },
25
+ "./testing": {
26
+ "types": "./dist/testing.d.ts",
27
+ "default": "./dist/testing.js"
28
+ },
29
+ "./package.json": "./package.json"
30
+ },
31
+ "bin": {
32
+ "symmetria": "./dist/cli.js"
33
+ },
34
+ "files": [
35
+ "dist",
36
+ "README.md",
37
+ "LICENSE"
38
+ ],
39
+ "scripts": {
40
+ "build": "bun run --cwd ../.. build:kit",
41
+ "prepack": "bun run build"
42
+ },
43
+ "peerDependencies": {
44
+ "symmetria": "1.0.0"
45
+ },
46
+ "dependencies": {
47
+ "pg": "^8.23.1",
48
+ "string-width": "^8.1.0"
49
+ },
50
+ "engines": {
51
+ "node": ">=24.11.0"
52
+ },
53
+ "publishConfig": {
54
+ "access": "public"
55
+ },
56
+ "keywords": [
57
+ "symmetria",
58
+ "postgres",
59
+ "migrations",
60
+ "bun",
61
+ "cli"
62
+ ],
63
+ "author": "Mikhail Demidoff <demidoff.mikhail@gmail.com>",
64
+ "license": "MIT"
65
+ }