Skip to content

Commit d6066f4

Browse files
Release v1.2.2 (#8)
- Fix: Wrong data return behavior on 'Create' and 'Upsert' methods due to different versions of MariaDB. - Fix: Incorrectly checking of MariaDB's version, that caused misbehavior on major versions of MariaDB equal or superior to 11 if the minor version was less than 5. - Fix(typo): Improved non-null check message, fixed orthography and added more context to the error.
2 parents ed609fa + 1d9080f commit d6066f4

File tree

6 files changed

+45
-35
lines changed

6 files changed

+45
-35
lines changed

CHANGELOG.md

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,60 @@
1+
# v1.2.2
2+
3+
- Fix: Wrong data return behavior on 'Create' and 'Upsert' methods due to different versions of MariaDB.
4+
5+
- Fix: Incorrectly checking of MariaDB's version, that caused misbehavior on major versions of MariaDB equal or superior to 11 if the minor version was less than 5.
6+
7+
- Fix(typo): Improved non-null check message, fixed orthography and added more context to the error.
8+
19
# v1.2.1
210

311
- Feat: New 'Count' method that allows the counting of how many rows are in a table, how many not null columns, distinct values of a column and set a custom name for the count key in the result object.
412

5-
- Fix(types): Fix typing mistake that caused an regression forcing an object to be passed to 'Select' and 'Find', calling those methods without any argument will behave the same way as passing an empty object, this is a typing only mistake, that doesn't change how the methods behave or parse its parameters.
13+
- Fix(types): Fix typing mistake that caused an regression forcing an object to be passed to 'Find', calling this method without any argument will behave the same way as passing an empty object.
14+
- This is a typing only mistake, that doesn't change how the method behave or parse its parameters.
615

716
# v1.2.0
817

9-
- Feat: 'Select' and 'Find' methods can now receive an orderBy parameter to order return the results from the database
18+
- Feat: 'Select' and 'Find' methods can now receive an orderBy parameter to order the return the results from the database.
1019

11-
- BREAKING: 'Select' and 'Find' now receive an object as parameter instead of separated params
12-
13-
- Chore: Updated dev dependencies
20+
- BREAKING: 'Select' and 'Find' now receive an object as parameter instead of separated params.
1421

1522
# v1.1.1
1623

17-
- Fix: Select with filter doesn't work if the value of the field 'value' is falsy
18-
- Fix(types): Small fix on timestamp and boolean types
24+
- Fix: Select with filter doesn't work if the value of the field 'value' is falsy.
25+
26+
- Fix(types): Small fix on timestamp and boolean types.
1927

2028
# v1.1.0
2129

22-
- Feat: Better type on BaseModel creation
30+
- Feat: Better type on BaseModel creation.
2331

24-
- Fix: Can't initialize orm when foreign keys are present due to wrong foreign key names
32+
- Fix: Can't initialize orm when foreign keys are present due to wrong foreign key names.
2533

26-
- Fix: Multiple primary keys not being created due to wrong handling of primary key fields
34+
- Fix: Multiple primary keys not being created due to wrong handling of primary key fields.
2735

28-
- Fix: Parsing of unique keys and foreign keys when updating tables
36+
- Fix: Parsing of unique keys and foreign keys when updating tables.
2937

30-
- BREAKING: Changed constructor of MariaDBConnection to accept an object instead of multiple parameters
38+
- BREAKING: Changed constructor of MariaDBConnection to accept an object instead of multiple parameters.
3139

3240
# v1.0.6
3341

34-
- Fix: Can't create tables with foreign keys due to invalid syntax for ON DELETE and ON UPDATE clauses
42+
- Fix: Can't create tables with foreign keys due to invalid syntax for ON DELETE and ON UPDATE clauses.
3543

3644
# v1.0.5
3745

38-
- Fix: Can't connect to MySql instances due to missing allowPublicKeyRetrieval: true in connection options
46+
- Fix: Can't connect to MySql instances due to missing allowPublicKeyRetrieval: true in connection options.
3947

4048
# v1.0.4
4149

42-
- NOTICE: Rebranding 'promisedb' to 'promiseorm'
43-
- CI: Add CI
50+
- NOTICE: Rebranding 'promisedb' to 'promiseorm'.
51+
52+
- CI: Add CI.
4453

4554
# v1.0.3
4655

47-
- Fix: Register schema not awaiting before returning
56+
- Fix: Register schema not awaiting before returning.
4857

49-
- Fix: Update now returns updated row, if nothing got updated it returns undefined
58+
- Fix: Update now returns updated row, if nothing got updated it returns undefined.
5059

51-
- Fix: Update now doesn't requires the whole row to be passed to be updated, and now will accept updating fields based on the passed search params
60+
- Fix: Update now doesn't require the whole row to be updated to be passed, and now will accept updating fields based on the passed search params.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "promiseorm",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "A Typescript ORM for automatic creation and management of models and entries from simple objects",
55
"main": "build/index.js",
66
"files": [

src/main/connection/DatabaseConnection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export abstract class DatabaseConnection {
5353
* @throws [{@link DatabaseException}]
5454
* @abstract
5555
*/
56-
public abstract create(database: string, keys: string[], values: any[]): Promise<Record<string, any>>
56+
public abstract create(database: string, keys: string[], fields: string[], values: any[]): Promise<Record<string, any>>
5757

5858
/**
5959
* Read records from the database
@@ -69,7 +69,7 @@ export abstract class DatabaseConnection {
6969
/**
7070
* Inserts data on database, if it already exists, updates the selected fields
7171
*/
72-
public abstract upsert(database: string, keys: string[], values: any[], updateFields: string[]): Promise<Record<string, any>>
72+
public abstract upsert(database: string, keys: string[], fields: string[], values: any[], updateFields: string[]): Promise<Record<string, any>>
7373

7474
/**
7575
* Update records in the database

src/main/connection/MariaDBConnection.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ export class MariaDBConnection extends DatabaseConnection {
7676
return await this.pool!.getConnection();
7777
}
7878

79-
private async processInstruction(conn: PoolConnection, database: string, keys: string[], values: any[], instructions: string[]): Promise<Record<string, any>> {
79+
private async processInstruction(conn: PoolConnection, database: string, keys: string[], fields: string[], values: any[], instructions: string[]): Promise<Record<string, any>> {
8080
let result;
81-
if (Number(this.version[0]) >= 10 && Number(this.version[1]) >= 5) {
81+
if (Number(this.version[0]) > 10 || (Number(this.version[0]) === 10 && Number(this.version[1]) >= 5)) {
8282
instructions.push('RETURNING');
83-
instructions.push(`${keys.join(',')}`);
84-
result = await conn.execute(instructions.join(' '));
83+
instructions.push(`${fields.join(',')}`);
84+
result = (await conn.execute(instructions.join(' ')))[0];
8585
} else {
8686
await conn.execute(instructions.join(' '));
8787
// selects just inserted data
@@ -98,15 +98,15 @@ export class MariaDBConnection extends DatabaseConnection {
9898
/**
9999
* @private
100100
*/
101-
override async create(database: string, keys: string[], values: any[]): Promise<Record<string, any>> {
101+
override async create(database: string, keys: string[], fields: string[], values: any[]): Promise<Record<string, any>> {
102102
const conn = await this.getConnection();
103103
const keysField = keys.map((key) => conn.escapeId(key)).join(', ');
104104
const instructions = ['INSERT INTO'];
105105
instructions.push(conn.escapeId(database));
106106
instructions.push(`(${keysField})`);
107107
instructions.push('VALUES');
108108
instructions.push(`(${values.map((value) => conn.escape(value)).join(', ')})`);
109-
const result = await this.processInstruction(conn, database, keys, values, instructions);
109+
const result = await this.processInstruction(conn, database, keys, fields, values, instructions);
110110
await conn.release();
111111
return result;
112112
}
@@ -165,7 +165,7 @@ export class MariaDBConnection extends DatabaseConnection {
165165
/**
166166
* @private
167167
*/
168-
override async upsert(database: string, keys: string[], values: any[], updateFields: string[]): Promise<Record<string, any>> {
168+
override async upsert(database: string, keys: string[], fields: string[], values: any[], updateFields: string[]): Promise<Record<string, any>> {
169169
const conn = await this.getConnection();
170170
const keysField = keys.map((key) => conn.escapeId(key)).join(', ');
171171
const instructions = ['INSERT'];
@@ -183,7 +183,7 @@ export class MariaDBConnection extends DatabaseConnection {
183183
});
184184
instructions.push(fieldsSQL.join(','));
185185
}
186-
const result = await this.processInstruction(conn, database, keys, values, instructions);
186+
const result = await this.processInstruction(conn, database, keys, fields, values, instructions);
187187
await conn.release();
188188
return result;
189189
}

src/main/models/BaseModel.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ export class BaseModel {
8585
private validFieldValueCheck(data: Record<string, any>): void {
8686
const keys = Object.keys(data);
8787
this.fieldsCheck(keys);
88-
if (keys.find((key) => this.nonNullableFields.includes(key) && (data[key] == null || data[key] === ''))) throw new DatabaseException(`An null param has given to a non nullable field!`);
88+
const nonNullCheck = keys.filter((key) => this.nonNullableFields.includes(key) && (data[key] == null || data[key] === ''));
89+
if (nonNullCheck.length > 0) throw new DatabaseException(`A null param was provided to a non-null field! Null was provided to the following non-null fields: ${nonNullCheck.join(', ')}`);
8990
keys.find((key) => {
9091
if (this.fields[key].type === EDatabaseTypes.SINT || this.fields[key].type === EDatabaseTypes.UINT) {
9192
if (typeof data[key] !== 'number') throw new DatabaseException(`Field ${key} has to be a number!`);
@@ -236,7 +237,7 @@ export class BaseModel {
236237
this.validFieldsCheck(data);
237238
const keys = Object.keys(this.fields).filter((field) => !this.fields[field].autoIncrement);
238239
const values = keys.map((fieldKey) => data[fieldKey] ?? this.fields[fieldKey].default ?? null);
239-
return this.connection!.create(this.name!, keys, values);
240+
return this.connection!.create(this.name!, keys, Object.keys(this.fields), values);
240241
}
241242

242243
/**
@@ -284,7 +285,7 @@ export class BaseModel {
284285
const keys = Object.keys(this.fields).filter((field) => !this.fields[field].autoIncrement);
285286
if (!Array.isArray(updateFields) && !updateFields) updateFields = keys;
286287
const values = keys.map((fieldKey) => data[fieldKey] ?? null);
287-
return this.connection!.upsert(this.name!, keys, values, updateFields);
288+
return this.connection!.upsert(this.name!, keys, Object.keys(this.fields), values, updateFields);
288289
}
289290

290291
/**

0 commit comments

Comments
 (0)