@loghead/core 0.1.12 → 0.1.14

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.
@@ -113,14 +113,22 @@ async function startApiServer(db) {
113
113
  if (!streamId) {
114
114
  return res.status(400).send("Missing streamId");
115
115
  }
116
- const limit = parseInt(req.query.limit || "50");
116
+ let page = parseInt(req.query.page || "1");
117
+ if (page < 1)
118
+ page = 1;
119
+ let pageSize = parseInt(req.query.pageSize || "100");
120
+ let limit = req.query.limit ? parseInt(req.query.limit) : pageSize;
121
+ // Enforce max limit
122
+ if (limit > 1000)
123
+ limit = 1000;
124
+ const offset = (page - 1) * limit;
117
125
  const query = req.query.q;
118
126
  let logs;
119
127
  if (query) {
120
128
  logs = await db.searchLogs(streamId, query, limit);
121
129
  }
122
130
  else {
123
- logs = db.getRecentLogs(streamId, limit);
131
+ logs = db.getRecentLogs(streamId, limit, offset);
124
132
  }
125
133
  res.json(logs);
126
134
  });
@@ -128,13 +128,13 @@ class DbService {
128
128
  };
129
129
  });
130
130
  }
131
- getRecentLogs(streamId, limit = 50) {
131
+ getRecentLogs(streamId, limit = 50, offset = 0) {
132
132
  const rows = client_1.db.prepare(`
133
133
  SELECT content, timestamp, metadata FROM logs
134
134
  WHERE stream_id = ?
135
135
  ORDER BY timestamp DESC
136
- LIMIT ?
137
- `).all(streamId, limit);
136
+ LIMIT ? OFFSET ?
137
+ `).all(streamId, limit, offset);
138
138
  return rows.map((row) => {
139
139
  let meta = row.metadata;
140
140
  if (typeof meta === "string") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loghead/core",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "Core API and Database for Loghead",
5
5
  "repository": {
6
6
  "type": "git",
package/src/api/server.ts CHANGED
@@ -125,14 +125,25 @@ export async function startApiServer(db: DbService) {
125
125
  if (!streamId) {
126
126
  return res.status(400).send("Missing streamId");
127
127
  }
128
- const limit = parseInt((req.query.limit as string) || "50");
128
+
129
+ let page = parseInt((req.query.page as string) || "1");
130
+ if (page < 1) page = 1;
131
+
132
+ let pageSize = parseInt((req.query.pageSize as string) || "100");
133
+ let limit = req.query.limit ? parseInt(req.query.limit as string) : pageSize;
134
+
135
+ // Enforce max limit
136
+ if (limit > 1000) limit = 1000;
137
+
138
+ const offset = (page - 1) * limit;
139
+
129
140
  const query = req.query.q as string;
130
141
 
131
142
  let logs;
132
143
  if (query) {
133
144
  logs = await db.searchLogs(streamId, query, limit);
134
145
  } else {
135
- logs = db.getRecentLogs(streamId, limit);
146
+ logs = db.getRecentLogs(streamId, limit, offset);
136
147
  }
137
148
  res.json(logs);
138
149
  });
@@ -143,13 +143,13 @@ export class DbService {
143
143
  });
144
144
  }
145
145
 
146
- getRecentLogs(streamId: string, limit = 50): Log[] {
146
+ getRecentLogs(streamId: string, limit = 50, offset = 0): Log[] {
147
147
  const rows = (db.prepare(`
148
148
  SELECT content, timestamp, metadata FROM logs
149
149
  WHERE stream_id = ?
150
150
  ORDER BY timestamp DESC
151
- LIMIT ?
152
- `) as unknown as DbAny).all(streamId, limit);
151
+ LIMIT ? OFFSET ?
152
+ `) as unknown as DbAny).all(streamId, limit, offset);
153
153
 
154
154
  return rows.map((row: Log) => {
155
155
  let meta = row.metadata;