neumino / thinky

JavaScript ORM for RethinkDB
http://justonepixel.com/thinky/
Other
1.12k stars 128 forks source link

How to store hasAndBelongsToMany with already created documents ? #615

Open smolleyes opened 7 years ago

smolleyes commented 7 years ago

hi

i have this models:

affaires:

const Affaires = thinky.createModel('affaires', {
      id: type.string(),
      name: type.string().required(),
      conformiteId: type.string(),
      affaireTypeId: type.string().required(),
      batId: type.string().required(),
      refAffaireClient: type.string(),
      plans: type.object(),
      createdAt: type.date().default(r.now()),
      modifiedAt: type.date().default(r.now())
});

feuillesDeRoute:

const FeuillesDeRoute = thinky.createModel('feuillesDeRoute', {
      id: type.string(),
      fdrTypeId: type.string().allowNull(false),
      teamId: type.string().allowNull(false),
      date: type.string().allowNull(false),
      createdAt: type.date().default(r.now()),
      modifiedAt: type.date().default(r.now())
});

and chargesAffaires:

const ChargesAffaires = thinky.createModel('chargesAffaires', {
      id: type.string(),
      clientId: type.string().allowNull(false),
      title: type.string(),
      firstname: type.string(),
      lastname: type.string(),
      email: type.string().email(),
      phone: type.string().min(10),
      mobile: type.string().min(10),
      createdAt: type.date().default(r.now()),
      modifiedAt: type.date().default(r.now())
});

and this relations

FeuillesDeRoute.hasAndBelongsToMany(Affaires, 'affaires', 'id', 'id', { type: 'affaire' });
Affaires.hasAndBelongsToMany(FeuillesDeRoute, 'fdr', 'id', 'id', { type: 'affaire' });

Affaires.hasAndBelongsToMany(ChargesAffaires, 'chargesAffaires', 'id', 'id', { type: 'chargeAffaire' });
ChargesAffaires.hasAndBelongsToMany(Affaires, 'affaires', 'id', 'id', { type: 'chargeAffaire' });

now i use graphql on top of on thinky / rethink

when i put this in graphiql (with chargesAffaires already created... so i just pass the id list)

mutation {
  FeuilleDeRoute {
    add(input: {date: "170224", 
      team: "da710b2a-b9be-485f-a426-348e2532207c", 
      type: "68e36d8d-b2df-419d-8559-a568799b15e8",
      affaires: [
        {affaireTypeId: "5ffd0960-9a6b-4e7c-928d-9033235cba53", 
          batId: "17CA0254", 
          name: "olaaaaaa", 
          refAffaireClient: "DB21-45122",
          chargesAffaires: [{id:"4963af5d-4c96-4b09-8ae9-e93775966935"},{id:"591da9cd-c00a-4ffc-8a6b-b679aab1f4f7"}],
        }
      ]}) {
      id
      affaires {
        name
        chargesAffaires {
          lastname
        }
      }
    }
  }
}

after many tests my current code in the corresponding resolve function is (bad i think...)


resolve: (root, { input }) => {
          try {
            const fdr = new models.feuillesDeRoute({
              date: input.date,
              fdrTypeId: input.type,
              teamId: input.team,
            });
            fdr.affaires = [];
            _.each(input.affaires, async (affaire) => {
                const newAffaire = affaire;
                console.log('AFFAIRE = ', affaire);
                const chargesAffaires = await models.chargesAffaires.filter(affaire.chargesAffaires).getJoin();
                console.log(chargesAffaires);
                newAffaire.chargesAffaires = chargesAffaires;
                fdr.affaires.push(new models.affaires(Object.assign({}, newAffaire)));
              });
            return fdr.saveAll({ affaire: true });
          } catch (err) {
            console.log(err);
          }
        }
      }

it return :

{
  "data": {
    "FeuilleDeRoute": {
      "add": {
        "id": "07c97291-2674-4f21-8c91-aed9d7a411b4",
        "affaires": [
          {
            "name": "olaaaaaa",
            "chargesAffaires": [
              {
                "lastname": "Furlin"
              }
            ]
          }
        ]
      }
    }
  }
}

so it SEEMS working but no, i don t have any "affaire" saved in my database and all tables for hasAndBelongsToMany are empty

how can i do to save nested object like this ? where am i wrong ?

i spend 4 days on it and i need it work my work so i m a bit annoyed :(, really appreciate if someone can put me on the good way!

thanks !

smolleyes commented 7 years ago

hummm, seems working if i just call fdr.saveAll() Oo confused with this...