alex8088 / electron-toolkit

Toolkit for Electron
MIT License
113 stars 6 forks source link

drizzle:generate fails to run #13

Open faribauc opened 3 months ago

faribauc commented 3 months ago

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

KillerCodeMonkey commented 1 month ago

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 commented 1 month ago

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 commented 27 minutes ago

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.