Skip to content

Commit c6aa0c6

Browse files
committed
Enhance CI workflows and repository structure: add publish workflow, configure PostgreSQL service, and update package metadata.
1 parent 8e16dbf commit c6aa0c6

File tree

7 files changed

+198
-19
lines changed

7 files changed

+198
-19
lines changed

.github/workflows/publish.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: Test and Publish on Release
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
services:
11+
postgres:
12+
image: postgres:latest
13+
env:
14+
POSTGRES_USER: rayhan
15+
POSTGRES_PASSWORD: rayhan123
16+
POSTGRES_DB: tinyorm_test
17+
ports:
18+
- 5432:5432
19+
options: >-
20+
--health-cmd "pg_isready -U rayhan"
21+
--health-interval 10s
22+
--health-timeout 5s
23+
--health-retries 5
24+
25+
steps:
26+
- name: Checkout code
27+
uses: actions/checkout@v3
28+
29+
- name: Set up Node.js
30+
uses: actions/setup-node@v3
31+
with:
32+
node-version: '16'
33+
34+
- name: Install dependencies
35+
run: npm install
36+
37+
- name: Run tests
38+
run: npm test
39+
40+
publish:
41+
needs: test
42+
runs-on: ubuntu-latest
43+
44+
steps:
45+
- name: Checkout code
46+
uses: actions/checkout@v3
47+
48+
- name: Set up Node.js
49+
uses: actions/setup-node@v3
50+
with:
51+
node-version: '16'
52+
53+
- name: Install dependencies
54+
run: npm install
55+
56+
- name: Extract version from release tag
57+
id: extract_version
58+
run: echo "::set-output name=version::${GITHUB_REF#refs/tags/}"
59+
60+
- name: Build project
61+
run: npm run build
62+
63+
- name: Publish to npm
64+
run: npm publish
65+
env:
66+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/test-on-release.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,22 @@ on:
55
types: [published]
66

77
jobs:
8-
test:
8+
test:
99
runs-on: ubuntu-latest
10+
services:
11+
postgres:
12+
image: postgres:latest
13+
env:
14+
POSTGRES_USER: rayhan
15+
POSTGRES_PASSWORD: rayhan123
16+
POSTGRES_DB: tinyorm_test
17+
ports:
18+
- 5432:5432
19+
options: >-
20+
--health-cmd "pg_isready -U rayhan"
21+
--health-interval 10s
22+
--health-timeout 5s
23+
--health-retries 5
1024
1125
steps:
1226
- name: Checkout code
@@ -21,4 +35,4 @@ jobs:
2135
run: npm install
2236

2337
- name: Run tests
24-
run: npm run test
38+
run: npm test

.idea/prettier.xml

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

