-1

Please help in accessing postgresql database view using strongloop.
im able to access table

{"name": "test", 
   "options": {
      "idInjection": false,
      "postgresql": {
        "schema": "public",
        "table": "test_data_v"
      }
   }, 
   "properties": {

      "assetid": {
        "type": "String",
        "required": false,
        "length": 40,
        "precision": null,
        "scale": null,
        "id": 1,
        "postgresql": {
          "columnName": "asset_id",
          "dataType": "character varying",
          "dataLength": 40,
          "dataPrecision": null,
          "dataScale": null,
          "nullable": "YES"
        }
      }  
    }}

in same way please suggest me how to access view Thanks Divya

Yasel
  • 2,920
  • 4
  • 40
  • 48
divya
  • 261
  • 1
  • 3
  • 8

1 Answers1

0

I am not installed postgresql but I tried in mysql, Its working fine. IN your model you can do directly see this Example

In database I have created view that is

CREATE VIEW shareviews AS
SELECT id,name
FROM share where id = 1;

In model you can call viewname directly like this example

module.exports = function(Share) {

    var server = require('../../server/server');
    var ds = server.dataSources.MySQL; // use  server.dataSources.postgres;

    Share.list = function(optionalparam, cb) {     

      var sql = 'select * from shareviews';
      ds.connector.execute(sql, function(err, data)
      {
      if(err) return err;
      console.log(err);
      console.log("data",data);
        cb(null, data);
      });

    }

    Share.remoteMethod(
        'list', 
        {
          accepts: {arg: 'param', type: 'string'},
          returns: {arg: 'result', type: 'object'},
          http: {path: '/list', verb: 'get'}
        }
    );


};

You need to set data source in datasource.json

{
  "db": {
    "name": "db",
    "connector": "memory"
  },
  "postgres": {
    "host": "localhost",
    "port": 5432,
    "database": "postgres",
    "username": "postgres",
    "password": "*******",
    "name": "postgres",
    "connector": "postgresql"
  }
}

Then in model-config.json you need to assign data source name to each model.

That is

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins"
    ]
  },
  "User": {
    "dataSource": "db"
  },
  "AccessToken": {
    "dataSource": "db",
    "public": false
  },
  "ACL": {
    "dataSource": "db",
    "public": false
  },
  "RoleMapping": {
    "dataSource": "db",
    "public": false
  },
  "Role": {
    "dataSource": "db",
    "public": false
  },
  "yourmodelname": {
    "dataSource": "postgres",
    "public": true
  },
  "yourmodelname": {
    "dataSource": "postgres",
    "public": true
  }
}

then you can access database in you model.js or Rest call(example localhost:3000/explorer) For Example my model name Grocerylist

