Skip to content

Commit

Permalink
new onTransaction method
Browse files Browse the repository at this point in the history
  • Loading branch information
juanchinovas committed May 26, 2022
1 parent 0994d8c commit 559d419
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tns plugin add [email protected]
`@nativescript/core?`

```bash
tns plugin add nativescript-sqlite-access@1.1.0
tns plugin add nativescript-sqlite-access@1.2.0
```
The command above automatically installs the necessary files, as well as stores nativescript-sqlite-access as a dependency in your project's package.json file.

Expand Down
50 changes: 46 additions & 4 deletions demo/app/tests/insert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("#insert()", () => {

describe("Transaction", () => {
describe("Commit", () => {
it("shoould committed the insert", () => {
it("should committed the insert", () => {
database.beginTransact();
const insertedId = database.insert(databaseTables.PERSONS, {
name: "Power Ranger"
Expand All @@ -44,7 +44,7 @@ describe("#insert()", () => {
});
});
describe("Roollback", () => {
it("shoould rollback the inserts", async () => {
it("should rollback the inserts", async () => {
database.beginTransact();
database.insert(databaseTables.PERSONS, {
name: "Power Ranger 1"
Expand All @@ -57,14 +57,56 @@ describe("#insert()", () => {
});
database.rollback();

const resutls = await database.select(`SELECT COUNT(*) account FROM ${databaseTables.PERSONS}`)
const results = await database.select(`SELECT name FROM ${databaseTables.PERSONS}`)
.process() as Array<Record<string, unknown>>;

expect(
resutls.filter( p => [ "Power Ranger 1", "Power Ranger 2", "Power Ranger 3" ].includes(p.name as string))
results.filter( p => [ "Power Ranger 1", "Power Ranger 2", "Power Ranger 3" ].includes(p.name as string))
).to.deep.equal([]);
});
});
describe("onTransaction", () => {
it("should commit the inserts", async () => {
database.onTransaction(() => {
database.insert(databaseTables.PERSONS, {
name: "Power Ranger commit1"
});
database.insert(databaseTables.PERSONS, {
name: "Power Ranger commit2"
});
database.insert(databaseTables.PERSONS, {
name: "Power Ranger commit3"
});
});

const results = await database.select<Array<Record<string, unknown>>>(`SELECT name FROM ${databaseTables.PERSONS}`)
.process();

expect(
results.map( p => p.name)
).to.deep.equal([ "Power Ranger", "Power Ranger commit1", "Power Ranger commit2", "Power Ranger commit3" ]);
});
it("should throw on rollback", async () => {
expect(
() => database.onTransaction(() => {
database.insert(databaseTables.PERSONS, {
name: "Power Ranger rollback1"
});
database.insert(databaseTables.PERSONS, {
name: "Power Ranger rollback2"
});

database.insert(databaseTables.PERSONS, {
_id: "Power Ranger rollback3"
});

database.insert(databaseTables.PERSONS, {
name: "Power Ranger rollback3"
});
})
).to.throws()
});
});
});

after(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-sqlite-access",
"version": "1.1.0",
"version": "1.2.0",
"description": "NativeScript plugin to access and manage sqlite data on Android & iOS",
"main": "sqlite-access",
"typings": "index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/sqlite-access.android.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ declare class SqliteAccess implements IDatabase {
rollback(): void;
close(): void;
isClose(): boolean;
onTransaction<T>(callback: () => T): T;
}
export declare function DbBuilder(dbName: string, options?: DbCreationOptions): SqliteAccess;
export * from "./sqlite-access.common";
12 changes: 12 additions & 0 deletions src/sqlite-access.android.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/sqlite-access.android.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/sqlite-access.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class SqliteAccess implements IDatabase {
*/
constructor(private db: android.database.sqlite.SQLiteDatabase, private returnType: ReturnType) { }


/**
* Insert a row into table with the values (key = columns and values = columns value)
*
Expand Down Expand Up @@ -214,6 +215,19 @@ class SqliteAccess implements IDatabase {
isClose(): boolean {
return this.db === null;
}

onTransaction<T>(callback: () => T): T {
try {
this.beginTransact();
const result: T = callback();
this.commit();

return result;
} catch (error) {
this.rollback();
throw error;
}
}
}

/** private function
Expand Down
Loading

0 comments on commit 559d419

Please sign in to comment.