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

Docs: bulk insert needs e.json_get #908

Open
MiroslavPetrik opened this issue Mar 15, 2024 · 0 comments
Open

Docs: bulk insert needs e.json_get #908

MiroslavPetrik opened this issue Mar 15, 2024 · 0 comments

Comments

@MiroslavPetrik
Copy link
Contributor

MiroslavPetrik commented Mar 15, 2024

Describe the bug
The bulk insert example needs to use the e.json_get() instead of directly accessing the element

title: e.cast(e.str, item.title),

Reproduction
image

No overload matches this call.
  Overload 1 of 2, '(target: scalarTypeWithConstructor<$str, never>, arg: null): $expr_Cast<scalarTypeWithConstructor<$str, never>, Cardinality.Empty>', gave the following error.
    Argument of type '$expr_Operator<$json, Cardinality.One> | undefined' is not assignable to parameter of type 'null'.
      Type 'undefined' is not assignable to type 'null'.
  Overload 2 of 2, '(target: scalarTypeWithConstructor<$str, never>, expr: orScalarLiteral<TypeSet<BaseType, Cardinality>>): $expr_Cast<...>', gave the following error.
    Argument of type '$expr_Operator<$json, Cardinality.One> | undefined' is not assignable to parameter of type 'orScalarLiteral<TypeSet<BaseType, Cardinality>>'.
      Type 'undefined' is not assignable to type 'orScalarLiteral<TypeSet<BaseType, Cardinality>>'.ts(2769)

Expected behavior

I believe to update docs is just ok.

 const query = e.params({ items: e.json }, (params) => {
    return e.for(e.json_array_unpack(params.items), (item) => {
      return e.insert(e.ProductCategory, {
        name: e.cast(e.str, e.json_get(item, "name")),
        icon: e.cast(e.str, e.json_get(item, "icon")),
      });
    });
  });

But the code works with the type error, so maybe the types in the generator needs to be updated?

Or just assert the undefined with !:

const query = e.params({ items: e.json }, (params) => {
    return e.for(e.json_array_unpack(params.items), (item) => {
      return e.insert(e.ProductCategory, {
        name: e.cast(e.str,  item.name!),
        icon: e.cast(e.str, item.icon!),
      });
    });
  });

Versions (please complete the following information):

  • OS: w11
  • "@edgedb/generate": "^0.4.1",
  • EdgeDB version (e.g. 2.0): 4.5
  • EdgeDB CLI version (e.g. 2.0): 4.1
  • edgedb-js version (e.g. 0.20.10;): 0.2.0-beta

Additional context
also with plain eql it does not work without json_get:

#error: InvalidReferenceError: invalid property reference on a primitive type expression

for item in json_array_unpack(<json>$items)
union (select {
	name := item.name,
	icon := item.icon
})
# OK

for item in json_array_unpack(<json>$items)
union (select {
	name := <str>json_get(item, "name"),
	icon := <str>json_get(item, "icon")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants