discover loopback models js

0

I am trying to discover the models in an oracle database with loopback as the documentation says but I do not know how to generate the .json models, they know of some example, in the documentation it says that it should be used with fs.writeFile() but not the example is shown, I am using the following to connect me

var loopback = require('loopback');    
var ds = loopback.createDataSource('oracle', {
   "host": "oracle.local",
    "port": 1521,
    "database": "XE",
    "password": "password",
    "user": "user",
});

function schemaCallback(err, schema) {
    if (err) {
      console.error(err);
    }
  console.log(schema)
  }
// Discover and build models from INVENTORY table
ds.discoverAndBuildModels('VISION', {owner:'owner', relations: true, all:true , associations:true}, schemaCallback);
    
asked by Paul 29.04.2017 в 06:10
source

1 answer

0

If you have already verified that you can connect to the base and that it detects the model correctly, follow these steps.

  • At the beginning of your script, load the module fs
  • Then define the path to save your model, usually common/models/modelo.json
  • Next, place the code you already have
  • Finally, save the file with the fs.writeFile method
  •   

    Notes

         
    • It is recommended that you place the discovered model in a variable.
    •   
    • The route I placed is relative to the location of the script, change it appropriately
    •   
    • Use the function JSON.stringifly to save the model, verify the final result and if it is not correct or it does not look good, you can delete it without any problem .
    •   
    • Check the generated JSON file and make sure that the fields base and plural are well defined.
    •   
    • Verify that the model is defined in ./server/model-config.json
    •   

    This is the sample code.

    // cargas el módulo fs
    const fs = require('fs)
    
    // la ruta donde vas a guardar tu modelo
    const archivoModelo = './common/models/vision.json'
    
    // El código de tu conexión
    // coloca en una variable el modelo "descubierto"
    // p. ej.
    // const modeloDescubierto = ds.discoverAndBuildModels('VISION'...
    
    // Guarda la salida en el archivo
    fs.writeFile(modeloGenerado, JSON.stringify(modeloDescubierto), (err) => {
      if(err) return console.log(err)
      console.log("El modelo se genero exitosamente")
    })
    
        
    answered by 29.04.2017 в 15:44