1

I want to create a model like below. users model having attribute agreements of type array.I was not able to find a example in waterline where attributes type is an array of object. Please advise `

module.exports = {

  attributes: {
    userName: {
      type: "string",
      unique: true,
      required: true
    },
    Name: {
      type: "string",
      required: true,
      minLength: 2
    },
    phone: {
      type: "string",
      required: true
    },
    password: {
      type: "string",
      minLength: 6

    },
    roles: {
      type: "array",
      required: true,
      enum: ['Admin', 'User']

    },
    agreements: {
       type : "array",
      agreement :{
        version : "string",
        dateSigned :"date",


      }

    },

`

1 Answer 1

1

Use one to many association.

Assume that your model is Model.js

agreements: { collection: 'Agreement', via : 'model' }

Create Agreement.js in models

module.exports: {
  attributes: {
    model      : { model: 'Model' },
    version    : { type: 'string' },
    dateSigned : { type: 'datetime' }
  }
}

See this documentation.

Sign up to request clarification or add additional context in comments.

3 Comments

Its many to many relationship. Users must sign all agreements. and apart from agreement model i need to store some extra fields which are specific to particular user and agreement. Like (dateSigned , IP address and User agent of client machine) .
You could create a model (maybe named UserAgreement or AgreementSignature) that has a OneToMany relation to User, a OneToMany relation to Agreement and the attributes that you want.
It's true, all that you need is fixing your database structure and associations, because agreement : { version : "string", dateSigned :"date" } is not a valid configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.