Using Array Attribute Type in Sails
Asked Answered
P

5

5

I'm looking to use the sails attribute type 'array' in my app, but I can't find documentation for this anywhere.

I'd like to do the following:

module.exports = {
 attributes : {
  images: {
   type: ['string']
  },
  location: {
   type: ['float','float']
  }
 }
}

image is an array that will hold a list of image urls and location will hold 2 floats. Will this work in sail's? Else how can I get this to work. Thanks

PS: I'm working solely with MongoDB

Paracelsus answered 24/7, 2016 at 8:19 Comment(0)
T
2

As far as I know, you can only specify it like this:

module.exports = {
    attributes : {
        images: {
            type: 'array'
        },
        location: {
            type: 'array'
        }
    }
}

See Sails ORM Attributes

Tableware answered 24/7, 2016 at 19:30 Comment(1)
The type "array" is no longer supported. To use this type in your model, change type to one of the supported types and set the columnType property to a column type supported by the model's adapter, e.g. { type: 'json', columnType: 'array' }Biancabiancha
B
6

As of Sails 1.0 type array is no longer supported.

The type "array" is no longer supported. To use this type in your model, change type to one of the supported types and set the columnType property to a column type supported by the model's adapter, e.g. { type: 'json', columnType: 'array' }

SOLUTION ONE

Set up property to store an images array and a location array...

module.exports = {
  attributes : {
    images: {
      type: 'json',                           
      columnType: 'array'
    }
    location: {
      type: 'json',
      columnType: 'array'
    }
  }
}

SOLUTION TWO

A more elegant solution is to set up a single object to store both filename and location data

module.exports = {
  attributes : {
    images: {
      type: 'json'
    }
  }
}

Then in your controller you would store object properties as arrays...

let imageData = {
  filename: ["image1.jpg", "image2.jpg", "image3.jpg"],
  location: [154.123123, 155.3434234, 35.12312312]
};

Images.create({images:imageData});

Some issues when storing data to the json object is that a string like "image1.jpg,image2.jpg,image3.jpg" will store in MongoDb no worries... doh. Ensure that when POSTing you may need to split the data .split(',').

Biancabiancha answered 29/7, 2018 at 13:4 Comment(0)
K
4

sailsjs provide a function to solve your question,you can try this

module.exports = {
 attributes : {
  images: {
   type: 'string'
  },
  location: {
   type: 'json'
  }
 }
}
Kaka answered 5/8, 2016 at 8:52 Comment(3)
Please add short explanation to your answer for future visitors.Kliment
I'm not sure how this is intended to work. The images attribute is supposed to be an array of strings but you have just string. Also from location's syntax, this specifies that it's value can either be float or floatParacelsus
sorry , im just edditing my post for solve your probKaka
T
2

As far as I know, you can only specify it like this:

module.exports = {
    attributes : {
        images: {
            type: 'array'
        },
        location: {
            type: 'array'
        }
    }
}

See Sails ORM Attributes

Tableware answered 24/7, 2016 at 19:30 Comment(1)
The type "array" is no longer supported. To use this type in your model, change type to one of the supported types and set the columnType property to a column type supported by the model's adapter, e.g. { type: 'json', columnType: 'array' }Biancabiancha
L
1

For sails 1.0, for array maybe you can try this way i'm using just for sharing. Also you can replace before update and process native query() and delete the attributes for updating by waterline. Hope this help you.

        variants:
        {
            type: 'json',
            columnType: 'array',
            custom: (value) =>
            {
                /*
                  [
                        code    : unique, string
                        name    : string, maxLength[30]
                        cost    : number, isFinite
                        price   : number, isFinite
                        default : boolean
                  ]
                */
                return _.isArray(value)
                &&     _.every(value, (variant1) =>
                {
                    return _.countBy(value, (variant2) =>
                    {
                        return variant2.code == variant1.code ? 'eq' : 'uneq';
                    }).eq <= 1
                    && _.isString(variant1.name) && variant1.name.length < 30
                    && _.isNumber(variant1.cost) && _.isFinite(variant1.cost)
                    && _.isNumber(variant1.price) && _.isFinite(variant1.price)
                    && _.isBoolean(variant1.default);
                });
            },
        },
Lumen answered 2/10, 2018 at 6:55 Comment(0)
R
-1

You can use type as ref for arrays and objects.

module.exports = {
 attributes: {
  images: {
   type: 'ref'
  },
  location: {
   type: 'ref'
  }
 }
}
Rale answered 17/8, 2022 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.