1.1.21 • Published 7 months ago

prisma-accelerate-local v1.1.21

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

prisma-accelerate-local

Prisma Accelerate functionality can be self-hosted locally.

Samples

usage

CLI Options

CategoryOptionDescription
USAGEoption \
ARGUMENTS\Datasource url
OPTIONS-t, --httpAccepted at http
-p, --port \Port to listen on (default:4000)
-h, --host \Host to listen on (default:localhost)
-c, --cert \Path to ssl cert file
-k, --key \Path to ssl key file
-w, --wasmUse wasm as the run-time engine(early-access)
-s, --secret \Secret used with API key
-m, --makemake api key
-b, --bodyLimit \<size(MB)>body limit size(default: 16MB)

CLI

Start without setting an API key for local use.

# Startup by specifying the Datasource url
npx prisma-accelerate-local postgresql://postgres:password@localhost:5432/postgres

# Startup by specifying Port
npx prisma-accelerate-local postgresql://postgres:password@localhost:5432/postgres -p 8000

When setting the API key

  • Create an API key
npx prisma-accelerate-local -s secret -m postgresql://postgres:password@localhost:5432/postgres

# Output
eyJhbGciOiJIUzI1NiJ9.eyJkYXRhc291cmNlVXJsIjoiYSIsImlhdCI6MTcwMzY2NDg1NywiaXNzIjoicHJpc21hLWFjY2VsZXJhdGUifQ.4ruaA1RAT9cD3PACSEVIdUs3i2exKkMpNYGks3hyos4
  • Activate with API key enabled.

If secret is used, the DB address is embedded in the API key

npx prisma-accelerate-local -s secret

Client Environment Variables

With regard to the Node.js configuration.

Please set the environment variable NODE_TLS_REJECT_UNAUTHORIZED because you are using an unauthenticated certificate.

With regard to api_key

  • If you are not using secret, the api_key can be any string.
  • If you are using secret, put --secret and the api_key created with --make in api_key

Example

  • .env
DATABASE_URL="prisma://localhost:4000/?api_key=API_KEY"
NODE_TLS_REJECT_UNAUTHORIZED="0"
# To remove the NODE_TLS_REJECT_UNAUTHORIZED warning
NODE_NO_WARNINGS="1"

library

If you call this package as a library, it will look like this.

import { createServer } from 'prisma-accelerate-local';

const server = createServer({
  datasourceUrl: 'postgresql://postgres:password@localhost:5432/postgres',
})
  .listen({ port: 4000 })
  .then((url) => console.log(`🚀  Server ready at ${url} `));

When self-hosting with Deno Deploy (PostgreSQL)

https://github.com/SoraKumo001/prisma-accelerate-deno

import pg from 'npm:pg';
import { PrismaPg } from 'npm:@prisma/adapter-pg';
import { createHandler, importModule } from 'npm:prisma-accelerate-local/deno';
import runtime from 'npm:@prisma/client/runtime/query_engine_bg.postgresql.js';

const engine = '@prisma/client/runtime/query_engine_bg.postgresql.wasm';

Deno.serve(
  createHandler({
    runtime: () => runtime,
    secret: Deno.env.get('SECRET')!,
    queryEngineWasmModule: importModule(engine, import.meta.url),
    adapter: (datasourceUrl: string) => {
      const url = new URL(datasourceUrl);
      const schema = url.searchParams.get('schema') ?? undefined;
      const pool = new pg.Pool({
        connectionString: url.toString() ?? undefined,
      });
      return new PrismaPg(pool, {
        schema,
      });
    },
  })
);

When self-hosting with Cloudflare Workers (D1)

https://github.com/SoraKumo001/prisma-accelerate-workers-d1

import WASM from '@prisma/client/runtime/query_engine_bg.sqlite.wasm';
import { PrismaD1 } from '@prisma/adapter-d1';
import { createFetcher } from 'prisma-accelerate-local/workers';

export type Env = {
  SECRET: string;
} & {
  [key: string]: D1Database;
};

export default {
  fetch: createFetcher({
    queryEngineWasmModule: WASM,
    secret: (env: Env) => env.SECRET,
    runtime: () => require(`@prisma/client/runtime/query_engine_bg.sqlite.js`),
    adapter: (datasourceUrl: string, env) => {
      return new PrismaD1(env[datasourceUrl]);
    },
    singleInstance: false,
  }),
};

When self-hosting with Cloudflare Workers (PostgreSQL)

https://github.com/SoraKumo001/prisma-accelerate-workers

  • package.json

Need pg-compat to patch pg to fix it.

{
  "name": "prisma-accelerate-workers",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "scripts": {
    "deploy": "wrangler deploy",
    "dev": "wrangler dev",
    "start": "wrangler dev"
  },
  "dependencies": {
    "@prisma/adapter-pg": "^5.20.0",
    "@prisma/client": "^5.20.0",
    "pg": "^8.13.0",
    "prisma-accelerate-local": "^1.1.10"
  },
  "devDependencies": {
    "@cloudflare/workers-types": "^4.20241011.0",
    "@types/pg": "^8.11.10",
    "pg-compat": "^0.0.7",
    "typescript": "^5.6.3",
    "wrangler": "^3.80.4"
  }
}
  • wrangler.toml

Set nodejs_compat_v2.

name = "prisma-accelerate-workers"
main = "src/index.ts"
minify = true
compatibility_date = "2024-09-23"
compatibility_flags = ["nodejs_compat_v2"]

[placement]
mode = "smart"

[observability]
enabled = true
  • src/index.ts
import { Pool } from 'pg';
import { PrismaPg } from '@prisma/adapter-pg';
import { createFetcher } from 'prisma-accelerate-local/workers';
import WASM from '@prisma/client/runtime/query_engine_bg.postgresql.wasm';

export type Env = {
  SECRET: string;
};

export default {
  fetch: createFetcher({
    runtime: () => require(`@prisma/client/runtime/query_engine_bg.postgresql.js`),
    secret: (env: Env) => env.SECRET,
    queryEngineWasmModule: WASM,
    adapter: (datasourceUrl: string) => {
      const url = new URL(datasourceUrl);
      const schema = url.searchParams.get('schema') ?? undefined;
      const pool = new Pool({
        connectionString: url.toString() ?? undefined,
      });
      return new PrismaPg(pool, {
        schema,
      });
    },
  }),
};
1.1.21

7 months ago

1.1.9

10 months ago

1.1.8

10 months ago

1.1.7

10 months ago

1.1.6

11 months ago

1.1.12

10 months ago

1.1.11

10 months ago

1.1.10

10 months ago

1.1.16

10 months ago

1.1.15

10 months ago

1.1.14

10 months ago

1.1.13

10 months ago

1.1.19

10 months ago

1.1.18

10 months ago

1.1.17

10 months ago

1.1.20

10 months ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.1

2 years ago

1.0.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.3

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago