bunsql-native-migrate 0.1.0 → 0.1.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunsql-native-migrate",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Zero-ORM SQL file migrations for Bun: PostgreSQL, MySQL/MariaDB and SQLite through the built-in Bun.SQL client",
5
5
  "keywords": [
6
6
  "bun",
package/src/api/down.ts CHANGED
@@ -1,16 +1,13 @@
1
1
  import path from "node:path";
2
- import { getDatabaseUrl } from "../core/env.js";
3
- import { createDriver } from "../core/driver.js";
4
2
  import { resolveListDir } from "../core/fs.js";
5
3
  import { log } from "../core/console.js";
6
4
  import type { MigrateDownResult, MigrateOptions } from "./options.js";
5
+ import { runWithDriver } from "./run-with-driver.js";
7
6
 
8
7
  export async function migrateDown(options: MigrateOptions = {}): Promise<MigrateDownResult> {
9
- const url = getDatabaseUrl(options.databaseUrl);
10
8
  const listDir = resolveListDir(options.listDir);
11
- const driver = await createDriver(url);
12
9
 
13
- try {
10
+ return runWithDriver(options, async (driver) => {
14
11
  const executed = await driver.listExecuted();
15
12
  if (executed.length === 0) {
16
13
  log({ text: "No migrations to rollback.", type: "warn" });
@@ -31,7 +28,5 @@ export async function migrateDown(options: MigrateOptions = {}): Promise<Migrate
31
28
  await driver.remove(file);
32
29
  log({ text: `${file} rolled back`, type: "success" });
33
30
  return { reverted: file };
34
- } finally {
35
- await driver.close();
36
- }
31
+ });
37
32
  }
@@ -1,15 +1,10 @@
1
- import { getDatabaseUrl } from "../core/env.js";
2
- import { createDriver } from "../core/driver.js";
3
1
  import { log } from "../core/console.js";
4
2
  import type { MigrateOptions } from "./options.js";
3
+ import { runWithDriver } from "./run-with-driver.js";
5
4
 
6
5
  export async function installMigrations(options: MigrateOptions = {}): Promise<void> {
7
- const url = getDatabaseUrl(options.databaseUrl);
8
- const driver = await createDriver(url);
9
- try {
6
+ await runWithDriver(options, async (driver) => {
10
7
  await driver.install();
11
8
  log({ text: "Migration table created!", type: "success" });
12
- } finally {
13
- await driver.close();
14
- }
9
+ });
15
10
  }
@@ -0,0 +1,16 @@
1
+ import { getDatabaseUrl } from "../core/env.js";
2
+ import { createDriver, type MigrationDriver } from "../core/driver.js";
3
+ import type { MigrateOptions } from "./options.js";
4
+
5
+ export async function runWithDriver<T>(
6
+ options: MigrateOptions,
7
+ run: (driver: MigrationDriver) => Promise<T>,
8
+ ): Promise<T> {
9
+ const url = getDatabaseUrl(options.databaseUrl);
10
+ const driver = await createDriver(url);
11
+ try {
12
+ return await run(driver);
13
+ } finally {
14
+ await driver.close();
15
+ }
16
+ }
package/src/api/up.ts CHANGED
@@ -1,23 +1,20 @@
1
1
  import path from "node:path";
2
- import { getDatabaseUrl } from "../core/env.js";
3
- import { createDriver } from "../core/driver.js";
4
2
  import { checksumFile, listFiles, resolveListDir } from "../core/fs.js";
5
3
  import { log } from "../core/console.js";
6
4
  import { type MigrateOptions, type MigrateUpResult, ChecksumDriftError } from "./options.js";
5
+ import { runWithDriver } from "./run-with-driver.js";
7
6
 
8
7
  export async function migrateUp(options: MigrateOptions = {}): Promise<MigrateUpResult> {
9
- const url = getDatabaseUrl(options.databaseUrl);
10
8
  const listDir = resolveListDir(options.listDir);
11
- const driver = await createDriver(url);
12
9
 
13
- try {
10
+ return runWithDriver(options, async (driver) => {
14
11
  await driver.install();
15
12
 
16
13
  const allFiles = await listFiles(listDir, "js");
17
- const checksums = new Map<string, string>();
18
- for (const file of allFiles) {
19
- checksums.set(file, await checksumFile(path.join(listDir, file)));
20
- }
14
+ const checksumEntries = await Promise.all(
15
+ allFiles.map(async (file) => [file, await checksumFile(path.join(listDir, file))] as const),
16
+ );
17
+ const checksums = new Map(checksumEntries);
21
18
 
22
19
  const executed = await driver.listExecuted();
23
20
  const executedByName = new Map(executed.map((entry) => [entry.name, entry]));
@@ -68,7 +65,5 @@ export async function migrateUp(options: MigrateOptions = {}): Promise<MigrateUp
68
65
  }
69
66
 
70
67
  return { applied };
71
- } finally {
72
- await driver.close();
73
- }
68
+ });
74
69
  }