@flowblade/sqlduck 0.34.0 → 0.35.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.
- package/dist/index.d.mts +8 -0
- package/dist/index.mjs +36 -7
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -381,6 +381,14 @@ declare class DuckDatabaseManager {
|
|
|
381
381
|
analyze: () => Promise<boolean>;
|
|
382
382
|
checkpoint: (dbAlias: string) => Promise<boolean>;
|
|
383
383
|
vacuum: () => Promise<boolean>;
|
|
384
|
+
getCurrentDatabase: () => Promise<string | null>;
|
|
385
|
+
getCurrentSchema: () => Promise<string | null>;
|
|
386
|
+
getCurrentCatalog: () => Promise<string | null>;
|
|
387
|
+
/**
|
|
388
|
+
* Set the default database to use
|
|
389
|
+
* @throws if the database alias does not exist
|
|
390
|
+
*/
|
|
391
|
+
use: (dbAlias: string) => Promise<true>;
|
|
384
392
|
/**
|
|
385
393
|
* Helper to create an initial database file.
|
|
386
394
|
*/
|
package/dist/index.mjs
CHANGED
|
@@ -425,22 +425,33 @@ var DuckDatabaseManager = class {
|
|
|
425
425
|
*/
|
|
426
426
|
attach = async (dbParams, options) => {
|
|
427
427
|
const params = duckConnectionParamsZodSchema.parse(dbParams);
|
|
428
|
-
const rawSql = new DuckDatabaseAttachCommand(params, options).getRawSql();
|
|
429
|
-
const
|
|
428
|
+
const rawSql = new DuckDatabaseAttachCommand(params, { behaviour: options?.behaviour }).getRawSql();
|
|
429
|
+
const restoreCurrentDb = await this.getCurrentDatabase();
|
|
430
|
+
const tempDbNameHack = "temp_run_detach_if_attached_hack_db";
|
|
431
|
+
let isTempDbNameHackCreated = false;
|
|
430
432
|
try {
|
|
431
|
-
await this.#executor.getRowObjectsJS([
|
|
432
|
-
`attach(${params.alias}`,
|
|
433
|
-
behaviour ?? null,
|
|
434
|
-
")"
|
|
435
|
-
].filter(Boolean).join(","), rawSql);
|
|
433
|
+
await this.#executor.getRowObjectsJS([`attach(${params.alias}`, options === void 0 ? null : JSON.stringify(options)].filter(Boolean).join(",") + ")", rawSql);
|
|
436
434
|
} catch (e) {
|
|
437
435
|
if (options?.behaviour === "OR REPLACE" && options?.runDetachIfAttachOrReplaceFailWithAlreadyAttached === true) {
|
|
438
436
|
const alreadyAttached = getAlreadyAttachedDatabaseFromError(e);
|
|
439
437
|
if (alreadyAttached.isAlreadyAttached === true) {
|
|
438
|
+
if (alreadyAttached.dbAlias === restoreCurrentDb) {
|
|
439
|
+
await this.attachIfNotExists({
|
|
440
|
+
type: "memory",
|
|
441
|
+
alias: tempDbNameHack
|
|
442
|
+
});
|
|
443
|
+
await this.use(tempDbNameHack);
|
|
444
|
+
isTempDbNameHackCreated = true;
|
|
445
|
+
}
|
|
440
446
|
await this.detachOrIgnore(alreadyAttached.dbAlias);
|
|
441
447
|
await this.attach(dbParams);
|
|
442
448
|
} else throw e;
|
|
443
449
|
} else throw e;
|
|
450
|
+
} finally {
|
|
451
|
+
if (restoreCurrentDb !== null && isTempDbNameHackCreated) {
|
|
452
|
+
await this.use(restoreCurrentDb);
|
|
453
|
+
await this.detachOrIgnore(tempDbNameHack);
|
|
454
|
+
}
|
|
444
455
|
}
|
|
445
456
|
return new Database({ alias: params.alias });
|
|
446
457
|
};
|
|
@@ -576,6 +587,24 @@ var DuckDatabaseManager = class {
|
|
|
576
587
|
await this.#executor.getRowObjectsJS("vacuum()", "VACUUM");
|
|
577
588
|
return true;
|
|
578
589
|
};
|
|
590
|
+
getCurrentDatabase = async () => {
|
|
591
|
+
return (await this.#executor.getRowObjectsJS("getCurrentDatabase()", "SELECT current_database() as current_database"))[0]?.current_database ?? null;
|
|
592
|
+
};
|
|
593
|
+
getCurrentSchema = async () => {
|
|
594
|
+
return (await this.#executor.getRowObjectsJS("getCurrentSchema()", "SELECT current_schema() as current_schema"))?.[0]?.current_schema ?? null;
|
|
595
|
+
};
|
|
596
|
+
getCurrentCatalog = async () => {
|
|
597
|
+
return (await this.#executor.getRowObjectsJS("getCurrentCatalog()", "SELECT current_catalog() as current_catalog"))?.[0]?.current_catalog ?? null;
|
|
598
|
+
};
|
|
599
|
+
/**
|
|
600
|
+
* Set the default database to use
|
|
601
|
+
* @throws if the database alias does not exist
|
|
602
|
+
*/
|
|
603
|
+
use = async (dbAlias) => {
|
|
604
|
+
const safeAlias = duckValidatorsZod.aliasName.parse(dbAlias);
|
|
605
|
+
await this.#executor.getRowObjectsJS(`use(${safeAlias})`, `USE ${safeAlias}`);
|
|
606
|
+
return true;
|
|
607
|
+
};
|
|
579
608
|
/**
|
|
580
609
|
* Helper to create an initial database file.
|
|
581
610
|
*/
|