npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@storecraft/database-cloudflare-d1

v1.0.0

Published

`Storecraft` database driver for `cloudflare` D1 (cloud sqlite)

Downloads

65

Readme

Storecraft Cloudflare D1 Database support

Two issues awaiting:

  1. make seed_templates migration work
  2. On CF side, they relaxed the FUNC_ARGS_LENGTH, so now json sql should work for me.

Two variants,

  1. D1 over http (used for migrations)
  2. D1 over cloudflare-worker runtime (used for backend)

Official Cloudflare D1 driver for StoreCraft on any platforms.

npm i @storecraft/database-cloudflare-d1

Setup

  • First, login to your cloudflare account.
  • Create a D1 database.
  • Create an API Key at here

Apply migrations with local driver

Migrations use a different D1_HTTP driver,

create a migrate.js file with

import 'dotenv/config';
import { D1_HTTP } from '@storecraft/database-cloudflare-d1';
import { migrateToLatest } from '@storecraft/database-cloudflare-d1/migrate.js';
 
const migrate = async () => {
  const d1_over_http = new D1_HTTP(
    {
      account_id: process.env.CLOUDFLARE_ACCOUNT_ID,
      api_token: process.env.CLOUDFLARE_D1_API_TOKEN,
      database_id: process.env.CLOUDFLARE_D1_DATABASE_ID
    }
  )
  
  await migrateToLatest(d1_over_http, true);
}

migrate();

create a .env file with (find the values from cloudflare dashboard)

CLOUDFLARE_ACCOUNT_ID=".."
CLOUDFLARE_D1_API_TOKEN=".."
CLOUDFLARE_D1_DATABASE_ID=".."

simply run it,

node run migrate.js

NOTE:

  • seeding of templates migration might fail because http driver does not allow for sql parameters
  • we are working on a solution for that
  • no big deal, you can still use it

D1 over Cloudflare Workers

To use the driver in cloudflare workers environment, we use the native driver of cloudflare, which allows for parameterized sql statements (the http driver does not for some reason, which we hope they will solve and then we can run d1 with parameterized statements at any cloud environment safely without fearing SQL Injection)

So, Create a worker with npx wrangler init

Populate wrangler.toml with

[[d1_databases]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "<YOUR-DATABASE-NAME>"
database_id = "<YOUR-DATABASE-ID>"

Create a src/index.ts file with

import { App } from "@storecraft/core"
import { D1_WORKER } from "@storecraft/database-cloudflare-d1"
import { CloudflareWorkersPlatform } from "@storecraft/platforms/cloudflare-workers"


export default {
	/**
	 * This is the standard fetch handler for a Cloudflare Worker
	 *
	 * @param request - The request submitted to the Worker from the client
	 * @param env - The interface to reference bindings declared in wrangler.toml
	 * @param ctx - The execution context of the Worker
	 * @returns The response to be sent back to the client
	 */
	async fetch(request, env, ctx): Promise<Response> {
    let app = new App(
      {
        storage_rewrite_urls: undefined,
        general_store_name: 'Wush Wush Games',
        general_store_description: 'We sell cool retro video games',
        general_store_website: 'https://wush.games',
        auth_admins_emails: ['[email protected]']
      }
    )
    .withPlatform(new CloudflareWorkersPlatform())
    .withDatabase(
      new D1_WORKER(
        {
          db: env.D1
        } 
      )
    );

    app = await app.init();
    
    const response = await app.handler(request);

    return response;

	},
} satisfies ExportedHandler<Env>;

run locally with remote database

npx wrangler dev --remote

Now, you are good to go, visit

  • http://localhost:8787/api/dashboard
  • http://localhost:8787/api/reference
Author: Tomer Shalev <[email protected]>