This repository was archived by the owner on Jan 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathpluginsdb.js
183 lines (149 loc) · 3.83 KB
/
pluginsdb.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var sqlite = require( "sqlite3" ),
config = require( "./config" );
var db;
function connect( fn ) {
db = new sqlite.Database( config.pluginsDb, fn );
}
function auto( fn ) {
return function() {
var that = this,
args = arguments;
if ( db ) {
return fn.apply( that, args );
}
connect(function( error ) {
if ( error ) {
return fn( error );
}
fn.apply( that, args );
});
};
}
var pluginsDb = module.exports = {
getOwner: auto(function( plugin, fn ) {
db.get( "SELECT owner FROM plugins WHERE plugin = ?",
[ plugin ], function( error, row ) {
if ( error ) {
return fn( error );
}
if ( !row ) {
return fn( null, null );
}
return fn( null, row.owner );
});
}),
setOwner: auto(function( plugin, owner, repo, fn ) {
db.run( "INSERT INTO plugins( plugin, owner, repo ) VALUES( ?, ?, ? )",
[ plugin, owner, repo ], function( error ) {
if ( error ) {
return fn( error );
}
fn( null );
});
}),
getOrSetOwner: auto(function( plugin, owner, repo, fn ) {
pluginsDb.setOwner( plugin, owner, repo, function( error ) {
// successfully set owner (new plugin)
if ( !error ) {
return fn( null, owner );
}
// there is already an owner
if ( error.code === "SQLITE_CONSTRAINT" ) {
return pluginsDb.getOwner( plugin, fn );
}
fn( error );
});
}),
transferOwnership: auto(function( plugin, owner, repo, fn ) {
db.run( "UPDATE plugins SET owner = ?, repo = ? WHERE plugin = ?",
[ owner, repo, plugin ], fn );
}),
getTags: auto(function( repoId, fn ) {
db.all( "SELECT tag FROM repos WHERE repo = ?", [ repoId ], function( error, tags ) {
if ( error ) {
return fn( error );
}
var ret = {};
tags.forEach(function( tag ) {
ret[ tag.tag ] = true;
});
fn( null, ret );
});
}),
addTag: auto(function( repoId, tag, fn ) {
db.run( "INSERT OR IGNORE INTO repos( repo, tag ) VALUES( ?, ? )", [ repoId, tag ], fn );
}),
addRelease: auto(function( repoId, tag, file, manifest, fn ) {
var data = JSON.stringify({
repo: repoId,
tag: tag,
file: file,
manifest: manifest
});
db.run( "INSERT INTO actions( action, data ) VALUES( ?, ? )",
[ "addRelease", data ], function( error ) {
if ( error ) {
return fn( error );
}
pluginsDb.addTag( repoId, tag, fn );
});
}),
updateRepoMeta: auto(function( repo, data, fn ) {
db.run( "UPDATE plugins SET watchers = ?, forks = ? " +
"WHERE repo = ?",
[ data.watchers, data.forks, repo ], fn );
}),
getMeta: auto(function( plugin, fn ) {
db.get( "SELECT watchers, forks FROM plugins WHERE plugin = ?",
[ plugin ], fn );
}),
getFirstAction: auto(function( fn ) {
db.get( "SELECT * FROM actions ORDER BY id ASC LIMIT 1", fn );
}),
getNextAction: auto(function( id, fn ) {
db.get( "SELECT * FROM actions WHERE id > " + id + " ORDER BY id ASC LIMIT 1", fn );
}),
getAllRepos: auto(function( fn ) {
db.all( "SELECT DISTINCT(repo) FROM repos", function( error, rows ) {
if ( error ) {
return fn( error );
}
fn( null, rows.map(function( row ) {
return row.repo;
}));
});
}),
_setup: function( fn ) {
var Step = require( "step" );
Step(
function() {
connect( this );
},
function( error ) {
if ( error ) {
return fn( error );
}
db.run( "CREATE TABLE plugins (" +
"plugin TEXT PRIMARY KEY, " +
"owner TEXT, " +
"repo TEXT, " +
"watchers INTEGER DEFAULT 0, " +
"forks INTEGER DEFAULT 0" +
")", this.parallel() );
db.run( "CREATE TABLE repos (" +
"repo TEXT, " +
"tag TEXT, " +
"PRIMARY KEY( repo, tag ) " +
")", this.parallel() );
db.run( "CREATE TABLE actions (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"action TEXT, " +
"data TEXT " +
")", this.parallel() );
},
function( error ) {
fn( error );
}
);
}
};