@stonyx/orm 0.2.1-beta.83 → 0.2.1-beta.85

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 (150) hide show
  1. package/dist/aggregates.d.ts +21 -0
  2. package/dist/aggregates.js +90 -0
  3. package/dist/attr.d.ts +2 -0
  4. package/dist/attr.js +22 -0
  5. package/dist/belongs-to.d.ts +11 -0
  6. package/dist/belongs-to.js +59 -0
  7. package/dist/cli.d.ts +22 -0
  8. package/dist/cli.js +148 -0
  9. package/dist/commands.d.ts +7 -0
  10. package/dist/commands.js +146 -0
  11. package/dist/db.d.ts +21 -0
  12. package/dist/db.js +174 -0
  13. package/dist/exports/db.d.ts +7 -0
  14. package/{src → dist}/exports/db.js +2 -4
  15. package/dist/has-many.d.ts +11 -0
  16. package/dist/has-many.js +58 -0
  17. package/dist/hooks.d.ts +47 -0
  18. package/dist/hooks.js +106 -0
  19. package/dist/index.d.ts +14 -0
  20. package/dist/index.js +34 -0
  21. package/dist/main.d.ts +46 -0
  22. package/dist/main.js +179 -0
  23. package/dist/manage-record.d.ts +13 -0
  24. package/dist/manage-record.js +114 -0
  25. package/dist/meta-request.d.ts +6 -0
  26. package/dist/meta-request.js +52 -0
  27. package/dist/migrate.d.ts +2 -0
  28. package/dist/migrate.js +57 -0
  29. package/dist/model-property.d.ts +9 -0
  30. package/dist/model-property.js +29 -0
  31. package/dist/model.d.ts +15 -0
  32. package/dist/model.js +18 -0
  33. package/dist/mysql/connection.d.ts +14 -0
  34. package/dist/mysql/connection.js +24 -0
  35. package/dist/mysql/migration-generator.d.ts +45 -0
  36. package/dist/mysql/migration-generator.js +245 -0
  37. package/dist/mysql/migration-runner.d.ts +12 -0
  38. package/dist/mysql/migration-runner.js +83 -0
  39. package/dist/mysql/mysql-db.d.ts +100 -0
  40. package/dist/mysql/mysql-db.js +415 -0
  41. package/dist/mysql/query-builder.d.ts +10 -0
  42. package/dist/mysql/query-builder.js +44 -0
  43. package/dist/mysql/schema-introspector.d.ts +19 -0
  44. package/dist/mysql/schema-introspector.js +286 -0
  45. package/dist/mysql/type-map.d.ts +21 -0
  46. package/dist/mysql/type-map.js +36 -0
  47. package/dist/orm-request.d.ts +38 -0
  48. package/dist/orm-request.js +455 -0
  49. package/dist/plural-registry.d.ts +4 -0
  50. package/{src → dist}/plural-registry.js +3 -6
  51. package/dist/postgres/connection.d.ts +15 -0
  52. package/dist/postgres/connection.js +30 -0
  53. package/dist/postgres/migration-generator.d.ts +45 -0
  54. package/dist/postgres/migration-generator.js +257 -0
  55. package/dist/postgres/migration-runner.d.ts +10 -0
  56. package/dist/postgres/migration-runner.js +82 -0
  57. package/dist/postgres/postgres-db.d.ts +119 -0
  58. package/dist/postgres/postgres-db.js +476 -0
  59. package/dist/postgres/query-builder.d.ts +27 -0
  60. package/dist/postgres/query-builder.js +98 -0
  61. package/dist/postgres/schema-introspector.d.ts +29 -0
  62. package/dist/postgres/schema-introspector.js +309 -0
  63. package/dist/postgres/type-map.d.ts +23 -0
  64. package/dist/postgres/type-map.js +53 -0
  65. package/dist/record.d.ts +75 -0
  66. package/dist/record.js +115 -0
  67. package/dist/relationships.d.ts +10 -0
  68. package/dist/relationships.js +39 -0
  69. package/dist/serializer.d.ts +17 -0
  70. package/dist/serializer.js +136 -0
  71. package/dist/setup-rest-server.d.ts +1 -0
  72. package/dist/setup-rest-server.js +54 -0
  73. package/dist/standalone-db.d.ts +58 -0
  74. package/dist/standalone-db.js +142 -0
  75. package/dist/store.d.ts +62 -0
  76. package/dist/store.js +271 -0
  77. package/dist/timescale/query-builder.d.ts +41 -0
  78. package/dist/timescale/query-builder.js +87 -0
  79. package/dist/timescale/timescale-db.d.ts +45 -0
  80. package/dist/timescale/timescale-db.js +84 -0
  81. package/dist/transforms.d.ts +2 -0
  82. package/dist/transforms.js +17 -0
  83. package/dist/types/orm-types.d.ts +142 -0
  84. package/dist/types/orm-types.js +1 -0
  85. package/dist/utils.d.ts +5 -0
  86. package/dist/utils.js +13 -0
  87. package/dist/view-resolver.d.ts +8 -0
  88. package/dist/view-resolver.js +169 -0
  89. package/dist/view.d.ts +11 -0
  90. package/dist/view.js +18 -0
  91. package/package.json +34 -11
  92. package/src/{aggregates.js → aggregates.ts} +27 -13
  93. package/src/{attr.js → attr.ts} +2 -2
  94. package/src/belongs-to.ts +90 -0
  95. package/src/{cli.js → cli.ts} +17 -11
  96. package/src/{commands.js → commands.ts} +179 -170
  97. package/src/{db.js → db.ts} +35 -26
  98. package/src/exports/db.ts +7 -0
  99. package/src/has-many.ts +92 -0
  100. package/src/{hooks.js → hooks.ts} +23 -27
  101. package/src/{index.js → index.ts} +4 -4
  102. package/src/{main.js → main.ts} +60 -34
  103. package/src/{manage-record.js → manage-record.ts} +42 -22
  104. package/src/{meta-request.js → meta-request.ts} +17 -14
  105. package/src/{migrate.js → migrate.ts} +9 -9
  106. package/src/{model-property.js → model-property.ts} +12 -6
  107. package/src/{model.js → model.ts} +5 -4
  108. package/src/mysql/{connection.js → connection.ts} +43 -28
  109. package/src/mysql/{migration-generator.js → migration-generator.ts} +332 -286
  110. package/src/mysql/{migration-runner.js → migration-runner.ts} +116 -110
  111. package/src/mysql/{mysql-db.js → mysql-db.ts} +537 -473
  112. package/src/mysql/{query-builder.js → query-builder.ts} +69 -64
  113. package/src/mysql/{schema-introspector.js → schema-introspector.ts} +355 -325
  114. package/src/mysql/{type-map.js → type-map.ts} +42 -37
  115. package/src/{orm-request.js → orm-request.ts} +169 -97
  116. package/src/plural-registry.ts +12 -0
  117. package/src/postgres/{connection.js → connection.ts} +14 -5
  118. package/src/postgres/{migration-generator.js → migration-generator.ts} +82 -38
  119. package/src/postgres/{migration-runner.js → migration-runner.ts} +11 -10
  120. package/src/postgres/{postgres-db.js → postgres-db.ts} +198 -114
  121. package/src/postgres/{query-builder.js → query-builder.ts} +27 -28
  122. package/src/postgres/{schema-introspector.js → schema-introspector.ts} +87 -58
  123. package/src/postgres/{type-map.js → type-map.ts} +10 -6
  124. package/src/{record.js → record.ts} +73 -34
  125. package/src/relationships.ts +53 -0
  126. package/src/{serializer.js → serializer.ts} +52 -36
  127. package/src/{setup-rest-server.js → setup-rest-server.ts} +18 -13
  128. package/src/{standalone-db.js → standalone-db.ts} +33 -24
  129. package/src/{store.js → store.ts} +90 -68
  130. package/src/timescale/{query-builder.js → query-builder.ts} +33 -38
  131. package/src/timescale/timescale-db.ts +119 -0
  132. package/src/transforms.ts +20 -0
  133. package/src/types/mysql2.d.ts +30 -0
  134. package/src/types/orm-types.ts +146 -0
  135. package/src/types/pg.d.ts +28 -0
  136. package/src/types/stonyx-cron.d.ts +5 -0
  137. package/src/types/stonyx-events.d.ts +4 -0
  138. package/src/types/stonyx-rest-server.d.ts +11 -0
  139. package/src/types/stonyx-utils.d.ts +33 -0
  140. package/src/types/stonyx.d.ts +21 -0
  141. package/src/utils.ts +16 -0
  142. package/src/{view-resolver.js → view-resolver.ts} +51 -24
  143. package/src/view.ts +22 -0
  144. package/src/belongs-to.js +0 -70
  145. package/src/has-many.js +0 -68
  146. package/src/relationships.js +0 -43
  147. package/src/timescale/timescale-db.js +0 -111
  148. package/src/transforms.js +0 -20
  149. package/src/utils.js +0 -12
  150. package/src/view.js +0 -21
