Skip to content

Commit 028aff7

Browse files
authored
Merge pull request #1 from GeoTecINIT/nsql-fix
Fix conflicts with plugins using nSQL
2 parents 4b97f1b + 49a0820 commit 028aff7

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,19 @@ Before requesting user's current location some options can be customized in orde
332332
| acquireLocation(options: [AcquireOptions](#geolocation-acquire-options) | `Promise<Geolocation>` | Allows to obtain user's current location |
333333
| locationStream(options: [StreamOptions](#geolocation-stream-options) | `Observable<Geolocation>` | Allows to actively obtain user's location updates |
334334

335+
## Known issues
336+
337+
### nanoSQL2
338+
339+
If your application depends on [nanoSQL 2](https://www.npmjs.com/package/@nano-sql/adapter-sqlite-nativescript) for data persistence, you should check which database is in use (and change it, if applicable) before running a query against your database. You can do it as follows:
340+
341+
```ts
342+
if (nSQL().selectedDB !== dbName) {
343+
nSQL().useDatabase(dbName);
344+
}
345+
nSQL(tableName).query(...);
346+
```
347+
335348
## Plugin authors
336349

337350
<a href="https://github.com/agonper" title="Alberto González Pérez">

src/internal/activity-recognition/recognizers/state/store.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { nSQL } from "@nano-sql/core/lib";
2-
31
import { HumanActivity, Resolution } from "../../index";
42
import { pluginDb } from "../../../persistence/plugin-db";
53
import { recognizersStateModel } from "./model";
@@ -19,8 +17,8 @@ class RecognizersStateStoreDb implements RecognizerStateStore {
1917
private tableName = recognizersStateModel.name;
2018

2119
async isActive(recognizer: Resolution): Promise<boolean> {
22-
await pluginDb.createDB();
23-
const rows = await nSQL(this.tableName)
20+
const instance = await this.db();
21+
const rows = await instance
2422
.query("select")
2523
.where(["id", "=", recognizer])
2624
.exec();
@@ -39,8 +37,8 @@ class RecognizersStateStoreDb implements RecognizerStateStore {
3937
}
4038

4139
async getLastActivity(recognizer: Resolution): Promise<HumanActivity> {
42-
await pluginDb.createDB();
43-
const rows = await nSQL(this.tableName)
40+
const instance = await this.db();
41+
const rows = await instance
4442
.query("select")
4543
.where(["id", "=", recognizer])
4644
.exec();
@@ -55,23 +53,27 @@ class RecognizersStateStoreDb implements RecognizerStateStore {
5553
recognizer: Resolution,
5654
activity: HumanActivity
5755
): Promise<void> {
58-
await pluginDb.createDB();
5956
const isActive = await this.isActive(recognizer);
6057
if (!isActive) {
6158
return null;
6259
}
63-
await nSQL(`${this.tableName}.lastActivity`)
60+
const instance = await this.db(`${this.tableName}.lastActivity`);
61+
await instance
6462
.query("upsert", activity)
6563
.where(["id", "=", recognizer])
6664
.exec();
6765
}
6866

6967
private async updateStatus(recognizer: Resolution, active: boolean) {
70-
await pluginDb.createDB();
71-
await nSQL(this.tableName)
68+
const instance = await this.db();
69+
await instance
7270
.query("upsert", { id: recognizer, active, lastActivity: null })
7371
.exec();
7472
}
73+
74+
private db(tableName = this.tableName) {
75+
return pluginDb.instance(tableName);
76+
}
7577
}
7678

7779
export const recognizersStateStoreDb = new RecognizersStateStoreDb();

src/internal/persistence/plugin-db.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ class PluginDb {
2222
await this.createDBProcedure;
2323
this.dbInitialized = true;
2424
}
25+
26+
async instance(tableName: string) {
27+
await this.createDB();
28+
if (nSQL().selectedDB !== dbName) {
29+
nSQL().useDatabase(dbName);
30+
}
31+
return nSQL(tableName);
32+
}
2533
}
2634

2735
export const pluginDb = new PluginDb();

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-context-apis",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Painless access to contextual information for your NativeScript apps",
55
"main": "context-apis",
66
"typings": "index.d.ts",
Binary file not shown.

0 commit comments

Comments
 (0)