loopbackjs - read data from file using loopback -
i have raw data file 100 records. name - start position 0. address - start position 50.
i want read data[name , address] , validation , store information in db. please suggest how can achieved loopback.
one way approach add remote method 1 of models actual import , use packages 'csv' , 'xml' npm read files. so, basic steps are:
- add remote method existing model
- add packages npm read actual files
- read through files , update database.
something this:
// mymodel.js import fs = require('fs'); import xml = require('xml') import csv = require('csv') export function import(info, callback) { // current database connection. var ds = this.datasource.connector; // *********************************************** // ** read files , write database here ** // *********************************************** // ---> // *********************************************** // return results and/or error information. return callback(null, data) } export function remotemethod(model: any) { model.remotemethod( 'import', { http: { verb: 'get' }, accepts: {arg: 'info', type: 'data' }, returns: [ { arg: 'info', type: 'data' }, { arg: 'data', type: 'data' } ] } ) return model; }
this called using http://mydomain/api/mymodel/import using 'get'.
Comments
Post a Comment