metal-orm 1.0.7 → 1.0.9
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.
- package/README.md +133 -121
- package/dist/decorators/index.cjs +2564 -0
- package/dist/decorators/index.cjs.map +1 -0
- package/dist/decorators/index.d.cts +53 -0
- package/dist/decorators/index.d.ts +53 -0
- package/dist/decorators/index.js +2530 -0
- package/dist/decorators/index.js.map +1 -0
- package/dist/index.cjs +4227 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +701 -0
- package/dist/index.d.ts +701 -0
- package/dist/index.js +4131 -0
- package/dist/index.js.map +1 -0
- package/dist/select-654m4qy8.d.cts +1522 -0
- package/dist/select-654m4qy8.d.ts +1522 -0
- package/package.json +27 -20
- package/src/codegen/typescript.ts +405 -393
- package/src/core/ast/aggregate-functions.ts +30 -0
- package/src/core/ast/builders.ts +43 -0
- package/src/core/ast/expression-builders.ts +310 -0
- package/src/core/ast/expression-nodes.ts +211 -0
- package/src/core/ast/expression-visitor.ts +99 -0
- package/src/core/ast/expression.ts +5 -0
- package/src/{utils → core/ast}/join-node.ts +20 -20
- package/src/{ast → core/ast}/join.ts +18 -18
- package/src/{ast → core/ast}/query.ts +113 -113
- package/src/core/ast/window-functions.ts +140 -0
- package/src/{dialect → core/dialect}/abstract.ts +94 -94
- package/src/{dialect → core/dialect}/mssql/index.ts +31 -31
- package/src/{dialect → core/dialect}/mysql/index.ts +31 -31
- package/src/{dialect → core/dialect}/postgres/index.ts +45 -45
- package/src/{dialect → core/dialect}/sqlite/index.ts +45 -45
- package/src/{constants → core/sql}/sql-operator-config.ts +39 -39
- package/src/decorators/bootstrap.ts +126 -0
- package/src/decorators/column.ts +78 -0
- package/src/decorators/entity.ts +36 -0
- package/src/decorators/index.ts +4 -0
- package/src/decorators/relations.ts +107 -0
- package/src/global.d.ts +1 -0
- package/src/index.ts +22 -22
- package/src/orm/db-executor.ts +11 -0
- package/src/orm/domain-event-bus.ts +52 -0
- package/src/{runtime → orm}/entity-meta.ts +52 -52
- package/src/orm/entity-metadata.ts +140 -0
- package/src/{runtime → orm}/entity.ts +252 -252
- package/src/{runtime → orm}/execute.ts +36 -36
- package/src/{runtime → orm}/hydration.ts +103 -103
- package/src/orm/identity-map.ts +37 -0
- package/src/{runtime → orm}/lazy-batch.ts +205 -205
- package/src/orm/orm-context.ts +154 -0
- package/src/orm/relation-change-processor.ts +140 -0
- package/src/{runtime → orm}/relations/belongs-to.ts +92 -92
- package/src/{runtime → orm}/relations/has-many.ts +111 -111
- package/src/{runtime → orm}/relations/many-to-many.ts +149 -149
- package/src/orm/runtime-types.ts +39 -0
- package/src/orm/transaction-runner.ts +17 -0
- package/src/orm/unit-of-work.ts +232 -0
- package/src/{builder/operations → query-builder}/column-selector.ts +78 -78
- package/src/{builder → query-builder}/delete-query-state.ts +38 -42
- package/src/{builder → query-builder}/delete.ts +46 -57
- package/src/{builder → query-builder}/hydration-manager.ts +87 -87
- package/src/{builder → query-builder}/hydration-planner.ts +182 -182
- package/src/{builder → query-builder}/insert-query-state.ts +51 -62
- package/src/{builder → query-builder}/insert.ts +48 -59
- package/src/{builder → query-builder}/query-ast-service.ts +208 -226
- package/src/{utils → query-builder}/raw-column-parser.ts +32 -32
- package/src/{builder → query-builder}/relation-conditions.ts +112 -112
- package/src/{builder/operations → query-builder}/relation-manager.ts +82 -82
- package/src/{builder → query-builder}/relation-projection-helper.ts +101 -101
- package/src/{builder → query-builder}/relation-service.ts +284 -284
- package/src/{builder → query-builder}/relation-types.ts +21 -21
- package/src/{builder → query-builder}/relation-utils.ts +12 -12
- package/src/{builder → query-builder}/select-query-builder-deps.ts +112 -94
- package/src/{builder → query-builder}/select-query-state.ts +179 -179
- package/src/{builder → query-builder}/select.ts +78 -69
- package/src/{builder → query-builder}/update-query-state.ts +55 -59
- package/src/{builder → query-builder}/update.ts +50 -61
- package/src/schema/column.ts +25 -25
- package/src/schema/relation.ts +116 -116
- package/src/schema/table.ts +34 -34
- package/src/schema/types.ts +76 -76
- package/.github/workflows/publish-metal-orm.yml +0 -38
- package/ROADMAP.md +0 -125
- package/docs/CHANGES.md +0 -104
- package/docs/advanced-features.md +0 -176
- package/docs/api-reference.md +0 -31
- package/docs/dml-operations.md +0 -156
- package/docs/getting-started.md +0 -171
- package/docs/hydration.md +0 -115
- package/docs/index.md +0 -36
- package/docs/multi-dialect-support.md +0 -59
- package/docs/query-builder.md +0 -135
- package/docs/runtime.md +0 -105
- package/docs/schema-definition.md +0 -112
- package/metadata.json +0 -5
- package/playground/api/playground-api.ts +0 -94
- package/playground/index.html +0 -15
- package/playground/src/App.css +0 -1
- package/playground/src/App.tsx +0 -114
- package/playground/src/components/CodeDisplay.tsx +0 -43
- package/playground/src/components/QueryExecutor.tsx +0 -189
- package/playground/src/components/ResultsTable.tsx +0 -67
- package/playground/src/components/ResultsTabs.tsx +0 -105
- package/playground/src/components/ScenarioList.tsx +0 -56
- package/playground/src/components/logo.svg +0 -45
- package/playground/src/data/scenarios.ts +0 -2
- package/playground/src/main.tsx +0 -9
- package/playground/src/services/PlaygroundApiService.ts +0 -60
- package/postcss.config.cjs +0 -5
- package/sql_sql-ansi-cheatsheet-2025.md +0 -264
- package/src/ast/expression.ts +0 -658
- package/src/builder/operations/cte-manager.ts +0 -34
- package/src/builder/operations/filter-manager.ts +0 -68
- package/src/builder/operations/join-manager.ts +0 -36
- package/src/builder/operations/pagination-manager.ts +0 -36
- package/src/playground/features/playground/api/types.ts +0 -16
- package/src/playground/features/playground/clients/MockClient.ts +0 -17
- package/src/playground/features/playground/clients/SqliteClient.ts +0 -57
- package/src/playground/features/playground/common/IDatabaseClient.ts +0 -10
- package/src/playground/features/playground/data/scenarios/aggregation.ts +0 -36
- package/src/playground/features/playground/data/scenarios/basics.ts +0 -25
- package/src/playground/features/playground/data/scenarios/edge_cases.ts +0 -57
- package/src/playground/features/playground/data/scenarios/filtering.ts +0 -94
- package/src/playground/features/playground/data/scenarios/hydration.ts +0 -27
- package/src/playground/features/playground/data/scenarios/index.ts +0 -29
- package/src/playground/features/playground/data/scenarios/ordering.ts +0 -25
- package/src/playground/features/playground/data/scenarios/pagination.ts +0 -16
- package/src/playground/features/playground/data/scenarios/relationships.ts +0 -75
- package/src/playground/features/playground/data/scenarios/types.ts +0 -70
- package/src/playground/features/playground/data/schema.ts +0 -91
- package/src/playground/features/playground/data/seed.ts +0 -104
- package/src/playground/features/playground/services/QueryExecutionService.ts +0 -121
- package/src/runtime/orm-context.ts +0 -539
- package/tests/belongs-to-many.test.ts +0 -57
- package/tests/between.test.ts +0 -43
- package/tests/case-expression.test.ts +0 -58
- package/tests/complex-exists.test.ts +0 -230
- package/tests/cte.test.ts +0 -118
- package/tests/dml.test.ts +0 -206
- package/tests/exists.test.ts +0 -127
- package/tests/like.test.ts +0 -33
- package/tests/orm-runtime.test.ts +0 -254
- package/tests/postgres.test.ts +0 -30
- package/tests/right-join.test.ts +0 -89
- package/tests/subquery-having.test.ts +0 -193
- package/tests/window-function.test.ts +0 -151
- package/tsconfig.json +0 -30
- package/tsup.config.ts +0 -10
- package/vite.config.ts +0 -22
- package/vitest.config.ts +0 -14
- /package/src/{constants → core/sql}/sql.ts +0 -0
- /package/src/{runtime → orm}/als.ts +0 -0
- /package/src/{utils → query-builder}/relation-alias.ts +0 -0
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { SelectQueryBuilder } from '../../../../../builder/select';
|
|
2
|
-
import { TableDef } from '../../../../../schema/table';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Extracts the TypeScript code from a build function
|
|
6
|
-
*/
|
|
7
|
-
function extractTypeScriptCode<TTable extends TableDef>(
|
|
8
|
-
buildFn: (builder: SelectQueryBuilder<any, TTable>) => SelectQueryBuilder<any, TTable>
|
|
9
|
-
): string {
|
|
10
|
-
const fnString = buildFn.toString();
|
|
11
|
-
|
|
12
|
-
// Remove the function wrapper and return statement
|
|
13
|
-
const bodyMatch = fnString.match(/=>\s*\{?\s*(.*?)\s*\}?$/s);
|
|
14
|
-
if (bodyMatch) {
|
|
15
|
-
let code = bodyMatch[1].trim();
|
|
16
|
-
|
|
17
|
-
// Remove trailing semicolon if present
|
|
18
|
-
code = code.replace(/;$/, '');
|
|
19
|
-
|
|
20
|
-
// Clean up indentation (remove common leading spaces)
|
|
21
|
-
const lines = code.split('\n');
|
|
22
|
-
if (lines.length > 1) {
|
|
23
|
-
// Find the minimum indentation of non-empty lines
|
|
24
|
-
const nonEmptyLines = lines.filter(line => line.trim().length > 0);
|
|
25
|
-
const minIndent = Math.min(...nonEmptyLines.map(line => {
|
|
26
|
-
const match = line.match(/^(\s*)/);
|
|
27
|
-
return match ? match[1].length : 0;
|
|
28
|
-
}));
|
|
29
|
-
|
|
30
|
-
// Remove the common indentation
|
|
31
|
-
code = lines.map(line => line.slice(minIndent)).join('\n').trim();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return code;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return fnString;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface Scenario {
|
|
41
|
-
id: string;
|
|
42
|
-
title: string;
|
|
43
|
-
description: string;
|
|
44
|
-
category: string;
|
|
45
|
-
build: <TTable extends TableDef>(builder: SelectQueryBuilder<any, TTable>) => SelectQueryBuilder<any, TTable>;
|
|
46
|
-
|
|
47
|
-
code?: string;
|
|
48
|
-
typescriptCode?: string;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Creates a scenario with auto-extracted TypeScript code
|
|
53
|
-
*/
|
|
54
|
-
export function createScenario(config: {
|
|
55
|
-
id: string;
|
|
56
|
-
title: string;
|
|
57
|
-
description: string;
|
|
58
|
-
category: string;
|
|
59
|
-
build: <TTable extends TableDef>(builder: SelectQueryBuilder<any, TTable>) => SelectQueryBuilder<any, TTable>;
|
|
60
|
-
}): Scenario {
|
|
61
|
-
return {
|
|
62
|
-
...config,
|
|
63
|
-
get code() {
|
|
64
|
-
return extractTypeScriptCode(config.build);
|
|
65
|
-
},
|
|
66
|
-
get typescriptCode() {
|
|
67
|
-
return extractTypeScriptCode(config.build);
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
}
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { defineTable } from '../../../../schema/table';
|
|
2
|
-
import { col } from '../../../../schema/column';
|
|
3
|
-
import { hasMany, belongsTo, belongsToMany } from '../../../../schema/relation';
|
|
4
|
-
|
|
5
|
-
export const Users = defineTable('users', {
|
|
6
|
-
id: col.primaryKey(col.int()),
|
|
7
|
-
name: col.varchar(255),
|
|
8
|
-
role: col.varchar(50),
|
|
9
|
-
settings: col.json(),
|
|
10
|
-
deleted_at: col.varchar(50)
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
export const Orders = defineTable('orders', {
|
|
14
|
-
id: col.primaryKey(col.int()),
|
|
15
|
-
user_id: col.int(),
|
|
16
|
-
total: col.int(),
|
|
17
|
-
status: col.varchar(50)
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
export const Profiles = defineTable('profiles', {
|
|
21
|
-
id: col.primaryKey(col.int()),
|
|
22
|
-
user_id: col.int(),
|
|
23
|
-
bio: col.varchar(255),
|
|
24
|
-
twitter: col.varchar(100)
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
export const Roles = defineTable('roles', {
|
|
28
|
-
id: col.primaryKey(col.int()),
|
|
29
|
-
name: col.varchar(50),
|
|
30
|
-
level: col.varchar(50)
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
export const UserRoles = defineTable('user_roles', {
|
|
34
|
-
id: col.primaryKey(col.int()),
|
|
35
|
-
user_id: col.int(),
|
|
36
|
-
role_id: col.int(),
|
|
37
|
-
assigned_at: col.varchar(50),
|
|
38
|
-
is_active: col.boolean()
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
export const Projects = defineTable('projects', {
|
|
42
|
-
id: col.primaryKey(col.int()),
|
|
43
|
-
name: col.varchar(255),
|
|
44
|
-
client: col.varchar(255)
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
export const ProjectAssignments = defineTable('project_assignments', {
|
|
48
|
-
id: col.primaryKey(col.int()),
|
|
49
|
-
project_id: col.int(),
|
|
50
|
-
user_id: col.int(),
|
|
51
|
-
role_id: col.int(),
|
|
52
|
-
assigned_at: col.varchar(50)
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
Users.relations = {
|
|
56
|
-
orders: hasMany(Orders, 'user_id'),
|
|
57
|
-
profiles: hasMany(Profiles, 'user_id'),
|
|
58
|
-
userRoles: hasMany(UserRoles, 'user_id'),
|
|
59
|
-
projects: belongsToMany(Projects, ProjectAssignments, {
|
|
60
|
-
pivotForeignKeyToRoot: 'user_id',
|
|
61
|
-
pivotForeignKeyToTarget: 'project_id'
|
|
62
|
-
})
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
Orders.relations = {
|
|
66
|
-
user: belongsTo(Users, 'user_id')
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
Profiles.relations = {
|
|
70
|
-
user: belongsTo(Users, 'user_id')
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
Roles.relations = {
|
|
74
|
-
userRoles: hasMany(UserRoles, 'role_id'),
|
|
75
|
-
projectAssignments: hasMany(ProjectAssignments, 'role_id')
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
UserRoles.relations = {
|
|
79
|
-
user: belongsTo(Users, 'user_id'),
|
|
80
|
-
role: belongsTo(Roles, 'role_id')
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
Projects.relations = {
|
|
84
|
-
projectAssignments: hasMany(ProjectAssignments, 'project_id')
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
ProjectAssignments.relations = {
|
|
88
|
-
project: belongsTo(Projects, 'project_id'),
|
|
89
|
-
user: belongsTo(Users, 'user_id'),
|
|
90
|
-
role: belongsTo(Roles, 'role_id')
|
|
91
|
-
};
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
export const SEED_SQL = `
|
|
2
|
-
DROP TABLE IF EXISTS project_assignments;
|
|
3
|
-
DROP TABLE IF EXISTS projects;
|
|
4
|
-
DROP TABLE IF EXISTS user_roles;
|
|
5
|
-
DROP TABLE IF EXISTS roles;
|
|
6
|
-
DROP TABLE IF EXISTS profiles;
|
|
7
|
-
DROP TABLE IF EXISTS orders;
|
|
8
|
-
DROP TABLE IF EXISTS users;
|
|
9
|
-
|
|
10
|
-
-- Core tables
|
|
11
|
-
CREATE TABLE users (
|
|
12
|
-
id INTEGER PRIMARY KEY,
|
|
13
|
-
name TEXT,
|
|
14
|
-
role TEXT,
|
|
15
|
-
settings TEXT,
|
|
16
|
-
deleted_at TEXT
|
|
17
|
-
);
|
|
18
|
-
|
|
19
|
-
CREATE TABLE orders (
|
|
20
|
-
id INTEGER PRIMARY KEY,
|
|
21
|
-
user_id INTEGER,
|
|
22
|
-
total INTEGER,
|
|
23
|
-
status TEXT
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
CREATE TABLE profiles (
|
|
27
|
-
id INTEGER PRIMARY KEY,
|
|
28
|
-
user_id INTEGER,
|
|
29
|
-
bio TEXT,
|
|
30
|
-
twitter TEXT
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
CREATE TABLE roles (
|
|
34
|
-
id INTEGER PRIMARY KEY,
|
|
35
|
-
name TEXT,
|
|
36
|
-
level TEXT
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
-- N:N join table with extra attributes ("rich association")
|
|
40
|
-
CREATE TABLE user_roles (
|
|
41
|
-
id INTEGER PRIMARY KEY,
|
|
42
|
-
user_id INTEGER,
|
|
43
|
-
role_id INTEGER,
|
|
44
|
-
assigned_at TEXT, -- when the role was assigned
|
|
45
|
-
is_active BOOLEAN -- current vs historical
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
-- Projects for ternary relationship
|
|
49
|
-
CREATE TABLE projects (
|
|
50
|
-
id INTEGER PRIMARY KEY,
|
|
51
|
-
name TEXT,
|
|
52
|
-
client TEXT
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
-- Ternary link: user + project + role
|
|
56
|
-
CREATE TABLE project_assignments (
|
|
57
|
-
id INTEGER PRIMARY KEY,
|
|
58
|
-
project_id INTEGER,
|
|
59
|
-
user_id INTEGER,
|
|
60
|
-
role_id INTEGER,
|
|
61
|
-
assigned_at TEXT
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
-- Users
|
|
65
|
-
INSERT INTO users VALUES (1, 'Alice Engineer', 'admin', '{"theme":"dark"}', NULL);
|
|
66
|
-
INSERT INTO users VALUES (2, 'Bob Manager', 'user', '{"theme":"light"}', NULL);
|
|
67
|
-
INSERT INTO users VALUES (3, 'Charlie Intern', 'user', '{"theme":"dark"}', '2023-01-01');
|
|
68
|
-
INSERT INTO users VALUES (4, 'David CTO', 'admin', '{"notifications":true}', NULL);
|
|
69
|
-
|
|
70
|
-
-- Orders
|
|
71
|
-
INSERT INTO orders VALUES (101, 1, 500, 'completed');
|
|
72
|
-
INSERT INTO orders VALUES (102, 1, 120, 'pending');
|
|
73
|
-
INSERT INTO orders VALUES (103, 2, 900, 'completed');
|
|
74
|
-
INSERT INTO orders VALUES (104, 3, 50, 'cancelled');
|
|
75
|
-
INSERT INTO orders VALUES (105, 1, 300, 'completed');
|
|
76
|
-
|
|
77
|
-
-- Profiles (1:1 with users)
|
|
78
|
-
INSERT INTO profiles VALUES (1, 1, 'Lead Systems Engineer with API obsession', '@alice_engineer');
|
|
79
|
-
INSERT INTO profiles VALUES (2, 2, 'Operations manager who optimizes everything', '@bob_ops');
|
|
80
|
-
INSERT INTO profiles VALUES (3, 3, 'Intern documenting every experiment', '@charlie_docs');
|
|
81
|
-
INSERT INTO profiles VALUES (4, 4, 'CTO crafting scalable futures', '@david_cto');
|
|
82
|
-
|
|
83
|
-
-- Roles
|
|
84
|
-
INSERT INTO roles VALUES (1, 'admin', 'platform');
|
|
85
|
-
INSERT INTO roles VALUES (2, 'manager', 'operations');
|
|
86
|
-
INSERT INTO roles VALUES (3, 'intern', 'learning');
|
|
87
|
-
INSERT INTO roles VALUES (4, 'viewer', 'guest'); -- extra role for projects
|
|
88
|
-
|
|
89
|
-
-- User roles (N:N with attributes on the edge)
|
|
90
|
-
INSERT INTO user_roles VALUES (1, 1, 1, '2022-01-01', 1); -- Alice is Admin (active)
|
|
91
|
-
INSERT INTO user_roles VALUES (2, 1, 2, '2022-06-01', 0); -- Alice was Manager (inactive)
|
|
92
|
-
INSERT INTO user_roles VALUES (3, 2, 2, '2023-01-15', 1); -- Bob is Manager (active)
|
|
93
|
-
INSERT INTO user_roles VALUES (4, 3, 3, '2023-08-20', 1); -- Charlie is Intern (active)
|
|
94
|
-
|
|
95
|
-
-- Projects
|
|
96
|
-
INSERT INTO projects VALUES (10, 'Alpha Redesign', 'Acme Corp');
|
|
97
|
-
INSERT INTO projects VALUES (20, 'Beta Migration', 'Globex');
|
|
98
|
-
|
|
99
|
-
-- Ternary: who has which role in which project
|
|
100
|
-
INSERT INTO project_assignments VALUES (1, 10, 1, 2, '2023-09-01'); -- Alice is manager on Alpha
|
|
101
|
-
INSERT INTO project_assignments VALUES (2, 10, 2, 4, '2023-09-05'); -- Bob is viewer on Alpha
|
|
102
|
-
INSERT INTO project_assignments VALUES (3, 20, 3, 3, '2023-10-01'); -- Charlie is intern on Beta
|
|
103
|
-
INSERT INTO project_assignments VALUES (4, 20, 1, 1, '2023-10-01'); -- Alice is admin on Beta
|
|
104
|
-
`;
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { performance } from 'node:perf_hooks';
|
|
2
|
-
import { SelectQueryBuilder } from '../../../../builder/select';
|
|
3
|
-
import { Users } from '../data/schema';
|
|
4
|
-
import { SqliteDialect } from '../../../../dialect/sqlite';
|
|
5
|
-
import { hydrateRows } from '../../../../runtime/hydration';
|
|
6
|
-
import type { IDatabaseClient } from '../common/IDatabaseClient';
|
|
7
|
-
import type { QueryExecutionResult } from '../api/types';
|
|
8
|
-
import type { Scenario } from '../data/scenarios';
|
|
9
|
-
import type { TableDef } from '../../../../schema/table';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Extracts the TypeScript code from a build function
|
|
13
|
-
*/
|
|
14
|
-
function extractTypeScriptCode<TTable extends TableDef>(buildFn: (builder: SelectQueryBuilder<any, TTable>) => SelectQueryBuilder<any, TTable>): string {
|
|
15
|
-
const fnString = buildFn.toString();
|
|
16
|
-
|
|
17
|
-
// Remove the function wrapper and return statement
|
|
18
|
-
const bodyMatch = fnString.match(/=>\s*\{?\s*(.*?)\s*\}?$/s);
|
|
19
|
-
if (bodyMatch) {
|
|
20
|
-
let code = bodyMatch[1].trim();
|
|
21
|
-
|
|
22
|
-
// Remove trailing semicolon if present
|
|
23
|
-
code = code.replace(/;$/, '');
|
|
24
|
-
|
|
25
|
-
// Clean up indentation (remove common leading spaces)
|
|
26
|
-
const lines = code.split('\n');
|
|
27
|
-
if (lines.length > 1) {
|
|
28
|
-
// Find the minimum indentation of non-empty lines
|
|
29
|
-
const nonEmptyLines = lines.filter(line => line.trim().length > 0);
|
|
30
|
-
const minIndent = Math.min(...nonEmptyLines.map(line => {
|
|
31
|
-
const match = line.match(/^(\s*)/);
|
|
32
|
-
return match ? match[1].length : 0;
|
|
33
|
-
}));
|
|
34
|
-
|
|
35
|
-
// Remove the common indentation
|
|
36
|
-
code = lines.map(line => line.slice(minIndent)).join('\n').trim();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return code;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return fnString;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export class QueryExecutionService {
|
|
46
|
-
constructor(private dbClient: IDatabaseClient) { }
|
|
47
|
-
|
|
48
|
-
async executeScenario(scenario: Scenario): Promise<QueryExecutionResult> {
|
|
49
|
-
const startTime = performance.now();
|
|
50
|
-
|
|
51
|
-
try {
|
|
52
|
-
if (!this.dbClient.isReady) {
|
|
53
|
-
return {
|
|
54
|
-
sql: '',
|
|
55
|
-
params: [],
|
|
56
|
-
typescriptCode: scenario.typescriptCode || extractTypeScriptCode(scenario.build),
|
|
57
|
-
results: [],
|
|
58
|
-
error: 'Database not ready',
|
|
59
|
-
executionTime: 0
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const queryBuilder = new SelectQueryBuilder(Users);
|
|
64
|
-
const builtQuery = scenario.build(queryBuilder);
|
|
65
|
-
|
|
66
|
-
const dialect = new SqliteDialect();
|
|
67
|
-
const compiled = builtQuery.compile(dialect);
|
|
68
|
-
|
|
69
|
-
const results = await this.dbClient.executeSql(compiled.sql, compiled.params);
|
|
70
|
-
const executionTime = performance.now() - startTime;
|
|
71
|
-
|
|
72
|
-
// Check if hydration is needed
|
|
73
|
-
const hydrationPlan = builtQuery.getHydrationPlan();
|
|
74
|
-
let hydratedResults: Record<string, any>[] | undefined;
|
|
75
|
-
|
|
76
|
-
if (hydrationPlan && results.length > 0) {
|
|
77
|
-
// Convert QueryResult[] to Record<string, any>[] for hydration
|
|
78
|
-
const rows: Record<string, any>[] = [];
|
|
79
|
-
const { columns, values } = results[0];
|
|
80
|
-
|
|
81
|
-
for (const row of values) {
|
|
82
|
-
const obj: Record<string, any> = {};
|
|
83
|
-
columns.forEach((col, idx) => {
|
|
84
|
-
obj[col] = row[idx];
|
|
85
|
-
});
|
|
86
|
-
rows.push(obj);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
hydratedResults = hydrateRows(rows, hydrationPlan);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return {
|
|
93
|
-
sql: compiled.sql,
|
|
94
|
-
params: compiled.params,
|
|
95
|
-
typescriptCode: scenario.typescriptCode || extractTypeScriptCode(scenario.build),
|
|
96
|
-
results,
|
|
97
|
-
hydratedResults,
|
|
98
|
-
error: this.dbClient.error,
|
|
99
|
-
executionTime
|
|
100
|
-
};
|
|
101
|
-
} catch (error) {
|
|
102
|
-
const executionTime = performance.now() - startTime;
|
|
103
|
-
return {
|
|
104
|
-
sql: '',
|
|
105
|
-
params: [],
|
|
106
|
-
typescriptCode: scenario.typescriptCode || extractTypeScriptCode(scenario.build),
|
|
107
|
-
results: [],
|
|
108
|
-
error: error instanceof Error ? error.message : 'Unknown error',
|
|
109
|
-
executionTime
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
isDatabaseReady(): boolean {
|
|
115
|
-
return this.dbClient.isReady;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
getLastError(): string | null {
|
|
119
|
-
return this.dbClient.error;
|
|
120
|
-
}
|
|
121
|
-
}
|