Navigation Menu

Skip to content

Commit

Permalink
Add "Domain#exists()" to know the table exists or not
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 2, 2012
1 parent 4fd821a commit 8409fd4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
16 changes: 12 additions & 4 deletions lib/database/domain.js
Expand Up @@ -68,13 +68,13 @@ Domain.prototype = {
initializeNameAndId: function(source) {
if (typeof source == 'string') {
this.name = source;
this.id = this.getIdForTable(this.toTableNamePart(this.name));
this.initializeId();
return;
}

if (source.query && source.query.DomainName) {
this.name = source.query.DomainName;
this.id = this.getIdForTable(this.toTableNamePart(this.name));
this.initializeId();
return;
}

Expand All @@ -99,8 +99,12 @@ Domain.prototype = {

throw new Error('no domain name');
},
getIdForTable: function(tableName) {
initializeId: function() {
this.id = this.getIdFromDatabase() || this.createNewId();
},
getIdFromDatabase: function() {
if (this.context) {
var tableName = this.toTableNamePart(this.name);
var tables = this.context.tableListSync();
var tableIdMatcher = new RegExp('^' + tableName + '_([^_]+)$');
var id;
Expand All @@ -114,7 +118,7 @@ Domain.prototype = {
}, this))
return id;
}
return this.createNewId();
return null;
},
createNewId: function() {
var size = 26;
Expand Down Expand Up @@ -278,6 +282,10 @@ Domain.prototype = {
return tables.some(function(table) {
return table.name === self.synonymTableName;
});
},

exists: function() {
return !!this.getIdFromDatabase();
}
};

Expand Down
13 changes: 8 additions & 5 deletions test/database-domain.test.js
Expand Up @@ -216,16 +216,19 @@ suite('database', function() {
});

test('id for database (known table)', function() {
assert.equal(domain.id, 'id0123');
assert.equal({ id: domain.id, exists: domain.exists() },
{ id: 'id0123', exists: true });
});

test('id for database (unknown, new table)', function() {
domain = new Domain('unknown', context);
assert.equal(typeof domain.id, 'string');
assert.deepEqual({ length: domain.id.length,
normalized: domain.id.replace(/[1-9a-z]/g, '0') },
{ length: 26,
normalized: Domain.DEFAULT_ID });
assert.deepEqual({ idLength: domain.id.length,
normalizedId: domain.id.replace(/[1-9a-z]/g, '0'),
exists: domain.exists() },
{ idLength: 26,
normalizedId: Domain.DEFAULT_ID,
exists: false });
});

test('indexFields', function() {
Expand Down

0 comments on commit 8409fd4

Please sign in to comment.