Skip to content

Initialize the D1 database ​

Cloudflare D1 is a distributed SQLite database. Signet uses it to store users, orders, products, licenses, and the audit log.

You've already created the database and pushed the schema in the previous step. This page explains what was created and why, so you understand the data model before you start using it.

The five tables ​

users ​

Stores registered accounts.

ColumnPurpose
idUUID, primary key
emailUnique, lowercase
nicknameDisplay name (shown publicly)
password_hashPBKDF2-SHA256 hash
password_saltPer-user salt
created_atUnix ms timestamp

The email is stored in lowercase and enforced unique. The password hash uses 100,000 iterations of PBKDF2-SHA256 — same parameters as the admin password.

orders ​

Stores paid orders from payment providers.

ColumnPurpose
idUUID, primary key
order_noOrder number from the provider
providerAdapter name (afdian, stripe, manual, …)
provider_userBuyer's ID on the payment platform (optional)
provider_rawOriginal webhook payload (JSON)
amountPayment amount as a string
status2 = paid
bound_user_idWhich user this order belongs to
bound_atWhen it was bound
created_atWhen the order arrived

The pair (provider, order_no) is unique. This means two different providers can issue order numbers that happen to collide without conflict.

Virtual orders created by the admin panel use the prefix MANUAL- in order_no to prevent anyone from trying to bind them through the normal flow.

products ​

Stores products created by users.

ColumnPurpose
idUUID, primary key
user_idOwner
nameProduct name
product_uidShort ID embedded in license files
created_atUnix ms timestamp

The product_uid is what ends up in the license file. It's short (16 chars) and uppercase, easy to read in support emails.

licenses ​

Stores every license issuance.

ColumnPurpose
idUUID, primary key
product_idWhich product
user_idWhich user
fingerprintBuyer's user ID (for tracing)
issued_atUnix seconds timestamp
banned1 if revoked
manual1 if issued by admin
noteFree-form admin notes
ban_reasonShown publicly if banned

Note that the same user can issue multiple licenses for the same product (different machines, re-downloads). Each issuance gets its own row. This is intentional — it gives you an audit trail.

audit_log ​

Stores every administrative action.

ColumnPurpose
idUUID, primary key
admin_idAdmin username (not a foreign key)
actionban_license, grant_pro, admin_reset_password, …
targetID of the affected record
detailJSON with action-specific data
created_atUnix ms timestamp

Admin usernames are stored directly, not as foreign keys, because admins are not rows in the users table — they come from environment variables.

Verifying the schema ​

Run:

bash
npx wrangler d1 execute signet-db \
  --command="SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;" \
  --remote

Expected output:

_cf_KV
audit_log
licenses
orders
products
users

If you see all six, you're good.

Inspecting a table ​

To see the structure of any table:

bash
npx wrangler d1 execute signet-db \
  --command="PRAGMA table_info(users);" \
  --remote

Querying data ​

You can run any SQL query through wrangler. For example:

bash
# Count registered users
npx wrangler d1 execute signet-db \
  --command="SELECT COUNT(*) FROM users;" \
  --remote

# List the 10 most recent orders
npx wrangler d1 execute signet-db \
  --command="SELECT order_no, provider, amount, created_at FROM orders ORDER BY created_at DESC LIMIT 10;" \
  --remote

You can also use the D1 Console in the Cloudflare dashboard — it has a nicer interface for ad-hoc queries.

Backups ​

D1 doesn't have automatic point-in-time recovery on the free tier. For critical data, periodically export:

bash
npx wrangler d1 export signet-db --output=backup.sql --remote

Store the file somewhere safe. In a disaster, you can restore with:

bash
npx wrangler d1 execute signet-db --file=backup.sql --remote

What's next ​

The infrastructure is ready. Now configure the payment provider so users can actually buy things.

→ Configure Afdian

Apache License 2.0 · Open source, no black box