@lexho111/plainblog 0.8.5 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ <div id="wrapper">
2
+ <div id="header">
3
+ <h1>${title}</h1>
4
+ </div>
5
+ <hr>
6
+ <div class="contentwrapper">
7
+ <h2>Login</h2>
8
+ <form action="/login" method="POST">
9
+ <div class="form_element">
10
+ <label>Password:</label>
11
+ <input type="password" name="password" class="password" required>
12
+ </div>
13
+ <button type="submit" class="btn">Login</button>
14
+ </form>
15
+ </div>
16
+ </div>
17
+ </body>
18
+ </html>
@@ -1 +1 @@
1
- <a href="#">more</a>
1
+ <a href="/article/${id}">more</a>
@@ -1,189 +0,0 @@
1
- import { createDebug } from "../debug-loader.js";
2
-
3
- const debug = createDebug("plainblog:DatabaseModel");
4
-
5
- export default class SequelizeAdapter {
6
- username;
7
- password;
8
- host;
9
- dbport = 5432;
10
- dbname = "blog";
11
-
12
- sequelize;
13
- Article;
14
- BlogInfo;
15
-
16
- // Dynamic properties
17
- Sequelize;
18
- DataTypes;
19
- Op;
20
-
21
- constructor(options = {}) {
22
- debug(JSON.stringify(options));
23
- this.ready = false;
24
- }
25
-
26
- async loadSequelize() {
27
- if (this.Sequelize) return;
28
- let sequelizePkg;
29
- try {
30
- sequelizePkg = await import("sequelize");
31
- } catch (error) {
32
- if (
33
- error.code === "ERR_MODULE_NOT_FOUND" ||
34
- error.code === "MODULE_NOT_FOUND"
35
- ) {
36
- console.error(
37
- "The 'sequelize' package is not installed. Please install it by running: npm install sequelize",
38
- );
39
- throw new Error("Missing optional dependency: 'sequelize'");
40
- } else {
41
- throw new Error("Missing optional dependencies");
42
- }
43
- }
44
- this.Sequelize = sequelizePkg.Sequelize;
45
- this.DataTypes = sequelizePkg.DataTypes;
46
- this.Op = sequelizePkg.Op;
47
- }
48
-
49
- isReady() {
50
- return this.ready;
51
- }
52
-
53
- async initializeModels() {
54
- await this.loadSequelize();
55
- this.Article = this.sequelize.define(
56
- "Article",
57
- {
58
- title: this.DataTypes.STRING,
59
- content: this.DataTypes.TEXT,
60
- image: this.DataTypes.STRING,
61
- createdAt: {
62
- type: this.DataTypes.DATE,
63
- defaultValue: this.DataTypes.NOW,
64
- },
65
- updatedAt: {
66
- type: this.DataTypes.DATE,
67
- defaultValue: this.DataTypes.NOW,
68
- },
69
- },
70
- {
71
- timestamps: true,
72
- },
73
- );
74
-
75
- this.BlogInfo = this.sequelize.define(
76
- "BlogInfo",
77
- {
78
- title: this.DataTypes.STRING,
79
- },
80
- {
81
- timestamps: false,
82
- },
83
- );
84
-
85
- // This creates the tables if they don't exist.
86
- await this.sequelize.sync({ alter: true });
87
- console.log("database tables synced and ready.");
88
-
89
- // Check for and create the initial blog title right after syncing.
90
- const blogInfoCount = await this.BlogInfo.count();
91
- if (blogInfoCount === 0) {
92
- await this.BlogInfo.create({ title: "My Default Blog Title" });
93
- console.log("initialized blog title in database.");
94
- }
95
- this.ready = true;
96
- }
97
-
98
- // model
99
- async findAll(
100
- limit = 4,
101
- offset = 0,
102
- startId = null,
103
- endId = null,
104
- order = "DESC",
105
- ) {
106
- await this.loadSequelize();
107
- const where = {};
108
- const isDate = typeof startId === "string" || typeof endId === "string";
109
-
110
- if (startId !== null && endId !== null) {
111
- if (isDate) {
112
- where.createdAt = {
113
- [this.Op.between]: [startId, endId],
114
- };
115
- } else {
116
- where.id = {
117
- [this.Op.between]: [
118
- Math.min(startId, endId),
119
- Math.max(startId, endId),
120
- ],
121
- };
122
- }
123
- } else if (startId !== null) {
124
- if (isDate) {
125
- where.createdAt = {
126
- [order === "DESC" ? this.Op.lte : this.Op.gte]: startId,
127
- };
128
- } else {
129
- where.id = { [order === "DESC" ? this.Op.lte : this.Op.gte]: startId };
130
- }
131
- }
132
- const options = {
133
- where,
134
- order: [
135
- ["createdAt", order],
136
- ["id", order],
137
- ],
138
- offset,
139
- };
140
- if (limit !== -1) {
141
- options.limit = limit;
142
- }
143
- const articles = await this.Article.findAll(options);
144
- return articles.map((article) => article.get({ plain: true }));
145
- }
146
-
147
- async save(newArticle) {
148
- await this.loadSequelize();
149
-
150
- // Extract only the fields we want to save
151
- // SQLite will auto-generate the ID, so don't pass temp IDs
152
- const articleData = {
153
- title: newArticle.title,
154
- content: newArticle.content,
155
- image: newArticle.image,
156
- createdAt: newArticle.createdAt
157
- ? new Date(newArticle.createdAt)
158
- : new Date(),
159
- };
160
-
161
- const createdArticle = await this.Article.create(articleData);
162
- debug("Added new article:", createdArticle.toJSON());
163
- return createdArticle.get({ plain: true });
164
- }
165
-
166
- async update(id, data) {
167
- await this.loadSequelize();
168
- await this.Article.update(data, { where: { id } });
169
- }
170
-
171
- async remove(id) {
172
- await this.loadSequelize();
173
- await this.Article.destroy({ where: { id } });
174
- }
175
-
176
- async getBlogTitle() {
177
- await this.loadSequelize();
178
- // Find the first (and only) entry in the BlogInfo table.
179
- const blogInfo = await this.BlogInfo.findOne();
180
- return blogInfo?.title || "Blog";
181
- }
182
-
183
- async updateBlogTitle(newTitle) {
184
- await this.loadSequelize();
185
- // Find the first (and only) entry and update its title.
186
- // Using where: {} will always find the first row.
187
- await this.BlogInfo.update({ title: newTitle }, { where: {} });
188
- }
189
- }