patmood / pocketbase-typegen

Typescript generation for pocketbase records
https://www.npmjs.com/package/pocketbase-typegen
542 stars 20 forks source link

Doesn't work with Bun #79

Closed vaaski closed 3 days ago

vaaski commented 1 year ago

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. (/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 commented 1 year ago

Thanks I’m keen to try bun and get this working.

From the error it looks like the SQLite dependency isn’t supported.

On Sat, 9 Sep 2023 at 9:55 pm, vaaski @.***> wrote:

Describe the bug After bun https://bun.sh 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 @.***/node_modules/sqlite3/lib/binding/napi-v6-darwin-unknown-arm64/node_sqlite3.node' Require stack:

  • @.***/node_modules/sqlite3/lib/sqlite3-binding.js
  • @./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. @./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: [ @./node_modules/sqlite3/lib/sqlite3-binding.js', @./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

— Reply to this email directly, view it on GitHub https://github.com/patmood/pocketbase-typegen/issues/79, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6YGPCS6MCWZLU47L2RDNTXZRN4TANCNFSM6AAAAAA4RNH37U . You are receiving this because you are subscribed to this thread.Message ID: @.***>

vaaski commented 1 year ago

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

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

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.

patmood commented 1 year ago

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

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

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

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

@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 commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

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

@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 commented 1 year ago

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 commented 5 months ago

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.