Skip to content
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

Drizzle ORM #13

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
845770b
Drizzle ORM
rastislavcore Sep 13, 2024
1927ca1
Fixes
rastislavcore Sep 13, 2024
48e267b
Updated compatibility mode
rastislavcore Sep 13, 2024
da38c9a
Update
rastislavcore Sep 13, 2024
aa120fa
Updated config for Postgres
rastislavcore Sep 13, 2024
580ce1e
Compat mode and Hyperdrive
rastislavcore Sep 15, 2024
0955e48
Fixed Env as always string and refactor db
rastislavcore Oct 2, 2024
54b8bd6
Updated schemas
rastislavcore Oct 7, 2024
19bbabc
Testing CF pages
rastislavcore Oct 28, 2024
5224d8d
Testing CF
rastislavcore Nov 4, 2024
295114e
Update environments
rastislavcore Nov 4, 2024
18441ad
Cleaned wrangler
rastislavcore Nov 4, 2024
b7ec28e
Updated wrangler
rastislavcore Nov 4, 2024
94509ec
Added dependency
rastislavcore Nov 4, 2024
7b906ca
Fixes
rastislavcore Nov 4, 2024
609955b
Replaced adapter to auto
rastislavcore Nov 4, 2024
2523b9c
Wallet connect
rastislavcore Nov 19, 2024
6c2fd4e
Fixed variables
rastislavcore Nov 19, 2024
1d7cf38
Fixes
rastislavcore Nov 27, 2024
8a2832f
Added toast
rastislavcore Nov 28, 2024
f3aa633
Added PWA
rastislavcore Nov 28, 2024
3408bf9
Updates
rastislavcore Nov 28, 2024
3115269
Updated deployment variables
rastislavcore Nov 28, 2024
13c1bac
Deleted staging
rastislavcore Nov 28, 2024
105309b
Access optional attributes
rastislavcore Nov 29, 2024
6e0fe28
Fixes
rastislavcore Nov 29, 2024
eea3c21
Fixed PWA; Auto adapter
rastislavcore Nov 29, 2024
aeb54f0
Refactor components
rastislavcore Nov 29, 2024
a1ced31
Move config file
rastislavcore Nov 29, 2024
afb7be6
Disabling PWA
rastislavcore Nov 29, 2024
15809f1
Turned off preprocess
rastislavcore Nov 29, 2024
2dfc4dc
Preprocess back
rastislavcore Nov 29, 2024
241885e
All to components
rastislavcore Nov 29, 2024
15fd49e
Test
rastislavcore Nov 29, 2024
7fd1445
Removed build command
rastislavcore Nov 29, 2024
693ced9
Update
rastislavcore Dec 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
CLOUDFLARE_ACCOUNT_ID=<YOUR_ACCOUNT_ID_VALUE>
CLOUDFLARE_API_TOKEN=<YOUR_API_TOKEN_VALUE>
ENABLE_API=false
ENABLE_AUTH=false
REG_COREID=true
VERIFIED_ONLY=false
VERIFIED_EXPIRATION_DAYS=
ENABLE_FILE_ACCESS=false
DB_TYPE=
DB_NAME=
KV_NAME=
R2_NAME=
PRISMA_PROVIDER=
PRISMA_API_KEY=
PASSKEY_DURATION=300
DB_TYPE=
DB_URL=
DB_AUTH_TOKEN=
BCH_DB_TYPE=
BCH_DB_URL=
BCH_DB_AUTH_TOKEN=
PUBLIC_ENABLE_AUTH=false
PUBLIC_ENABLE_WEB4=true
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ blank_issues_enabled: false
contact_links:
- name: Readme
url: https://github.com/bchainhub/dapp-sveltekit-boilerplate#readme
about: Read more about the Dapp Boilerplate
about: Read more about the dApp Boilerplate
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ node_modules
# OS
.DS_Store
Thumbs.db
Icon
/Icon
._*

