Skip to content

Commit fef802d

Browse files
committed
added option to explicitly and manually manage database transactions (begin, commit, rollback)
1 parent 8950721 commit fef802d

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ionic-database-builder",
3-
"version": "0.0.10",
3+
"version": "0.0.11",
44
"description": "Extended library from database-builder to assist in creating and maintaining SQL commands. Allowing integrate execute commands with SQLite ('@ionic-native/sqlite'), Web Sql, etc. Through the interface injection 'DatabaseCreatorContract' returning an implementation of 'DatabaseObject'.",
55
"main": "index.js",
66
"scripts": {

src/database.module.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ export class DatabaseModule {
4949
providers: Provider[],
5050
databaseMigrationContract?: Type<DatabaseMigrationContract>
5151
): ModuleWithProviders {
52-
console.log("databaseMigrationContract:");
53-
console.log(databaseMigrationContract);
5452
if (databaseMigrationContract) {
5553
providers.push({
5654
provide: DatabaseMigrationContract,

src/test/mapper.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,35 @@ describe('Mapper', () => {
104104
const database: Database = TestBed.get(Database);
105105
database.crud().then(crud => {
106106
const result = crud.insert(Cliente, clienteToSave).compile();
107-
expect(result.params.toString()).toEqual([1, 'Razão', 'Apelido', false, 2, 3].toString());
107+
expect(result.params.toString()).toEqual([
108+
clienteToSave.id, clienteToSave.razaoSocial, clienteToSave.apelido,
109+
clienteToSave.desativo, clienteToSave.cidade.id, clienteToSave.classificacao.id
110+
].toString());
108111
expect(result.query).toEqual('INSERT INTO Cliente (id, razaoSocial, apelido, desativo, cidade_id, classificacao_id) VALUES (?, ?, ?, ?, ?, ?)');
109112
});
110113
}));
111114

115+
it('Test transaction mapper insert T', async(() => {
116+
const database: Database = TestBed.get(Database);
117+
let rollback = () => {
118+
database.rollbackTransaction().then().catch();
119+
}
120+
database.beginTransaction().then(crud => {
121+
try {
122+
const result = crud.insert(Cliente, clienteToSave).compile();
123+
expect(result.params.toString()).toEqual([
124+
clienteToSave.id, clienteToSave.razaoSocial, clienteToSave.apelido,
125+
clienteToSave.desativo, clienteToSave.cidade.id, clienteToSave.classificacao.id
126+
].toString());
127+
expect(result.query).toEqual('INSERT INTO Cliente (id, razaoSocial, apelido, desativo, cidade_id, classificacao_id) VALUES (?, ?, ?, ?, ?, ?)');
128+
database.commitTransaction().then(x => {
129+
expect(x).toEqual(true);
130+
}).catch(rollback);
131+
}
132+
catch (e) {
133+
rollback();
134+
}
135+
});
136+
}));
137+
112138
});

src/utils/buildable-database-manager.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,37 @@ export abstract class BuildableDatabaseManager extends DatabaseManager {
5757
});
5858
}
5959

60+
public beginTransaction(): Promise<Crud> {
61+
return new Promise((resolve, reject) => {
62+
this.sql("BEGIN TRANSACTION").then(r => {
63+
this.crud().then(crud => {
64+
resolve(crud);
65+
}).catch(reject);
66+
}).catch(reject);
67+
});
68+
}
69+
70+
public commitTransaction(): Promise<boolean> {
71+
return new Promise((resolve, reject) => {
72+
this.sql("COMMIT").then(r => {
73+
resolve(true);
74+
}).catch(reject);
75+
});
76+
}
77+
78+
public rollbackTransaction(): Promise<boolean> {
79+
return new Promise((resolve, reject) => {
80+
this.sql("ROLLBACK").then(r => {
81+
resolve(true);
82+
}).catch(reject);
83+
});
84+
}
85+
6086
public crud(): Promise<Crud> {
6187
return new Promise((resolve, reject) => {
6288
this.databaseInstance().then(database => {
6389
resolve(new Crud(database, this._mapper, this.enableLog));
64-
})
65-
.catch(reject);
90+
}).catch(reject);
6691
});
6792
}
6893

@@ -78,7 +103,7 @@ export abstract class BuildableDatabaseManager extends DatabaseManager {
78103
resolve(cursor);
79104
});
80105
})
81-
.catch(reject);
106+
.catch(reject);
82107
});
83108
}
84109

0 commit comments

Comments
 (0)