edgedb-js icon indicating copy to clipboard operation
edgedb-js copied to clipboard

Add Deno compilation target for query builder

Open seanaye opened this issue 2 years ago • 19 comments

This PR adds support for a --target deno via the query builder cli. The cli itself still requires the use of npx due to the syntax folder not being compatible with deno as of yet.

TODO:

  • [x] resolve conflicts

  • [x] link repro project (due to the changes to the Deno compiler the use of an import map is required in the interim until a new release is cut for https://deno.land/x/edgedb)

  • add compiling deno version number

  • add missing deno adapter fn readdir

  • refactor estimateCompilationTarget and getProjectRoot

  • add deno compatible compilation to query builder

seanaye avatar Jul 11 '22 06:07 seanaye

Reproduction with code generation for deno https://github.com/seanaye/fresh-edgedb-starter

seanaye avatar Jul 16 '22 00:07 seanaye

Reproduction with code generation for deno seanaye/fresh-edgedb-starter

Could you share the deno-npx code? I wanna run your repro but that one is missing. :q @seanaye

hyp3rflow avatar Jul 27 '22 05:07 hyp3rflow

Whoops sorry, yea I'll put it on GitHub and update example

seanaye avatar Jul 27 '22 13:07 seanaye

@hyp3rflow updated the deno-npx in deno.json

seanaye avatar Jul 28 '22 00:07 seanaye

@seanaye Now it works! thanks :)

hyp3rflow avatar Jul 28 '22 01:07 hyp3rflow

I've done some works on my forked branch to run compilation without npx. Could you apply commits or review this? 😊 @seanaye https://github.com/hyp3rflow/edgedb-js/commits/deno-qb You can simply compile codes for deno like this: deno run -A --unstable ../edgedb-js/edgedb-deno/_src/reflection/cli.ts --target deno

hyp3rflow avatar Jul 28 '22 05:07 hyp3rflow

All commit authors signed the Contributor License Agreement.
CLA signed

edgedb-cla[bot] avatar Jul 28 '22 14:07 edgedb-cla[bot]

This is much better, thanks @hyp3rflow

seanaye avatar Jul 28 '22 14:07 seanaye

Haven't tested with conflicts resolved, will try tonight

seanaye avatar Jul 28 '22 14:07 seanaye

Good! In addition, I found that my change has some type error due to the wrong or missing Buffer shim. So I pushed one more commit and It seems to be passed with deno check, but I think there will be more clever solution to resolve Buffer shim issue. :q https://github.com/hyp3rflow/edgedb-js/commit/6db93c0a489a98dd75be7894b7f892b63a443373

hyp3rflow avatar Jul 28 '22 14:07 hyp3rflow

@hyp3rflow I have merged in your changes and upstream changes. I am unable to generate with edgedb 2.0. When trying to connect to instance I receive A client connection error occurred; reconnecting because of "waitUntilAvailable=30000". ClientConnectionClosedError: network error: ConnectionRefused: Connection refused (os error 61)

Have you been able to resolve this?

seanaye avatar Aug 02 '22 02:08 seanaye

@seanaye I guess it is related with https://github.com/edgedb/edgedb-js/issues/376. I'm also suffering that issue but sadly I couldn't resolve it. https://github.com/edgedb/edgedb-js/issues/376#issuecomment-1173840632 will resolve the error you received, but it makes another error error: Uncaught (in promise) ClientConnectionFailedError: unexpected message type 115 ("s"). :q

hyp3rflow avatar Aug 02 '22 04:08 hyp3rflow

@hyp3rflow In MacOS I do not get that error, I think the connection succeeds but instead I get

error: Uncaught (in promise) Error: ref name: std::assert_single already registered
      throw new Error(`ref name: ${name} already registered`);
            ^
    at CodeBuilder.registerRef (file:///Users/seanaye/dev/edge2/edgedb-deno/_src/reflection/builders.ts:547:13)
    at generateFuncopTypes (file:///Users/seanaye/dev/edge2/edgedb-deno/_src/reflection/generators/generateFunctionTypes.ts:134:10)
    at generateFunctionTypes (file:///Users/seanaye/dev/edge2/edgedb-deno/_src/reflection/generators/generateFunctionTypes.ts:37:3)
    at generateQB (file:///Users/seanaye/dev/edge2/edgedb-deno/_src/reflection/generate.ts:123:5)
    at async run (file:///Users/seanaye/dev/edge2/edgedb-deno/_src/reflection/cli.ts:304:3)

seanaye avatar Aug 02 '22 05:08 seanaye

@seanaye Okay I forgot pulling the branch so now I get the same error :)

hyp3rflow avatar Aug 02 '22 05:08 hyp3rflow

@seanaye I found that you resolved merge conflict wrong. As you can see reflection/generators/generateFunctionTypes.ts in this commit, the code.registerRef is reordered but in our branch code.registerRef is duplicated after conflict resolved. https://github.com/edgedb/edgedb-js/commit/50869e81cb17f9873ed549a09dd67ca2ae55c83a

hyp3rflow avatar Aug 02 '22 05:08 hyp3rflow

I've re-formatted files and fixed conflict issue! here you can see the PR: https://github.com/seanaye/edgedb-js/pull/3

hyp3rflow avatar Aug 02 '22 05:08 hyp3rflow

Whoops, I'll fix that

seanaye avatar Aug 02 '22 05:08 seanaye

@hyp3rflow I added the root project detection, but in doing so I noticed that if the generation script is inside the deno.json file then the cwd is already identified as the root of the repo. This is because if the script is run with deno task codegen then the cwd of the deno context is the project root. The deno cli also correctly identifies root deno.json when your shell is in a subdir.

seanaye avatar Aug 05 '22 15:08 seanaye

@colinhacks Can you tell us about your schedule for review this PR? We are waiting for this 🥺

disjukr avatar Aug 08 '22 06:08 disjukr

Deno support has officially landed in [email protected] @seanaye @hyp3rflow

Basic usage guidelines:

Generation

$ deno run -A --unstable --reload https://deno.land/x/edgedb/generate.ts

This command supports all same flags as the npx equivalent:

  • Connection flags like -I, --dsn
  • Specify output dir with --output-dir
  • Specify target with --target

Import map

The generated code contains imports from "edgedb". You'll need to define an import map so Deno knows how to resolve this.

deno.json

// deno.json
{
  // ...
  "importMap": "./import_map.json"
}

import_map.json

{
  "imports": {
    "edgedb": "https://deno.land/x/edgedb/mod.ts",
    "edgedb/": "https://deno.land/x/edgedb/"
  }
}

Usage

Nothing special here, most of the examples in the query builder docs will work as-is. Here's a minimal script you can try out.

test.ts

import {createClient} from 'edgedb';

import e from './dbschema/edgeql-js/index.ts';

const client = createClient();
const query = e.select({
  num: e.int64(35),
  msg: e.str('Hello world'),
});

const result = await query.run(client);
console.log(JSON.stringify(result, null, 2));

Deno.exit();
$ deno run -A --unstable test.ts

colinhacks avatar Aug 25 '22 03:08 colinhacks