Skip to content

Commit cc20fd0

Browse files
authored
Merge pull request #9 from maifeeulasad/orm-flavor-runtime-method-generation
[feature]: orm flavor runtime method generation w repo pattern;
2 parents 37609cc + a6f613e commit cc20fd0

5 files changed

Lines changed: 294 additions & 187 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ jobs:
1515
uses: actions/setup-node@v4
1616
with:
1717
node-version: 20.x
18+
- name: Install build tools
19+
run: npm i -g pnpm
1820
- name: Install dependencies
19-
run: npm ci
21+
run: pnpm i
2022
- name: Run tests
21-
run: npm run test
23+
run: pnpm test
2224
- name: Run tests with coverage
23-
run: npm run test:coverage
25+
run: pnpm test:coverage

__tests__/annotation.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,23 @@ describe('IndexedDB annotation', () => {
7070
expect(db).toBeDefined();
7171
});
7272

73+
it('should expose getAvailableEntities utility', async () => {
74+
@DataClass()
75+
class Foo {
76+
@KeyPath()
77+
id!: string;
78+
}
79+
80+
@DataClass()
81+
class Bar {
82+
@KeyPath()
83+
id!: string;
84+
}
85+
86+
const db = await Database.build('UtilDB', [Foo, Bar]);
87+
const entities = db.getAvailableEntities();
88+
expect(entities).toContain('Foo');
89+
expect(entities).toContain('Bar');
90+
});
91+
7392
});

__tests__/database.test.ts

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Database, KeyPath, DataClass, Index } from '../index';
1+
import { Database, KeyPath, DataClass, Index, EntityRepository } from '../index';
22

