ApiResource.ts 780 B

1234567891011121314151617181920212223242526272829
  1. import {Model} from "pinia-orm";
  2. /**
  3. * Base class for resources that can be fetched from the API
  4. */
  5. export class ApiResource extends Model {
  6. public static relations: Record<string, ApiResource>
  7. /**
  8. * Fix the 'Cannot stringify arbitrary non-POJOs' warning, meaning server can not parse the store
  9. *
  10. * @see https://github.com/vuex-orm/vuex-orm/issues/255#issuecomment-876378684
  11. */
  12. toJSON () {
  13. return { ...this }
  14. }
  15. /**
  16. * Is it a newly created entity?
  17. *
  18. * If it is, it means this entity does not exist in the data source and that it has a temporary id
  19. */
  20. public isNew(): boolean {
  21. return !this.id || (typeof this.id === 'string' && this.id.slice(0, 3) === 'tmp')
  22. }
  23. }
  24. export default ApiResource