简体   繁体   中英

Breeze-Sequelize with autoGeneratedKeyType Identity

I am trying to create an MS SQL db with breeze-breeze sequelize and i like to generate the ids on the db server. My solution is oriented on the tempHire example from the breeze samples repo

My Metadata.json looks like this:

{
    "metadataVersion": "1.0.5",
    "namingConvetion": "camelCase",
    "localQueryComparisonOptions": "caseInsensitiveSQL",
    "dataServices": [{
        "serviceName": "breeze/",
        "hasServerMetadata": true,
        "useJsonp": false
    }],
    "structuralTypes": [{

        "shortName": "User",
        "namespace": "Model",
        "autoGeneratedKeyType": "Identity",
        "defaultResourceName": "Users",
        "dataProperties": [{
            "nameOnServer": "id",
            "dataType": "Int32",
            "isPartOfKey": true,
            "isNullable": false
        }, {
            "name": "firstName",
            "dataType": "String"
        }, {
            "name": "lastName",
            "dataType": "String"
        }, {
            "name": "userName",
            "dataType": "String",
            "isNullable": false,
            "maxLength": 64,
            "validators": [{
                "name": "required"
            }, {
                "maxLength": 64,
                "name": "maxLength"
            }]
        }, {
            "name": "email",
            "dataType": "String"
        }]
    }],
    "resourceEntityTypeMap": {
        "Users": "User:#Model"
    }
}

though this will not create an identity id column. the created table looks like the following create script:

CREATE TABLE [User] (
    [id] INTEGER NOT NULL , 
    [firstName] NVARCHAR(255) DEFAULT NULL, 
    [lastName] NVARCHAR(255) DEFAULT NULL, 
    [userName] NVARCHAR(64) NOT NULL DEFAULT '', 
    [email] NVARCHAR(255) DEFAULT NULL, 
    PRIMARY KEY ([id])
)

In addition here are some breeze server side implementations:

var dbConfig = {
    user: 'user',
    password: 'secret',
    dbName: 'dbname'
};

var sequelizeOptions = {
    host: 'hostname',
    dialect: 'mssql',
    port: 1433
};

function createSequelizeManager() {
    var metadata = readMetadata();
    var sm = new SequelizeManager(dbConfig, sequelizeOptions);
    sm.importMetadata(metadata);

    return sm;
}

var _sequelizeManager = createSequelizeManager();

_sequelizeManager.authenticate();

_sequelizeManager.sync(false /* createDb */)
    .then(seed)
    .then(function () {
        console.log('db init successful');
    });

Do i have a wrong configuration? Is the Identity not available with the mssql dialect? Am i doing something wrong?

With the configuration is nothing wrong i guess. I just found out that there is a bug in the MetadataMapper from breeze-sequelize. I tested it with the sequelize version 2.1.3 and 3.x.

The autoIncrement attribute for sequelize will never get set. The if statement will never be true. I'll report this on github. ;)

The fix would be the following code in the MetadataMapper.js at line 134 :

  if (attributes.type.key == "INTEGER" || attributes.type.key =="BIGINT") {
    attributes.autoIncrement = true;
  }

In the original code the if statement is attributes.type== "INTEGER" || attributes.type=="BIGINT" attributes.type== "INTEGER" || attributes.type=="BIGINT" where the type actually never is a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM