loopback icon indicating copy to clipboard operation
loopback copied to clipboard

get array of data at start of REST API

Open tomson7777 opened this issue 9 years ago • 3 comments

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:

  1. i prepared file "data.json" where i keep array of cars in json format
  2. 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!

tomson7777 avatar Jul 04 '17 20:07 tomson7777

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; } }) }

}

evgen-volk avatar Jul 04 '17 20:07 evgen-volk

@z106a Thanks so much, it works! finally:D

tomson7777 avatar Jul 04 '17 21:07 tomson7777

@z106a Thanks. closing

Setogit avatar Jul 07 '17 20:07 Setogit