Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/palform/prefixed-tsid
Create type-safe Stripe-like prefixed database IDs using TSIDs
https://github.com/palform/prefixed-tsid
database rust tsid
Last synced: 3 months ago
JSON representation
Create type-safe Stripe-like prefixed database IDs using TSIDs
- Host: GitHub
- URL: https://github.com/palform/prefixed-tsid
- Owner: palform
- License: mit
- Created: 2024-09-11T11:06:37.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-09-22T10:16:52.000Z (3 months ago)
- Last Synced: 2024-09-29T11:25:34.172Z (3 months ago)
- Topics: database, rust, tsid
- Language: Rust
- Homepage: https://docs.rs/prefixed-tsid/latest/prefixed_tsid/index.html
- Size: 26.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Prefixed TSIDs
[Documentation](https://docs.rs/prefixed-tsid/latest/prefixed_tsid/index.html)
This library allows you to easily create and manage Stripe-style prefixed IDs for your database, using the [`tsid`](https://github.com/jakudlaty/tsid/) crate. For example:
```
org_0GTM2MERJJE1T
```Each type of resource gets its own unique prefix, and is represented as a type-safe ID in your code. You can declare resources manually or using our handy macro:
```rust
id_resource_type!(IDOrganisation, "org");// equals:
#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)]
pub struct IDOrganisation;
impl TSIDResource for IDOrganisation {
fn prefix() -> Option {
Some("org".to_owned())
}
}
```Then, you can easily create a random instance of the ID:
```rust
let org_id = TSIDDatabaseID::::random();
```When stringified (or serialised using the `serde` feature), it will always include the prefix in the `TSIDResource` type.
When stored in your database, it's simply a `u64` without the prefix information, since your database presumably keeps different resources in different tables anyway. The library comes with a built-in `sea-orm` integration.
## Features
By default, only `serde` is enabled.
- `serde`: serialize/deserialize integration. Adds the correct prefix when serializing, and returns an error if the prefix is missing or is wrong during deserialization.
- `rocket`: integration for `FromParam` and `FromFormField` so you can use a `TSIDDatabaseID` in path components, query parameters, form requests, etc.
- `sea-orm`: allows you to use `TSIDDatabaseID` in your entity structs, so the ID gets turned into a `u64` and vice versa.
- `schemars`: allows generating JSON Schema containing a RegEx to match your prefixed ID.
- `ts-rs`: for extra-nice WASM compatibility, allows generating a string type to represent the ID type in Typescript.## License
Made by Palform Ltd (registered company 15796859 in the UK) under the **MIT License**.