Browse Source

added special 'property' field to columns

When defining a column, you can specify a "property" field
that will allow a more programmer-friendly name to be used
when selecting fields.

e.g.

var user = sql.define({
  name: 'user',
  columns: [{ state: 'state_or_province', property: 'state' }]
});

user.select(user.state);
auto-join
tmont 12 years ago
parent
commit
8fb83185c2
  1. 17
      README.md
  2. 3
      lib/table.js
  3. 21
      test/table-tests.js

17
README.md

@ -76,7 +76,24 @@ var friendsWhoUseGmailQuery = user.from(userToFriends).where(friends.email.like(
//LEFT JOIN "friendship" ON ("user"."id" = "friendship"."userId")
//LEFT JOIN "user" AS "friends" ON ("friendship"."friendId" = "friends"."id")
//WHERE "friends"."email" LIKE %1
```
## Using different property names for columns
```javascript
var user = sql.define({
name: 'user',
columns: [{
name: 'id'
}, {
name: 'state_or_province',
property: 'state'
}
]
});
//now, instead of user.state_or_province, you can just use user.state
console.log(user.select().where(user.state.equals('WA')).toQuery().text);
// "SELECT "user".* FROM "user" WHERE ("user"."state_or_province" = $1)"
```
There are a __lot__ more examples included in the `test/dialects` folder.

3
lib/table.js

@ -60,7 +60,8 @@ Table.prototype.addColumn = function(col) {
console.log('Please notice that you have just defined the column "' + col.name + '". In order to access it, you need to use "table.getColumn(\'' + col.name + '\');"!');
}
this.columns.push(col);
this[col.name] = this[col.name] || col;
var property = col.property || col.name;
this[property] = this[property] || col;
return this;
};

21
test/table-tests.js

@ -56,6 +56,27 @@ suite('table', function() {
});
});
test('table with user-defined column property names', function () {
var table = Table.define({
name: 'blah',
columns: [{
name: 'id',
property: 'theId'
}, {
name: 'email',
property: 'uniqueEmail'
}]
});
var cols = table.columns;
assert.equal(cols.length, 2);
assert.equal(cols[0].name, 'id');
assert(cols[0] === table.theId, 'Expected table.theId to be the first column');
assert(table.id === undefined, 'Expected table.id to not exist');
assert.equal(cols[1].name, 'email');
assert(cols[1] === table.uniqueEmail, 'Expected table.uniqueEmail to be the second column');
assert(table.email === undefined, 'Expected table.email to not exist');
});
test('table with fancier column definitions', function() {
var table = Table.define({
name: 'blah',

Loading…
Cancel
Save