Skip to content

feat(entity): Provide a new API for entities#56199

Draft
CarlSchwan wants to merge 7 commits into
masterfrom
carl/attributes-entity-2
Draft

feat(entity): Provide a new API for entities#56199
CarlSchwan wants to merge 7 commits into
masterfrom
carl/attributes-entity-2

Conversation

@CarlSchwan

@CarlSchwan CarlSchwan commented Nov 4, 2025

Copy link
Copy Markdown
Member

Entity are now simple data object with attributes for the database mapping. They are manipulated via a Repository which reads the attributes to insert, update, delete and queries the PDO from the database.

Using attributes makes it easier to extends in the future with relations (ManyToMany, OneToMany, OneToOne, ...).

The design of the Repository is based on a mix between the Doctrine ORM repository while keeping some methods from the QBMapper for easier porting.

Example of a entity

#[Entity(name: 'twofactor_backupcodes')]
final class BackupCode {
	#[Id(generatorClass: IGenerator::class)]
	#[Column(name: 'id', type: Types::STRING, length: 64, nullable: false)]
	public ?string $id = null;

	#[Column(name: 'user_id', type: Types::STRING, length: 64, nullable: false)]
	public string $userId;

	#[Column(name: 'code', type: Types::STRING, length: 128, nullable: false)]
	public string $code;

	#[Column(name: 'used', type: Types::SMALLINT, nullable: false, default: 0)]
	public int $used = 0;
}

Checklist

@blizzz

blizzz commented Nov 5, 2025

Copy link
Copy Markdown
Member

Would have preferred the new API and the reference implementation in backup codes and Tagging to be two different commits. But cool.

What do you think of a CacheEnabled repository next to it (that wraps the Repository and just has a runtime cache)? Have a mapper with such functionality in tables, and saw it on at least another app as well, might be worth to have something like this implemented once properly?

@CarlSchwan

Copy link
Copy Markdown
Member Author

Would have preferred the new API and the reference implementation in backup codes and Tagging to be two different commits. But cool.

Currently it's more of a prototype, I'll split that in multiple commits.

What do you think of a CacheEnabled repository next to it (that wraps the Repository and just has a runtime cache)? Have a mapper with such functionality in tables, and saw it on at least another app as well, might be worth to have something like this implemented once properly?

Yes that could make sense. That I also saw in multiple apps is the relation stuff: https://github.com/nextcloud/deck/blob/main/lib/Db/RelationalEntity.php which could be quite handy

Comment thread lib/public/AppFramework/Db/Attribute/Column.php Outdated
Comment thread lib/public/AppFramework/Db/Repository.php Outdated
Comment thread lib/public/AppFramework/Db/Repository.php Outdated

@come-nc come-nc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks promising.

Comment thread apps/twofactor_backupcodes/lib/Db/BackupCode.php Outdated
Comment thread lib/private/Tags.php
Comment on lines +381 to +387
$this->mapper->insert($tag);
} catch (Exception $e) {
if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$this->logger->error($e->getMessage(), [
'exception' => $e,
'app' => 'core',
]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an insertIfNotExists method for this in Repository.
There is insertOrUpdate already.

Comment thread lib/public/AppFramework/Db/Repository.php Outdated
Comment thread lib/public/AppFramework/Db/Repository.php Outdated
@CarlSchwan
CarlSchwan force-pushed the carl/attributes-entity-2 branch from 27d1fd8 to 29a9903 Compare June 15, 2026 15:27
CarlSchwan and others added 5 commits July 21, 2026 01:16
Entity are now simple data object with attributes for the database
mapping. They are manipulated via a Repository which reads the
attributes to insert, update, delete and queries the PDO from the
database.

Using attributes makes it easier to extends in the future with relations
(ManyToMany, OneToMany, OneToOne, ...).

The design of the Repository is based on a mix between the Doctrine ORM
repository while keeping some methods from the QBMapper for easier
porting.

Signed-off-by: Carl Schwan <carl.schwan@nextcloud.com>
And do a lot of refactoring

Signed-off-by: Carl Schwan <carlschwan@kde.org>
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
Assisted-by: ClaudeCode:claude-opus-4-8
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
@CarlSchwan
CarlSchwan force-pushed the carl/attributes-entity-2 branch from 29a9903 to 0ac62ed Compare July 21, 2026 11:52
@CarlSchwan
CarlSchwan force-pushed the carl/attributes-entity-2 branch from 0ac62ed to a2cdc02 Compare July 21, 2026 11:54
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
This is a left over from the maps prototype

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
@CarlSchwan
CarlSchwan force-pushed the carl/attributes-entity-2 branch from f362314 to 68c1790 Compare July 21, 2026 14:12
@CarlSchwan CarlSchwan added 3. to review Waiting for reviews feature: database Database related DB labels Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review Waiting for reviews AI assisted feature: database Database related DB

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants