get array of data at start of REST API
Hello, I didn't find a solution for my case and i am fighting with it second day:/ I created REST API and it works correctly, i can post/delete/get etc. But i want to load an array of data at start, so then i can give someone my application and that person will have the initial data at start.
What i did so far:
- i prepared file "data.json" where i keep array of cars in json format
- i added this file to datasources.json, as file property with "data.json" value
Then i run server i invoke GET http method, by an array is empty, it doesn't include my cars. any idea how to solve it? i don't want to use any SQL / Mongo databases, i just want only to load initial data from JSON file to memory.
Thanks!
In your server/boot create file genData.js.
const data = [ { email: "[email protected]", firstName: "Maxwell", password: "123", }, { email: "[email protected]", firstName: "Dakota", password: "123" } ];
module.exports = function(app) { var db = app.dataSources.db; const AnyModel = app.models.AnyModel || db.buildModelFromInstance("AnyModel", data, { idInjection: true });
data.map(val => { AnyModel.create(user, function(err, u) { if (err) { throw err; } }) }
}
@z106a Thanks so much, it works! finally:D
@z106a Thanks. closing