Breeze / breeze.tooling

Utilities and tooling for Breeze
17 stars 12 forks source link

Error in registration-helper.ts #9

Open bancroftway opened 7 years ago

bancroftway commented 7 years ago

After running "gulp generate", I did RegistrationHelper.register in my repository class like so:

constructor() {
        this.db = new EntityManager("http://localhost:62749/breeze/data");
        RegistrationHelper.register(this.db.metadataStore);
    }

but I am getting an error: Property 'initializer' does not exist on type 'typeof Location'.

The generated registration-helper.ts is below. When watching Brian Noyes' course, I did not see the ".initializer" code....so is it safe to remove this?

import { MetadataStore } from 'breeze-client';

import { Location } from './location';

export class RegistrationHelper {

    static register(metadataStore: MetadataStore) {
        metadataStore.registerEntityTypeCtor('Location', Location, Location.initializer);
    }
}

The generated Location class does not have initializer property. Here is the generated Location class:


import { EntityBase } from './entity-base';

/// <code-import> Place custom imports between <code-import> tags

/// </code-import>

export class Location extends EntityBase {

   /// <code> Place custom code between <code> tags

   /// </code>

   // Generated code. Do not place code below this line.
   id: number;
   description: string;
   lat: number;
   lng: number;
   dateCreated: Date;
   dateModified: Date;
   createdByUserId: string;
}
marcelgood commented 7 years ago

Run generate again. There's a bug in the logic, which doesn't create the initializer the first time around.

bancroftway commented 7 years ago

I can confirm that running "gulp generate" multiple times did not fix this issue.

Here is the output...BTW, the output has the wording "Initializer function "initializer" discovered for entity type: Location" but the generated class for 'Location' does not contain 'initializer'.

Also, should the 'initializer' property have been generated as part of the generated model classes (e.g. Location in this case or do you think it needs to be on EntityBase? If latter, could you please provide an updated definition of what EntityBase should look like. I am noting my EntityBase class below.

[19:06:47] Using gulpfile R:\Projects\Bancroftway\Fruitmap\Tools\gulpfile.js
[19:06:47] Starting 'generate'...
[19:06:47] running: ./bin/metadatagenerator -i ../Data/bin/Debug/Data.dll -o ./db.metadata.json
[19:06:47] Finished 'generate' after 15 ms
[19:06:47] MetadataGenerator -i ../Data/bin/Debug/Data.dll -o ./db.metadata.json

[19:06:47] completed: ./bin/metadatagenerator -i ../Data/bin/Debug/Data.dll -o ./db.metadata.json
{ inputFileName: './db.metadata.json',
  outputFolder: '../Webapp/src/model',
  camelCase: true,
  kebabCaseFileNames: true,
  baseClassFileName: 'EntityBase' }
Generating typescript classes...
Input: R:\Projects\Bancroftway\Fruitmap\Tools\db.metadata.json
Source: R:\Projects\Bancroftway\Fruitmap\Webapp\src\model
Output: R:\Projects\Bancroftway\Fruitmap\Webapp\src\model
BaseClass: EntityBase
camelCase: true
kebabCaseFileNames: true
useEnumTypes: false
Injected base class: EntityBase
Initializer function "initializer" discovered for entity type: Location
R:\Projects\Bancroftway\Fruitmap\Webapp\src\model\location.ts hasn't changed. Skipping...
R:\Projects\Bancroftway\Fruitmap\Webapp\src\model\registration-helper.ts hasn't changed. Skipping...
R:\Projects\Bancroftway\Fruitmap\Webapp\src\model\entity-model.ts hasn't changed. Skipping...
R:\Projects\Bancroftway\Fruitmap\Webapp\src\model\metadata.ts hasn't changed. Skipping...

My EntityBase class is:

import { Entity, EntityAspect, EntityType } from "breeze-client";

export class EntityBase implements Entity {
    entityAspect: EntityAspect;
    entityType: EntityType;

    get $typeName(): string {
        if (!this.entityAspect) return "";
        return this.entityAspect.getKey().entityType.shortName;
    }
}
marcelgood commented 7 years ago

The initializer is something that you would put into the generated entity and then if it's discovered, it's called by the registration helper. I'm not sure why the generator says it discovered it, but it's not actually there. Here's an example of a generated entity with an initializer. The generator is supposed to add an empty stub the second time around, but it shouldn't generate an invalid registration helper.

import { EntityBase } from './entity-base';

/// <code-import> Place custom imports between <code-import> tags

/// </code-import>

export class State extends EntityBase {

    /// <code> Place custom code between <code> tags
    /// [Initializer]
    static initializer(entity: State) { }
    /// </code>

    // Generated code. Do not place code below this line.
    id: string;
    shortName: string;
    name: string;
    rowVersion: number;
}

I've updated the repo with the tools code from TempHire. It looks like the version here was behind. Try if the updated code works better. It works in the TempHire example.

sskasim commented 6 years ago

@marcelgood I cannot get the tool to update [entity].ts files because it does not like the existence of an initializer() in the file. Can you help?

I have been using the generator for a while and I had no problems when re-generating after adding column to my table. But ever since I added a code in the block for initializer I am not able to update .ts files with the new columns I added to my table. The generator successfully creates the [App}.metdata.json file but fails to update the .ts files with new columns I may add to my entity in Database. It says for eg. "Initializer function "initializer" discovered for entity type: Country"

I have this code in the Country.ts /// <code> Place custom code between <code> tags /// [Initializer] static initializer(entity: Country) { EntityBase.initializeEntity(entity, entity.entityType.keyProperties[0].name); } /// </code>

marcelgood commented 6 years ago

Not sure what would cause that. Can you do the same thing in TempHire and then add a new column/property and see if the same thing happens. Basically if you can give me a repro example, then I can take a look at it.