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

Improve drizzle-orm SQLite default template #484

Open
2 of 6 tasks
hyunbinseo opened this issue Mar 12, 2025 · 7 comments
Open
2 of 6 tasks

Improve drizzle-orm SQLite default template #484

hyunbinseo opened this issue Mar 12, 2025 · 7 comments
Labels
enhancement New feature or request pkg:add sv add

Comments

@hyunbinseo
Copy link
Contributor

hyunbinseo commented Mar 12, 2025

As of Svelte CLI v0.6.26

  • db:generate is not included in the scripts:
  "db:push": "drizzle-kit push",
  "db:migrate": "drizzle-kit migrate",
  "db:studio": "drizzle-kit studio"
  "db:migrate": "drizzle-kit migrate", // missing
  • Unused variable is included in the schema.ts file:
// 'text' is declared but its value is never read.ts(6133)
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';

export const user = sqliteTable('user', {
  id: integer('id').primaryKey(),
  age: integer('age')
});
  • Line-breaks are missing in the drizzle.config.ts file:
import { defineConfig } from 'drizzle-kit';
if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');
  • loadEnvFile should probably be called in the drizzle.config.ts file:
import { defineConfig } from 'drizzle-kit';
import { loadEnvFile } from 'node:process';

loadEnvFile();

if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');
  • @types/node should probably be installed out-of-the box.
import { defineConfig } from 'drizzle-kit';

// Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.ts(2580)
if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');
  • Line-breaks are missing in the src\lib\server\db\index.ts file:
import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client';
import * as schema from './schema';
import { env } from '$env/dynamic/private';
if (!env.DATABASE_URL) throw new Error('DATABASE_URL is not set');
const client = createClient({ url: env.DATABASE_URL });

Are there reasons behind these decisions? If not, I would love to create a PR.

@manuel3108
Copy link
Member

1. db:generate is not included in the scripts:

You are right. Either we should remove migrate or add generate, as per the docs.

2. Unused variable is included in the schema.ts file:

You are right!

3. Line-breaks are missing in the drizzle.config.ts file:

Does not only apply to the drizzle add-on, but to a bunch of different ones. Should be resolved after #380 is merged. Feel free to try that PR via pnpm dlx https://pkg.pr.new/sveltejs/cli/sv@380 and provide feedback there. This should also resolve all tabs vs space inconsistencies.

4. loadEnvFile should probably be called in the drizzle.config.ts file:

I guess that makes sense technically, but have you already encountered such a use-case? Using vitest in example does not make this command necessary. I assume this would only be a problem if executing node directly? Also I was unable to find any reference to this method in the drizzle docs as a recommended step or something: https://orm.drizzle.team/docs/get-started/sqlite-new

5. @types/node should probably be installed out-of-the box.

I might be asking strange questions today, but I'm currently unable to reproduce this. I used the minimal template with the js option, and even pnpm check is not reporting anything for me.

6. Line-breaks are missing in the src\lib\server\db\index.ts file:

Same as above.

Would appreciate your PR, but I would suggest to wait until #380 is merged to avoid merge conflicts.

@manuel3108 manuel3108 added enhancement New feature or request pkg:add sv add labels Mar 15, 2025
@hyunbinseo
Copy link
Contributor Author

hyunbinseo commented Mar 15, 2025

  1. loadEnvFile() is needed for these commands:
  • drizzle-kit generate
  • drizzle-kit migrate

It works in Vitest because it's basically Vite which imports env files accordingly.

Reference https://vite.dev/guide/env-and-mode

  1. It happened in my Windows dev environment, which I can revisit on Monday. I cannot reproduce it in macOS.
// var process: NodeJS.Process - works in macOS out-of-the-box
if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');
┌  Welcome to the Svelte CLI! (v0.6.26)
│
◇  Which template would you like?
│  SvelteKit minimal
│
◇  Add type checking with TypeScript?
│  Yes, using JavaScript with JSDoc comments
│
◆  Project created
│
◇  What would you like to add to your project? (use arrow keys / space bar)
│  drizzle
│
◇  Which database would you like to use?
│  SQLite
│
◇  Which SQLite client would you like to use?
│  libSQL

@manuel3108
Copy link
Member

Oh yeah, able to reproduce 4. now 👍

@hyunbinseo
Copy link
Contributor Author

@types/node issue happens in Windows environment only.

Property 'dirname' does not exist on type 'ImportMeta'. ts(2339)

For some reason, installing @types/node once and then uninstalling it fixes the problem.

(property) ImportMeta.dirname: string

@manuel3108
Copy link
Member

Ok, then let's add @types/node. I'm wondering if this only happened to you once you installed drizzle or was the default template already throwing such errors? Im asking this because im unsure if the @ŧypes/node should be part of the default templates, or only added if drizzle is detected.
We could have a look at how different frameworks are handling this.

Now that #380 is merged, issues 3. and 6. should not be happening anymore.

@hyunbinseo
Copy link
Contributor Author

  1. I've converted the issue body to a checklist and checked 3 and 6.
  2. I can create a separate @types/node issue and see if people are experiencing it as well.

I don't have multiple Windows environments to test from, and I can always install @types/node on my own when a problem occurs.

@manuel3108
Copy link
Member

As for the @types/node issue, looks like you are not the only one. Maybe it's related in to the library template. Let's investigate this in #509

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pkg:add sv add
Projects
None yet
Development

No branches or pull requests

2 participants