Skip to content

Commit

Permalink
Merge pull request typeorm#2854 from championswimmer/fix_nativescript…
Browse files Browse the repository at this point in the history
…_driver

unbreak release: externalise nativescript
  • Loading branch information
pleerock committed Sep 27, 2018
2 parents d0adc66 + 56c75c9 commit bcd5d82
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
14 changes: 14 additions & 0 deletions docs/supported-platforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ TypeORM is able to run on Expo apps using the [Expo SQLite API](https://docs.exp

1. `tns install webpack` (read below why webpack is required)
2. `tns plugin add nativescript-sqlite`
3. Create Database connetion in your app's entry point
```typescript
import driver from 'nativescript-sqlite'

const connection = await createConnection({
database: 'test.db',
type: 'nativescript',
driver,
entities: [
Todo //... whatver entities you have
],
logging: true
})
```

Note: This works only with NativeScript 4.x and above

Expand Down
8 changes: 7 additions & 1 deletion src/driver/nativescript/NativescriptConnectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ export interface NativescriptConnectionOptions extends BaseConnectionOptions {
*/
readonly database: string;

}
/**
* The driver object
* you should pass `require('nativescript-sqlite') here
*/
readonly driver: any;

}
22 changes: 15 additions & 7 deletions src/driver/nativescript/NativescriptDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ export class NativescriptDriver extends AbstractSqliteDriver {
*/
options: NativescriptConnectionOptions;

/**
* Nativescript driver module
* this is most likely `nativescript-sqlite`
* but user can pass his own
*/
driver: any;

// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
Expand All @@ -30,10 +37,12 @@ export class NativescriptDriver extends AbstractSqliteDriver {
this.connection = connection;
this.options = connection.options as NativescriptConnectionOptions;
this.database = this.options.database;
this.driver = this.options.driver;

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

// load sqlite package
this.loadDependencies();
Expand All @@ -58,8 +67,9 @@ export class NativescriptDriver extends AbstractSqliteDriver {
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master"|"slave" = "master"): QueryRunner {
if (!this.queryRunner)
if (!this.queryRunner) {
this.queryRunner = new NativescriptQueryRunner(this);
}

return this.queryRunner;
}
Expand Down Expand Up @@ -106,11 +116,9 @@ export class NativescriptDriver extends AbstractSqliteDriver {
* If driver dependency is not given explicitly, then try to load it via "require".
*/
protected loadDependencies(): void {
try {
this.sqlite = require("nativescript-sqlite");

} catch (e) {
this.sqlite = this.driver
if (!this.driver) {
throw new DriverPackageNotInstalledError("Nativescript", "nativescript-sqlite");
}
}
}
}

0 comments on commit bcd5d82

Please sign in to comment.