oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.13k stars 2.76k forks source link

seed prisma dont work #13551

Closed godzzdeal closed 2 months ago

godzzdeal commented 2 months ago

What version of Bun is running?

1.1.27-canary.14+11d7a9d5e

What platform is your computer?

Microsoft Windows NT 10.0.22631.0 x64

What steps can reproduce the bug?


// prisma/seed.ts

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();
async function main() {
  const responsible = await prisma.account.upsert({
    where: { email: 'general@test.ru' },
    update: { password: await Bun.password.hash('gen1234') },
    create: {
      email: 'general@test.ru',
      name: 'John Dough',
      rights: ['USER', 'RESPONSIBLE_CONTRACTOR'],
      password: await Bun.password.hash('gen1234'),
      masterContractor: {
        create: {
          name: 'Developer Inc.',
          generalData: {
            create: {
              inn: '8342441926',
              kpp: '798446823',
            },
          },
        },
      },
    },
  });
  const admin = await prisma.account.upsert({
    where: { email: 'admin@test.ru' },
    update: { password: await Bun.password.hash('admin1234') },
    create: {
      email: 'admin@test.ru',
      name: 'Admin',
      rights: ['USER', 'ADMIN'],
      password: await Bun.password.hash('admin1234'),
    },
  });
  console.log({ responsible, admin });
}
main()
  .then(async () => {
    await prisma.$disconnect();
  })
  .catch(async (e) => {
    console.error(e);
    await prisma.$disconnect();
    process.exit(1);
  });

im use seed for db, its works on node-js, but if use bun im have error for this


// use cmd - **bun run seed **

ReferenceError: Bun is not defined
    at main (E:\VisualStudioProjects\project-lk\api-private-office-bun\prisma\seed.ts:7:31)
    at main (E:\VisualStudioProjects\project-lk\api-private-office-bun\prisma\seed.ts:7:31)
    at Object.<anonymous> (E:\VisualStudioProjects\project-lk\api-private-office-bun\prisma\seed.ts:38:1)
    at Module._compile (node:internal/modules/cjs/loader:1378:14)
    at Module.m._compile (E:\VisualStudioProjects\project-lk\api-private-office-bun\node_modules\ts-node\src\index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1437:10)
    at Object.require.extensions.<computed> [as .ts] (E:\VisualStudioProjects\project-lk\api-private-office-bun\node_modules\ts-node\src\index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1212:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1028:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at phase4 (E:\VisualStudioProjects\project-lk\api-private-office-bun\node_modules\ts-node\src\bin.ts:649:14)
error: script "seed" exited with code 1

// package.json

{
  "name": "s-private-office-bun",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "prisma": {
    "seed": "ts-node prisma/seed.ts"
  },
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json",
    "seed": "ts-node prisma/seed.ts"
  },
  "dependencies": {
    "@nestjs-modules/mailer": "^2.0.2",
    "@nestjs/axios": "^3.0.3",
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.2.3",
    "@nestjs/core": "^10.0.0",
    "@nestjs/jwt": "^10.2.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/passport": "^10.0.3",
    "@nestjs/platform-express": "^10.0.0",
    "@nestjs/serve-static": "^4.0.2",
    "@prisma/client": "5.18.0",
    "@react-email/render": "^1.0.0",
    "app-root-path": "^3.1.0",
    "bun": "^1.1.26",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.1",
    "cookie-parser": "^1.4.6",
    "passport-jwt": "^4.0.1",
    "react": "^18.3.1",
    "reflect-metadata": "^0.2.0",
    "rxjs": "^7.8.1"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0",
    "@types/bun": "^1.1.8",
    "@types/cookie-parser": "^1.4.7",
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.2",
    "@types/lodash": "^4.17.7",
    "@types/node": "^20.3.1",
    "@types/passport-jwt": "^4.0.1",
    "@types/supertest": "^6.0.0",
    "@typescript-eslint/eslint-plugin": "^7.0.0",
    "@typescript-eslint/parser": "^7.0.0",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "^29.5.0",
    "prettier": "^3.0.0",
    "prisma": "^5.18.0",
    "source-map-support": "^0.5.21",
    "supertest": "^7.0.0",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.2",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

// use cmd - **bunx prisma db seed **

PS E:\VisualStudioProjects\project-lk\api-private-office-bun> bunx prisma db seed  
Environment variables loaded from .env
Running seed command `ts-node prisma/seed.ts` ...
ReferenceError: Bun is not defined
    at main (E:\VisualStudioProjects\project-lk\api-private-office-bun\prisma\seed.ts:7:31)
    at Object.<anonymous> (E:\VisualStudioProjects\project-lk\api-private-office-bun\prisma\seed.ts:38:1)
    at Module._compile (node:internal/modules/cjs/loader:1378:14)
    at Module.m._compile (E:\VisualStudioProjects\project-lk\api-private-office-bun\node_modules\ts-node\src\index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1437:10)
    at Object.require.extensions.<computed> [as .ts] (E:\VisualStudioProjects\project-lk\api-private-office-bun\node_modules\ts-node\src\index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1212:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1028:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at phase4 (E:\VisualStudioProjects\project-lk\api-private-office-bun\node_modules\ts-node\src\bin.ts:649:14)

An error occurred while running the seed command:
Error: Command failed with exit code 1: ts-node prisma/seed.ts

// use cmd - **bun --bun run seed**

PS E:\VisualStudioProjects\project-lk\api-private-office-bun> bun --bun run seed
$ ts-node prisma/seed.ts
131 |     process[exports.REGISTER_INSTANCE] = service;
132 |     // Register the extensions.
133 |     registerExtensions(service.options.preferTsExts, service.extensions.compiled, service, originalJsHandler);
134 |     (0, cjs_resolve_hooks_1.installCommonjsResolveHooksIfNecessary)(service);
135 |     // Require specified modules before start-up.
136 |     module_1.Module._preloadModules(service.options.require);
                          ^
TypeError: module_1.Module._preloadModules is not a function. (In 'module_1.Module._preloadModules(service.options.require)', 'module_1.Module._preloadModules' is undefined)
      at register (E:\VisualStudioProjects\project-lk\api-private-office-bun\node_modules\ts-node\dist\index.js:136:21)
      at phase4 (E:\VisualStudioProjects\project-lk\api-private-office-bun\node_modules\ts-node\dist\bin.js:397:17)
      at E:\VisualStudioProjects\project-lk\api-private-office-bun\node_modules\ts-node\dist\bin.js:579:5

Bun v1.1.27-canary.14+11d7a9d5e (Windows x64)
error: script "seed" exited with code 1

What is the expected behavior?

I expect the script to work correctly so that the database is filled with the original data, but due to the Bun error,

I can't use it at the moment, you guys are creating a great product, but all expectations crumble when you encounter such pitfalls, it's a terrible experience but hopefully you can release a stable release

What do you see instead?

No response

Additional information

No response

godzzdeal commented 2 months ago

God, after a few hours of reflection and analysis, I realized that the ts-node was superfluous here. In the end, I decided it with the command

someone has such a problem, you can use this

bun prisma/seed.ts

I don't know how much is correct, but it works