Skip to content

Commit

Permalink
fix: DataSource.setOptions doesn't properly update the database in th…
Browse files Browse the repository at this point in the history
…e drivers (#9635)

* make sure we update database in the driver if it was dynamically set (via dataSource.setOptions)

* removed validation for database since we definitely shouldn't have it in the constructor, database can be set later on (e.g. datasource.setOptions)
  • Loading branch information
pleerock committed Dec 18, 2022
1 parent 8251812 commit a95bed7
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 49 deletions.
11 changes: 11 additions & 0 deletions src/data-source/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ export class DataSource {
this.queryResultCache = new QueryResultCacheFactory(this).create()
}

// todo: we must update the database in the driver as well, if it was set by setOptions method
// in the future we need to refactor the code and remove "database" from the driver, and instead
// use database (and options) from a single place - data source.
if (options.database) {
this.driver.database = DriverUtils.buildDriverOptions(
this.options,
).database
}

// todo: need to take a look if we need to update schema and other "poor" properties

return this
}

Expand Down
5 changes: 0 additions & 5 deletions src/driver/better-sqlite3/BetterSqlite3Driver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import mkdirp from "mkdirp"
import path from "path"
import { DriverPackageNotInstalledError } from "../../error"
import { DriverOptionNotSetError } from "../../error"
import { PlatformTools } from "../../platform/PlatformTools"
import { DataSource } from "../../data-source"
import { ColumnType } from "../types/ColumnTypes"
Expand Down Expand Up @@ -41,10 +40,6 @@ export class BetterSqlite3Driver extends AbstractSqliteDriver {
this.options = connection.options as BetterSqlite3ConnectionOptions
this.database = this.options.database

// validate options to make sure everything is set
if (!this.options.database)
throw new DriverOptionNotSetError("database")

// load sqlite package
this.loadDependencies()
}
Expand Down
11 changes: 1 addition & 10 deletions src/driver/capacitor/CapacitorDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { CapacitorConnectionOptions } from "./CapacitorConnectionOptions"
import { CapacitorQueryRunner } from "./CapacitorQueryRunner"
import { QueryRunner } from "../../query-runner/QueryRunner"
import { DataSource } from "../../data-source/DataSource"
import {
DriverOptionNotSetError,
DriverPackageNotInstalledError,
} from "../../error"
import { DriverPackageNotInstalledError } from "../../error"
import { ReplicationMode } from "../types/ReplicationMode"

export class CapacitorDriver extends AbstractSqliteDriver {
Expand All @@ -23,12 +20,6 @@ export class CapacitorDriver extends AbstractSqliteDriver {
this.database = this.options.database
this.driver = this.options.driver

// validate options to make sure everything is set
if (!this.options.database)
throw new DriverOptionNotSetError("database")

if (!this.options.driver) throw new DriverOptionNotSetError("driver")

// load sqlite package
this.sqlite = this.options.driver
}
Expand Down
8 changes: 0 additions & 8 deletions src/driver/cordova/CordovaDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { CordovaConnectionOptions } from "./CordovaConnectionOptions"
import { CordovaQueryRunner } from "./CordovaQueryRunner"
import { QueryRunner } from "../../query-runner/QueryRunner"
import { DataSource } from "../../data-source/DataSource"
import { DriverOptionNotSetError } from "../../error/DriverOptionNotSetError"
import { DriverPackageNotInstalledError } from "../../error/DriverPackageNotInstalledError"
import { ReplicationMode } from "../types/ReplicationMode"

Expand All @@ -30,13 +29,6 @@ export class CordovaDriver extends AbstractSqliteDriver {
// this.options = connection.options as CordovaConnectionOptions;
this.database = this.options.database

// validate options to make sure everything is set
if (!this.options.database)
throw new DriverOptionNotSetError("database")

if (!this.options.location)
throw new DriverOptionNotSetError("location")

// load sqlite package
this.loadDependencies()
}
Expand Down
7 changes: 0 additions & 7 deletions src/driver/expo/ExpoDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ExpoConnectionOptions } from "./ExpoConnectionOptions"
import { ExpoQueryRunner } from "./ExpoQueryRunner"
import { QueryRunner } from "../../query-runner/QueryRunner"
import { DataSource } from "../../data-source/DataSource"
import { DriverOptionNotSetError } from "../../error/DriverOptionNotSetError"
import { ReplicationMode } from "../types/ReplicationMode"

