Skip to content

Commit 0efb8e1

Browse files
committed
chore: bump version to 1.0.8 in package.json and enhance pagination functionality in Repository class
1 parent 591e8a5 commit 0efb8e1

File tree

5 files changed

+87
-25
lines changed

5 files changed

+87
-25
lines changed

__tests__/repository/paginate.test.ts

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
import {
2-
cleanupTestTables,
3-
DomainPost,
4-
DomainUser,
5-
executor,
6-
seedTestData,
7-
setupTestTables,
8-
} from "../../test-setup";
9-
import { Repository } from "../../src";
10-
import { asc, desc } from "../../src";
1+
import {cleanupTestTables, DomainPost, DomainUser, executor, seedTestData, setupTestTables,} from "../../test-setup";
2+
import {asc, desc, Repository} from "../../src";
113

124
describe("Repository Pagination", () => {
135
let postRepository: Repository<DomainPost>;
@@ -79,4 +71,27 @@ describe("Repository Pagination", () => {
7971
);
8072
}
8173
});
74+
75+
it('Should return all result when limit is -1', async () => {
76+
const countQuery = await executor.executeSQL<{ count: 0 }>(`
77+
SELECT COUNT(*) as count
78+
FROM "posts"
79+
`, []);
80+
81+
const page = await postRepository.paginate({
82+
page: 1,
83+
limit: -1,
84+
});
85+
86+
expect(page.meta.totalCount).toBe(+countQuery.rows[0].count);
87+
expect(page.nodes.length).toBe(+countQuery.rows[0].count);
88+
})
89+
90+
it('Should return 10 result when limit is not provided', async () => {
91+
const page = await postRepository.paginate({
92+
page: 1,
93+
});
94+
95+
expect(page.nodes.length).toBe(10);
96+
})
8297
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sqlkit",
3-
"version": "1.0.6",
3+
"version": "1.0.8",
44
"description": "A lightweight SQL builder for TypeScript",
55
"license": "MIT",
66
"author": {

src/dialects/postgres.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { SQLKITException } from "../exceptions";
44
export class PostgresAdapter implements SqlExecutor {
55
constructor(private pgPool: any) {}
66

7-
async executeSQL<T>(sql: string, values: any[]): Promise<QueryResult> {
7+
async executeSQL<T>(sql: string, values: any[]): Promise<QueryResult<T>> {
88
return new Promise((resolve, reject) => {
99
this.pgPool.query(sql, values, (err, result) => {
1010
if (err) {

src/query-builder/select.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,17 @@ export class SelectQueryBuilder<T> extends BaseQueryBuilder<T> {
9292
${joinConditionClause ? joinConditionClause : ""}
9393
${whereClause ? `WHERE ${whereClause}` : ""}
9494
${orderByClause ? orderByClause : ""}
95-
${limit ? `LIMIT ${limit}` : ""} ${offset ? `OFFSET ${offset}` : ""};
95+
${limit != -1 ? `LIMIT ${limit}` : ""} ${offset ? `OFFSET ${offset}` : ""};
9696
`;
9797

9898
return { sql, values };
9999
}
100100

101101
async paginate(options: PaginationOptions<T>): Promise<PaginatedResult<T>> {
102-
if(!this?.executor){
102+
if (!this?.executor) {
103103
throw new SQLKITException("Executor is not set for the query builder.");
104104
}
105-
105+
106106
const limit = options.limit || 10;
107107
const page = options.page || 1;
108108
const offset = (page - 1) * limit;

src/repository/repository.ts

+57-10
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,27 @@ import {
1313
} from "../types";
1414
import { buildWhereClause } from "../utils";
1515

16+
interface RepositoryOptions {
17+
logging?: boolean;
18+
}
19+
1620
export class Repository<T> {
1721
constructor(
1822
protected readonly tableName: string,
19-
protected readonly executor: SqlExecutor
23+
protected readonly executor: SqlExecutor,
24+
protected options?: RepositoryOptions
2025
) {}
2126

2227
async findRow(where: WhereCondition<T>): Promise<T | null> {
2328
const builder = new SelectQueryBuilder<T>(this.tableName, this.executor);
2429
const result = await builder.where(where).limit(1).commit();
30+
if (this.options?.logging) {
31+
console.log({
32+
sql: builder.build().sql,
33+
values: builder.build().values,
34+
result: result.rows[0]
35+
});
36+
}
2537
return result.rows[0] ?? null;
2638
}
2739

@@ -34,9 +46,15 @@ export class Repository<T> {
3446
if (payload?.orderBy) builder.orderBy(payload.orderBy);
3547
if (payload?.limit) builder.limit(payload.limit);
3648
if (payload?.offset) builder.offset(payload.offset);
37-
// console.log(builder.build().sql);
38-
// console.log(builder.build().values);
3949
const result = await builder.commit();
50+
51+
if (this.options?.logging) {
52+
console.log({
53+
sql: builder.build().sql,
54+
values: builder.build().values,
55+
result: result.rows[0]
56+
});
57+
}
4058
return result.rows;
4159
}
4260

@@ -76,6 +94,13 @@ export class Repository<T> {
7694
): Promise<T> {
7795
const builder = new InsertQueryBuilder<T>(this.tableName, this.executor);
7896
const result = await builder.values(data).returning(returning).commit();
97+
if (this.options?.logging) {
98+
console.log({
99+
sql: builder.build().sql,
100+
values: builder.build().values,
101+
result: result.rows[0]
102+
});
103+
}
79104
return result.rows[0];
80105
}
81106

@@ -85,6 +110,13 @@ export class Repository<T> {
85110
): Promise<T[]> {
86111
const builder = new InsertQueryBuilder<T>(this.tableName, this.executor);
87112
const result = await builder.values(data).returning(returning).commit();
113+
if (this.options?.logging) {
114+
console.log({
115+
sql: builder.build().sql,
116+
values: builder.build().values,
117+
result: result.rows[0]
118+
});
119+
}
88120
return result.rows;
89121
}
90122

@@ -101,17 +133,32 @@ export class Repository<T> {
101133
.where(where)
102134
.returning(returning)
103135
.commit();
136+
if (this.options?.logging) {
137+
console.log({
138+
sql: builder.build().sql,
139+
values: builder.build().values,
140+
result: result.rows[0]
141+
});
142+
}
104143
return result.rows[0] ?? null;
105144
}
106145

107-
async delete(
108-
arg: {
109-
where: WhereCondition<T>;
110-
returning?: Array<keyof T>;
111-
}
112-
): Promise<T | null> {
146+
async delete(arg: {
147+
where: WhereCondition<T>;
148+
returning?: Array<keyof T>;
149+
}): Promise<T | null> {
113150
const builder = new DeleteQueryBuilder<T>(this.tableName, this.executor);
114-
const result = await builder.where(arg.where).returning(arg.returning).commit();
151+
const result = await builder
152+
.where(arg.where)
153+
.returning(arg.returning)
154+
.commit();
155+
if (this.options?.logging) {
156+
console.log({
157+
sql: builder.build().sql,
158+
values: builder.build().values,
159+
result: result.rows[0]
160+
});
161+
}
115162
return result.rows[0] ?? null;
116163
}
117164
}

0 commit comments

Comments
 (0)