Comparing version
@@ -8,3 +8,3 @@ 'use strict'; | ||
}; | ||
const {redactSensitiveDataSymbol} = require('./redaction'); | ||
const { redactSensitiveDataSymbol } = require('./redaction'); | ||
const deepClone = require('./clone.js').cloneDeep; | ||
@@ -15,32 +15,31 @@ | ||
/** | ||
* Postgres Active Model class to extend a custom model from. | ||
* @extends PGEncryptModel | ||
* @example | ||
* | ||
* class User extends Base(BaseModel, 'users', { | ||
* id: PGTypes.PK, | ||
* username: null, | ||
* password: null, | ||
* encrypted_profile: PGTypes.EncryptProfile, | ||
* hashed_password: PGTypes.Hash, | ||
* phone: PGTypes.Encrypt, | ||
* auto_phone: PGTypes.AutoCrypt, | ||
* memes: PGTypes.EncryptWithoutHash, | ||
* auto_memes: PGTypes.AutoCryptWithoutHash, | ||
* email: null, | ||
* created_on: null, | ||
* last_login: null, | ||
* }) { | ||
* constructor(...args) { | ||
* super(...args); | ||
* } | ||
* | ||
* static async createUserWithRandomName(model) { | ||
* model.username = 'user' + Math.floor(Math.random() * 1000); | ||
* return await super.create(model); | ||
* } | ||
* } | ||
*/ | ||
* Postgres Active Model class to extend a custom model from. | ||
* @extends PGEncryptModel | ||
* @example | ||
* | ||
* class User extends Base(BaseModel, 'users', { | ||
* id: PGTypes.PK, | ||
* username: null, | ||
* password: null, | ||
* encrypted_profile: PGTypes.EncryptProfile, | ||
* hashed_password: PGTypes.Hash, | ||
* phone: PGTypes.Encrypt, | ||
* auto_phone: PGTypes.AutoCrypt, | ||
* memes: PGTypes.EncryptWithoutHash, | ||
* auto_memes: PGTypes.AutoCryptWithoutHash, | ||
* email: null, | ||
* created_on: null, | ||
* last_login: null, | ||
* }) { | ||
* constructor(...args) { | ||
* super(...args); | ||
* } | ||
* | ||
* static async createUserWithRandomName(model) { | ||
* model.username = 'user' + Math.floor(Math.random() * 1000); | ||
* return await super.create(model); | ||
* } | ||
* } | ||
*/ | ||
class PGActiveModel extends BaseModel { | ||
constructor(model, table, pk, encryptedProfileField, setModel, encryptProfile) { | ||
@@ -69,2 +68,3 @@ super(model, table, pk, encryptedProfileField, encryptProfile); | ||
} | ||
this.changedProps = {}; | ||
@@ -137,3 +137,2 @@ | ||
async delete() { | ||
var deletedModel = await this.ChildClass.deleteById(this[this._pkKey]); | ||
@@ -156,3 +155,2 @@ | ||
async create() { | ||
var newModel = await this.ChildClass.create(this.changedProps, true, this.getEncryptedProfile()); | ||
@@ -210,3 +208,9 @@ | ||
async update(model) { | ||
var updatedModel = await this.ChildClass.updateById(this[this._pkKey], model, true, false, this.getEncryptedProfile()); | ||
var updatedModel = await this.ChildClass.updateById( | ||
this[this._pkKey], | ||
model, | ||
true, | ||
false, | ||
this.getEncryptedProfile() | ||
); | ||
@@ -322,8 +326,11 @@ if (!updatedModel) { | ||
*/ | ||
static async findById(id) { | ||
static async findById(id, params = '*') { | ||
return await this.findAllBy( | ||
{ | ||
[this._pkKey]: id, | ||
}, 'AND', true).then(res=>{ | ||
[this._pkKey]: id | ||
}, | ||
'AND', | ||
true, | ||
params | ||
).then((res) => { | ||
return res && res.length > 0 ? res[0] : null; | ||
@@ -346,4 +353,4 @@ }); | ||
*/ | ||
static async findLimtedBy(fieldValues, operator = 'AND', limit) { | ||
var fModels = await super.findAllBy(fieldValues, operator, limit); | ||
static async findLimtedBy(fieldValues, operator = 'AND', limit, params = '*') { | ||
var fModels = await super.findAllBy(fieldValues, operator, limit, params); | ||
@@ -365,4 +372,4 @@ return this._toADModels(fModels); | ||
*/ | ||
static async findAllBy(fieldValues, operator = 'AND', isById = false) { | ||
var fModels = await super.findAllBy(fieldValues, operator); | ||
static async findAllBy(fieldValues, operator = 'AND', isById = false, params = '*') { | ||
var fModels = await super.findAllBy(fieldValues, operator, null, params); | ||
@@ -442,5 +449,12 @@ return isById ? fModels : this._toADModels(fModels); | ||
static async updateById(id, model, returnModel = true, isById = false, encryptProfile) { | ||
return await this.updateAllBy({ | ||
[this._pkKey]: id, | ||
}, model, 'AND', returnModel, isById, encryptProfile).then(res=>{ | ||
return await this.updateAllBy( | ||
{ | ||
[this._pkKey]: id | ||
}, | ||
model, | ||
'AND', | ||
returnModel, | ||
isById, | ||
encryptProfile | ||
).then((res) => { | ||
return res && res.length > 0 ? res[0] : null; | ||
@@ -470,3 +484,3 @@ }); | ||
return this._toADModels(uModels); | ||
return this._toADModels(uModels); | ||
} | ||
@@ -511,11 +525,10 @@ | ||
toJSON() { | ||
return {...this._}; | ||
return { ...this._ }; | ||
} | ||
toString() { | ||
return {...this._}; | ||
return { ...this._ }; | ||
} | ||
} | ||
module.exports = PGActiveModel; |
@@ -7,46 +7,44 @@ 'use strict'; | ||
/** | ||
* Postgres Base Model class to extend a custom model from. | ||
* | ||
* @example | ||
* | ||
* class User extends Base(BaseModel, 'users', { | ||
* id: PGTypes.PK, | ||
* username: null, | ||
* password: null, | ||
* email: null, | ||
* created_on: null, | ||
* last_login: null, | ||
* }) { | ||
* | ||
* constructor() { | ||
* super(); | ||
* } | ||
* | ||
* static async createUserWithRandomName(model) { | ||
* model.username = 'user' + Math.floor(Math.random() * 1000); | ||
* return await this.create(model); | ||
* } | ||
* } | ||
* | ||
*/ | ||
* Postgres Base Model class to extend a custom model from. | ||
* | ||
* @example | ||
* | ||
* class User extends Base(BaseModel, 'users', { | ||
* id: PGTypes.PK, | ||
* username: null, | ||
* password: null, | ||
* email: null, | ||
* created_on: null, | ||
* last_login: null, | ||
* }) { | ||
* | ||
* constructor() { | ||
* super(); | ||
* } | ||
* | ||
* static async createUserWithRandomName(model) { | ||
* model.username = 'user' + Math.floor(Math.random() * 1000); | ||
* return await this.create(model); | ||
* } | ||
* } | ||
* | ||
*/ | ||
class PGBaseModel { | ||
constructor(model, table, pk) { | ||
this.table = table; | ||
this._defaultModel = model, | ||
this._pkKey = pk; | ||
(this._defaultModel = model), (this._pkKey = pk); | ||
this.models = []; | ||
} | ||
static async query(query, values, modelConvert = true){ | ||
if(modelConvert) { | ||
return this._convertToModel(await pg.query(query, values)); | ||
} | ||
return await pg.query(query, values); | ||
static async query(query, values, modelConvert = true) { | ||
if (modelConvert) { | ||
return this._convertToModel(await pg.query(query, values)); | ||
} | ||
return await pg.query(query, values); | ||
} | ||
static async findById(id) { | ||
return await this.findAllBy( | ||
{ | ||
[this._pkKey]: id, | ||
}).then(res=>{ | ||
static async findById(id, params = '*') { | ||
return await this.findAllBy({ | ||
[this._pkKey]: id | ||
},'AND',1,params).then((res) => { | ||
return res && res.length > 0 ? res[0] : null; | ||
@@ -56,8 +54,7 @@ }); | ||
static async findAllBy(fieldValues, operator = 'AND', limit) { | ||
static async findAllBy(fieldValues, operator = 'AND', limit, params = '*') { | ||
var builtQuery = this._builtQuery(fieldValues, operator); | ||
var query = `SELECT * FROM ${this.table} WHERE ${builtQuery.query} ${limit ? 'LIMIT ' + limit : ''}`; | ||
var query = `SELECT ${params} FROM ${this.table} WHERE ${builtQuery.query} ${limit ? 'LIMIT ' + limit : ''}`; | ||
return this._convertToModel(await pg.query(query, builtQuery.values)); | ||
@@ -71,6 +68,5 @@ } | ||
static async deleteById(id) { | ||
return await this.deleteAllBy( | ||
{ | ||
[this._pkKey]: id, | ||
}); | ||
return await this.deleteAllBy({ | ||
[this._pkKey]: id | ||
}); | ||
} | ||
@@ -108,4 +104,6 @@ | ||
{ | ||
[this._pkKey]: id, | ||
}, model); | ||
[this._pkKey]: id | ||
}, | ||
model | ||
); | ||
} | ||
@@ -166,16 +164,18 @@ | ||
static _builtQuery(fieldValues, operator) { | ||
var builtQuery = {query:'', values:[]}; | ||
if(Array.isArray(fieldValues)){ | ||
for (var i = 0; i < fieldValues.length; i++) { | ||
var a = this._buildQueryFromFV(fieldValues[i], operator[i] || 'AND', i+1); | ||
builtQuery.query += a.query; | ||
if(i+1 < fieldValues.length) { | ||
builtQuery.query += ' '+(operator[i] || 'AND')+' '; | ||
} | ||
builtQuery.values = builtQuery.values.concat(a.values) | ||
var builtQuery = { query: '', values: [] }; | ||
if (Array.isArray(fieldValues)) { | ||
for (var i = 0; i < fieldValues.length; i++) { | ||
var a = this._buildQueryFromFV(fieldValues[i], operator[i] || 'AND', i + 1); | ||
builtQuery.query += a.query; | ||
if (i + 1 < fieldValues.length) { | ||
builtQuery.query += ' ' + (operator[i] || 'AND') + ' '; | ||
} | ||
builtQuery.values = builtQuery.values.concat(a.values); | ||
} | ||
} else { | ||
builtQuery = this._buildQueryFromFV(fieldValues, operator); | ||
} | ||
} else { | ||
builtQuery = this._buildQueryFromFV(fieldValues, operator); | ||
} | ||
return builtQuery; | ||
return builtQuery; | ||
} | ||
@@ -194,6 +194,6 @@ | ||
if (val[j + 1] === null) { | ||
query += `"${fieldValuesKey[i]}" IS NULL${j < val.length ? ` ${val[j]} ` : ' ' }`; | ||
query += `"${fieldValuesKey[i]}" IS NULL${j < val.length ? ` ${val[j]} ` : ' '}`; | ||
} else { | ||
values.push(val[j++]); | ||
query += `"${fieldValuesKey[i]}"=$${startIndex}${j < val.length ? ` ${val[j]} ` : ' ' }`; | ||
query += `"${fieldValuesKey[i]}"=$${startIndex}${j < val.length ? ` ${val[j]} ` : ' '}`; | ||
startIndex++; | ||
@@ -213,7 +213,6 @@ } | ||
} | ||
return ( | ||
{ | ||
query, | ||
values, | ||
}); | ||
return { | ||
query, | ||
values | ||
}; | ||
} | ||
@@ -220,0 +219,0 @@ } |
@@ -10,5 +10,5 @@ 'use strict'; | ||
/** | ||
* Postgres Encryption Model class to extend a custom model from. | ||
* @extends PGBaseModel | ||
*/ | ||
* Postgres Encryption Model class to extend a custom model from. | ||
* @extends PGBaseModel | ||
*/ | ||
class PGEncryptModel extends BaseModel { | ||
@@ -22,4 +22,4 @@ constructor(model, table, pk, encryptedProfileField, encryptProfile = 'default') { | ||
static async findById(id) { | ||
const newModel = await super.findById(id); | ||
static async findById(id, params = '*') { | ||
const newModel = await super.findById(id, params); | ||
@@ -29,4 +29,9 @@ return newModel ? await this.decrypt(newModel, this.getEncryptedProfile(newModel), true) : newModel; | ||
static async findAllBy(fieldValues, operator = 'AND', limit) { | ||
const newModels = await super.findAllBy(await this._queryFieldsHash(fieldValues, this.getEncryptedProfile(fieldValues)), operator, limit); | ||
static async findAllBy(fieldValues, operator = 'AND', limit, params = '*') { | ||
const newModels = await super.findAllBy( | ||
await this._queryFieldsHash(fieldValues, this.getEncryptedProfile(fieldValues)), | ||
operator, | ||
limit, | ||
params | ||
); | ||
@@ -57,3 +62,7 @@ for (var i = 0; i < newModels.length; i++) { | ||
static async deleteAllBy(fieldValues, operator = 'AND', limit) { | ||
return super.deleteAllBy(await this._queryFieldsHash(fieldValues, this.getEncryptedProfile(fieldValues)), operator, limit); | ||
return super.deleteAllBy( | ||
await this._queryFieldsHash(fieldValues, this.getEncryptedProfile(fieldValues)), | ||
operator, | ||
limit | ||
); | ||
} | ||
@@ -66,6 +75,3 @@ | ||
static async create(model, returnModel = true, encryptProfile) { | ||
const newModel = await super.create( | ||
await this.encrypt( | ||
model, encryptProfile), returnModel | ||
); | ||
const newModel = await super.create(await this.encrypt(model, encryptProfile), returnModel); | ||
@@ -82,3 +88,9 @@ return [await this.decrypt(newModel && newModel.length > 0 ? newModel[0] : {}, encryptProfile, true)]; | ||
static async updateAllBy(fieldValues, model, operator = 'AND', returnModel = true, encryptProfile, limit) { | ||
const newModels = await super.updateAllBy(await this._queryFieldsHash(fieldValues, encryptProfile), await this.encrypt(model, encryptProfile), operator, returnModel, limit); | ||
const newModels = await super.updateAllBy( | ||
await this._queryFieldsHash(fieldValues, encryptProfile), | ||
await this.encrypt(model, encryptProfile), | ||
operator, | ||
returnModel, | ||
limit | ||
); | ||
@@ -102,3 +114,2 @@ for (var i = 0; i < newModels.length; i++) { | ||
return newModels; | ||
} | ||
@@ -122,3 +133,5 @@ | ||
case PGTypes.EncryptWithoutHash: | ||
if (onlyAutoCrypt) {break;} | ||
if (onlyAutoCrypt) { | ||
break; | ||
} | ||
case PGTypes.AutoCrypt: | ||
@@ -168,16 +181,18 @@ case PGTypes.AutoCryptWithoutHash: | ||
static _builqueryFieldsHash(fieldValues, operator) { | ||
var builtQuery = {query:'', values:[]}; | ||
if(Array.isArray(fieldValues)){ | ||
for (var i = 0; i < fieldValues.length; i++) { | ||
var a = this._buildQueryFromFV(fieldValues[i], operator[i] || 'AND', i+1); | ||
builtQuery.query += a.query; | ||
if(i+1 < fieldValues.length) { | ||
builtQuery.query += ' '+(operator[i] || 'AND')+' '; | ||
} | ||
builtQuery.values = builtQuery.values.concat(a.values) | ||
var builtQuery = { query: '', values: [] }; | ||
if (Array.isArray(fieldValues)) { | ||
for (var i = 0; i < fieldValues.length; i++) { | ||
var a = this._buildQueryFromFV(fieldValues[i], operator[i] || 'AND', i + 1); | ||
builtQuery.query += a.query; | ||
if (i + 1 < fieldValues.length) { | ||
builtQuery.query += ' ' + (operator[i] || 'AND') + ' '; | ||
} | ||
builtQuery.values = builtQuery.values.concat(a.values); | ||
} | ||
} else { | ||
builtQuery = this._buildQueryFromFV(fieldValues, operator); | ||
} | ||
} else { | ||
builtQuery = this._buildQueryFromFV(fieldValues, operator); | ||
} | ||
return builtQuery; | ||
return builtQuery; | ||
} | ||
@@ -187,11 +202,13 @@ static async _queryFieldsHash(fields, encryptProfile = 'default') { | ||
var keys = Object.keys(this._encryptionFields); | ||
var isa = Array.isArray(fields); | ||
var isa = Array.isArray(fields); | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
if(isa) { | ||
for (var j = 0; j < fields.length; j++) { | ||
await this._queryFieldToHash(fields[j],key,encryptProfile) | ||
} | ||
if (isa) { | ||
for (var j = 0; j < fields.length; j++) { | ||
await this._queryFieldToHash(fields[j], key, encryptProfile); | ||
} | ||
} else { | ||
await this._queryFieldToHash(fields,key,encryptProfile) | ||
await this._queryFieldToHash(fields, key, encryptProfile); | ||
} | ||
@@ -203,28 +220,30 @@ } | ||
static async _queryFieldToHash(fields,key,encryptProfile) { | ||
static async _queryFieldToHash(fields, key, encryptProfile) { | ||
var field = fields[key]; | ||
if (field !== undefined && (PGTypes.AutoCrypt === this._encryptionFields[key] || PGTypes.Encrypt == this._encryptionFields[key])) { | ||
// remove the unhashed filed nad replace it with the hashed version | ||
fields[`__${key}`] = fields[key]; | ||
delete fields[key]; | ||
key = `__${key}`; | ||
field = fields[key]; | ||
if ( | ||
field !== undefined && | ||
(PGTypes.AutoCrypt === this._encryptionFields[key] || PGTypes.Encrypt == this._encryptionFields[key]) | ||
) { | ||
// remove the unhashed filed nad replace it with the hashed version | ||
fields[`__${key}`] = fields[key]; | ||
delete fields[key]; | ||
key = `__${key}`; | ||
field = fields[key]; | ||
if (Array.isArray(field)) { | ||
for (var j = 0; j < field.length; j++) { | ||
// skip the operator in the query array. | ||
if (j % 2 === 0) { | ||
field[j] = await pg.options.crypto.checksum(field[j], encryptProfile); | ||
} | ||
} | ||
fields[key] = field; | ||
} else { | ||
fields[key] = await pg.options.crypto.checksum(field, encryptProfile); | ||
} | ||
} | ||
if (Array.isArray(field)) { | ||
for (var j = 0; j < field.length; j++) { | ||
// skip the operator in the query array. | ||
if (j % 2 === 0) { | ||
field[j] = await pg.options.crypto.checksum(field[j], encryptProfile); | ||
} | ||
} | ||
fields[key] = field; | ||
} else { | ||
fields[key] = await pg.options.crypto.checksum(field, encryptProfile); | ||
} | ||
} | ||
} | ||
} | ||
module.exports = PGEncryptModel; |
{ | ||
"name": "adost", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "A fast postgres CRUD ORM", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
108988
0.77%2198
1.48%