module.exports = function(Grocerylist) {
  Grocerylist.beforeRemote('create', function(context, user, next) {
    var req = context.req;
    req.body.date = Date.now();
    req.body.shopperId = req.accessToken.userId;
    next();
  });

  Grocerylist.complete = function(shopperId, cb) {
    Grocerylist.find({
      where: {
        purchased:false,
        shopperId: shopperId,
      }
    }, function(err, list) {
        var response;
        if (typeof list === 'undefined' || list.length === 0) {
          response = "All done shopping!"
        }
        else {
          response = "Shopping is not done.";
        }
        cb(null, response);
    });
  };
  Grocerylist.remoteMethod(
    'complete',
    {
      accepts: {
        arg: 'shopperId', type: 'string'
      },
      http: {
        path: '/complete',
        verb: 'get'
      },
      returns: {
        arg: 'complete',
        type: 'string'
      }
    }
  );
};
Riaz
  • 561
  • 6
  • 15
  • Please help in accessing postgresql database view using strongloop. im able to access table – divya Mar 06 '16 at 08:13
  • im able to access table by adding table in model.json using the below code. please help to access view from database in the same way – divya Mar 06 '16 at 08:14
  • {"name": "test", "options": { "idInjection": false, "postgresql": { "schema": "public", "table": "test_data_v" } }, "properties": { "assetid": { "type": "String", "required": false, "length": 40, "precision": null, "scale": null, "id": 1, "postgresql": { "columnName": "asset_id", "dataType": "character varying", "dataLength": 40, "dataPrecision": null, "dataScale": null, "nullable": "YES" } } }} – divya Mar 06 '16 at 08:14
  • supervisor running without clustering (unsupervised) module.js:489 throw err; – divya Mar 07 '16 at 06:52
  • SyntaxError: D:\GEGDC\MD98012\DB CONNECTIN SYSTEM STATUS\hitsyss\server\common\models\iso_serverscope_view.json: Unexpected token v at Object.parse (native) at Object.Module._extensions..json (module.js:486:27) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) – divya Mar 07 '16 at 06:52
  • at Module.module.__proto__.require (C:\Users\md98012\AppData\Roaming\npm\node_modules\strongloop\node_modules\strong-supervisor\node_mod ules\strong-agent\lib\agent.js:185:42) at require (module.js:384:17) at loadModelDefinition (D:\GEGDC\MD98012\DB CONNECTIN SYSTEM STATUS\hitsyss\server\node_modules\loopback-boot\lib\compiler.js:440:20) at D:\GEGDC\MD98012\DB CONNECTIN SYSTEM STATUS\hitsyss\server\node_modules\loopback-boot\lib\compiler.js:336:21 at Array.forEach (native) – divya Mar 07 '16 at 06:53
  • "isocmdbviwe": { "dataSource": "dev_corp_it", "public": true } – divya Mar 07 '16 at 07:16
  • "dev_corp_it": { "host": "0.0.0.0", "port": 5432, "database": "corp_it", "username": "test", "password": "testt", "name": "dev_corp_it", "debug": true, "connector": "postgresql" } – divya Mar 07 '16 at 07:17
  • module.exports = function(isocmdbviwe) { var isocmdc = require('../../server/server/datasources.json'); var ds = isocmdc.dataSources.dev_corp_it; isocmdbviwe.list = function(optionalparam, cb) { var sql = 'select * from test'; ds.connector.execute(sql, function(err, data) {if(err) return err; console.log("data",data); cb(null, data); }); } isocmdbviwe.remoteMethod( 'list', { returns: {arg: 'result', type: 'object'}, http: {path: '/list', verb: 'get'} } );}; – divya Mar 07 '16 at 07:20
  • Riax thanx for your quick respone.. i have added the above code 1st once is mode-config.json 2nd databaource.json 3rd model.js – divya Mar 07 '16 at 07:22
  • SyntaxError: D:test\setrver\common\models\iso_serverscope_view.json: Unexpected token v at Object.parse at Object.Module._extensions..json module.js at Module.load module.js at Function.Module._load module.js at Module.require module.js at Module.module.__proto__.require C:\Users\md98012\AppData\Roaming\npm\node_modules\strongloop\node_modules\strong-supervisor\node_moules\strong-agent\lib\agent.js:185:42 at require module.js:384:17 at loadModelDefinition D:test\server\node_modules\loopback-boot\lib\compiler.js at D:test\server\node_modules\loopback-boot\lib\compiler.js at Array.forEach – divya Mar 07 '16 at 07:31
  • var isocmdc = require('../../server/server'); try this – Riaz Mar 07 '16 at 07:31
  • Riaz, i have added var isocmdc = require('../../server/server'); – divya Mar 07 '16 at 07:33
  • its syntax error. I think if you add any comments remove it. in your datasource or modelconfig.Json check once all the things – Riaz Mar 07 '16 at 07:40
  • otherwise create new sample app and try. – Riaz Mar 07 '16 at 07:44
  • im able to access table by adding below code in model,js with same database.json and model-config.json – divya Mar 07 '16 at 07:52
  • {"name": "isocmdbviwe", "options": { "idInjection": false, "postgresql": { "schema": "public", "table": "mstr_invnt_tech" } }, "properties": { "assetid": { "type": "String", "required": false, "length": 50, "precision": null, "scale": null, "id": 1, "postgresql": { "columnName": "asset_id", "dataType": "character varying", "dataLength": 50, "dataPrecision": null, "dataScale": null, "nullable": "YES" } } }} – divya Mar 07 '16 at 07:52
  • but im not able to access view – divya Mar 07 '16 at 07:52
  • Unexpected token v at Object.parse (native) at Object.Module._extensions..json (module.js:486:27) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) – divya Mar 07 '16 at 09:01
  • did you try new sample application? – Riaz Mar 07 '16 at 09:09
  • riaz i have installed npm install pg pg-native.. but im nt getting how to start nthat application – divya Mar 07 '16 at 09:13
  • do you have basic knowledge in strongloop ? – Riaz Mar 07 '16 at 09:17
  • yes , in my project im using loop-back-mysql perviously – divya Mar 07 '16 at 09:19
  • now i need to access view from postgresql im facing issue there – divya Mar 07 '16 at 09:20
  • look this demo application, https://github.com/0candy/grocerylist . . I am not having knowledge in postgresql – Riaz Mar 07 '16 at 09:23
  • Riaz, thanks for your help but in that application they are not exeguting any queries – divya Mar 07 '16 at 09:40