# Env
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
^20.9.0
202 changes: 119 additions & 83 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@ Welcome to the SvelteKit Boilerplate! This project provides a solid foundation f
- **[PostCSS](https://postcss.org/)**: Transform CSS with JavaScript plugins.
- **[ESLint](https://eslint.org/)**: Linting utility for identifying and reporting on patterns in JavaScript.
- **[Prettier](https://prettier.io/)**: Code formatter for maintaining consistent style.
- **[Auth.js](https://authjs.dev/)**: Authentication library for secure login functionality.
- **[Hono API](https://hono.dev/)**: Web framework for building fast web applications and APIs.
- **[Lucide icons](https://lucide.dev/)**: Beautiful and customizable icon set.
- **[Wallet Connect](https://walletconnect.network/)**: Connect to decentralized applications using WalletConnect.
- **[blo identicons](https://github.com/bchainhub/blo)**: blo is a small and fast library to generate Blockchain identicons.
- **[ICAN / IBAN validation](https://github.com/bchainhub/ican.js)**: Validate international bank account numbers.
- **[Exchange Number Format](https://github.com/bchainhub/exchange-rounding)**: Utility for formatting and rounding exchange numbers.

## Compatibility
## Databases

- **[CloudFlare Pages](https://developers.cloudflare.com/pages/)**: Deploy web applications directly to Cloudflare's global network.
- **[CloudFlare Functions API](https://developers.cloudflare.com/workers/)**: Build and deploy serverless functions.
- **[CloudFlare KV](https://developers.cloudflare.com/workers/runtime-apis/kv/)**: Key-Value storage for serverless applications.
- **[Cloudflare R2](https://developers.cloudflare.com/r2/)**: Cloudflare R2 Storage allows developers to store large amounts of unstructured data.
- **[Drizzle ORM](https://orm.drizzle.team)**: Headless TypeScript ORM with a head.
- SQLite / CloudFlare D1 database.
- Postgres database.
- Hyperdrive connector.
- Web4 devices.
- and many more... (write your own connector in the `src/server/db` directory)

## Database Support
### Database connectors

- **[CloudFlare D1](https://developers.cloudflare.com/d1/)**: Database solution for Cloudflare applications.
- **[Prisma](https://www.prisma.io/)**: Prisma with Accelerate. Modern database access for TypeScript & Node.js.
- [Postgres.js](https://github.com/porsager/postgres) for Postgres database.
- [libSQL](https://github.com/tursodatabase/libsql) for SQLite and Web4 database.
- [Hyperdrive](https://developers.cloudflare.com/hyperdrive/) connector for Cloudflare (paid plan).

## Installation

Expand Down Expand Up @@ -63,51 +65,62 @@ npm run preview

## Configuration

Update the `site.config.ts` file located in `/src` to customize the project settings.
Update the `vite.config.ts` file located in the main folder to customize the project settings.

You can customize application settings in `wrangler.toml` file:

- `AUTH_SECRET`: Secret key for authentication.
- `LOGIN_MAX_AGE`: Login expiration time. Default is 24 hours (86400).
- `DB_INIT`: Enable the functionality to initialize the database using token value. Append `tk` to the URL to define custom token. (Example: `…?tk=yourtoken`).
- `NODE_VERSION`: Node.js version.

Environment variables are stored in the `.env` file. You can add your own environment variables to this file.

- `CLOUDFLARE_ACCOUNT_ID`: Cloudflare account ID.
- `CLOUDFLARE_API_TOKEN`: Cloudflare API token.
- `ENABLE_API`: Enable or disable the API.
- `ENABLE_AUTH`: Enable or disable authentication.
- `REG_COREID`: Valid Core ID for WebAuthn provider registration. Default is true.
- `VERIFIED_ONLY`: Enable or disable only verified users. This prevents not KYC verified users to register.
- `VERIFIED_EXPIRATION_DAYS`: Verified expiration time. (Value in days).
- `ENABLE_FILE_ACCESS`: Enable or disable file access.
- `KV_NAME`: Cloudflare KV namespace name.
- `R2_NAME`: Cloudflare R2 namespace name.
- `PASSKEY_DURATION`: Passkey expiration time. Default is 2 minutes (120000).
- `CAPTURE_COUNTRY`: Capture country from CF pages, Netlify, Vercel. If enabled.
- `CAPTURE_CITY`: Capture city from CF pages, Netlify, Vercel. If enabled.
- `CAPTURE_COUNTRY`: Provide country as a variable from CF pages, Netlify, Vercel. If enabled.
- `CAPTURE_CITY`: Provide city as a variable from CF pages, Netlify, Vercel. If enabled.

Public variables:

- `PUBLIC_ENABLE_AUTH`: Enable or disable authentication. Default is false (disabled).
- `PUBLIC_ENABLE_WEB4`: Enable or disable Web4 functionality. Default is false (disabled).
- `PUBLIC_WEB4_URL`: Web4 URL if differs from default. Default is: `file:local.db`.

Environment variables for database setup:

- `DB_TYPE`: Database type: D1, PRISMA. Default is no database (empty string).
- `DB_NAME`: Database name.
- `PRISMA_PROVIDER`: Prisma provider.
- `PRISMA_API_KEY`: Prisma API key.
- `DB_TYPE`: Database type: SQLite, PostgeSQL. Default is no database (empty string).
- `DB_URL`: Database URL.
- `DB_AUTH_TOKEN`: Database authentication token.
- `DB_SSL`: Enable or disable SSL for Postgres database.

Generate the authentication secret key, cleaning token, JWT secret in secure way or using the following command:
Blockchain database setup:

```bash
openssl rand -base64 33
```
- `BCH_DB_TYPE`: Database type: SQLite, PostgeSQL. Default is no database (empty string).
- `BCH_DB_URL`: Database URL.
- `BCH_DB_AUTH_TOKEN`: Database authentication token.
- `BCH_DB_SSL`: Enable or disable SSL for Postgres database.

## Web4

Web4 is the tool for connecting to the offline blockchain data and ecosystem using specialized devices. You can use it for blockchain operations and interaction using radio frequencies. Native support for Web4 is included in the project.

## API
Currently, we are supporting the following functionality:

Api is disabled by default. You can enable it by setting `ENABLE_API=true` in the `.env` filed. If you don't need the API, you can remove the `functions/` directory. It is good practice to disable the API in production environment if you don't need it as well as removing the folder.
- Read-only data from the blockchain ETL service.
- TxMS transactions.

Api is using Hono API framework. You can find more information in the [Hono API documentation](https://hono.dev/).
What is Web4?

Api is versioned and the version is defined in the `functions/api/${version}/` folder, where `${version}` is the number of version. It is good practice to divide the versions in the folders.
In short Web4 is an alternative network and operations instead of the Internet. It is a secure and reliable way to interact with the blockchain data. You can use it for blockchain operations and interaction using radio frequencies as well as 0G connectivity. But it is not focused only on Blockchain, can it be various types of data.

Why do you need Web4?

In some cases you need to interact with the blockchain data offline. You can use Web4 + Web3 for this purpose. It is a secure and reliable way to interact with the blockchain data. You can use it for blockchain operations and interaction using radio frequencies as well as 0G connectivity.

## PWA

The project is a Progressive Web Application (PWA) by default. You can customize the PWA settings in the `vite.config.ts` file.

## Styling

Expand All @@ -117,12 +130,6 @@ Custom styles and variables are defined in the `src/css` directory. You can use

You can use tool to generate TailwindCSS colors: [TailwindCSS Color Generator](https://javisperez.github.io/tailwindcolorshades/) or [TailwindCSS Color Shades](https://www.tailwindshades.com/).

## Connection to D1

You can connect your application to Cloudflare D1 by setting the `DB_NAME` variable in the `.env` file. You can find the D1 namespace in the Cloudflare dashboard and bind it with your `DB_NAME` setup.

Make sure the `wrangler.toml` file is properly configured with the correct DB binding information.

## Connection to KV

You can connect your application to Cloudflare KV by setting the `KV_NAME` variable in the `.env` file. You can find the KV namespace in the Cloudflare dashboard and bind it with your `KV_NAME` setup.
Expand Down Expand Up @@ -188,78 +195,107 @@ For more details on configuring and using Wrangler with Cloudflare Pages, visit
3. Configure the build settings.
4. Deploy the project.

## Authentication
## Database setup

The project uses Auth.js for authentication together with Passkey & CorePass.
Types of connectors:

Dependencies:
- [D1 database](https://developers.cloudflare.com/d1/) Cloudflare's free plan
- [Hyperdrive connector](https://developers.cloudflare.com/hyperdrive/) Cloudflare's paid plan
- ORB Web4 devices - local blockchain - SQLite based
- DePIN connector for ORB devices

- Cloudflare D1 database.
- DB initialization process.
- Node.js version 20.9.0 or higher.
- CorePass if you would like to use Pipe - activation model.
We are recommending purchasing the Hyperdrive connector for production use. There are many advantages and you can bind plenty of databases.

Before first run of your application you need to:
You can use SQLite, Postgres database, or any other supported by Drizzle. Supported drivers by Cloudflare are listed [here](https://developers.cloudflare.com/hyperdrive/configuration/connect-to-postgres/#supported-drivers). The database setup is defined in the `.env` file prefixed with `DB_`.

- Create a new database.
- Set the DB name `DB_NAME` in the `wrangler.toml` file or dashboard.
- Initialize the database with enabling `DB_INIT=yourtoken` in the `wrangler.toml` file or dashboard.
- Load the url: `yoururl.com/db/init?tk=yourtoken` to initialize the seeding.
- Set the `DB_INIT=false` in the `wrangler.toml` file or dashboard. This is important to disable db initialization.
Database requires Cloudflare flag `compatibility_flags = [ "nodejs_compat" ]` which is indicationg of using [Node.js compatibility mode](https://developers.cloudflare.com/workers/runtime-apis/nodejs/#enable-nodejs-with-workers). You can consider to use `nodejs_compat_v2` if you need additional functionality.

## Security
### ORM Database Setup

There is difference between access local environment variables and environment variables in Cloudflare.
The project uses Drizzle ORM for database setup. You can find more information in the [Drizzle ORM documentation](https://orm.drizzle.team/).

Key points:
This step is optional, but required for authentication and blockchain operations.

- Separate Management Systems: The environment variables for Cloudflare Workers and SvelteKit are managed separately and do not overlap. `platform.env` in Cloudflare Workers is not directly accessible to SvelteKit components or server-side logic.
- Deployment Context: When using SvelteKit, the environment variables accessed via `import { env } from '$env/dynamic/private';` are provided by the deployment environment's configuration. These could come from services like Vercel, Netlify, or other hosting providers that support environment variable management.
- No Cross-Access: SvelteKit does not have a built-in mechanism to directly access Cloudflare Workers' environment variables set via `wrangler.toml`. Similarly, Cloudflare Workers cannot directly access environment variables set up for a SvelteKit deployment.
We are supporting three categories of databases:

About Platform Environment Variables:
- Ordinary databases
- Blockchain databases
- Local Blockchain databases on the ORB (or any other) device

- Platform environment variables are stored in the `wrangler.toml` file.
- These variables are accessible in server-side code except those that are prefixed with `PUBLIC_`.
### Drizzle setup

### Local environment variables
Drizzle setup is located in the `drizzle.config.ts` file. Configure it for your database setup.

We can access local environment variables using the `env` function. This function is used to read the environment variables from the `.env` file. Any value is returned as `string`.
Schemas are located in the `src/schemas` directory. You can add your own schema files. Blockchain schemas are located in the `src/schemas/bch` directory.

```ts
import { env } from '$env/dynamic/private';
const secret = env.SECRET;
```
### Blockchain databases

You can use SQLite, Postgres database, or any other supported by Drizzle. The database setup is defined in the `.env` file prefixed with `BCH_DB_`.

Blockchain data are parsed by oracle, which you can deploy.

### Recommended database setup

We are recommending to use the following setup:

If you own the Orb device, you can use it for blockchain operations. You can use the D1 database for ordinary operations. You can use the Hyperdrive connector for production use - for Apps they need the heavy load.

- Small applications: D1 database + Blockchain database on Orb device or D1.
- Medium applications: Hyperdrive connector + Blockchain database on Orb device.
- Large applications: Hyperdrive connector + Blockchain database on Orb device or another Hyperdrive instance.

## Authentication

This is SvelteKit way of reading the environment variables.
The project uses CorePass Extension for authentication but Passkey is possible to build also.

### Cloudflare environment variables
Dependencies:

- CorePass Extension
- Node.js version 22 or higher.

## Sitemaps

We have helper function `genv` to read Cloudflare's environment variables. This function is used to read the environment variables directly from Cloudflare instead of the classic `.env` variables. `true` and `false` (not case sensitive) is returned as boolean value, all other values are returned as `string`.
Sitemaps help search engines prioritize pages within your site, particularly when you have a large amount of content. You can create a sitemap dynamically using an endpoint: `src/routes/sitemap.xml/+server.ts`

```ts
import { genv } from from '$lib/helpers/genv';
const secret = genv(platform).SECRET;
export async function GET() {
return new Response(
`
<?xml version="1.0" encoding="UTF-8" ?>
<urlset
xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="https://www.w3.org/1999/xhtml"
xmlns:mobile="https://www.google.com/schemas/sitemap-mobile/1.0"
xmlns:news="https://www.google.com/schemas/sitemap-news/0.9"
xmlns:image="https://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="https://www.google.com/schemas/sitemap-video/1.1"
>
<!-- <url> elements go here -->
</urlset>`.trim(),
{
headers: {
'Content-Type': 'application/xml'
}
}
);
}
```

## Management API

### DB / Initialize
## Security

- Initialize the database.
About Platform Environment Variables:

`GET /db/init`
- Platform environment variables are stored in the `wrangler.toml` file.
- These variables are accessible in server-side code except those that are prefixed with `PUBLIC_`.

## Error codes (WIP)
### Local environment variables

Err ID | Err Code | Category | Description
--- | --- | --- | ---
400 | 400.01 | Validation | Invalid request
We can access local environment variables using the `env` function. This function is used to read the environment variables from the `.env` file. Any value is returned as `string`.

## Contributing

Contributions are welcome! For feature requests, bug reports, or questions, please [open an issue](https://github.com/bchainhub/dapp-sveltekit-boilerplate/issues).

## License

This project is open source and available under the [Core License](LICENSE).
This project is open source and available under the [CORE License](LICENSE).
45 changes: 45 additions & 0 deletions drizzle-bch.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { defineConfig } from "drizzle-kit";

// Define the allowed dialect types explicitly
type Dialect = "postgresql" | "mysql" | "sqlite";

// Map BCH_DB_TYPE to the appropriate dialect, with special handling for "d1"
const bchDbType = process.env.BCH_DB_TYPE?.toLowerCase();
const bchDialect: Dialect = bchDbType === 'd1' ? 'sqlite' : (bchDbType as Dialect) || 'postgresql';

// Define available schemas for different dialects
const schemasBch = {
postgresql: [
"./src/schemas/bch/postgres.ts"
],
sqlite: [
"./src/schemas/bch/sqlite.ts"
],
d1: [
"./src/schemas/bch/d1.ts"
],
};

// Select the appropriate schema based on the BCH dialect
const selectedBchSchemas = bchDbType === 'd1'
? schemasBch['d1']
: schemasBch[bchDialect] || [];

// Define D1-specific configurations for BCH (only when BCH_DB_TYPE is "d1")
const d1BchConfig = bchDbType === 'd1'
? {
driver: "d1-http",
dbCredentials: {
accountId: process.env.CLOUDFLARE_ACCOUNT_ID || "",
token: process.env.CLOUDFLARE_API_TOKEN || "",
},
}
: {};

// Export the Drizzle config for BCH
export default defineConfig({
dialect: bchDialect, // Use the determined dialect for BCH
schema: selectedBchSchemas, // Choose the correct schemas for BCH
out: "./drizzle/bch", // Output directory
...d1BchConfig, // Spread D1-specific configuration for BCH if the database type is "d1"
});
Loading