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

Cloudflare D1 adapter is inefficient - calls "create table if not exists" on instantiation #230

Open
treasureseth opened this issue Jul 28, 2024 · 2 comments

Comments

@treasureseth
Copy link

treasureseth commented Jul 28, 2024

The Cloudflare D1 Adapter has a private constructor and the only way to instantiate it is to call the static create method. That method internally calls init, which makes a "create table if not exists" query.

This means that simply instantiating the session storage driver would trigger this query - so effectively it pollutes the whole app. Instead, we should run a migration on setup and remove this superfluous query on instantiation.

private constructor(db: D1Database, tableName?: string) {
this.db = db;
this.tableName = tableName ?? defaultTableName;
}
static async create<T>(db: D1Database, tableName?: string): Promise<D1Adapter<T>> {
const adapter = new D1Adapter<T>(db, tableName);
await adapter.#init();
return adapter;
}
async #init() {
await this.db
.exec(`CREATE TABLE IF NOT EXISTS "${this.tableName}" (key TEXT PRIMARY KEY, value TEXT)`);
await this.db
.exec(`CREATE INDEX IF NOT EXISTS "IDX_${this.tableName}" ON ${this.tableName} (key)`);
}

@treasureseth treasureseth changed the title Cloudflare D1 adapter is inefficient - calls "create table if exists" on every read/write Cloudflare D1 adapter is inefficient - calls "create table if exists" on instantiation Jul 28, 2024
@treasureseth treasureseth changed the title Cloudflare D1 adapter is inefficient - calls "create table if exists" on instantiation Cloudflare D1 adapter is inefficient - calls "create table if not exists" on instantiation Jul 28, 2024
@Satont
Copy link
Member

Satont commented Jul 28, 2024

Is this kind of expensive operation for cloudflare? I mean usually those sql things takes small ms.

But anyways we can drop this thing and instead move those queries into readme to setup section.

Are you wanna make PR?

@treasureseth
Copy link
Author

I think the problem is more widespread. Many other adapters follow the same pattern of private constructor and force you to fire a create table if not exists query.

Here's the psql adapter that actually fires 2 extra queries:

private constructor(opts: AdapterConstructor) {
this.tableName = opts.tableName;
this.query = opts.query;
}
static async create(opts = { tableName: 'sessions' } as Omit<AdapterConstructor, 'query'>) {
const queryString = `
CREATE TABLE IF NOT EXISTS "${opts.tableName}" (
"key" VARCHAR NOT NULL,
"value" TEXT
)`;
const query = buildQueryRunner(opts.client);
await query(queryString);
await query(`CREATE UNIQUE INDEX IF NOT EXISTS IDX_${opts.tableName} ON "${opts.tableName}" ("key")`);
return new PsqlAdapter({
...opts,
query,
});
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants