Skip to content

Commit 1f56e9c

Browse files
Release 1.2.0 (#6)
- Feat: 'Select' and 'Find' methods can now receive an orderBy parameter to order return the results from the database - BREAKING: 'Select' and 'Find' now receive an object as parameter instead of separated params - Chore: Updated dev dependencies
2 parents 1e3d841 + 26b1b33 commit 1f56e9c

File tree

10 files changed

+174
-110
lines changed

10 files changed

+174
-110
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# v1.2.0
2+
3+
- Feat: 'Select' and 'Find' methods can now receive an orderBy parameter to order return the results from the database
4+
5+
- BREAKING: 'Select' and 'Find' now receive an object as parameter instead of separated params
6+
7+
- Chore: Updated dev dependencies
8+
19
# v1.1.1
210

311
- Fix: Select with filter doesn't work if the value of the field 'value' is falsy

package-lock.json

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

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "promiseorm",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "A Typescript ORM for automatic creation and management of models and entries from simple objects",
55
"main": "build/index.js",
66
"files": [
@@ -34,10 +34,10 @@
3434
"mariadb": "3.4.5"
3535
},
3636
"devDependencies": {
37-
"@types/node": "^24.7.2",
38-
"@typescript-eslint/eslint-plugin": "^8.46.0",
39-
"@typescript-eslint/parser": "^8.46.0",
40-
"eslint": "^9.37.0",
37+
"@types/node": "^24.9.2",
38+
"@typescript-eslint/eslint-plugin": "^8.46.2",
39+
"@typescript-eslint/parser": "^8.46.2",
40+
"eslint": "^9.38.0",
4141
"eslint-plugin-import": "^2.32.0",
4242
"eslint-plugin-node": "^11.1.0",
4343
"eslint-plugin-promise": "^7.2.1",

src/main/connection/DatabaseConnection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IDatabaseField, IDatabaseQueryFilterExpression } from '../interfaces';
1+
import { IDatabaseConnectionRead, IDatabaseField, IDatabaseQueryFilterExpression } from '../interfaces';
22
// eslint-disable-next-line @typescript-eslint/no-unused-vars
33
import { DatabaseException } from '../errors';
44
import { BaseModel } from '../models';
@@ -64,7 +64,7 @@ export abstract class DatabaseConnection {
6464
* @throws [{@link DatabaseException}]
6565
* @abstract
6666
*/
67-
public abstract read(keys: ('*' | string[]), database: string, filter?: IDatabaseQueryFilterExpression, limit?: number): Promise<Record<string, any>[]>
67+
public abstract read({ keys, database, filter, limit, orderBy }: IDatabaseConnectionRead): Promise<Record<string, any>[]>
6868

6969
/**
7070
* Inserts data on database, if it already exists, updates the selected fields

src/main/connection/MariaDBConnection.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { EDatabaseQueryFilterOperator,
22
EDatabaseTypes, EMariaDBFieldTypes,
3+
IDatabaseConnectionRead,
34
IDatabaseField, IDatabaseQueryFilter,
45
IDatabaseQueryFilterExpression,
56
IMariaDBDescribeField,
@@ -83,7 +84,12 @@ export class MariaDBConnection extends DatabaseConnection {
8384
} else {
8485
await conn.execute(instructions.join(' '));
8586
// selects just inserted data
86-
result = (await this.read('*', database, { type: 'AND', filters: keys.map((key, index) => ({ tableKey: key, operator: EDatabaseQueryFilterOperator.EQUALS, value: values[index] })) }, 1))[0];
87+
result = (await this.read({
88+
keys: '*',
89+
database,
90+
filter: { type: 'AND', filters: keys.map((key, index) => ({ tableKey: key, operator: EDatabaseQueryFilterOperator.EQUALS, value: values[index] })) },
91+
limit: 1,
92+
}))[0];
8793
}
8894
return result;
8995
}
@@ -107,14 +113,21 @@ export class MariaDBConnection extends DatabaseConnection {
107113
/**
108114
* @private
109115
*/
110-
override async read(keys: ('*' | string[]), database: string, filter?: IDatabaseQueryFilterExpression, limit?: number): Promise<Record<string, any>[]> {
116+
override async read({ keys, database, filter, limit, orderBy }: IDatabaseConnectionRead): Promise<Record<string, any>[]> {
111117
const conn = await this.getConnection();
112118
const operators: string[] = [];
113119
operators.push(typeof keys === 'string' ? '*' : keys.map(key => conn.escapeId(key)).join(', '));
114120
operators.push(`FROM ${conn.escapeId(database)}`);
115121
if (filter) operators.push(`WHERE ${this.filterBuilder(conn, filter)}`);
122+
if (orderBy) {
123+
operators.push(`ORDER BY ${
124+
Array.isArray(orderBy) ?
125+
orderBy.map((items) => `${conn.escapeId(items.field)} ${items.direction}`).join(', ') :
126+
`${conn.escapeId(orderBy.field)} ${orderBy.direction}`
127+
}`);
128+
}
116129
if (limit) {
117-
if (limit < 1) throw new DatabaseException('Limit must be a positive number greater than ');
130+
if (limit < 1) throw new DatabaseException('Limit must be a positive number greater than 1');
118131
operators.push(`LIMIT ${limit}`);
119132
}
120133

@@ -135,8 +148,15 @@ export class MariaDBConnection extends DatabaseConnection {
135148
instructions.push('WHERE');
136149
instructions.push(this.filterBuilder(conn, filter));
137150
await conn.execute(instructions.join(' '));
138-
const result = (await this.read('*', database, { type: 'AND', filters:
139-
fields.map((key, index) => ({ tableKey: key, operator: EDatabaseQueryFilterOperator.EQUALS, value: newData[index] })) }, 1))[0];
151+
const result = (await this.read({
152+
keys: '*',
153+
database,
154+
filter: {
155+
type: 'AND',
156+
filters: fields.map((key, index) => ({ tableKey: key, operator: EDatabaseQueryFilterOperator.EQUALS, value: newData[index] })),
157+
},
158+
limit: 1,
159+
}))[0];
140160
await conn.release();
141161
return result;
142162
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { IDatabaseOrderBy, IDatabaseQueryFilterExpression } from '../..';
2+
3+
export interface IDatabaseConnectionRead {
4+
keys: ('*' | string[]),
5+
database: string,
6+
filter?: IDatabaseQueryFilterExpression,
7+
limit?: number,
8+
orderBy?: IDatabaseOrderBy | IDatabaseOrderBy[]
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './mariadb';
2+
export * from './IDatabaseConnectionRead';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface IDatabaseOrderBy {
2+
field: string;
3+
direction: 'ASC' | 'DESC';
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './IDatabaseQueryFilterExpression';
22
export * from './EDatabaseQueryFilterOperator';
33
export * from './IDatabaseQueryFilter';
4+
export * from './IDatabaseOrderBy';
45
export * from './IDatabaseField';
56
export * from './EDatabaseTypes';

src/main/models/BaseModel.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EDatabaseQueryFilterOperator, EDatabaseTypes, IDatabaseField, IDatabaseQueryFilter, IDatabaseQueryFilterExpression } from '../interfaces';
1+
import { EDatabaseQueryFilterOperator, EDatabaseTypes, IDatabaseField, IDatabaseOrderBy, IDatabaseQueryFilter, IDatabaseQueryFilterExpression } from '../interfaces';
22
import { DatabaseConnection } from '../connection';
33
import { DatabaseException } from '../errors';
44

@@ -157,10 +157,11 @@ export class BaseModel {
157157
* @returns The data found
158158
* @throws [{@link DatabaseException}]
159159
*/
160-
public async find(query?: Record<string, any>, limit?: number): Promise<Record<string, unknown>[]> {
160+
public async find(params: { query?: Record<string, any>, limit?: number, orderBy?: IDatabaseOrderBy }): Promise<Record<string, unknown>[]> {
161+
const { query, limit, orderBy } = params || {};
161162
this.checkIsReady();
162-
return this.connection!.read('*', this.name!,
163-
query
163+
return this.connection!.read({ keys: '*', database: this.name!,
164+
filter: (query
164165
? {
165166
type: 'AND',
166167
filters: Object.keys(query).map((fieldKey) => ({
@@ -169,9 +170,10 @@ export class BaseModel {
169170
value: query[fieldKey],
170171
})),
171172
}
172-
: undefined,
173+
: undefined),
173174
limit,
174-
);
175+
orderBy,
176+
});
175177
}
176178

177179
/**
@@ -181,7 +183,7 @@ export class BaseModel {
181183
* @throws [{@link DatabaseException}]
182184
*/
183185
public async findOne(query?: Record<string, any>): Promise<Record<string, unknown> | undefined> {
184-
return Promise.resolve((await this.find(query, 1))[0]);
186+
return Promise.resolve((await this.find({ query, limit: 1 }))[0]);
185187
}
186188

187189
/**
@@ -203,11 +205,14 @@ export class BaseModel {
203205
* @returns The data found
204206
* @throws [{@link DatabaseException}]
205207
*/
206-
public async select(fields: string[], filter?: IDatabaseQueryFilterExpression, limit?: number): Promise<Record<string, unknown>[]> {
208+
public async select(params: { fields: string[], query?: Record<string, any>, filter?: IDatabaseQueryFilterExpression, limit?: number, orderBy?: IDatabaseOrderBy },
209+
): Promise<Record<string, unknown>[]> {
207210
this.checkIsReady();
211+
if (!params) throw new DatabaseException('Missing params for \'select\' method call, if you want to retrieve all data on this table call \'find\' instead.');
212+
const { fields, filter, limit, orderBy } = params;
208213
this.fieldsCheck(fields);
209214
this.filterCheck(filter);
210-
return this.connection!.read(fields, this.name!, filter ?? undefined, limit);
215+
return this.connection!.read({ keys: fields, database: this.name!, filter, limit, orderBy });
211216
}
212217

213218
/**
@@ -218,7 +223,7 @@ export class BaseModel {
218223
* @throws [{@link DatabaseException}]
219224
*/
220225
public async selectOne(fields: string[], filter?: IDatabaseQueryFilterExpression): Promise<Record<string, unknown> | undefined> {
221-
return (await this.select(fields, filter, 1))[0];
226+
return (await this.select({ fields, filter, limit: 1 }))[0];
222227
}
223228

224229
/**

0 commit comments

Comments
 (0)