-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
176 lines (157 loc) · 5.21 KB
/
db.js
File metadata and controls
176 lines (157 loc) · 5.21 KB
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
const sqlite3 = require('sqlite3').verbose();
const path = require('path');
// Create database file in the project directory
const dbPath = path.join(__dirname, 'images.db');
const db = new sqlite3.Database(dbPath);
// Initialize the database with the photos table
db.serialize(() => {
// Check if the old table exists with deletionPassword column
db.get("PRAGMA table_info(photos)", (err, rows) => {
if (err) {
// Table doesn't exist, create new schema
createNewTable();
} else {
// Check if deletionPassword column exists
db.all("PRAGMA table_info(photos)", (err, columns) => {
if (err) {
createNewTable();
} else {
const hasDeletionPassword = columns.some(col => col.name === 'deletionPassword');
if (hasDeletionPassword) {
// Migrate old table to new schema
migrateTable();
} else {
// Already using new schema
createNewTable();
}
}
});
}
});
});
function createNewTable() {
db.run(`CREATE TABLE IF NOT EXISTS photos (
id TEXT PRIMARY KEY,
extn TEXT NOT NULL
)`);
console.log('SQLite database initialized with new schema');
}
function migrateTable() {
// Create new table with correct schema
db.run(`CREATE TABLE photos_new (
id TEXT PRIMARY KEY,
extn TEXT NOT NULL
)`, (err) => {
if (err) {
console.error('Error creating new table:', err);
return;
}
// Copy data from old table to new table
db.run(`INSERT INTO photos_new (id, extn)
SELECT id, extn FROM photos`, (err) => {
if (err) {
console.error('Error copying data:', err);
return;
}
// Drop old table
db.run(`DROP TABLE photos`, (err) => {
if (err) {
console.error('Error dropping old table:', err);
return;
}
// Rename new table to photos
db.run(`ALTER TABLE photos_new RENAME TO photos`, (err) => {
if (err) {
console.error('Error renaming table:', err);
return;
}
console.log('Database migrated successfully from old schema');
});
});
});
});
}
// Photo model methods
class Photo {
constructor(data = {}) {
this.id = data.id;
this.extn = data.extn;
}
filename() {
return this.id + '.' + this.extn;
}
save() {
return new Promise((resolve, reject) => {
db.run(
'INSERT INTO photos (id, extn) VALUES (?, ?)',
[this.id, this.extn],
function(err) {
if (err) reject(err);
else resolve(new Photo({ id: this.id, extn: this.extn }));
}.bind(this)
);
});
}
static findOne(query) {
return new Promise((resolve, reject) => {
const conditions = Object.keys(query).map(key => `${key} = ?`).join(' AND ');
const values = Object.values(query);
db.get(
`SELECT * FROM photos WHERE ${conditions}`,
values,
(err, row) => {
if (err) reject(err);
else {
if (row) {
resolve(new Photo(row));
} else {
resolve(null);
}
}
}
);
});
}
static findAll({ offset = 0, limit = 50 } = {}) {
return new Promise((resolve, reject) => {
db.all(
'SELECT * FROM photos ORDER BY id LIMIT ? OFFSET ?',
[limit, offset],
(err, rows) => {
if (err) reject(err);
else resolve(rows.map(r => new Photo(r)));
}
);
});
}
static count() {
return new Promise((resolve, reject) => {
db.get('SELECT COUNT(*) as cnt FROM photos', [], (err, row) => {
if (err) reject(err);
else resolve(row ? row.cnt : 0);
});
});
}
static exists(id) {
return new Promise((resolve, reject) => {
db.get('SELECT 1 FROM photos WHERE id = ? LIMIT 1', [id], (err, row) => {
if (err) reject(err);
else resolve(!!row);
});
});
}
remove() {
return new Promise((resolve, reject) => {
db.run(
'DELETE FROM photos WHERE id = ?',
[this.id],
function(err) {
if (err) reject(err);
else resolve(this);
}
);
});
}
}
console.log('SQLite database initialized successfully');
module.exports = Photo;