@lexho111/plainblog 0.0.6 → 0.0.8

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.
package/.eslintignore ADDED
File without changes
package/.eslintrc.json ADDED
File without changes
package/README.md CHANGED
@@ -50,7 +50,11 @@ docker run -p 5432:5432 --name postgresdb --restart always -e POSTGRES_USER=user
50
50
 
51
51
  ```
52
52
  import { Blog, storageserver} from "@lexho111/plainblog";
53
- await storageserver("postgres", 8081); // you can use a postgres db too
53
+
54
+ storageserver.setUsername("username");
55
+ storageserver.setPassword("password");
56
+ storageserver.setHost("localhost");
57
+ await storageserver.start("postgres", 8081);
54
58
  const blog = new Blog();
55
59
  blog.setAPI(storageserver.getAPIURL());
56
60
  blog.setStyle("body { font-family: Arial, sans-serif; } h1 { color: #333; }");
package/api-server.js CHANGED
@@ -21,7 +21,8 @@ const blogData = {
21
21
  let username;
22
22
  let password;
23
23
  let host;
24
- let dbport = 5432;
24
+ const dbport = 5432;
25
+ const dbname = process.env.POSTGRES_DB || "blog";
25
26
 
26
27
  export function setUsername(username1) {
27
28
  username = username1;
@@ -51,20 +52,13 @@ export async function start(databasetype, port = 8081) {
51
52
  "PostgreSQL credentials not set. Please use setUsername(), setPassword(), and setHost() before starting the server."
52
53
  );
53
54
  }
55
+
54
56
  sequelize = new Sequelize(
55
- `postgres://${username}:${password}@${host}:${dbport}/blog`,
56
- {
57
- logging: false,
58
- }
59
- ); // Connect to local Docker container
60
- } else {
61
- console.error(
62
- `Invalid or no database type specified. Received: "${databasetype}". Exiting.`
57
+ `postgres://${username}:${password}@${host}:${dbport}/${dbname}`,
58
+ { logging: false }
63
59
  );
64
- process.exit(1);
65
60
  }
66
61
 
67
- // Define the Article model using the single sequelize instance
68
62
  const Article = sequelize.define(
69
63
  "Article",
70
64
  {
@@ -77,15 +71,34 @@ export async function start(databasetype, port = 8081) {
77
71
  );
78
72
 
79
73
  try {
80
- // Sync all models with the database (creates tables if they don't exist)
81
- await sequelize.sync();
82
- console.log("Database synchronized.");
83
- // Optionally, populate initial data if the database is empty
84
- if ((await Article.count()) === 0) {
85
- for (const articleData of blogData.articles) {
86
- await Article.create(articleData);
74
+ // The connectWithRetry logic is now integrated here and properly awaited.
75
+ let retries = 5;
76
+ while (retries) {
77
+ try {
78
+ await sequelize.authenticate();
79
+ console.log("Connection has been established successfully.");
80
+
81
+ // Sync all models and populate data
82
+ await sequelize.sync({ alter: true });
83
+ console.log("All models were synchronized successfully.");
84
+
85
+ if ((await Article.count()) === 0) {
86
+ for (const articleData of blogData.articles) {
87
+ await Article.create(articleData);
88
+ }
89
+ console.log("Initial blog data populated.");
90
+ }
91
+ break; // Success, exit retry loop
92
+ } catch (err) {
93
+ // For sqlite, we don't need to retry, just throw the error.
94
+ if (databasetype === "sqlite") throw err;
95
+
96
+ console.error("Unable to connect to the database:", err.name);
97
+ retries -= 1;
98
+ console.log(`Retries left: ${retries}`);
99
+ if (retries === 0) throw err; // Throw error if max retries reached
100
+ await new Promise((res) => setTimeout(res, 5000));
87
101
  }
88
- console.log("Initial blog data populated.");
89
102
  }
90
103
 
91
104
  const server = http.createServer(async (req, res) => {
package/blog.db ADDED
Binary file
@@ -0,0 +1,45 @@
1
+ import globals from "globals";
2
+ import pluginJs from "@eslint/js";
3
+ import pluginJest from "eslint-plugin-jest";
4
+
5
+ export default [
6
+ {
7
+ // Configuration for all JavaScript files
8
+ files: ["**/*.js"],
9
+ languageOptions: {
10
+ ecmaVersion: 2022, // Supports modern JavaScript features
11
+ sourceType: "module",
12
+ globals: {
13
+ ...globals.node, // Defines Node.js global variables (e.g., `process`, `require`)
14
+ },
15
+ },
16
+ // ESLint's recommended rules for general JavaScript
17
+ rules: {
18
+ ...pluginJs.configs.recommended.rules,
19
+ // Add or override general JavaScript rules here.
20
+ // Example: Enforce semicolons at the end of statements
21
+ semi: ["error", "always"],
22
+ // Example: Prefer `const` over `let` where variables are not reassigned
23
+ "prefer-const": "error",
24
+ // Example: Prevent unused variables (can be configured further)
25
+ "no-unused-vars": ["warn", { args: "none" }],
26
+ },
27
+ },
28
+ {
29
+ // Configuration specifically for Jest test files
30
+ files: ["**/*.test.js", "**/*.spec.js"],
31
+ languageOptions: {
32
+ globals: {
33
+ ...globals.jest, // Defines Jest global variables (e.g., `describe`, `it`, `expect`)
34
+ },
35
+ },
36
+ plugins: {
37
+ jest: pluginJest,
38
+ },
39
+ // Recommended Jest rules from `eslint-plugin-jest`
40
+ rules: {
41
+ ...pluginJest.configs.recommended.rules,
42
+ // Add or override Jest-specific rules here.
43
+ },
44
+ },
45
+ ];
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@lexho111/plainblog",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "A tool for creating and serving a minimalist, single-page blog.",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
- "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
8
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
9
+ "lint": "eslint ."
9
10
  },
10
11
  "keywords": [
11
12
  "blog",
@@ -22,6 +23,8 @@
22
23
  "sqlite3": "^5.1.7"
23
24
  },
24
25
  "devDependencies": {
25
- "jest": "^30.2.0"
26
+ "eslint": "^9.8.0",
27
+ "eslint-plugin-jest": "^28.6.0",
28
+ "jest": "^29.7.0"
26
29
  }
27
30
  }
@@ -12,7 +12,9 @@ describe("API Server Health Check", () => {
12
12
  });
13
13
 
14
14
  afterAll((done) => {
15
- server.close(done);
15
+ server.close(() => {
16
+ done();
17
+ });
16
18
  });
17
19
 
18
20
  test("should respond with 200 OK on the /health endpoint", async () => {