@fedify/postgres 0.1.0 → 0.2.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.
- package/README.md +12 -0
- package/esm/src/kv.js +3 -3
- package/esm/src/mq.js +13 -13
- package/package.json +2 -2
- package/script/src/kv.js +3 -3
- package/script/src/mq.js +13 -13
- package/types/src/kv.d.ts +3 -2
- package/types/src/kv.d.ts.map +1 -1
- package/types/src/mq.d.ts +2 -2
package/README.md
CHANGED
|
@@ -64,6 +64,18 @@ bun add @fedify/postgres
|
|
|
64
64
|
Changelog
|
|
65
65
|
---------
|
|
66
66
|
|
|
67
|
+
### Version 0.2.0
|
|
68
|
+
|
|
69
|
+
Released on November 3, 2024.
|
|
70
|
+
|
|
71
|
+
- Fixed a bug where JSON values are double-quoted in the database. Since it's
|
|
72
|
+
a breaking change data-wise, the default values of the following options
|
|
73
|
+
are also changed:
|
|
74
|
+
|
|
75
|
+
- `PostgresKvStoreOptions.tableName` defaults to `"fedify_kv_v2"`.
|
|
76
|
+
- `PostgresMessageQueueOptions.tableName` defaults to
|
|
77
|
+
`"fedify_message_v2"`.
|
|
78
|
+
|
|
67
79
|
### Version 0.1.0
|
|
68
80
|
|
|
69
81
|
Initial release. Released on September 26, 2024.
|
package/esm/src/kv.js
CHANGED
|
@@ -40,7 +40,7 @@ export class PostgresKvStore {
|
|
|
40
40
|
_PostgresKvStore_tableName.set(this, void 0);
|
|
41
41
|
_PostgresKvStore_initialized.set(this, void 0);
|
|
42
42
|
__classPrivateFieldSet(this, _PostgresKvStore_sql, sql, "f");
|
|
43
|
-
__classPrivateFieldSet(this, _PostgresKvStore_tableName, options.tableName ?? "
|
|
43
|
+
__classPrivateFieldSet(this, _PostgresKvStore_tableName, options.tableName ?? "fedify_kv_v2", "f");
|
|
44
44
|
__classPrivateFieldSet(this, _PostgresKvStore_initialized, options.initialized ?? false, "f");
|
|
45
45
|
}
|
|
46
46
|
async get(key) {
|
|
@@ -52,14 +52,14 @@ export class PostgresKvStore {
|
|
|
52
52
|
`;
|
|
53
53
|
if (result.length < 1)
|
|
54
54
|
return undefined;
|
|
55
|
-
return
|
|
55
|
+
return result[0].value;
|
|
56
56
|
}
|
|
57
57
|
async set(key, value, options) {
|
|
58
58
|
await this.initialize();
|
|
59
59
|
const ttl = options?.ttl == null ? null : options.ttl.toString();
|
|
60
60
|
await __classPrivateFieldGet(this, _PostgresKvStore_sql, "f").bind(this) `
|
|
61
61
|
INSERT INTO ${__classPrivateFieldGet(this, _PostgresKvStore_sql, "f").call(this, __classPrivateFieldGet(this, _PostgresKvStore_tableName, "f"))} (key, value, ttl)
|
|
62
|
-
VALUES (${key}, ${
|
|
62
|
+
VALUES (${key}, ${value}, ${ttl})
|
|
63
63
|
ON CONFLICT (key)
|
|
64
64
|
DO UPDATE SET value = EXCLUDED.value, ttl = EXCLUDED.ttl;
|
|
65
65
|
`;
|
package/esm/src/mq.js
CHANGED
|
@@ -40,7 +40,7 @@ export class PostgresMessageQueue {
|
|
|
40
40
|
_PostgresMessageQueue_pollIntervalMs.set(this, void 0);
|
|
41
41
|
_PostgresMessageQueue_initialized.set(this, void 0);
|
|
42
42
|
__classPrivateFieldSet(this, _PostgresMessageQueue_sql, sql, "f");
|
|
43
|
-
__classPrivateFieldSet(this, _PostgresMessageQueue_tableName, options?.tableName ?? "
|
|
43
|
+
__classPrivateFieldSet(this, _PostgresMessageQueue_tableName, options?.tableName ?? "fedify_message_v2", "f");
|
|
44
44
|
__classPrivateFieldSet(this, _PostgresMessageQueue_channelName, options?.channelName ?? "fedify_channel", "f");
|
|
45
45
|
__classPrivateFieldSet(this, _PostgresMessageQueue_pollIntervalMs, dntShim.Temporal.Duration.from(options?.pollInterval ?? { seconds: 5 }).total("millisecond"), "f");
|
|
46
46
|
__classPrivateFieldSet(this, _PostgresMessageQueue_initialized, options?.initialized ?? false, "f");
|
|
@@ -52,7 +52,7 @@ export class PostgresMessageQueue {
|
|
|
52
52
|
const delay = options?.delay ?? dntShim.Temporal.Duration.from({ seconds: 0 });
|
|
53
53
|
await __classPrivateFieldGet(this, _PostgresMessageQueue_sql, "f").bind(this) `
|
|
54
54
|
INSERT INTO ${__classPrivateFieldGet(this, _PostgresMessageQueue_sql, "f").call(this, __classPrivateFieldGet(this, _PostgresMessageQueue_tableName, "f"))} (message, delay)
|
|
55
|
-
VALUES (${
|
|
55
|
+
VALUES (${message}, ${delay.toString()});
|
|
56
56
|
`;
|
|
57
57
|
await __classPrivateFieldGet(this, _PostgresMessageQueue_sql, "f").notify(__classPrivateFieldGet(this, _PostgresMessageQueue_channelName, "f"), delay.toString());
|
|
58
58
|
}
|
|
@@ -64,23 +64,23 @@ export class PostgresMessageQueue {
|
|
|
64
64
|
const poll = async () => {
|
|
65
65
|
while (!signal?.aborted) {
|
|
66
66
|
const query = __classPrivateFieldGet(this, _PostgresMessageQueue_sql, "f").bind(this) `
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
67
|
+
DELETE FROM ${__classPrivateFieldGet(this, _PostgresMessageQueue_sql, "f").call(this, __classPrivateFieldGet(this, _PostgresMessageQueue_tableName, "f"))}
|
|
68
|
+
WHERE id = (
|
|
69
|
+
SELECT id
|
|
70
|
+
FROM ${__classPrivateFieldGet(this, _PostgresMessageQueue_sql, "f").call(this, __classPrivateFieldGet(this, _PostgresMessageQueue_tableName, "f"))}
|
|
71
|
+
WHERE created + delay < CURRENT_TIMESTAMP
|
|
72
|
+
ORDER BY created
|
|
73
|
+
LIMIT 1
|
|
74
|
+
)
|
|
75
|
+
RETURNING message;
|
|
76
|
+
`.execute();
|
|
77
77
|
const cancel = query.cancel.bind(query);
|
|
78
78
|
signal?.addEventListener("abort", cancel);
|
|
79
79
|
let i = 0;
|
|
80
80
|
for (const message of await query) {
|
|
81
81
|
if (signal?.aborted)
|
|
82
82
|
return;
|
|
83
|
-
await handler(
|
|
83
|
+
await handler(message.message);
|
|
84
84
|
i++;
|
|
85
85
|
}
|
|
86
86
|
signal?.removeEventListener("abort", cancel);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fedify/postgres",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "PostgreSQL drivers for Fedify",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fedify",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
],
|
|
62
62
|
"dependencies": {
|
|
63
63
|
"@fedify/fedify": "^1.0.0",
|
|
64
|
-
"postgres": "^3.4.
|
|
64
|
+
"postgres": "^3.4.5",
|
|
65
65
|
"@deno/shim-deno": "~0.18.0",
|
|
66
66
|
"@js-temporal/polyfill": "^0.4.4"
|
|
67
67
|
},
|
package/script/src/kv.js
CHANGED
|
@@ -43,7 +43,7 @@ class PostgresKvStore {
|
|
|
43
43
|
_PostgresKvStore_tableName.set(this, void 0);
|
|
44
44
|
_PostgresKvStore_initialized.set(this, void 0);
|
|
45
45
|
__classPrivateFieldSet(this, _PostgresKvStore_sql, sql, "f");
|
|
46
|
-
__classPrivateFieldSet(this, _PostgresKvStore_tableName, options.tableName ?? "
|
|
46
|
+
__classPrivateFieldSet(this, _PostgresKvStore_tableName, options.tableName ?? "fedify_kv_v2", "f");
|
|
47
47
|
__classPrivateFieldSet(this, _PostgresKvStore_initialized, options.initialized ?? false, "f");
|
|
48
48
|
}
|
|
49
49
|
async get(key) {
|
|
@@ -55,14 +55,14 @@ class PostgresKvStore {
|
|
|
55
55
|
`;
|
|
56
56
|
if (result.length < 1)
|
|
57
57
|
return undefined;
|
|
58
|
-
return
|
|
58
|
+
return result[0].value;
|
|
59
59
|
}
|
|
60
60
|
async set(key, value, options) {
|
|
61
61
|
await this.initialize();
|
|
62
62
|
const ttl = options?.ttl == null ? null : options.ttl.toString();
|
|
63
63
|
await __classPrivateFieldGet(this, _PostgresKvStore_sql, "f").bind(this) `
|
|
64
64
|
INSERT INTO ${__classPrivateFieldGet(this, _PostgresKvStore_sql, "f").call(this, __classPrivateFieldGet(this, _PostgresKvStore_tableName, "f"))} (key, value, ttl)
|
|
65
|
-
VALUES (${key}, ${
|
|
65
|
+
VALUES (${key}, ${value}, ${ttl})
|
|
66
66
|
ON CONFLICT (key)
|
|
67
67
|
DO UPDATE SET value = EXCLUDED.value, ttl = EXCLUDED.ttl;
|
|
68
68
|
`;
|
package/script/src/mq.js
CHANGED
|
@@ -69,7 +69,7 @@ class PostgresMessageQueue {
|
|
|
69
69
|
_PostgresMessageQueue_pollIntervalMs.set(this, void 0);
|
|
70
70
|
_PostgresMessageQueue_initialized.set(this, void 0);
|
|
71
71
|
__classPrivateFieldSet(this, _PostgresMessageQueue_sql, sql, "f");
|
|
72
|
-
__classPrivateFieldSet(this, _PostgresMessageQueue_tableName, options?.tableName ?? "
|
|
72
|
+
__classPrivateFieldSet(this, _PostgresMessageQueue_tableName, options?.tableName ?? "fedify_message_v2", "f");
|
|
73
73
|
__classPrivateFieldSet(this, _PostgresMessageQueue_channelName, options?.channelName ?? "fedify_channel", "f");
|
|
74
74
|
__classPrivateFieldSet(this, _PostgresMessageQueue_pollIntervalMs, dntShim.Temporal.Duration.from(options?.pollInterval ?? { seconds: 5 }).total("millisecond"), "f");
|
|
75
75
|
__classPrivateFieldSet(this, _PostgresMessageQueue_initialized, options?.initialized ?? false, "f");
|
|
@@ -81,7 +81,7 @@ class PostgresMessageQueue {
|
|
|
81
81
|
const delay = options?.delay ?? dntShim.Temporal.Duration.from({ seconds: 0 });
|
|
82
82
|
await __classPrivateFieldGet(this, _PostgresMessageQueue_sql, "f").bind(this) `
|
|
83
83
|
INSERT INTO ${__classPrivateFieldGet(this, _PostgresMessageQueue_sql, "f").call(this, __classPrivateFieldGet(this, _PostgresMessageQueue_tableName, "f"))} (message, delay)
|
|
84
|
-
VALUES (${
|
|
84
|
+
VALUES (${message}, ${delay.toString()});
|
|
85
85
|
`;
|
|
86
86
|
await __classPrivateFieldGet(this, _PostgresMessageQueue_sql, "f").notify(__classPrivateFieldGet(this, _PostgresMessageQueue_channelName, "f"), delay.toString());
|
|
87
87
|
}
|
|
@@ -93,23 +93,23 @@ class PostgresMessageQueue {
|
|
|
93
93
|
const poll = async () => {
|
|
94
94
|
while (!signal?.aborted) {
|
|
95
95
|
const query = __classPrivateFieldGet(this, _PostgresMessageQueue_sql, "f").bind(this) `
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
96
|
+
DELETE FROM ${__classPrivateFieldGet(this, _PostgresMessageQueue_sql, "f").call(this, __classPrivateFieldGet(this, _PostgresMessageQueue_tableName, "f"))}
|
|
97
|
+
WHERE id = (
|
|
98
|
+
SELECT id
|
|
99
|
+
FROM ${__classPrivateFieldGet(this, _PostgresMessageQueue_sql, "f").call(this, __classPrivateFieldGet(this, _PostgresMessageQueue_tableName, "f"))}
|
|
100
|
+
WHERE created + delay < CURRENT_TIMESTAMP
|
|
101
|
+
ORDER BY created
|
|
102
|
+
LIMIT 1
|
|
103
|
+
)
|
|
104
|
+
RETURNING message;
|
|
105
|
+
`.execute();
|
|
106
106
|
const cancel = query.cancel.bind(query);
|
|
107
107
|
signal?.addEventListener("abort", cancel);
|
|
108
108
|
let i = 0;
|
|
109
109
|
for (const message of await query) {
|
|
110
110
|
if (signal?.aborted)
|
|
111
111
|
return;
|
|
112
|
-
await handler(
|
|
112
|
+
await handler(message.message);
|
|
113
113
|
i++;
|
|
114
114
|
}
|
|
115
115
|
signal?.removeEventListener("abort", cancel);
|
package/types/src/kv.d.ts
CHANGED
|
@@ -5,8 +5,9 @@ import type { Sql } from "postgres";
|
|
|
5
5
|
*/
|
|
6
6
|
export interface PostgresKvStoreOptions {
|
|
7
7
|
/**
|
|
8
|
-
* The table name to use for the key-value store.
|
|
9
|
-
*
|
|
8
|
+
* The table name to use for the key-value store.
|
|
9
|
+
* `"fedify_kv_v2"` by default.
|
|
10
|
+
* @default `"fedify_kv_v2"`
|
|
10
11
|
*/
|
|
11
12
|
tableName?: string;
|
|
12
13
|
/**
|
package/types/src/kv.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kv.d.ts","sourceRoot":"","sources":["../../src/src/kv.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACxE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAEpC;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC
|
|
1
|
+
{"version":3,"file":"kv.d.ts","sourceRoot":"","sources":["../../src/src/kv.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACxE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAEpC;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED;;;;;;;;;;;;;;GAcG;AACH,qBAAa,eAAgB,YAAW,OAAO;;IAM7C;;;;OAIG;gBAGD,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC,EACZ,OAAO,GAAE,sBAA2B;IAchC,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,KAAK,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAWpD,GAAG,CACP,GAAG,EAAE,KAAK,EACV,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,iBAAiB,GAAG,SAAS,GACtC,OAAO,CAAC,IAAI,CAAC;IAYV,MAAM,CAAC,GAAG,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;IASvC;;;OAGG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAajC;;;OAGG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAG5B"}
|
package/types/src/mq.d.ts
CHANGED
|
@@ -7,8 +7,8 @@ import type { Sql } from "postgres";
|
|
|
7
7
|
export interface PostgresMessageQueueOptions {
|
|
8
8
|
/**
|
|
9
9
|
* The table name to use for the message queue.
|
|
10
|
-
* `"
|
|
11
|
-
* @default `"
|
|
10
|
+
* `"fedify_message_v2"` by default.
|
|
11
|
+
* @default `"fedify_message_v2"`
|
|
12
12
|
*/
|
|
13
13
|
tableName?: string;
|
|
14
14
|
/**
|