stabilize-orm 1.1.5 → 1.1.6

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 (2) hide show
  1. package/migrations.ts +24 -43
  2. package/package.json +1 -1
package/migrations.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  * @file migrations.ts
3
3
  * @description Contains functions for generating and running database migrations based on model metadata.
4
4
  * @author ElectronSz
5
+ * @date 2025-10-15 20:55:49
5
6
  */
6
7
 
7
8
  import { DBClient } from "./client";
@@ -12,6 +13,23 @@ type ColumnData = { name: string; type: string };
12
13
  type ColumnMetadata = Record<string, ColumnData>;
13
14
  type ValidatorMetadata = Record<string, string[]>;
14
15
 
16
+ // --- FIX: New Helper Function to format queries for different DBs ---
17
+ /**
18
+ * @internal
19
+ * Formats a SQL query with placeholders for the target database dialect.
20
+ * Replaces '?' with '$1', '$2', etc. for PostgreSQL.
21
+ * @param query The SQL query string with '?' placeholders.
22
+ * @param dbType The target database dialect.
23
+ * @returns The formatted SQL query string.
24
+ */
25
+ function formatQuery(query: string, dbType: DBType): string {
26
+ if (dbType === DBType.Postgres) {
27
+ let paramIndex = 1;
28
+ return query.replace(/\?/g, () => `$${paramIndex++}`);
29
+ }
30
+ return query;
31
+ }
32
+
15
33
  /**
16
34
  * @internal
17
35
  * Gets the database-specific SQL for an auto-incrementing primary key.
@@ -74,23 +92,6 @@ function getTimestampDefault(dbType: DBType): string {
74
92
  * @param name A descriptive name for the migration (used for the migration object).
75
93
  * @param dbType The target database dialect to generate SQL for. Defaults to Postgres.
76
94
  * @returns A promise that resolves to a `Migration` object containing the `up` and `down` SQL scripts.
77
- * @example
78
- * ```
79
- * // In a script like 'scripts/generate_user_migration.ts'
80
- * import { generateMigration, DBType } from 'stabilize-orm';
81
- * import { User } from './models/user';
82
- * import fs from 'fs';
83
- *
84
- * async function createMigration() {
85
- * const migration = await generateMigration(User, 'create_users_table', DBType.Postgres);
86
- * fs.writeFileSync(
87
- * `migrations/${new Date().getTime()}_create_users.json`,
88
- * JSON.stringify(migration, null, 2)
89
- * );
90
- * }
91
- *
92
- * createMigration();
93
- * ```
94
95
  */
95
96
  export async function generateMigration(
96
97
  model: new (...args: any[]) => any,
@@ -180,24 +181,6 @@ function getMigrationsTableSQL(dbType: DBType): string {
180
181
  *
181
182
  * @param config The database configuration object.
182
183
  * @param migrations An array of `Migration` objects to be executed.
183
- * @example
184
- * ```
185
- * // In a script like 'scripts/run_all_migrations.ts'
186
- * import { runMigrations } from 'stabilize-orm';
187
- * import { dbConfig } from './config';
188
- * import migration1 from '../migrations/1_create_users.json';
189
- * import migration2 from '../migrations.ts/2_create_profiles.json';
190
- *
191
- * const allMigrations = [migration1, migration2];
192
- *
193
- * async function applyMigrations() {
194
- * console.log('Starting migration process...');
195
- * await runMigrations(dbConfig, allMigrations);
196
- * console.log('All pending migrations applied successfully.');
197
- * }
198
- *
199
- * applyMigrations();
200
- * ```
201
184
  */
202
185
  export async function runMigrations(config: DBConfig, migrations: Migration[]) {
203
186
  const client = new DBClient(config);
@@ -208,10 +191,8 @@ export async function runMigrations(config: DBConfig, migrations: Migration[]) {
208
191
  for (const [index, migration] of migrations.entries()) {
209
192
  const name = migration.name || `migration_${index}_${new Date().getTime()}`;
210
193
 
211
- const applied = await client.query<{ id: number }>(
212
- `SELECT id FROM migrations WHERE name = ?`,
213
- [name],
214
- );
194
+ const selectQuery = formatQuery(`SELECT id FROM migrations WHERE name = ?`, dbType);
195
+ const applied = await client.query<{ id: number }>(selectQuery, [name]);
215
196
 
216
197
  if (applied.length === 0) {
217
198
  await client.transaction(async (txClient) => {
@@ -219,10 +200,10 @@ export async function runMigrations(config: DBConfig, migrations: Migration[]) {
219
200
  for (const query of migration.up) {
220
201
  await txClient.query(query);
221
202
  }
222
- await txClient.query(
223
- `INSERT INTO migrations (name, applied_at) VALUES (?, ?)`,
224
- [name, new Date().toISOString()],
225
- );
203
+
204
+ const insertQuery = formatQuery(`INSERT INTO migrations (name, applied_at) VALUES (?, ?)`, dbType);
205
+ await txClient.query(insertQuery, [name, new Date().toISOString()]);
206
+
226
207
  console.log(`Migration ${name} applied successfully.`);
227
208
  });
228
209
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stabilize-orm",
3
- "version": "1.1.5",
3
+ "version": "1.1.6",
4
4
  "description": "A lightweight, type-safe ORM for Bun.js with support for SQLite, MySQL, PostgreSQL, and Redis caching",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",