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

Docs: bulk insert needs `e.json_get`

Open MiroslavPetrik opened this issue 1 year ago • 0 comments

Describe the bug The bulk insert example needs to use the e.json_get() instead of directly accessing the element https://github.com/edgedb/edgedb-js/blob/f9f39c918bac0385f4e55e0667a15c35d66f3a2d/docs/for.rst?plain=1#L31

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")
})

MiroslavPetrik avatar Mar 15 '24 10:03 MiroslavPetrik