This repository was archived by the owner on Jun 8, 2026. It is now read-only.
forked from maelink-communications/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.js
More file actions
53 lines (48 loc) · 1.52 KB
/
Copy pathcodegen.js
File metadata and controls
53 lines (48 loc) · 1.52 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
import { Sequelize, DataTypes, Model } from 'sequelize';
import { Community } from "./database/tables.js";
const sequelize = new Sequelize('database', 'username', 'password', {
dialect: 'sqlite',
storage: 'database/db.sqlite',
logging: false
});
class Code extends Model {}
Code.init({
value: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
expires_at: DataTypes.DATE
}, {
sequelize,
modelName: 'code'
});
async function gen() {
try {
await sequelize.sync({ alter: true });
} catch (syncErr) {
console.error('Failed to sync DB schema:', syncErr);
}
const code = `MLNK-${Math.random().toString(36).slice(2, 10).toUpperCase()}-${Math.random().toString(36).slice(2, 10).toUpperCase()}`;
try {
await Code.create({ value: code, expires_at: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7) });
console.log('Created code:', code);
} catch (e) {
if (e.name === 'SequelizeUniqueConstraintError') {
console.log('Code collision, try again');
} else {
console.error('Failed to create code:', e);
Deno.exit(1);
}
}
const all = await Code.findAll({ order: [['createdAt', 'DESC']] });
console.log('All codes:\n', all.map(c => ({ id: c.id, value: c.value, expires_at: c.expires_at })).slice(0, 50));
}
if (import.meta.main) {
// when run as a standalone script, run gen() and then close the DB connection
(async () => {
await gen();
try { await sequelize.close(); } catch (e) { console.error('Error closing sequelize:', e); }
})();
}
export { gen };