Skip to content
This repository was archived by the owner on Jan 15, 2020. It is now read-only.
This repository was archived by the owner on Jan 15, 2020. It is now read-only.

LB4: Model discovery to create model file #50

@DivCorleone

Description

@DivCorleone

Feature proposal

Model discovery to create model file

Current Behavior

Model discovery do not create model file

Expected Behavior

I have a mySQL database already created, i would like to discover the models and create their model file.

versions:

@loopback/cli version: 1.0.1

@loopback/* dependencies:
  - @loopback/authentication: ^1.0.1
  - @loopback/boot: ^1.0.1
  - @loopback/build: ^1.0.0
  - @loopback/context: ^1.0.0
  - @loopback/core: ^1.0.0
  - @loopback/metadata: ^1.0.0
  - @loopback/openapi-spec-builder: ^1.0.0
  - @loopback/openapi-v3-types: ^1.0.0
  - @loopback/openapi-v3: ^1.0.1
  - @loopback/repository-json-schema: ^1.0.1
  - @loopback/repository: ^1.0.1
  - @loopback/rest: ^1.0.1
  - @loopback/testlab: ^1.0.0
  - @loopback/docs: ^1.0.1
  - @loopback/example-hello-world: ^1.0.1
  - @loopback/example-log-extension: ^1.0.1
  - @loopback/example-rpc-server: ^1.0.0
  - @loopback/example-todo: ^1.0.1
  - @loopback/example-soap-calculator: ^1.0.1
  - @loopback/service-proxy: ^1.0.0
  - @loopback/http-caching-proxy: ^1.0.0
  - @loopback/http-server: ^1.0.0
  - @loopback/example-todo-list: ^1.0.1
  - @loopback/dist-util: ^0.4.0

Using this connector loopback-connector-mysql i can connect to the database and retrieve models:

let dataSrc = new juggler.DataSource(name, {...});

dataSrc.on('connected', () => {
  dataSrc.discoverAndBuildModels(modelName, {relations: true}, (err, model) => {
    console.log('model:', model);
  })
})

The model is correctly imported, but the corresponding file is not created. I have found nothing in the doc about creating the file so i made a little somewhat in nodejs to generate the files:

var util = require('util'),
  process = require('child_process'),
  fs = require('fs'),
  path = require('path'),
  appDir = path.dirname(require.main.filename);

export function writeModel(name, schema, upperCaseName) {
  let stream = fs.createWriteStream(appDir + "/src/models/" + name + ".model.ts");

  let header = "import {Entity, model, property} from '@loopback/repository';\n\n"
             + "@model()\n"
             + "export class " + upperCaseName + " extends Entity {";

  let footer = "\n\tconstructor(data?: Partial<" + upperCaseName + ">) {\n"
             + "\t\tsuper(data);\n"
             + "\t}\n"
             + "}"

  let mapping = {};
  mapping['date'] = 'string';
  mapping['binary'] = 'boolean';

  stream.once('open', function(fd) {
    stream.write(header);

    for (let prop in schema) {
      console.log('prop: ', prop);

      let property = "\n\t@property({\n"
                   + "\t\ttype: '" + schema[prop].type.toLowerCase() + "',\n"
                   + (schema[prop].id ? "\t\tid: true,\n" : "")
                   + (schema[prop].require ? "\t\trequired: true,\n" : "")
                   + "\t})\n"

      let declaration = "\t" + prop
                      + (schema[prop].require ? "": "?") + ": "
                      + (mapping[schema[prop].type.toLowerCase()]
                        ? mapping[schema[prop].type.toLowerCase()]
                        : schema[prop].type.toLowerCase())
                      + ";\n";

     stream.write(property + declaration);
    }

    stream.write(footer);

    stream.end();
  });
}

The files are created, compiled by typescript, but when using lb4 repository and selecting a model, it has an error: Generation is aborted: Error: You did not select a valid model.

Example of generated file:

import {Entity, model, property} from '@loopback/repository';

@model()
export class Webmessagessent extends Entity {
	@property({
		type: 'number',
		id: true,
	})
	id?: number;

	@property({
		type: 'number',
	})
	messageid?: number;

	@property({
		type: 'number',
	})
	destid?: number;

	@property({
		type: 'date',
	})
	datetimecreation?: string;

	@property({
		type: 'date',
	})
	datetimeopen?: string;

	@property({
		type: 'date',
	})
	datetimetrash?: string;

	@property({
		type: 'binary',
	})
	visible?: boolean;

	@property({
		type: 'number',
	})
	reptomsgid?: number;

	@property({
		type: 'number',
	})
	destcompanyid?: number;

	constructor(data?: Partial<Webmessagessent>) {
		super(data);
	}
}

Is creating a model file from model discovery already a feature ?

Can we create a model file programmatically like the command line client lb4 would do it ?

Am i missing something when generating the file ?

See Reporting Issues for more tips on writing good issues

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions