Giter Club home page Giter Club logo

Comments (3)

VictorPulzz avatar VictorPulzz commented on June 2, 2024

@agentdon But my problem is that relations generally don’t arrive in the get response, do you know what the problem could be?

import { faker } from '@faker-js/faker';
import { belongsTo, createServer, Factory, Model } from 'miragejs';
import { ModelDefinition } from 'miragejs/-types';

import { TaskModel } from '~/entities/Task';
import { UserModel } from '~/entities/User';

export const userFactory = Factory.extend<UserModel>({
  id(i) {
    return i;
  },
  firstName() {
    return faker.person.firstName();
  },
  lastName() {
    return faker.person.lastName();
  },
  email() {
    return faker.internet.email({
      firstName: faker.person.firstName(),
      lastName: faker.person.lastName(),
    });
  },
  address() {
    return faker.location.country();
  },
  dob() {
    return faker.date.birthdate({ min: 18, max: 65, mode: 'age' }).toString();
  },
  avatar() {
    return faker.internet.avatar();
  },
});

export const taskFactory = Factory.extend<TaskModel>({
  id(i) {
    return i;
  },
  description() {
    return faker.lorem.paragraph(4);
  },
  createdAt() {
    return faker.date.recent().toString();
  },
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  afterCreate(task, server) {
    task.update({
      user: server.create('user'),
    });
  },
});

const UserDefModel: ModelDefinition<UserModel> = Model.extend({});
const TaskDefModel: ModelDefinition<TaskModel> = Model.extend({
  user: belongsTo(),
});

export function startMockServer() {
  createServer({
    namespace: 'api',
    models: {
      user: UserDefModel,
      task: TaskDefModel,
    },
    factories: {
      user: userFactory,
      task: taskFactory,
    },
    seeds(server) {
      server.createList('task', 100);
    },
    routes() {
      this.get(`/tasks`);
    },
  });
}

Response from console:
image

from miragejs.

cah-brian-gantzler avatar cah-brian-gantzler commented on June 2, 2024

@agentdon JsonAPI is an entire container, I would assume that once you start a JsonApi container, what would a none JsonApi object even look like within that container. Just given the spec, I dont think that would work. I understand wanting to do it gradually but I would think you would have to do it response by response (everything in the response) rather than a table by table thing.

@VictorPulzz I havent used factories much, but did you confirm that the afterCreate is being called and the user is being added to tasks. I would put a debugger in the seeds after server.createList and inspect the mirage DB. I think it would be server.schema.tasks.all(). I would do the same for users and if there were any created. server.schema.users.add()

from miragejs.

agentdon avatar agentdon commented on June 2, 2024

@agentdon JsonAPI is an entire container, I would assume that once you start a JsonApi container, what would a none JsonApi object even look like within that container. Just given the spec, I dont think that would work. I understand wanting to do it gradually but I would think you would have to do it response by response (everything in the response) rather than a table by table thing.

Got it. That was the route that we wound up taking. Thanks for weighing in!

from miragejs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.