33
@DataClass()
44
class User {
@@ -20,65 +20,59 @@ class User {
2020
}
2121

2222
describe('IndexedDB CRUD', () => {
23-
let db: Database;
23+
let db: Record<string, EntityRepository<any>>;
2424

2525
beforeAll(async () => {
2626
db = await Database.build('TestDB', [User]);
2727
});
2828

2929
it('should create a new user', async () => {
3030
const user = new User('u1', 'Alice', 30);
31-
await db.create(User, user);
32-
33-
const stored = await db.read(User, 'u1');
31+
await db.User.create(user);
32+
const stored = await db.User.read('u1');
3433
expect(stored).toEqual(user);
3534
});
3635

3736
it('should update an existing user', async () => {
3837
const user = new User('u1', 'Alice', 31);
39-
await db.update(User, user);
40-
41-
const updated = await db.read(User, 'u1');
38+
await db.User.update(user);
39+
const updated = await db.User.read('u1');
4240
expect(updated?.age).toBe(31);
4341
});
4442

4543
it('should list all users', async () => {
46-
const users = await db.list(User);
44+
const users = await db.User.list();
4745
expect(users.length).toBeGreaterThan(0);
48-
expect(users.find(u => u.id === 'u1')).toBeDefined();
46+
expect(users.find((u: User) => u.id === 'u1')).toBeDefined();
4947
});
5048

5149
it('should paginate results', async () => {
52-
await db.create(User, new User('u2', 'Bob', 25));
53-
await db.create(User, new User('u3', 'Charlie', 22));
54-
55-
const page1 = await db.listPaginated(User, 1, 2);
56-
const page2 = await db.listPaginated(User, 2, 2);
57-
50+
await db.User.create(new User('u2', 'Bob', 25));
51+
await db.User.create(new User('u3', 'Charlie', 22));
52+
const page1 = await db.User.listPaginated(1, 2);
53+
const page2 = await db.User.listPaginated(2, 2);
5854
expect(page1.length).toBe(2);
5955
expect(page2.length).toBeGreaterThanOrEqual(0);
6056
});
6157

6258
it('should delete a user', async () => {
63-
await db.delete(User, 'u1');
64-
const deleted = await db.read(User, 'u1');
59+
await db.User.delete('u1');
60+
const deleted = await db.User.read('u1');
6561
expect(deleted).toBeUndefined();
6662
});
6763

6864
it('should find users by email index', async () => {
6965
const user = new User('u5', 'Diana', 35, 'diana@example.com');
70-
await db.create(User, user);
71-
72-
const found = await db.findByIndex(User, 'email', 'diana@example.com');
66+
await db.User.create(user);
67+
const found = await db.User.findByIndex('email', 'diana@example.com');
7368
expect(found.length).toBe(1);
7469
expect(found[0].name).toBe('Diana');
7570
});
7671

7772
it('should find one user by email index', async () => {
7873
const user = new User('u6', 'Eve', 29, 'eve@example.com');
79-
await db.create(User, user);
80-
81-
const found = await db.findOneByIndex(User, 'email', 'eve@example.com');
74+
await db.User.create(user);
75+
const found = await db.User.findOneByIndex('email', 'eve@example.com');
8276
expect(found).toBeDefined();
8377
expect(found!.name).toBe('Eve');
8478
});

__tests__/indexing.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,72 +22,72 @@ class User {
2222
}
2323

2424
describe('IndexedDB Indexing', () => {
25-
let db: Database;
25+
let db: any;
2626

2727
beforeAll(async () => {
2828
db = await Database.build('IndexingTestDB', [User]);
2929
});
3030

3131
beforeEach(async () => {
3232
// Clear existing data
33-
const users = await db.list(User);
33+
const users = await db.User.list();
3434
for (const user of users) {
35-
await db.delete(User, user.id);
35+
await db.User.delete(user.id);
3636
}
3737

3838
// Add test data
39-
await db.create(User, new User('u1', 'alice@example.com', 30, 'Alice'));
40-
await db.create(User, new User('u2', 'bob@example.com', 25, 'Bob'));
41-
await db.create(User, new User('u3', 'charlie@example.com', 30, 'Charlie'));
42-
await db.create(User, new User('u4', 'alice@work.com', 28, 'Alice Smith'));
39+
await db.User.create(new User('u1', 'alice@example.com', 30, 'Alice'));
40+
await db.User.create(new User('u2', 'bob@example.com', 25, 'Bob'));
41+
await db.User.create(new User('u3', 'charlie@example.com', 30, 'Charlie'));
42+
await db.User.create(new User('u4', 'alice@work.com', 28, 'Alice Smith'));
4343
});
4444

4545
it('should find users by email index', async () => {
46-
const users = await db.findByIndex(User, 'email', 'alice@example.com');
46+
const users = await db.User.findByIndex('email', 'alice@example.com');
4747
expect(users.length).toBe(1);
4848
expect(users[0].name).toBe('Alice');
4949
expect(users[0].email).toBe('alice@example.com');
5050
});
5151

5252
it('should find multiple users by age index', async () => {
53-
const users = await db.findByIndex(User, 'age', 30);
53+
const users = await db.User.findByIndex('age', 30);
5454
expect(users.length).toBe(2);
55-
const names = users.map(u => u.name).sort();
55+
const names = users.map((u: User) => u.name).sort();
5656
expect(names).toEqual(['Alice', 'Charlie']);
5757
});
5858

5959
it('should find single user by email index', async () => {
60-
const user = await db.findOneByIndex(User, 'email', 'bob@example.com');
60+
const user = await db.User.findOneByIndex('email', 'bob@example.com');
6161
expect(user).toBeDefined();
6262
expect(user!.name).toBe('Bob');
6363
expect(user!.age).toBe(25);
6464
});
6565

6666
it('should return undefined when no user found by index', async () => {
67-
const user = await db.findOneByIndex(User, 'email', 'nonexistent@example.com');
67+
const user = await db.User.findOneByIndex('email', 'nonexistent@example.com');
6868
expect(user).toBeUndefined();
6969
});
7070

7171
it('should return empty array when no users found by index', async () => {
72-
const users = await db.findByIndex(User, 'age', 99);
72+
const users = await db.User.findByIndex('age', 99);
7373
expect(users).toEqual([]);
7474
});
7575

7676
it('should throw error when querying non-existent index', async () => {
77-
await expect(db.findByIndex(User, 'nonexistent', 'value')).rejects.toThrow(
77+
await expect(db.User.findByIndex('nonexistent', 'value')).rejects.toThrow(
7878
"Index 'nonexistent' does not exist on User"
7979
);
8080
});
8181

8282
it('should throw error when querying non-existent index with findOneByIndex', async () => {
83-
await expect(db.findOneByIndex(User, 'nonexistent', 'value')).rejects.toThrow(
83+
await expect(db.User.findOneByIndex('nonexistent', 'value')).rejects.toThrow(
8484
"Index 'nonexistent' does not exist on User"
8585
);
8686
});
8787

8888
it('should work with non-indexed fields not being queryable by index', async () => {
8989
// name field is not indexed, so this should throw an error
90-
await expect(db.findByIndex(User, 'name', 'Alice')).rejects.toThrow(
90+
await expect(db.User.findByIndex('name', 'Alice')).rejects.toThrow(
9191
"Index 'name' does not exist on User"
9292
);
9393
});

0 commit comments

Comments
 (0)