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

Doesn't work with Bun #79

Open
vaaski opened this issue Sep 9, 2023 · 14 comments · May be fixed by #80
Open

Doesn't work with Bun #79

vaaski opened this issue Sep 9, 2023 · 14 comments · May be fixed by #80

Comments

@vaaski
Copy link

vaaski commented Sep 9, 2023

Describe the bug
After bun hit v1.0.0 yesterday I was eager to give it a spin, but unfortunately this package doesn't seem to work right out of the box. Not sure what the cause is, but if I find some time I might look into it.

error log
bunx pocketbase-typegen --db pb_data/data.db --out plugins/pocketbase-types.ts
node:internal/modules/cjs/loader:995
  const err = new Error(message);
              ^

Error: Cannot find module '/private/tmp/pocketbase-typegen@latest--bunx/node_modules/sqlite3/lib/binding/napi-v6-darwin-unknown-arm64/node_sqlite3.node'
Require stack:
- /private/tmp/pocketbase-typegen@latest--bunx/node_modules/sqlite3/lib/sqlite3-binding.js
- /private/tmp/pocketbase-typegen@latest--bunx/node_modules/sqlite3/lib/sqlite3.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/private/tmp/pocketbase-typegen@latest--bunx/node_modules/sqlite3/lib/sqlite3-binding.js:4:17)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/private/tmp/pocketbase-typegen@latest--bunx/node_modules/sqlite3/lib/sqlite3-binding.js',
    '/private/tmp/pocketbase-typegen@latest--bunx/node_modules/sqlite3/lib/sqlite3.js'
  ]
}

Node.js v18.12.1
error: "pocketbase-typegen" exited with code 1 (SIGHUP)

Versions
pocketbase-typegen version: 1.1.13
pocketbase version: 0.18.1

edit
probably related to oven-sh/bun#158

@patmood
Copy link
Owner

patmood commented Sep 9, 2023 via email

@vaaski
Copy link
Author

vaaski commented Sep 10, 2023

Glad to hear you're interested as well!
From what I can tell with my little experience with bun, we could probably just use the built-in SQLite driver.

Looks like it might even be possible to use bun test.
There seems to be one function not implemented, but it already runs crazy fast compared to the jest runner (2s vs 100ms).

bun test output
❯ bun test
bun test v1.0.0 (822a00c4)

test/generics.test.ts:
✓ getGenericArgList > returns a list of generic args [0.99ms]
✓ getGenericArgList > sorts the arg list [0.03ms]
✓ getGenericArgStringWithDefault > empty string when no generic fields [0.04ms]
✓ getGenericArgStringWithDefault > returns a single generic string [0.03ms]
✓ getGenericArgStringWithDefault > multiple generics with a record [0.03ms]
✓ getGenericArgStringWithDefault > sorts the arguments [0.02ms]
✓ getGenericArgStringWithDefault > includes generic arg for expand fields [0.02ms]
✓ getGenericArgStringForRecord > empty string when no generic fields [0.03ms]
✓ getGenericArgStringForRecord > returns a single generic string [0.04ms]
✓ getGenericArgStringForRecord > multiple generics with a record [0.08ms]
✓ getGenericArgStringForRecord > sorts the arguments [0.04ms]

test/utils.test.ts:
✓ toPascalCase > return pascal case string [3.06ms]
✓ sanitizeFieldName > returns valid typescript fields [0.04ms]
✓ getSystemFields > returns the system field type name for a given collection type [0.02ms]
✓ getOptionEnumName > returns the enum name for select field options [0.03ms]
✓ getOptionValues > returns empty array when no select field values [0.03ms]
✓ getOptionValues > returns deduped select field values [0.04ms]

test/collections.test.ts:
✓ createCollectionEnum > creates enum of collection names [0.15ms]
✓ createCollectionRecords > creates mapping of collection name to record type [0.03ms]
✓ createCollectionResponses > creates mapping of collection name to response type [0.03ms]

test/fromJSON.test.ts:
Created typescript definitions at /Users/o/projects/_contrib/pocketbase-typegen/test/pocketbase-types-example.ts
✓ creates a type file from json schema [3.29ms]