@@ -1,110 +1,116 @@
1
- import { readFile, fileExists } from '@stonyx/utils/file';
2
- import path from 'path';
3
- import fs from 'fs/promises';
4
-
5
- export async function ensureMigrationsTable(pool, tableName = '__migrations') {
6
- await pool.execute(`
7
- CREATE TABLE IF NOT EXISTS \`${tableName}\` (
8
- id INT AUTO_INCREMENT PRIMARY KEY,
9
- filename VARCHAR(255) NOT NULL UNIQUE,
10
- applied_at DATETIME DEFAULT CURRENT_TIMESTAMP
11
- )
12
- `);
13
- }
14
-
15
- export async function getAppliedMigrations(pool, tableName = '__migrations') {
16
- const [rows] = await pool.execute(
17
- `SELECT filename FROM \`${tableName}\` ORDER BY id ASC`
18
- );
19
-
20
- return rows.map(row => row.filename);
21
- }
22
-
23
- export async function getMigrationFiles(migrationsDir) {
24
- const exists = await fileExists(migrationsDir);
25
- if (!exists) return [];
26
-
27
- const entries = await fs.readdir(migrationsDir);
28
-
29
- return entries
30
- .filter(f => f.endsWith('.sql'))
31
- .sort();
32
- }
33
-
34
- export function parseMigrationFile(content) {
35
- const upMarker = '-- UP';
36
- const downMarker = '-- DOWN';
37
- const upIndex = content.indexOf(upMarker);
38
- const downIndex = content.indexOf(downMarker);
39
-
40
- if (upIndex === -1) {
41
- return { up: content.trim(), down: '' };
42
- }
43
-
44
- const upStart = upIndex + upMarker.length;
45
- const upEnd = downIndex !== -1 ? downIndex : content.length;
46
- const up = content.slice(upStart, upEnd).trim();
47
- const down = downIndex !== -1 ? content.slice(downIndex + downMarker.length).trim() : '';
48
-
49
- return { up, down };
50
- }
51
-
52
- export async function applyMigration(pool, filename, upSql, tableName = '__migrations') {
53
- const connection = await pool.getConnection();
54
-
55
- try {
56
- await connection.beginTransaction();
57
-
58
- // Execute each statement separately (split on semicolons)
59
- const statements = splitStatements(upSql);
60
-
61
- for (const stmt of statements) {
62
- await connection.execute(stmt);
63
- }
64
-
65
- await connection.execute(
66
- `INSERT INTO \`${tableName}\` (filename) VALUES (?)`,
67
- [filename]
68
- );
69
-
70
- await connection.commit();
71
- } catch (error) {
72
- await connection.rollback();
73
- throw error;
74
- } finally {
75
- connection.release();
76
- }
77
- }
78
-
79
- export async function rollbackMigration(pool, filename, downSql, tableName = '__migrations') {
80
- const connection = await pool.getConnection();
81
-
82
- try {
83
- await connection.beginTransaction();
84
-
85
- const statements = splitStatements(downSql);
86
-
87
- for (const stmt of statements) {
88
- await connection.execute(stmt);
89
- }
90
-
91
- await connection.execute(
92
- `DELETE FROM \`${tableName}\` WHERE filename = ?`,
93
- [filename]
94
- );
95
-
96
- await connection.commit();
97
- } catch (error) {
98
- await connection.rollback();
99
- throw error;
100
- } finally {
101
- connection.release();
102
- }
103
- }
104
-
105
- function splitStatements(sql) {
106
- return sql
107
- .split(';')
108
- .map(s => s.trim())
109
- .filter(s => s.length > 0 && !s.startsWith('--'));
110
- }
1
+ import { readFile, fileExists } from '@stonyx/utils/file';
2
+ import path from 'path';
3
+ import fs from 'fs/promises';
4
+ import type { Pool, PoolConnection } from 'mysql2/promise';
5
+
6
+ interface ParsedMigration {
7
+ up: string;
8
+ down: string;
9
+ }
10
+
11
+ export async function ensureMigrationsTable(pool: Pool, tableName: string = '__migrations'): Promise<void> {
12
+ await pool.execute(`
13
+ CREATE TABLE IF NOT EXISTS \`${tableName}\` (
14
+ id INT AUTO_INCREMENT PRIMARY KEY,
15
+ filename VARCHAR(255) NOT NULL UNIQUE,
16
+ applied_at DATETIME DEFAULT CURRENT_TIMESTAMP
17
+ )
18
+ `);
19
+ }
20
+
21
+ export async function getAppliedMigrations(pool: Pool, tableName: string = '__migrations'): Promise<string[]> {
22
+ const [rows] = await pool.execute(
23
+ `SELECT filename FROM \`${tableName}\` ORDER BY id ASC`
24
+ ) as [Array<{ filename: string }>, unknown];
25
+
26
+ return rows.map(row => row.filename);
27
+ }
28
+
29
+ export async function getMigrationFiles(migrationsDir: string): Promise<string[]> {
30
+ const exists = await fileExists(migrationsDir);
31
+ if (!exists) return [];
32
+
33
+ const entries = await fs.readdir(migrationsDir);
34
+
35
+ return entries
36
+ .filter(f => f.endsWith('.sql'))
37
+ .sort();
38
+ }
39
+
40
+ export function parseMigrationFile(content: string): ParsedMigration {
41
+ const upMarker = '-- UP';
42
+ const downMarker = '-- DOWN';
43
+ const upIndex = content.indexOf(upMarker);
44
+ const downIndex = content.indexOf(downMarker);
45
+
46
+ if (upIndex === -1) {
47
+ return { up: content.trim(), down: '' };
48
+ }
49
+
50
+ const upStart = upIndex + upMarker.length;
51
+ const upEnd = downIndex !== -1 ? downIndex : content.length;
52
+ const up = content.slice(upStart, upEnd).trim();
53
+ const down = downIndex !== -1 ? content.slice(downIndex + downMarker.length).trim() : '';
54
+
55
+ return { up, down };
56
+ }
57
+
58
+ export async function applyMigration(pool: Pool, filename: string, upSql: string, tableName: string = '__migrations'): Promise<void> {
59
+ const connection = await pool.getConnection();
60
+
61
+ try {
62
+ await connection.beginTransaction();
63
+
64
+ // Execute each statement separately (split on semicolons)
65
+ const statements = splitStatements(upSql);
66
+
67
+ for (const stmt of statements) {
68
+ await connection.execute(stmt);
69
+ }
70
+
71
+ await connection.execute(
72
+ `INSERT INTO \`${tableName}\` (filename) VALUES (?)`,
73
+ [filename]
74
+ );
75
+
76
+ await connection.commit();
77
+ } catch (error) {
78
+ await connection.rollback();
79
+ throw error;
80
+ } finally {
81
+ connection.release();
82
+ }
83
+ }
84
+
85
+ export async function rollbackMigration(pool: Pool, filename: string, downSql: string, tableName: string = '__migrations'): Promise<void> {
86
+ const connection = await pool.getConnection();
87
+
88
+ try {
89
+ await connection.beginTransaction();
90
+
91
+ const statements = splitStatements(downSql);
92
+
93
+ for (const stmt of statements) {
94
+ await connection.execute(stmt);
95
+ }
96
+
97
+ await connection.execute(
98
+ `DELETE FROM \`${tableName}\` WHERE filename = ?`,
99
+ [filename]
100
+ );
101
+
102
+ await connection.commit();
103
+ } catch (error) {
104
+ await connection.rollback();
105
+ throw error;
106
+ } finally {
107
+ connection.release();
108
+ }
109
+ }
110
+
111
+ function splitStatements(sql: string): string[] {
112
+ return sql
113
+ .split(';')
114
+ .map(s => s.trim())
115
+ .filter(s => s.length > 0 && !s.startsWith('--'));
116
+ }