forked from jblanche/node-bower-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
56 lines (52 loc) · 1.45 KB
/
database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var Sequelize = require("sequelize");
var _ = Sequelize.Utils._ ;
var Database = {
init: function () {
this.sequelize = new Sequelize('development', 'jblanche', '', {
dialect: 'postgres',
port: 5432
}),
this.Package = this.sequelize.define('Package',
{
name: {
type: Sequelize.STRING,
unique: true,
allowNull: false
},
url: {
type: Sequelize.STRING,
unique: true,
allowNull: false,
validate: {
isGitUrl: function(value) {
if (!value.match(/^git\:\/\//)) {
throw new Error('is not correct format');
}
return this;
}
}
},
hits: {
type: Sequelize.INTEGER,
defaultValue: 0
}
} , {
instanceMethods: {
hit: function () {
this.hits += 1 ;
this.save();
}
}
});
return this;
},
onSync: function () {
var addIndex = this.sequelize.getQueryInterface().addIndex('Packages', ['name']);
addIndex.error(function(e) {
if(e.toString() !== 'error: relation "packages_name" already exists'){
throw e;
}
});
}
};
module.exports = Object.create(Database);