test/fields.test.ts:
✓ createTypeField > handles required and optional fields [0.04ms]
✓ createTypeField > converts default types to typescript [0.02ms]
✓ createTypeField > converts number type [0.02ms]
✓ createTypeField > converts bool type [0.01ms]
✓ createTypeField > converts email type [0.03ms]
✓ createTypeField > converts url type [0.01ms]
✓ createTypeField > converts date type [0.01ms]
✓ createTypeField > converts select type [0.02ms]
✓ createTypeField > converts select type with value [0.02ms]
✓ createTypeField > converts multi-select type [0.01ms]
✓ createTypeField > converts multi-select type with values [0.02ms]
✓ createTypeField > converts json type [0.01ms]
✓ createTypeField > converts editor type [0.01ms]
✓ createTypeField > converts file type [0.03ms]
✓ createTypeField > converts file type with multiple files [0.01ms]
✓ createTypeField > converts relation type [0.01ms]
✓ createTypeField > converts relation type with multiple options
✓ createTypeField > converts relation type with unset maxSelect [0.01ms]
✓ createTypeField > converts user relation type [0.02ms]
WARNING: unknown type "unknowntype" found in schema
243 |     createTypeField("test_collection", {
244 |       ...defaultFieldSchema,
245 |       // @ts-ignore
246 |       type: "unknowntype",
247 |     })
248 |     expect(logSpy).toHaveBeenCalledWith(
        ^
error: Not implemented
      at /Users/o/projects/_contrib/pocketbase-typegen/test/fields.test.ts:248:4
✗ createTypeField > warns when encountering unexpected types [0.23ms]
✓ createSelectOptions > creates enum types for select options [0.10ms]
✓ getSelectOptionEnumName > uses the select option value as the enum name [0.01ms]
✓ getSelectOptionEnumName > prefixes the enum name when the value is a number [0.01ms]

test/lib.test.ts:
✓ generate > generates correct output given db input [0.14ms]
✓ createRecordType > creates type definition for a record [0.05ms]
✓ createRecordType > handles file fields with multiple files [0.02ms]
✓ createRecordType > sorts fields alphabetically [0.04ms]
✓ createResponseType > creates type definition for a response [0.02ms]
✓ createResponseType > handles file fields with multiple files [0.01ms]

1 tests failed:
✗ createTypeField > warns when encountering unexpected types [0.23ms]

 49 pass
 1 fail
 10 snapshots, 72 expect() calls
Ran 50 tests across 6 files. [84.00ms]

Even running each test 100 times is still multiple times faster than jest :D
image

@vaaski
Copy link
Author

vaaski commented Sep 10, 2023

It was actually rather simple to get running, however I did run into some issues.

  • Using the build script in your package.json I ran into an issue with ESM. But since bun can run typescript directly, we could theoretically skip that part. I added another bin entry for now.

    ESM Error
     Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data are supported by the default ESM loader. Received protocol 'bun:'
         at new NodeError (node:internal/errors:393:5)
         at throwIfUnsupportedURLScheme (node:internal/modules/esm/resolve:1026:11)
         at defaultResolve (node:internal/modules/esm/resolve:1106:3)
         at nextResolve (node:internal/modules/esm/loader:163:28)
         at ESMLoader.resolve (node:internal/modules/esm/loader:841:30)
         at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
         at ESMLoader.import (node:internal/modules/esm/loader:525:22)
         at importModuleDynamically (node:internal/modules/esm/translators:110:35)
         at importModuleDynamicallyCallback (node:internal/process/esm_loader:35:14)
         at getCollectionsIsomorphic (file:///Users/o/projects/_contrib/pocketbase-typegen/dist/index.js:15:26) {
       code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
     }
    
  • Bun doesn't like to import directly from the package.json, will be an easy fix tho.

    package.json error
     SyntaxError: Import named 'version' not found in module '/Users/o/projects/_contrib/pocketbase-typegen/package.json'.
           at processTicksAndRejections (:1:2602)
     error: "pocketbase-typegen-bun" exited with code 1 (SIGHUP)
    
  • By default bun will respect the #!/usr/bin/env node shebang and spin up a node process, which can be disabled using --bun. We should probably add the shebang during the build process tho.

I think ideally there shouldn't be multiple binaries separating the two runtimes, but I'm not sure how to work that into your build setup.

@vaaski vaaski linked a pull request Sep 10, 2023 that will close this issue
@patmood
Copy link
Owner

patmood commented Sep 13, 2023

I just tried to reproduce this with bun v1.0.1 and it worked without errors. These all seem like issues that could be fixed on the bun side. Can you try again with the newer version of bun?

I'm also on Mac OSX 12.6.5

@mattmccray
Copy link

It doesn't work for me using bun 1.0.2 on an M2 Mac, OSX v13.5.2

I'm trying to run it as a package.json script

bun run --bun typegen

And it's throwing:

error: Cannot find module "/Users/mattm/Projects/Elucidata/aegis-v4/node_modules/sqlite3/lib/binding/napi-v6-darwin-unknown-arm64/node_sqlite3.node" from "/Users/mattm/Projects/Elucidata/aegis-v4/node_modules/sqlite3/lib/sqlite3-binding.js"
error: script "typegen" exited with code 1 (SIGHUP)

@mattmccray
Copy link

Looks like my issue was Mac Silicon-related. Running this got it to work.

npm install sqlite3 --build-from-source --target_arch=arm64 --fallback-to-build

I'm not sure what the bun version of that command is, though.

@jakubdonovan
Copy link

jakubdonovan commented Oct 21, 2023

I just tried to reproduce this with bun v1.0.1 and it worked without errors. These all seem like issues that could be fixed on the bun side. Can you try again with the newer version of bun?

I'm also on Mac OSX 12.6.5

I just tried it and i could not get it to work. Here's what I did:
bun install pocketbase-typegen
added env variables to my .env

PB_TYPEGEN_URL
PB_TYPEGEN_EMAIL
PB_TYPEGEN_PASSWORD

added the following script to my package.json
"typegen": "pocketbase-typegen --db ./pb_data/data.db"

and ran bunx typegen --env

which yielded
🔍 Resolving...
GET https://registry.npmjs.org/typegen/-/typegen-0.2.0.tgz - 404
error: could not determine executable to run for package typegen

I'm on bun 1.0.7
@vaaski did you run into this issue?

@patmood
Copy link
Owner

patmood commented Oct 21, 2023

@jakubdonovan bunx is for running packages. So you can run bunx pocketbase-typegen --db ./pb_data/data.db.

The command for running package.json scripts for your case would be bun run typegen, since you called the script "typegen"

Whats confusing is that the package entry point does have a node shebang #!/usr/bin/env node, so it's supposed to be running it with node anyway.

The reason I couldn't repro earlier is because it only errors when trying to generate with the database option.

@jakubdonovan
Copy link

@jakubdonovan bunx is for running packages. So you can run bunx pocketbase-typegen --db ./pb_data/data.db.

The command for running package.json scripts for your case would be bun run typegen, since you called the script "typegen"

Whats confusing is that the package entry point does have a node shebang #!/usr/bin/env node, so it's supposed to be running it with node anyway.

The reason I couldn't repro earlier is because it only errors when trying to generate with the database option.

bun run typegen resulted in
$ pocketbase-typegen --db ./pb_data/data.db
node:internal/process/promises:289
triggerUncaughtException(err, true /* fromPromise */);
^

[Error: SQLITE_CANTOPEN: unable to open database file] {
errno: 14,
code: 'SQLITE_CANTOPEN'
}

Node.js v20.3.0
error: script "typegen" exited with code 1 (SIGHUP)

@jakubdonovan
Copy link

jakubdonovan commented Oct 21, 2023

@patmood I was able to get it to work by running
npm i sqlite3 --build-from-source --target_arch=arm64 --fallback-to-build
and
npx pocketbase-typegen --env
which generated types as expected, however I don't think it generated the correct types as

export const pb = new PocketBase(URL) as TypedPocketBase
const res = await pb.collection('menus').getFirstListItem('core_menu_id="628"')

res is of type any and not MenusResponse and each collection in TypedPocketBase gives me the following warning
Type 'RecordService' is not generic.ts(2315)

whereas removing as TypedPocketBase from the client and
const res = await pb.collection(Collections.Menus).getFirstListItem<MenusResponse>('core_menu_id="628"') gave me the correct type.

This is problematic because I now need to do extra stuff to also get the system fields for say my RecipesRecord.

await pb.collection(Collections.Menus).update(menuRecord.id, {
	'collections+': [...collectionRecordIDs]
})

now gives me Property 'id' does not exist on type 'MenusRecord'.ts(2339)
because id is a system field.
Any suggestions?

@jakubdonovan
Copy link

jakubdonovan commented Oct 21, 2023

Glad to hear you're interested as well! From what I can tell with my little experience with bun, we could probably just use the built-in SQLite driver.

Looks like it might even be possible to use bun test. There seems to be one function not implemented, but it already runs crazy fast compared to the jest runner (2s vs 100ms).

bun test output
Even running each test 100 times is still multiple times faster than jest :D image

did you manage to get it to run with the built in sqlite?
bun run typegen

$ pocketbase-typegen --db ./pb_data/data.db
node:internal/modules/cjs/loader:1070
const err = new Error(message);
^

Error: Cannot find module '/Users/jakubdonovan/Documents/code/foodDiscounts-datacenter/node_modules/sqlite3/lib/binding/napi-v6-darwin-unknown-arm64/node_sqlite3.node'
Require stack:

  • /Users/jakubdonovan/Documents/code/foodDiscounts-datacenter/node_modules/sqlite3/lib/sqlite3-binding.js
  • /Users/jakubdonovan/Documents/code/foodDiscounts-datacenter/node_modules/sqlite3/lib/sqlite3.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1070:15)
    at Module._load (node:internal/modules/cjs/loader:923:27)
    at Module.require (node:internal/modules/cjs/loader:1137:19)
    at require (node:internal/modules/helpers:121:18)
    at Object. (/Users/jakubdonovan/Documents/code/foodDiscounts-datacenter/node_modules/sqlite3/lib/sqlite3-binding.js:4:17)
    at Module._compile (node:internal/modules/cjs/loader:1255:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1309:10)
    at Module.load (node:internal/modules/cjs/loader:1113:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1137:19) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [
    '/Users/jakubdonovan/Documents/code/foodDiscounts-datacenter/node_modules/sqlite3/lib/sqlite3-binding.js',
    '/Users/jakubdonovan/Documents/code/foodDiscounts-datacenter/node_modules/sqlite3/lib/sqlite3.js'
    ]
    }