export class ExpoDriver extends AbstractSqliteDriver {
Expand All @@ -18,12 +17,6 @@ export class ExpoDriver extends AbstractSqliteDriver {

this.database = this.options.database

// validate options to make sure everything is set
if (!this.options.database)
throw new DriverOptionNotSetError("database")

if (!this.options.driver) throw new DriverOptionNotSetError("driver")

// load sqlite package
this.sqlite = this.options.driver
}
Expand Down
6 changes: 0 additions & 6 deletions src/driver/nativescript/NativescriptDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { NativescriptConnectionOptions } from "./NativescriptConnectionOptions"
import { NativescriptQueryRunner } from "./NativescriptQueryRunner"
import { QueryRunner } from "../../query-runner/QueryRunner"
import { DataSource } from "../../data-source/DataSource"
import { DriverOptionNotSetError } from "../../error/DriverOptionNotSetError"
import { DriverPackageNotInstalledError } from "../../error/DriverPackageNotInstalledError"
import { ColumnType } from "../types/ColumnTypes"
import { ReplicationMode } from "../types/ReplicationMode"
Expand Down Expand Up @@ -40,11 +39,6 @@ export class NativescriptDriver extends AbstractSqliteDriver {
this.database = this.options.database
this.driver = this.options.driver

// validate options to make sure everything is set
if (!this.options.database) {
throw new DriverOptionNotSetError("database")
}

// load sqlite package
this.loadDependencies()
}
Expand Down
8 changes: 0 additions & 8 deletions src/driver/react-native/ReactNativeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ReactNativeConnectionOptions } from "./ReactNativeConnectionOptions"
import { ReactNativeQueryRunner } from "./ReactNativeQueryRunner"
import { QueryRunner } from "../../query-runner/QueryRunner"
import { DataSource } from "../../data-source/DataSource"
import { DriverOptionNotSetError } from "../../error/DriverOptionNotSetError"
import { DriverPackageNotInstalledError } from "../../error/DriverPackageNotInstalledError"
import { ReplicationMode } from "../types/ReplicationMode"

Expand All @@ -19,13 +18,6 @@ export class ReactNativeDriver extends AbstractSqliteDriver {

this.database = this.options.database

// validate options to make sure everything is set
if (!this.options.database)
throw new DriverOptionNotSetError("database")

if (!this.options.location)
throw new DriverOptionNotSetError("location")

// load sqlite package
this.loadDependencies()
}
Expand Down
5 changes: 0 additions & 5 deletions src/driver/sqlite/SqliteDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import mkdirp from "mkdirp"
import path from "path"
import { DriverPackageNotInstalledError } from "../../error/DriverPackageNotInstalledError"
import { SqliteQueryRunner } from "./SqliteQueryRunner"
import { DriverOptionNotSetError } from "../../error/DriverOptionNotSetError"
import { PlatformTools } from "../../platform/PlatformTools"
import { DataSource } from "../../data-source/DataSource"
import { SqliteConnectionOptions } from "./SqliteConnectionOptions"
Expand Down Expand Up @@ -41,10 +40,6 @@ export class SqliteDriver extends AbstractSqliteDriver {
this.options = connection.options as SqliteConnectionOptions
this.database = this.options.database

// validate options to make sure everything is set
if (!this.options.database)
throw new DriverOptionNotSetError("database")

// load sqlite package
this.loadDependencies()
}
Expand Down

0 comments on commit a95bed7

Please sign in to comment.