Comparing version 0.1.4 to 0.1.5
@@ -0,1 +1,5 @@ | ||
#0.1.5 | ||
* Added transaction, commit and rollback method | ||
#0.1.4 | ||
@@ -2,0 +6,0 @@ |
@@ -37,2 +37,11 @@ import { Model, ModelConstructor } from './model'; | ||
query(query: string, extras?: any): Promise<any>; | ||
/** | ||
* Starts the transaction | ||
* @param callback | ||
*/ | ||
transaction(callback: Function): Promise<any>; | ||
/** Commit a transaction */ | ||
commit(): Promise<any>; | ||
/** Rollback a transaction */ | ||
rollback(): Promise<any>; | ||
/** Escape any mysql injection string */ | ||
@@ -39,0 +48,0 @@ escape(data: string): string; |
@@ -5,2 +5,3 @@ "use strict"; | ||
const mysql = require('promise-mysql'); | ||
const promisify = require('es6-promisify'); | ||
/** | ||
@@ -35,2 +36,17 @@ * Main class to be instantiated to connect to the database | ||
} | ||
/** | ||
* Starts the transaction | ||
* @param callback | ||
*/ | ||
transaction(callback) { | ||
return this.conn.beginTransaction(callback); | ||
} | ||
/** Commit a transaction */ | ||
commit() { | ||
return promisify(this.conn.commit, this.conn); | ||
} | ||
/** Rollback a transaction */ | ||
rollback() { | ||
return promisify(this.conn.rollback, this.conn); | ||
} | ||
/** Escape any mysql injection string */ | ||
@@ -37,0 +53,0 @@ escape(data) { |
@@ -46,12 +46,14 @@ "use strict"; | ||
getAll(wheres, options) { | ||
let statement = 'SELECT * FROM ' + this.table; | ||
if (wheres && (wheres.offset || wheres.limit)) { | ||
statement = helpers_1.mapOptionClause(statement, wheres); | ||
} | ||
else if (wheres && !wheres.offset && !wheres.limit) { | ||
statement = helpers_1.mapWhereClause(statement, wheres); | ||
if (options) | ||
statement = helpers_1.mapOptionClause(statement, options); | ||
} | ||
return this.db.query(statement); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let statement = 'SELECT * FROM ' + this.table; | ||
if (wheres && (wheres.offset || wheres.limit)) { | ||
statement = helpers_1.mapOptionClause(statement, wheres); | ||
} | ||
else if (wheres && !wheres.offset && !wheres.limit) { | ||
statement = helpers_1.mapWhereClause(statement, wheres); | ||
if (options) | ||
statement = helpers_1.mapOptionClause(statement, options); | ||
} | ||
return this.db.query(statement); | ||
}); | ||
} | ||
@@ -64,7 +66,9 @@ /** | ||
save(data) { | ||
let statement = [data.id ? 'UPDATE' : 'INSERT INTO', this.table, 'SET ?'].join(' '); | ||
if (data.id) | ||
statement += ' WHERE id = ?'; | ||
let queryData = data.id ? [data, data.id] : data; | ||
return this.db.query(statement, queryData); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let statement = [data.id ? 'UPDATE' : 'INSERT INTO', this.table, 'SET ?'].join(' '); | ||
if (data.id) | ||
statement += ' WHERE id = ?'; | ||
let queryData = data.id ? [data, data.id] : data; | ||
return this.db.query(statement, queryData); | ||
}); | ||
} | ||
@@ -77,19 +81,21 @@ /** | ||
saveAll(items) { | ||
let statement = 'INSERT INTO ' + this.table; | ||
const columns = Object.keys(items[0]); | ||
statement += ' (' + columns.toString() + ') VALUES ?'; | ||
let data = items.map(item => { | ||
return columns.map(field => item[field]); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let statement = 'INSERT INTO ' + this.table; | ||
const columns = Object.keys(items[0]); | ||
statement += ' (' + columns.toString() + ') VALUES ?'; | ||
let data = items.map(item => { | ||
return columns.map(field => item[field]); | ||
}); | ||
if (columns.indexOf('id') > -1) { | ||
statement += ' ON DUPLICATE KEY UPDATE '; | ||
const noIdColumns = columns.filter(field => field !== 'id'); | ||
for (let i = 0; i < noIdColumns.length; i++) { | ||
statement += noIdColumns[i] + ' = VALUES(' + noIdColumns[i] + ')'; | ||
if (noIdColumns.length - i !== 1) | ||
statement += ', '; | ||
} | ||
; | ||
} | ||
return this.db.query(statement, [data]); | ||
}); | ||
if (columns.indexOf('id') > -1) { | ||
statement += ' ON DUPLICATE KEY UPDATE '; | ||
const noIdColumns = columns.filter(field => field !== 'id'); | ||
for (let i = 0; i < noIdColumns.length; i++) { | ||
statement += noIdColumns[i] + ' = VALUES(' + noIdColumns[i] + ')'; | ||
if (noIdColumns.length - i !== 1) | ||
statement += ', '; | ||
} | ||
; | ||
} | ||
return this.db.query(statement, [data]); | ||
} | ||
@@ -101,7 +107,9 @@ /** Delete an item from the db | ||
delete(wheres) { | ||
if (!wheres) | ||
throw new Error(exports.errors.where); | ||
let statement = 'DELETE FROM ' + this.table; | ||
statement = helpers_1.mapWhereClause(statement, wheres); | ||
return this.db.query(statement); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!wheres) | ||
throw new Error(exports.errors.where); | ||
let statement = 'DELETE FROM ' + this.table; | ||
statement = helpers_1.mapWhereClause(statement, wheres); | ||
return this.db.query(statement); | ||
}); | ||
} | ||
@@ -108,0 +116,0 @@ } |
{ | ||
"name": "classql", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"main": "dist/index.js", | ||
@@ -20,2 +20,3 @@ "bugs": "https://github.com/sorakthunly/classql/issues", | ||
"dependencies": { | ||
"es6-promisify": "^5.0.0", | ||
"promise-mysql": "^3.0.1", | ||
@@ -22,0 +23,0 @@ "reflect-metadata": "^0.1.10", |
@@ -5,2 +5,3 @@ import { Model, ModelConstructor } from './model'; | ||
const mysql = require('promise-mysql'); | ||
const promisify = require('es6-promisify'); | ||
@@ -53,2 +54,20 @@ /** Default MySQL connection option */ | ||
/** | ||
* Starts the transaction | ||
* @param callback | ||
*/ | ||
public transaction(callback: Function): Promise<any> { | ||
return this.conn.beginTransaction(callback); | ||
} | ||
/** Commit a transaction */ | ||
public commit(): Promise<any> { | ||
return promisify(this.conn.commit, this.conn); | ||
} | ||
/** Rollback a transaction */ | ||
public rollback(): Promise<any> { | ||
return promisify(this.conn.rollback, this.conn); | ||
} | ||
/** Escape any mysql injection string */ | ||
@@ -55,0 +74,0 @@ public escape(data: string): string { |
@@ -51,3 +51,3 @@ const mysql = require('promise-mysql'); | ||
*/ | ||
public getAll(wheres?: any, options?: QueryOption): Promise<any> { | ||
public async getAll(wheres?: any, options?: QueryOption): Promise<any> { | ||
let statement = 'SELECT * FROM ' + this.table; | ||
@@ -70,3 +70,3 @@ | ||
*/ | ||
public save(data: T): Promise<any> { | ||
public async save(data: T): Promise<any> { | ||
let statement = [data.id ? 'UPDATE' : 'INSERT INTO', this.table, 'SET ?'].join(' '); | ||
@@ -83,3 +83,3 @@ if (data.id) statement += ' WHERE id = ?'; | ||
*/ | ||
public saveAll(items: T[]): Promise<any> { | ||
public async saveAll(items: T[]): Promise<any> { | ||
let statement = 'INSERT INTO ' + this.table; | ||
@@ -110,3 +110,3 @@ const columns = Object.keys(items[0]); | ||
*/ | ||
public delete(wheres: any): Promise<any> { | ||
public async delete(wheres: any): Promise<any> { | ||
if (!wheres) throw new Error(errors.where); | ||
@@ -113,0 +113,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47299
989
4
+ Addedes6-promisify@^5.0.0
+ Addedes6-promise@4.2.8(transitive)
+ Addedes6-promisify@5.0.0(transitive)