__tests__/repository/join.test.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { Repository } from "../../src/repository/repository";
2+
import {
3+
DomainPost,
4+
DomainUser,
5+
executor,
6+
seedTestData,
7+
setupTestTables,
8+
} from "../../test-setup";
9+
10+
describe("Repository Joins", () => {
11+
let userRepository: Repository<DomainUser>;
12+
let postRepository: Repository<DomainPost>;
13+
14+
beforeAll(async () => {
15+
await setupTestTables();
16+
await seedTestData();
17+
userRepository = new Repository("users", executor);
18+
postRepository = new Repository("posts", executor);
19+
});
20+
21+
afterAll(async () => {
22+
// await cleanupTestData();
23+
});
24+
25+
it("true", () => {
26+
expect(true).toBe(true);
27+
});
28+
29+
// it("should perform an inner join between users and posts", async () => {
30+
// const result = await userRepository.findRows({
31+
// joins: [
32+
// {
33+
// table: "posts",
34+
// type: "inner",
35+
// on: { localField: "id", foreignField: "author_id" },
36+
// columns: ["title", "content"],
37+
// },
38+
// ],
39+
// });
40+
//
41+
// expect(result).toBeDefined();
42+
// expect(result.length).toBeGreaterThan(0);
43+
// result.forEach((user) => {
44+
// expect(user).toHaveProperty("posts");
45+
// expect(user.posts).toBeDefined();
46+
// });
47+
// });
48+
//
49+
// it("should perform a left join between users and posts", async () => {
50+
// const result = await userRepository.findRows({
51+
// joins: [
52+
// {
53+
// table: "posts",
54+
// type: "left",
55+
// on: { localField: "id", foreignField: "author_id" },
56+
// columns: ["title", "content"],
57+
// },
58+
// ],
59+
// });
60+
//
61+
// expect(result).toBeDefined();
62+
// expect(result.length).toBeGreaterThan(0);
63+
// result.forEach((user) => {
64+
// expect(user).toHaveProperty("posts");
65+
// });
66+
// });
67+
//
68+
69+
it("should perform a right join between users and posts", async () => {
70+
//
71+
72+
const result = await postRepository.findRows({
73+
joins: [
74+
{
75+
table: "users",
76+
as: "author",
77+
type: "left",
78+
on: { localField: "author_id", foreignField: "id" },
79+
columns: ["name", "email"],
80+
},
81+
],
82+
});
83+
84+
console.log(result);
85+
86+
// expect(result).toBeDefined();
87+
// expect(result.length).toBeGreaterThan(0);
88+
// result.forEach((post) => {
89+
// expect(post).toHaveProperty("users");
90+
// });
91+
});
92+
});

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"name": "tinyorm",
2+
"name": "sqlkit",
33
"version": "1.0.0",
44
"description": "A lightweight SQL builder for TypeScript",
5-
"license": "ISC",
5+
"license": "MIT",
66
"author": {
77
"name": "KingRayhan",
88
"url": "https://www.rayhan.dev"

src/query-builder/select.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { SqlExecutor } from "../types";
2-
import {
3-
PaginatedResult,
4-
PaginationMeta,
5-
PaginationOptions,
6-
} from "../types";
2+
import { PaginatedResult, PaginationMeta, PaginationOptions } from "../types";
73
import { Join, OrderBy, QueryRowsPayload, WhereCondition } from "../types";
84
import {
95
buildJoinClause,
@@ -70,13 +66,16 @@ export class SelectQueryBuilder<T> extends BaseQueryBuilder<T> {
7066

7167
const { whereClause, values } = buildWhereClause(
7268
this.payload.where,
73-
this.tableName
69+
this.tableName,
7470
);
7571

76-
const orderByClause = buildOrderByClause(this.payload.orderBy, this.tableName);
72+
const orderByClause = buildOrderByClause(
73+
this.payload.orderBy,
74+
this.tableName,
75+
);
7776
const { joinConditionClause, joinSelectClause } = buildJoinClause(
7877
this.payload.joins,
79-
this.tableName
78+
this.tableName,
8079
);
8180

8281
// Build the SQL query with LIMIT, OFFSET, and ORDER BY
@@ -85,7 +84,7 @@ export class SelectQueryBuilder<T> extends BaseQueryBuilder<T> {
8584

8685
const sql = `
8786
SELECT ${columns}
88-
${joinSelectClause ? `${joinSelectClause.join(",")}` : ""}
87+
${joinSelectClause ? `,${joinSelectClause.join(",")}` : ""}
8988
FROM "${this.tableName}"
9089
${joinConditionClause ? joinConditionClause : ""}
9190
${whereClause ? `WHERE ${whereClause}` : ""}
@@ -112,7 +111,7 @@ export class SelectQueryBuilder<T> extends BaseQueryBuilder<T> {
112111
// Execute count query for pagination metadata
113112
const countBuilder = new SelectQueryBuilder<T>(
114113
this.tableName,
115-
this.executor
114+
this.executor,
116115
);
117116
if (options.where) {
118117
countBuilder.where(options.where);
@@ -133,7 +132,7 @@ export class SelectQueryBuilder<T> extends BaseQueryBuilder<T> {
133132

134133
const countResult = await this.executor.executeSQL(
135134
countSql,
136-
buildWhereClause(options.where, this.tableName).values
135+
buildWhereClause(options.where, this.tableName).values,
137136
);
138137

139138
const totalCount = parseInt(countResult.rows[0].count, 10);

src/repository/repository.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { buildWhereClause } from "../utils";
1616
export class Repository<T> {
1717
constructor(
1818
protected readonly tableName: string,
19-
protected readonly executor: SqlExecutor
19+
protected readonly executor: SqlExecutor,
2020
) {}
2121

2222
async findRow(where: WhereCondition<T>): Promise<T | null> {
@@ -72,7 +72,7 @@ export class Repository<T> {
7272

7373
async insertOne(
7474
data: Partial<T>,
75-
returning: Array<keyof T> = ["*"] as any
75+
returning: Array<keyof T> = ["*"] as any,
7676
): Promise<T> {
7777
const builder = new InsertQueryBuilder<T>(this.tableName, this.executor);
7878
const result = await builder.values(data).returning(returning).commit();
@@ -81,7 +81,7 @@ export class Repository<T> {
8181

8282
async insertMany(
8383
data: Partial<T>[],
84-
returning: Array<keyof T> = ["*"] as any
84+
returning: Array<keyof T> = ["*"] as any,
8585
): Promise<T[]> {
8686
const builder = new InsertQueryBuilder<T>(this.tableName, this.executor);
8787
const result = await builder.values(data).returning(returning).commit();
@@ -106,7 +106,7 @@ export class Repository<T> {
106106

107107
async delete(
108108
where: WhereCondition<T>,
109-
returning?: Array<keyof T>
109+
returning?: Array<keyof T>,
110110
): Promise<T | null> {
111111
const builder = new DeleteQueryBuilder<T>(this.tableName, this.executor);
112112
const result = await builder.where(where).returning(returning).commit();

0 commit comments

Comments
 (0)