memorix 1.6.2 → 1.6.3

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.
@@ -59,7 +59,7 @@ export interface ObservationStore {
59
59
  /** Load one project's observations without scanning the shared flat store. */
60
60
  loadByProject(
61
61
  projectId: string,
62
- options?: { status?: string; limit?: number; offset?: number; afterId?: number },
62
+ options?: { status?: string; limit?: number; offset?: number; afterId?: number; newestFirst?: boolean },
63
63
  ): Promise<Observation[]>;
64
64
 
65
65
  /** Count one project's observations without materializing the rows. */
@@ -231,7 +231,7 @@ export class DegradedBackend implements ObservationStore {
231
231
  return [];
232
232
  }
233
233
 
234
- async loadByProject(_projectId: string, _options?: { status?: string; limit?: number; offset?: number; afterId?: number }): Promise<Observation[]> {
234
+ async loadByProject(_projectId: string, _options?: { status?: string; limit?: number; offset?: number; afterId?: number; newestFirst?: boolean }): Promise<Observation[]> {
235
235
  return [];
236
236
  }
237
237
 
@@ -16,6 +16,7 @@ import { createRequire } from 'node:module';
16
16
  import path from 'node:path';
17
17
  import fs from 'node:fs';
18
18
  import { createDatabase, loadSqlite } from './bun-sqlite-compat.js';
19
+ import { assertNotHomeDataDir } from '../project/launch-root.js';
19
20
 
20
21
  // Dynamic require for SQLite (better-sqlite3, node:sqlite, or bun:sqlite)
21
22
  let BetterSqlite3: any;
@@ -998,6 +999,7 @@ const _dbCache = new Map<string, any>();
998
999
  */
999
1000
  export function getDatabase(dataDir: string): any {
1000
1001
  const normalized = path.resolve(dataDir);
1002
+ assertNotHomeDataDir(normalized);
1001
1003
  const existing = _dbCache.get(normalized);
1002
1004
  if (existing) return existing;
1003
1005
 
@@ -122,6 +122,8 @@ export class SqliteBackend implements ObservationStore {
122
122
  private stmtSelectByProjectStatus: any = null;
123
123
  private stmtSelectByProjectAfterId: any = null;
124
124
  private stmtSelectByProjectStatusAfterId: any = null;
125
+ private stmtSelectByProjectNewest: any = null;
126
+ private stmtSelectByProjectStatusNewest: any = null;
125
127
  private stmtCountByProject: any = null;
126
128
  private stmtCountByProjectStatus: any = null;
127
129
  private stmtSelectByTopicKey: any = null;
@@ -175,6 +177,14 @@ export class SqliteBackend implements ObservationStore {
175
177
  SELECT * FROM observations WHERE projectId = ? AND status = ? AND id > ?
176
178
  ORDER BY id ASC LIMIT ?
177
179
  `);
180
+ this.stmtSelectByProjectNewest = this.db.prepare(`
181
+ SELECT * FROM observations WHERE projectId = ?
182
+ ORDER BY createdAt DESC, id DESC LIMIT ? OFFSET ?
183
+ `);
184
+ this.stmtSelectByProjectStatusNewest = this.db.prepare(`
185
+ SELECT * FROM observations WHERE projectId = ? AND status = ?
186
+ ORDER BY createdAt DESC, id DESC LIMIT ? OFFSET ?
187
+ `);
178
188
  this.stmtCountByProject = this.db.prepare(
179
189
  `SELECT COUNT(*) AS count FROM observations WHERE projectId = ?`,
180
190
  );
@@ -288,15 +298,20 @@ export class SqliteBackend implements ObservationStore {
288
298
 
289
299
  async loadByProject(
290
300
  projectId: string,
291
- options: { status?: string; limit?: number; offset?: number; afterId?: number } = {},
301
+ options: { status?: string; limit?: number; offset?: number; afterId?: number; newestFirst?: boolean } = {},
292
302
  ): Promise<Observation[]> {
293
303
  const limit = options.limit == null ? -1 : Math.max(1, Math.floor(options.limit));
294
304
  const offset = options.offset == null ? 0 : Math.max(0, Math.floor(options.offset));
295
305
  const afterId = options.afterId == null ? null : Math.max(0, Math.floor(options.afterId));
306
+ const newestFirst = options.newestFirst === true && afterId === null;
296
307
  const rows = afterId === null
297
- ? (options.status
298
- ? this.stmtSelectByProjectStatus.all(projectId, options.status, limit, offset)
299
- : this.stmtSelectByProject.all(projectId, limit, offset))
308
+ ? (newestFirst
309
+ ? (options.status
310
+ ? this.stmtSelectByProjectStatusNewest.all(projectId, options.status, limit, offset)
311
+ : this.stmtSelectByProjectNewest.all(projectId, limit, offset))
312
+ : (options.status
313
+ ? this.stmtSelectByProjectStatus.all(projectId, options.status, limit, offset)
314
+ : this.stmtSelectByProject.all(projectId, limit, offset)))
300
315
  : (options.status
301
316
  ? this.stmtSelectByProjectStatusAfterId.all(projectId, options.status, afterId, limit)
302
317
  : this.stmtSelectByProjectAfterId.all(projectId, afterId, limit));