batwadi
batwadi

Reputation: 1906

Sails.js attribute type array in waterline mongoDB

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",


      }

    },

`

Upvotes: 1

Views: 3816

Answers (1)

Andi N. Dirgantara
Andi N. Dirgantara

Reputation: 2051

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.

Upvotes: 1

Related Questions