Node.js v20.3.0
error: script "typegen" exited with code 1 (SIGHUP)

bun run --bun typegen

$ pocketbase-typegen --db ./pb_data/data.db
error: Cannot find module "/Users/jakubdonovan/Documents/code/foodDiscounts-datacenter/node_modules/sqlite3/lib/binding/napi-v6-darwin-unknown-arm64/node_sqlite3.node" from "/Users/jakubdonovan/Documents/code/foodDiscounts-datacenter/node_modules/sqlite3/lib/sqlite3-binding.js"
error: script "typegen" exited with code 1 (SIGHUP)
 jakubdonovan at …/foodDiscounts-datacenter via  db-refactor-relational  

@patmood
Copy link
Owner

patmood commented Oct 22, 2023

@jakubdonovan if you are using as TypedPocketBase you need to be using PocketBase SDK v0.18.3 or above. If you are using that version, create a new issue with steps to reproduce it.

As mentioned above, bun seems to work after installing the sqlite package for apple silicon. I'm not sure if that is bun related though

bunx --bun pocketbase-typegen -d pb_data/data.db
Created typescript definitions at pocketbase-types.ts

@jakubdonovan
Copy link

bunx --bun pocketbase-typegen -d pb_data/data.db

That's right, looks like I was on an older version of pocketbase. As for sqlite, how'd you install the apple silicon compatible version?
I saw another user install it with npm install sqlite3 --build-from-source --target_arch=arm64 --fallback-to-build however using this command with bun instead of npm failed.

@babakfp
Copy link

babakfp commented May 30, 2024

I'm using bun v1.1.8 on Windows and runing bun pocketbase-typegen --env -o src/lib/utilities/pb/types-auto-generated.ts works.

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

Successfully merging a pull request may close this issue.

5 participants