Hey all you onetable folks, when doing a create or...
# help
k
Hey all you onetable folks, when doing a create or update how do you pass an array into a model? let transaction = {}       await Foo.create(       {         pk: key,         sk: key,         objects: data.object[0] <--- how do you pass more than one in here? 🙂       }, {transaction})
g
If you are just trying to create one item in the database you would just pass the array. If you are trying to make multiple items in the database they all need different pk/sk values and you would call Foo.create for each item.
k
Thanks Garret. My json looks like this objects: [ { type: 'foo', ... }, { type: 'bar', ... } ] Its okay for the contents to exist as one entry in the database as its not large data and would rarely be viewed (only by an admin if needed)
r
So you want to store an array as a property of a single record?
k
yes, its not ideal, but the alternative is alot of small items in the DB that would be very rarely accessed. not sure if thats bad or not. maybe its not because if they are on the main item they would be accessed every time i call a get on it?
r
In your schema definition you can define your model to have a property with type Array i.e.
Copy code
someProperty:   { type: Array, required: true },
Having separate items for them might be fine too though, it really depends on your access patterns
k
do you know if array works with schema? objects:    { type: Array, schema: {           type:           { type: String }, ...         } }, I have tried this and feel like this doesnt work as it should
r
I don’t know, I don’t think I’ve ever actually used it in that way. I’d be more inclined to define the TypeScript type that reflects the model.
k
I'm going to take that this is difficult because its probably an antipattern Amazon is trying to pull me away from. I will need to link the objects using a pk/sk i guess in the single table.
r
Not necessarily, I think it’s difficult because it’s not well documented in the OneTable library. AWS allows you to have arrays as properties and that might well be perfectly fine. In terms of the OneTable definition you could take very simplistic approach - define it as a string then JSON.stringify it to store the data, then parse it on the way back out. Not great but would work. Alternatively (and better) just define it as a simple Array type in OneTable. Then in your TypeScript type that you’re using (assuming you’re using TypeScript) define the shape of the objects in the array
k
thanks, I will be using TS once I got to grips with the database stuff yeah.