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

feat(core): type safe references #691

Merged
merged 1 commit into from Jul 30, 2020
Merged

feat(core): type safe references #691

merged 1 commit into from Jul 30, 2020

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Jul 29, 2020

EM now returns Loaded<T, P> instead of the entity (T). This type automatically
adds synchronous method get() that returns the entity (for references) or array
of entities (for collections).

There is also $ property that contains the same as return value of this get()
method.

const book1 = await orm.em.findOneOrFail(Book, bible, { populate: ['publisher', 'tags'] });
expect(book1.publisher!.$.name).toBe('Publisher 123');
expect(book1.tags.$[0].name).toBe('t1');
expect(book1.tags.$[1].name).toBe('t2');
expect(book1.tags.$[2].name).toBe('t3');
orm.em.clear();

const books = await orm.em.find(Book, { id: bible.id }, { populate:
  { publisher: { books: { publisher: true } } },
});
expect(books[0].publisher!.$.books.$[0].publisher!.$.name).toBe('Publisher 123');

const book5 = await orm.em.findOneOrFail(Book, bible, { populate:
  { publisher: true, tags: true, perex: true },
});
expect(book5.publisher!.$.name).toBe('Publisher 123');
expect(book5.tags.$[0].name).toBe('t1');

Closes #214

BREAKING CHANGE:
Reference.get() is now available only with correct Loaded type hint and is used
as a sync getter for the entity, just like unwrap(). You can use Reference.load(prop)
for the original get() method functionality.

em.find() and similar methods now have two type arguments, due to TypeScript not supporting
partial type inference, it is no longer possible to specify the T explicitly (without also
explicitly specifying the load hint).

@B4nan B4nan mentioned this pull request Jul 29, 2020
46 tasks
@lgtm-com
Copy link

lgtm-com bot commented Jul 29, 2020

This pull request introduces 1 alert when merging da33d0d into de5e66d - view on LGTM.com

new alerts:

  • 1 for Unused variable, import, function or class

@lgtm-com
Copy link

lgtm-com bot commented Jul 30, 2020

This pull request introduces 1 alert when merging 95ccebe into de5e66d - view on LGTM.com

new alerts:

  • 1 for Unused variable, import, function or class

EM now returns `Loaded<T, P>` instead of the entity (`T`). This type automatically
adds synchronous method `get()` that returns the entity (for references) or array
of entities (for collections).

There is also `$` property that contains the same as return value of this `get()`
method.

```typescript
const book1 = await orm.em.findOneOrFail(Book, bible, { populate: ['publisher', 'tags'] });
expect(book1.publisher!.$.name).toBe('Publisher 123');
expect(book1.tags!.$[0].name).toBe('t1');
expect(book1.tags!.$[1].name).toBe('t2');
expect(book1.tags!.$[2].name).toBe('t3');
orm.em.clear();

const books = await orm.em.find(Book, { id: bible.id }, { populate:
  { publisher: { books: { publisher: true } } },
});
expect(books[0].publisher!.$.books.$[0].publisher!.$.name).toBe('Publisher 123');

const book5 = await orm.em.findOneOrFail(Book, bible, { populate:
  { publisher: true, tags: true, perex: true },
});
expect(book5.publisher!.$.name).toBe('Publisher 123');
expect(book5.tags.$[0].name).toBe('t1');
```

Closes #214

BREAKING CHANGE:
`Reference.get()` is now available only with correct `Loaded` type hint and is used
as a sync getter for the entity, just like `unwrap()`. You can use `Reference.load(prop)`
for the original `get()` method functionality.

`em.find()` and similar methods now have two type arguments, due to TypeScript not supporting
partial type inference, it is no longer possible to specify the `T` explicitly (without also
explicitly specifying the load hint).
@lgtm-com
Copy link

lgtm-com bot commented Jul 30, 2020

This pull request introduces 1 alert when merging 4b57cf4 into de5e66d - view on LGTM.com

new alerts:

  • 1 for Unused variable, import, function or class

@B4nan B4nan merged commit 7071630 into dev Jul 30, 2020
@B4nan B4nan deleted the type-safe-refs2 branch July 30, 2020 08:36
B4nan added a commit that referenced this pull request Jul 30, 2020
B4nan added a commit that referenced this pull request Jul 30, 2020
B4nan added a commit that referenced this pull request Aug 2, 2020
EM now returns `Loaded<T, P>` instead of the entity (`T`). This type automatically
adds synchronous method `get()` that returns the entity (for references) or array
of entities (for collections).

There is also `$` property that contains the same as return value of this `get()`
method.

```typescript
const book1 = await orm.em.findOneOrFail(Book, bible, { populate: ['publisher', 'tags'] });
expect(book1.publisher!.$.name).toBe('Publisher 123');
expect(book1.tags!.$[0].name).toBe('t1');
expect(book1.tags!.$[1].name).toBe('t2');
expect(book1.tags!.$[2].name).toBe('t3');
orm.em.clear();

const books = await orm.em.find(Book, { id: bible.id }, { populate:
  { publisher: { books: { publisher: true } } },
});
expect(books[0].publisher!.$.books.$[0].publisher!.$.name).toBe('Publisher 123');

const book5 = await orm.em.findOneOrFail(Book, bible, { populate:
  { publisher: true, tags: true, perex: true },
});
expect(book5.publisher!.$.name).toBe('Publisher 123');
expect(book5.tags.$[0].name).toBe('t1');
```

Closes #214

BREAKING CHANGE:
`Reference.get()` is now available only with correct `Loaded` type hint and is used
as a sync getter for the entity, just like `unwrap()`. You can use `Reference.load(prop)`
for the original `get()` method functionality.

`em.find()` and similar methods now have two type arguments, due to TypeScript not supporting
partial type inference, it is no longer possible to specify the `T` explicitly (without also
explicitly specifying the load hint).
B4nan added a commit that referenced this pull request Aug 2, 2020
B4nan added a commit that referenced this pull request Aug 9, 2020
EM now returns `Loaded<T, P>` instead of the entity (`T`). This type automatically
adds synchronous method `get()` that returns the entity (for references) or array
of entities (for collections).

There is also `$` property that contains the same as return value of this `get()`
method.

```typescript
const book1 = await orm.em.findOneOrFail(Book, bible, { populate: ['publisher', 'tags'] });
expect(book1.publisher!.$.name).toBe('Publisher 123');
expect(book1.tags!.$[0].name).toBe('t1');
expect(book1.tags!.$[1].name).toBe('t2');
expect(book1.tags!.$[2].name).toBe('t3');
orm.em.clear();

const books = await orm.em.find(Book, { id: bible.id }, { populate:
  { publisher: { books: { publisher: true } } },
});
expect(books[0].publisher!.$.books.$[0].publisher!.$.name).toBe('Publisher 123');

const book5 = await orm.em.findOneOrFail(Book, bible, { populate:
  { publisher: true, tags: true, perex: true },
});
expect(book5.publisher!.$.name).toBe('Publisher 123');
expect(book5.tags.$[0].name).toBe('t1');
```

Closes #214

BREAKING CHANGE:
`Reference.get()` is now available only with correct `Loaded` type hint and is used
as a sync getter for the entity, just like `unwrap()`. You can use `Reference.load(prop)`
for the original `get()` method functionality.

`em.find()` and similar methods now have two type arguments, due to TypeScript not supporting
partial type inference, it is no longer possible to specify the `T` explicitly (without also
explicitly specifying the load hint).
B4nan added a commit that referenced this pull request Aug 9, 2020
@B4nan B4nan mentioned this pull request Aug 16, 2021
48 tasks
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 this pull request may close these issues.

None yet

1 participant