-
Notifications
You must be signed in to change notification settings - Fork 0
π Add Composite Primary Key Support (Fixes #1) #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
omvmike
wants to merge
9
commits into
main
Choose a base branch
from
feature/typeorm-composite-keys
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5ab0dee
enhance TypeormFakeEntityService to support composite primary keys anβ¦
omvmike 04e9162
add FakeFollowerService and Follower entity with composite key support
omvmike 96fb2c9
add tests for entityIds management in fake services, including singleβ¦
omvmike bcd4b00
add tests for entityIds management in fake services, including composβ¦
omvmike d4db5a3
add tests for entityIds management in FakeFollowerService, including β¦
omvmike 30d0180
update TypeScript configuration to change root directory and test regβ¦
omvmike 23fe930
add primary key detection tests for single and composite keys in Sequβ¦
omvmike 626942d
add integration tests for composite key operations in Sequelize, inclβ¦
omvmike 878d10a
update documentation and version to 0.10.0
omvmike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "fake-entity-service", | ||
"version": "0.9.3", | ||
"version": "0.10.0", | ||
"description": "A fake database entities service for testing", | ||
"author": { | ||
"email": "[email protected]", | ||
|
@@ -61,8 +61,8 @@ | |
"json", | ||
"ts" | ||
], | ||
"rootDir": "src", | ||
"testRegex": ".*\\.spec\\.ts$", | ||
"rootDir": ".", | ||
"testRegex": "tests/.*\\.spec\\.ts$", | ||
"transform": { | ||
"^.+\\.(t|j)s$": "ts-jest" | ||
}, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,8 +200,8 @@ export class SequelizeFakeEntityService<TEntity extends Model> extends FakeEntit | |
} | ||
const idFieldName = this.getIdFieldNames()[0]; | ||
const idValue = e[idFieldName]; | ||
if (idValue === undefined) { | ||
throw new Error(`Id field "${idFieldName}" is empty`) | ||
if (idValue === undefined || idValue === null) { | ||
throw new Error(`Primary key field "${idFieldName}" is empty or null in entity ${this.repository.name}`); | ||
} | ||
return e[this.getIdFieldNames()[0]]; | ||
} | ||
|
@@ -337,11 +337,25 @@ export class SequelizeFakeEntityService<TEntity extends Model> extends FakeEntit | |
where, | ||
transaction: tx, | ||
}); | ||
}, transaction).then((deletionResult) => { | ||
}, transaction).then((affectedCount) => { | ||
// Remove deleted entity IDs from the entityIds array | ||
this.entityIds = []; | ||
return deletionResult; | ||
}) | ||
if (this.hasCompositeId()) { | ||
// For composite keys, need deep comparison of objects | ||
this.entityIds = this.entityIds.filter(entityId => { | ||
return !entityIds.some(deletedId => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The delete method references Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
// Check if all key fields match | ||
return this.getIdFieldNames().every(field => | ||
entityId[field] === deletedId[field] | ||
); | ||
}); | ||
}); | ||
} else { | ||
// For single keys, use simple includes | ||
this.entityIds = this.entityIds.filter(id => !entityIds.includes(id)); | ||
} | ||
return affectedCount; | ||
}); | ||
|
||
} | ||
|
||
/** | ||
|
@@ -381,4 +395,32 @@ export class SequelizeFakeEntityService<TEntity extends Model> extends FakeEntit | |
}); | ||
return this; | ||
} | ||
|
||
/** | ||
* Build where conditions for composite primary keys | ||
*/ | ||
private buildCompositeKeyWhere(keyValues: Record<string, any>): any { | ||
const where = {}; | ||
for (const [key, value] of Object.entries(keyValues)) { | ||
if (!this.getIdFieldNames().includes(key)) { | ||
throw new Error(`Invalid primary key field "${key}" for entity ${this.repository.name}`); | ||
} | ||
where[key] = value; | ||
} | ||
return where; | ||
} | ||
|
||
/** | ||
* Find entity by composite primary key | ||
*/ | ||
public async findByCompositeKey(keyValues: Record<string, any>, transaction?: Transaction): Promise<TEntity | undefined> { | ||
return this.withTransaction(async (tx) => { | ||
const where = this.buildCompositeKeyWhere(keyValues); | ||
const result = await this.repository.findOne({ | ||
where, | ||
transaction: tx | ||
}); | ||
return result || undefined; // Convert null to undefined | ||
}, transaction); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Composite primary keys arenβt handled in
getId
; it only returns a single key. You need to detecthasCompositeId()
and return an object of all key fields (e.g., via a sharedpickKeysFromObject
).Copilot uses AI. Check for mistakes.