Skip to content

Commit 13d0f3d

Browse files
committed
feat: Prefer snowflake ids table
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent cae329c commit 13d0f3d

1 file changed

Lines changed: 26 additions & 26 deletions

File tree

developer_manual/basics/storage/database.rst

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,13 @@ the table's primary key.
149149
use OCP\AppFramework\ORM\Attribute\Entity;
150150
use OCP\AppFramework\ORM\Attribute\Id;
151151
use OCP\DB\Schema\ColumnType;
152+
use OCP\Snowflake\ISnowflakeGenerator;
152153
153154
#[Entity(name: 'myapp_authors')]
154155
final class Author {
155-
#[Id]
156-
#[Column(name: 'id', type: ColumnType::Integer)]
157-
public ?int $id = null;
156+
#[Id(generatorClass: ISnowflakeGenerator::class)]
157+
#[Column(name: 'id', type: ColumnType::Bigint)]
158+
public ?string $id = null;
158159
159160
#[Column(name: 'name', type: ColumnType::String, length: 64)]
160161
public string $name;
@@ -169,24 +170,23 @@ generated for you: read and write the properties directly, for example ``$author
169170
in the ``#[Column]`` attribute, so there is no implicit camelCase-to-underscore conversion to reason about,
170171
and no need to override a mapping method to deviate from it.
171172

172-
By default, as in the example above, a bare ``#[Id]`` relies on the database's autoincrement column:
173-
``insert()`` only fills in ``$author->id`` after the row has been written. Pass a ``generatorClass`` to
174-
generate the id application-side, before the row is inserted, instead. ``OCP\Snowflake\ISnowflakeGenerator``
175-
produces such an id — a `Snowflake ID <https://en.wikipedia.org/wiki/Snowflake_ID>`_, unique across your
176-
whole cluster and sortable by creation time — which is useful when you need the id before the row exists
177-
(for example to pass it to another service as part of the same request), or to avoid the write contention
178-
a single autoincrement column creates across a cluster. Snowflake ids are ``non-empty-string`` values even
179-
though they are typically stored in a ``ColumnType::Bigint`` column:
173+
The ``generatorClass`` passed to ``#[Id]`` generates the id application-side, before the row is inserted.
174+
``OCP\Snowflake\ISnowflakeGenerator`` produces such an id — a
175+
`Snowflake ID <https://en.wikipedia.org/wiki/Snowflake_ID>`_, unique across your whole cluster and sortable
176+
by creation time — which is useful when you need the id before the row exists (for example to pass it to
177+
another service as part of the same request), or to avoid the write contention a single autoincrement
178+
column creates across a cluster. Snowflake ids are ``non-empty-string`` values even though they are
179+
typically stored in a ``ColumnType::Bigint`` column, as in the example above.
180180

181-
.. code-block:: php
181+
.. note:: For a legacy table that already has an auto-incremented primary key, a bare ``#[Id]`` without a
182+
``generatorClass`` also works: it relies on the database's autoincrement column instead, and
183+
``insert()`` only fills in the id property after the row has been written.
182184

183-
<?php
185+
.. code-block:: php
184186
185-
use OCP\Snowflake\ISnowflakeGenerator;
186-
187-
#[Id(generatorClass: ISnowflakeGenerator::class)]
188-
#[Column(name: 'id', type: ColumnType::Bigint)]
189-
public ?string $id = null;
187+
#[Id]
188+
#[Column(name: 'id', type: ColumnType::Integer)]
189+
public ?int $id = null;
190190
191191
A property without a ``#[Column]`` attribute is never read from or written to the database. This is the
192192
replacement for what used to be called *transient attributes*: just leave the property unannotated.
@@ -381,19 +381,19 @@ the same query, via a ``LEFT JOIN`` — there is no lazy-loading, and a missing
381381
382382
#[Entity(name: 'myapp_merchants')]
383383
final class Merchant {
384-
#[Id]
384+
#[Id(generatorClass: ISnowflakeGenerator::class)]
385385
#[Column(name: 'id', type: ColumnType::Bigint)]
386-
public ?int $id = null;
386+
public ?string $id = null;
387387
388388
#[Column(name: 'name', type: ColumnType::String, length: 64)]
389389
public string $name;
390390
}
391391
392392
#[Entity(name: 'myapp_orders')]
393393
final class Order {
394-
#[Id]
394+
#[Id(generatorClass: ISnowflakeGenerator::class)]
395395
#[Column(name: 'id', type: ColumnType::Bigint)]
396-
public ?int $id = null;
396+
public ?string $id = null;
397397
398398
#[ManyToOne(targetEntity: Merchant::class)]
399399
#[JoinColumn(name: 'merchant_id', referencedColumnName: 'id', nullable: true)]
@@ -414,9 +414,9 @@ the property on the other side, and the ``#[JoinColumn]`` only repeated on the o
414414
415415
#[Entity(name: 'myapp_customers')]
416416
final class Customer {
417-
#[Id]
417+
#[Id(generatorClass: ISnowflakeGenerator::class)]
418418
#[Column(name: 'id', type: ColumnType::Bigint)]
419-
public ?int $id = null;
419+
public ?string $id = null;
420420
421421
#[OneToOne(targetEntity: Cart::class, mappedBy: 'customer')]
422422
#[JoinColumn(name: 'cart_id', referencedColumnName: 'id')]
@@ -425,9 +425,9 @@ the property on the other side, and the ``#[JoinColumn]`` only repeated on the o
425425
426426
#[Entity(name: 'myapp_carts')]
427427
final class Cart {
428-
#[Id]
428+
#[Id(generatorClass: ISnowflakeGenerator::class)]
429429
#[Column(name: 'id', type: ColumnType::Bigint)]
430-
public ?int $id = null;
430+
public ?string $id = null;
431431
432432
#[OneToOne(targetEntity: Customer::class, invertedBy: 'cart')]
433433
#[JoinColumn(name: 'customer_id', referencedColumnName: 'id')]

0 commit comments

Comments
 (0)