@holo-js/cache-db 0.1.9 → 0.2.1

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/dist/index.d.ts CHANGED
@@ -3,6 +3,54 @@ import { HoloProjectConnectionConfig, DatabaseContext, DatabaseContextOptions, S
3
3
 
4
4
  declare const DEFAULT_CACHE_DATABASE_TABLE = "cache";
5
5
  declare const DEFAULT_CACHE_DATABASE_LOCK_TABLE = "cache_locks";
6
+ type CacheDatabaseTableColumnKind = 'string' | 'text' | 'bigInteger';
7
+ type CacheDatabaseTableColumnDefinition = {
8
+ readonly name: string;
9
+ readonly kind: CacheDatabaseTableColumnKind;
10
+ readonly nullable?: boolean;
11
+ readonly primaryKey?: boolean;
12
+ };
13
+ type CacheDatabaseTableDefinition = {
14
+ readonly role: 'entries' | 'locks';
15
+ readonly defaultName: string;
16
+ readonly columns: readonly CacheDatabaseTableColumnDefinition[];
17
+ readonly indexColumn: string;
18
+ readonly indexName: (tableName: string) => string;
19
+ };
20
+ declare const CACHE_DATABASE_TABLE_DEFINITIONS: readonly [{
21
+ readonly role: "entries";
22
+ readonly defaultName: "cache";
23
+ readonly columns: readonly [{
24
+ readonly name: "key";
25
+ readonly kind: "string";
26
+ readonly primaryKey: true;
27
+ }, {
28
+ readonly name: "payload";
29
+ readonly kind: "text";
30
+ }, {
31
+ readonly name: "expires_at";
32
+ readonly kind: "bigInteger";
33
+ readonly nullable: true;
34
+ }];
35
+ readonly indexColumn: "expires_at";
36
+ readonly indexName: (tableName: string) => string;
37
+ }, {
38
+ readonly role: "locks";
39
+ readonly defaultName: "cache_locks";
40
+ readonly columns: readonly [{
41
+ readonly name: "name";
42
+ readonly kind: "string";
43
+ readonly primaryKey: true;
44
+ }, {
45
+ readonly name: "owner";
46
+ readonly kind: "string";
47
+ }, {
48
+ readonly name: "expires_at";
49
+ readonly kind: "bigInteger";
50
+ }];
51
+ readonly indexColumn: "expires_at";
52
+ readonly indexName: (tableName: string) => string;
53
+ }];
6
54
  type DatabaseCacheDriverOptions = {
7
55
  readonly name: string;
8
56
  readonly connectionName: string;
@@ -35,7 +83,6 @@ declare function resolveDriver(connection: HoloProjectConnectionConfig | string)
35
83
  declare function createDatabaseContextOptions(connectionName: string, connection: HoloProjectConnectionConfig | string): DatabaseContextOptions;
36
84
  declare function createDatabaseConnection(connectionName: string, connection: HoloProjectConnectionConfig | string): DatabaseContext;
37
85
  declare function prepareCacheDatabaseTables(connection: DatabaseContext, tableName?: string, lockTableName?: string): Promise<void>;
38
- declare function renderCacheTableMigration(tableName?: string, lockTableName?: string): string;
39
86
  declare function isDatabaseCacheTableMissingError(error: unknown, tableName: string, lockTableName: string): boolean;
40
87
  declare function normalizeDatabaseCacheTableError(error: unknown, tableName: string, lockTableName: string): never;
41
88
  declare function withDatabaseCacheTableGuard<TValue>(tableName: string, lockTableName: string, callback: () => Promise<TValue>): Promise<TValue>;
@@ -51,9 +98,8 @@ declare const cacheDbInternals: {
51
98
  prepareCacheDatabaseTables: typeof prepareCacheDatabaseTables;
52
99
  readEntry: typeof readEntry;
53
100
  readLock: typeof readLock;
54
- renderCacheTableMigration: typeof renderCacheTableMigration;
55
101
  resolveDriver: typeof resolveDriver;
56
102
  withDatabaseCacheTableGuard: typeof withDatabaseCacheTableGuard;
57
103
  };
58
104
 
59
- export { DEFAULT_CACHE_DATABASE_LOCK_TABLE, DEFAULT_CACHE_DATABASE_TABLE, type DatabaseCacheDriverOptions, cacheDbInternals, createDatabaseCacheDriver };
105
+ export { CACHE_DATABASE_TABLE_DEFINITIONS, type CacheDatabaseTableColumnDefinition, type CacheDatabaseTableColumnKind, type CacheDatabaseTableDefinition, DEFAULT_CACHE_DATABASE_LOCK_TABLE, DEFAULT_CACHE_DATABASE_TABLE, type DatabaseCacheDriverOptions, cacheDbInternals, createDatabaseCacheDriver };
package/dist/index.mjs CHANGED
@@ -15,6 +15,30 @@ import {
15
15
  } from "@holo-js/db";
16
16
  var DEFAULT_CACHE_DATABASE_TABLE = "cache";
17
17
  var DEFAULT_CACHE_DATABASE_LOCK_TABLE = "cache_locks";
18
+ var CACHE_DATABASE_TABLE_DEFINITIONS = [
19
+ {
20
+ role: "entries",
21
+ defaultName: DEFAULT_CACHE_DATABASE_TABLE,
22
+ columns: [
23
+ { name: "key", kind: "string", primaryKey: true },
24
+ { name: "payload", kind: "text" },
25
+ { name: "expires_at", kind: "bigInteger", nullable: true }
26
+ ],
27
+ indexColumn: "expires_at",
28
+ indexName: (tableName) => `${tableName.replaceAll(".", "_")}_expires_at_index`
29
+ },
30
+ {
31
+ role: "locks",
32
+ defaultName: DEFAULT_CACHE_DATABASE_LOCK_TABLE,
33
+ columns: [
34
+ { name: "name", kind: "string", primaryKey: true },
35
+ { name: "owner", kind: "string" },
36
+ { name: "expires_at", kind: "bigInteger" }
37
+ ],
38
+ indexColumn: "expires_at",
39
+ indexName: (tableName) => `${tableName.replaceAll(".", "_")}_expires_at_index`
40
+ }
41
+ ];
18
42
  function defaultSleep(milliseconds) {
19
43
  return new Promise((resolveDelay) => {
20
44
  setTimeout(resolveDelay, milliseconds);
@@ -67,53 +91,44 @@ function createDatabaseConnection(connectionName, connection) {
67
91
  }
68
92
  }).connection(connectionName);
69
93
  }
94
+ function applyCacheDatabaseColumnDefinition(table, columnDefinition) {
95
+ let columnBuilder = table[columnDefinition.kind](columnDefinition.name);
96
+ if (columnDefinition.primaryKey) {
97
+ columnBuilder = columnBuilder.primaryKey();
98
+ }
99
+ if (columnDefinition.nullable) {
100
+ columnBuilder.nullable();
101
+ }
102
+ }
103
+ function applyCacheDatabaseTableDefinition(table, tableName, tableDefinition) {
104
+ for (const columnDefinition of tableDefinition.columns) {
105
+ applyCacheDatabaseColumnDefinition(table, columnDefinition);
106
+ }
107
+ table.index([tableDefinition.indexColumn], tableDefinition.indexName(tableName));
108
+ }
109
+ function resolveCacheDatabaseTableDefinition(role) {
110
+ const tableDefinition = CACHE_DATABASE_TABLE_DEFINITIONS.find((definition) => definition.role === role);
111
+ if (!tableDefinition) {
112
+ throw new Error(`Missing cache database table definition for "${role}".`);
113
+ }
114
+ return tableDefinition;
115
+ }
70
116
  async function prepareCacheDatabaseTables(connection, tableName = DEFAULT_CACHE_DATABASE_TABLE, lockTableName = DEFAULT_CACHE_DATABASE_LOCK_TABLE) {
71
117
  const schema = createSchemaService(connection);
72
118
  await connection.initialize();
119
+ const entryTableDefinition = resolveCacheDatabaseTableDefinition("entries");
120
+ const lockTableDefinition = resolveCacheDatabaseTableDefinition("locks");
73
121
  if (!await schema.hasTable(tableName)) {
74
122
  await schema.createTable(tableName, (table) => {
75
- table.string("key").primaryKey();
76
- table.text("payload");
77
- table.bigInteger("expires_at").nullable();
78
- table.index(["expires_at"], `${tableName.replaceAll(".", "_")}_expires_at_index`);
123
+ applyCacheDatabaseTableDefinition(table, tableName, entryTableDefinition);
79
124
  });
80
125
  }
81
126
  if (!await schema.hasTable(lockTableName)) {
82
127
  await schema.createTable(lockTableName, (table) => {
83
- table.string("name").primaryKey();
84
- table.string("owner");
85
- table.bigInteger("expires_at");
86
- table.index(["expires_at"], `${lockTableName.replaceAll(".", "_")}_expires_at_index`);
128
+ applyCacheDatabaseTableDefinition(table, lockTableName, lockTableDefinition);
87
129
  });
88
130
  }
89
131
  }
90
- function renderCacheTableMigration(tableName = DEFAULT_CACHE_DATABASE_TABLE, lockTableName = DEFAULT_CACHE_DATABASE_LOCK_TABLE) {
91
- return [
92
- "import { defineMigration, type MigrationContext } from '@holo-js/db'",
93
- "",
94
- "export default defineMigration({",
95
- " async up({ schema }: MigrationContext) {",
96
- ` await schema.createTable('${tableName}', (table) => {`,
97
- " table.string('key').primaryKey()",
98
- " table.text('payload')",
99
- " table.bigInteger('expires_at').nullable()",
100
- ` table.index(['expires_at'], '${tableName.replaceAll(".", "_")}_expires_at_index')`,
101
- " })",
102
- ` await schema.createTable('${lockTableName}', (table) => {`,
103
- " table.string('name').primaryKey()",
104
- " table.string('owner')",
105
- " table.bigInteger('expires_at')",
106
- ` table.index(['expires_at'], '${lockTableName.replaceAll(".", "_")}_expires_at_index')`,
107
- " })",
108
- " },",
109
- " async down({ schema }: MigrationContext) {",
110
- ` await schema.dropTable('${lockTableName}')`,
111
- ` await schema.dropTable('${tableName}')`,
112
- " },",
113
- "})",
114
- ""
115
- ].join("\n");
116
- }
117
132
  function resolveExecutionResultAffectedRows(result) {
118
133
  return result.affectedRows ?? 0;
119
134
  }
@@ -366,11 +381,11 @@ var cacheDbInternals = {
366
381
  prepareCacheDatabaseTables,
367
382
  readEntry,
368
383
  readLock,
369
- renderCacheTableMigration,
370
384
  resolveDriver,
371
385
  withDatabaseCacheTableGuard
372
386
  };
373
387
  export {
388
+ CACHE_DATABASE_TABLE_DEFINITIONS,
374
389
  DEFAULT_CACHE_DATABASE_LOCK_TABLE,
375
390
  DEFAULT_CACHE_DATABASE_TABLE,
376
391
  cacheDbInternals,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@holo-js/cache-db",
3
- "version": "0.1.9",
3
+ "version": "0.2.1",
4
4
  "description": "Holo-JS Framework - DB-backed cache driver",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -23,15 +23,15 @@
23
23
  "test": "vitest --run"
24
24
  },
25
25
  "peerDependencies": {
26
- "@holo-js/cache": "^0.1.9",
27
- "@holo-js/db": "^0.1.9"
26
+ "@holo-js/cache": "^0.2.1",
27
+ "@holo-js/db": "^0.2.1"
28
28
  },
29
29
  "dependencies": {
30
30
  "tslib": "^2.8.1"
31
31
  },
32
32
  "devDependencies": {
33
- "@holo-js/cache": "^0.1.9",
34
- "@holo-js/db": "^0.1.9",
33
+ "@holo-js/cache": "^0.2.1",
34
+ "@holo-js/db": "^0.2.1",
35
35
  "@types/node": "^22.10.2",
36
36
  "tsup": "^8.3.5",
37
37
  "typescript": "^5.7.2",