Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ INSERT INTO
Test2.dbo.Tweets
(Lang, "Text", id, CreatedAt, Username, ScreenName)
VALUES
(@lang:string, @text:string, @id:bigint, @created_at:date, @username:string, @screenname:string)
(@lang, @text, @id:bigint, @created_at:date, @username, @screenname)
```

Following types are supported:
* ``string``
* ``string`` (also default type if type is omitted)
* ``number`` (will be converted to MSSQL ``int``)
* ``bigint``
* ``boolean`` (will be converted to MSSQL ``bit``)
Expand Down
12 changes: 11 additions & 1 deletion lib/actions/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const cosql = require('co-mssql');

let pstmt;

const VARS_REGEXP = /@([\w_$][\d\w_$]*:(string|boolean|date|number|bigint|float|real|money))/g;
const VARS_REGEXP = /@([\w_$][\d\w_$]*(:(string|boolean|date|number|bigint|float|real|money))?)/g;

/**
* This function will be called during component intialization
Expand All @@ -31,6 +31,7 @@ function init(cfg) {
const [placeholder, type] = tuple.split(':');
const name = placeholder.substr(1);
switch (type) {
case undefined:
case 'string':
pstmt.input(name, cosql.NVarChar);
break;
Expand Down Expand Up @@ -94,6 +95,15 @@ function getMetaModel(cfg, cb) {
case 'bigint':
jsType = 'number';
break;
case 'real':
jsType = 'number';
break;
case 'float':
jsType = 'number';
break;
case 'money':
jsType = 'number';
break;
}
fields[key.substr(1)] = {
type: jsType
Expand Down
2 changes: 1 addition & 1 deletion spec-integration/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Integration test', () => {
const cfg = {
uri : process.env.MSSQL_URL,
query: 'INSERT INTO Test2.dbo.Tweets (Lang, Retweeted, Favorited, "Text", id, CreatedAt, Username, ScreenName) '
+ 'VALUES (@lang:string, @retweeted:boolean, @favorited:boolean, @text:string, @id:bigint, @created_at:date, @username:string, @screenname:string)'
+ 'VALUES (@lang, @retweeted:boolean, @favorited:boolean, @text:string, @id:bigint, @created_at:date, @username, @screenname:string)'
};

before(() => {
Expand Down
54 changes: 52 additions & 2 deletions spec/metadata.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const insert = require('../lib/actions/insert');
describe('Metadata test', () => {
const cfg = {
query: 'INSERT INTO Test2.dbo.Tweets (Lang, Retweeted, Favorited, "Text", id, CreatedAt, Username, ScreenName) '
+ 'VALUES (@lang:string, @retweeted:boolean, @favorited:boolean, '
+ 'VALUES (@lang:string, @retweeted:float, @money:money, @favorited:boolean, '
+ '@text:string, @id:bigint, @created_at:date, @username:string, @screenname:string)'
};

Expand All @@ -28,8 +28,11 @@ describe('Metadata test', () => {
lang: {
type: 'string'
},
money: {
type: 'number'
},
retweeted: {
type: 'string'
type: 'number'
},
screenname: {
type: 'string'
Expand Down Expand Up @@ -89,5 +92,52 @@ describe('Metadata test', () => {
});
});

it('should assume default metadata as stirng', (done) => {
insert.getMetaModel({
query: 'INSERT INTO Test2.dbo.Tweets '
+ '(Lang, Retweeted, Favorited, "Text", id, CreatedAt, Username, ScreenName) '
+ 'VALUES (@lang, @retweeted:float, @money:money, @favorited:boolean, '
+ '@text, @id:bigint, @created_at:date, @username, @screenname)'
}, (err, result) => {
expect(err).to.be.null;
expect(result).to.deep.equal({
in: {
properties: {
created_at: {
type: 'string'
},
favorited: {
type: 'string'
},
id: {
type: 'number'
},
lang: {
type: 'string'
},
money: {
type: 'number'
},
retweeted: {
type: 'number'
},
screenname: {
type: 'string'
},
text: {
type: 'string'
},
username: {
type: 'string'
}
},
type: 'object'
},
out: {}
}
);
done();
});
});

});