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:generate fails to run #13

Open
4 tasks done
faribauc opened this issue Jun 6, 2024 · 4 comments
Open
4 tasks done

drizzle:generate fails to run #13

faribauc opened this issue Jun 6, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@faribauc
Copy link

faribauc commented Jun 6, 2024

Describe the bug

Hi!

I have an electron/drizzle app with the following config:

drizzle.config.ts

import type { Config } from 'drizzle-kit'
import { DatabaseConfig } from './src/libs/config'

export default {
  schema: './database/schema.ts',
  out: './database/migrations',
  driver: 'better-sqlite',
  dbCredentials: {
    url: DatabaseConfig.databaseName
  },
  verbose: true,
  strict: true
} satisfies Config

config.ts

import path from 'path'
import { app } from 'electron'
import { is } from '@electron-toolkit/utils'

const databaseFileName = 'database.db'
export const defaultDatabaseName =
  !app || is.dev ? databaseFileName : path.join(app.getPath('userData'), databaseFileName)
export const databaseName = process.env.VITE_DATABASE_NAME || defaultDatabaseName

export const DatabaseConfig = {
  databaseName: databaseName
}

When I run drizzle:generate I get the following error:

/home/<user>/.nvm/versions/node/v21.7.1/bin/npm run drizzle:generate

> <project>@1.0.0-alpha20 drizzle:generate
> drizzle-kit generate:sqlite

drizzle-kit: v0.20.14
drizzle-orm: v0.30.6

No config path provided, using default 'drizzle.config.ts'
Reading config file '/home/<user>/path/to/<project>/drizzle.config.ts'
/home/<user>/path/to/<project>/node_modules/@electron-toolkit/utils/dist/index.cjs:6
  dev: !electron.app.isPackaged
                     ^

TypeError: Cannot read properties of undefined (reading 'isPackaged')
    at Object.<anonymous> (/home/<user>/path/to/<project>/node_modules/@electron-toolkit/utils/dist/index.cjs:6:22)
    at Module._compile (node:internal/modules/cjs/loader:1368:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
    at Object.newLoader (/home/<user>/path/to/<project>/node_modules/drizzle-kit/bin.cjs:8648:13)
    at extensions..js (/home/<user>/path/to/<project>/node_modules/drizzle-kit/bin.cjs:11176:28)
    at Module.load (node:internal/modules/cjs/loader:1205:32)
    at Module._load (node:internal/modules/cjs/loader:1021:12)
    at Module.require (node:internal/modules/cjs/loader:1230:19)
    at require (node:internal/modules/helpers:179:18)
    at Object.<anonymous> (/home/<user>/path/to/<project>/src/libs/config.ts:3:20)

Node.js v21.7.1

Process finished with exit code 1

I created PR #12 as a possible solution.

Any thoughts?

Electron-Toolkit Version

3.0.0

Electron Version

28.2.0

Validations

@faribauc faribauc added the bug Something isn't working label Jun 6, 2024
@KillerCodeMonkey
Copy link

i do not think this is a bug... i mean you are using an electron package inside of a node script that is called outside of electron.

just use your process.env.VITE_DATABASE_NAME || defaultDatabaseName in that file and when you init the db in your electron context override the name if not dev.

@faribauc
Copy link
Author

It is an issue because the electron variable behaves differently when running in dev mode (missing the app property) and when running in compiled mode. It has nothing to do with how I get the database name.

Also, I can't define the VITE_DATABASE_NAME on a customer computer. The db file needs to be created by the app, on launch, in the userData directory. But that needs to happen only when running the compiled app, not in dev mode because in dev mode, the app variable does not exist, and I can't get the userData directory value. That is also the mode in which drizzle:generate runs in within the electron project.

In this specific case, failing to check if electron.app exists prior to check for isPackaged is the exact issue.

@SaidbekAbdiganiev
Copy link

I encountered a similar issue, although I wasn't using Drizzle in my application. In my case, the error was caused by the ELECTRON_RUN_AS_NODE environment variable. When this variable is set, it forces Electron to use the Node.js version installed on your system instead of Electron's built-in Node.js runtime (link). Can you try unsetting it or was there a specific reason to do this? I tried looking into Electron's documentation about dev mode removing app property, but seems like this is not the case.

@faribauc
Copy link
Author

@SaidbekAbdiganiev Thanks for your feedback. It has no effect since this happens when running drizzle, not when running the electron app. That's when the check fails, the electron app does not exist. And that's why the is.dev check is failing. It assumes the app exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants