@20baht/elysia-idempotency 0.0.0-stage → 0.1.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/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +110 -2
- package/dist/core.d.ts +95 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +219 -0
- package/dist/index.js.map +11 -0
- package/dist/pg.d.ts +106 -0
- package/dist/pg.d.ts.map +1 -0
- package/dist/pg.js +57 -0
- package/dist/pg.js.map +10 -0
- package/dist/plugin.d.ts +541 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/sqlite.d.ts +112 -0
- package/dist/sqlite.d.ts.map +1 -0
- package/dist/sqlite.js +56 -0
- package/dist/sqlite.js.map +10 -0
- package/package.json +78 -4
package/dist/sqlite.d.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { type BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core';
|
|
2
|
+
import type { IdempotencyStore, StoredResponse } from './core.js';
|
|
3
|
+
/** The table of keys, for the host's drizzle schema. */
|
|
4
|
+
export declare const sqliteIdempotencyTable: (name?: string) => import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
5
|
+
name: string;
|
|
6
|
+
schema: undefined;
|
|
7
|
+
columns: {
|
|
8
|
+
key: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
9
|
+
name: "key";
|
|
10
|
+
tableName: string;
|
|
11
|
+
dataType: "string";
|
|
12
|
+
columnType: "SQLiteText";
|
|
13
|
+
data: string;
|
|
14
|
+
driverParam: string;
|
|
15
|
+
notNull: true;
|
|
16
|
+
hasDefault: false;
|
|
17
|
+
isPrimaryKey: true;
|
|
18
|
+
isAutoincrement: false;
|
|
19
|
+
hasRuntimeDefault: false;
|
|
20
|
+
enumValues: [string, ...string[]];
|
|
21
|
+
baseColumn: never;
|
|
22
|
+
identity: undefined;
|
|
23
|
+
generated: undefined;
|
|
24
|
+
}, {}, {
|
|
25
|
+
length: number | undefined;
|
|
26
|
+
}>;
|
|
27
|
+
fingerprint: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
28
|
+
name: "fingerprint";
|
|
29
|
+
tableName: string;
|
|
30
|
+
dataType: "string";
|
|
31
|
+
columnType: "SQLiteText";
|
|
32
|
+
data: string;
|
|
33
|
+
driverParam: string;
|
|
34
|
+
notNull: true;
|
|
35
|
+
hasDefault: false;
|
|
36
|
+
isPrimaryKey: false;
|
|
37
|
+
isAutoincrement: false;
|
|
38
|
+
hasRuntimeDefault: false;
|
|
39
|
+
enumValues: [string, ...string[]];
|
|
40
|
+
baseColumn: never;
|
|
41
|
+
identity: undefined;
|
|
42
|
+
generated: undefined;
|
|
43
|
+
}, {}, {
|
|
44
|
+
length: number | undefined;
|
|
45
|
+
}>;
|
|
46
|
+
status: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
47
|
+
name: "status";
|
|
48
|
+
tableName: string;
|
|
49
|
+
dataType: "string";
|
|
50
|
+
columnType: "SQLiteText";
|
|
51
|
+
data: "pending" | "completed";
|
|
52
|
+
driverParam: string;
|
|
53
|
+
notNull: true;
|
|
54
|
+
hasDefault: false;
|
|
55
|
+
isPrimaryKey: false;
|
|
56
|
+
isAutoincrement: false;
|
|
57
|
+
hasRuntimeDefault: false;
|
|
58
|
+
enumValues: ["pending", "completed"];
|
|
59
|
+
baseColumn: never;
|
|
60
|
+
identity: undefined;
|
|
61
|
+
generated: undefined;
|
|
62
|
+
}, {}, {
|
|
63
|
+
length: number | undefined;
|
|
64
|
+
}>;
|
|
65
|
+
response: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
66
|
+
name: "response";
|
|
67
|
+
tableName: string;
|
|
68
|
+
dataType: "json";
|
|
69
|
+
columnType: "SQLiteTextJson";
|
|
70
|
+
data: StoredResponse;
|
|
71
|
+
driverParam: string;
|
|
72
|
+
notNull: false;
|
|
73
|
+
hasDefault: false;
|
|
74
|
+
isPrimaryKey: false;
|
|
75
|
+
isAutoincrement: false;
|
|
76
|
+
hasRuntimeDefault: false;
|
|
77
|
+
enumValues: undefined;
|
|
78
|
+
baseColumn: never;
|
|
79
|
+
identity: undefined;
|
|
80
|
+
generated: undefined;
|
|
81
|
+
}, {}, {
|
|
82
|
+
$type: StoredResponse;
|
|
83
|
+
}>;
|
|
84
|
+
createdAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
85
|
+
name: "created_at";
|
|
86
|
+
tableName: string;
|
|
87
|
+
dataType: "date";
|
|
88
|
+
columnType: "SQLiteTimestamp";
|
|
89
|
+
data: Date;
|
|
90
|
+
driverParam: number;
|
|
91
|
+
notNull: true;
|
|
92
|
+
hasDefault: false;
|
|
93
|
+
isPrimaryKey: false;
|
|
94
|
+
isAutoincrement: false;
|
|
95
|
+
hasRuntimeDefault: false;
|
|
96
|
+
enumValues: undefined;
|
|
97
|
+
baseColumn: never;
|
|
98
|
+
identity: undefined;
|
|
99
|
+
generated: undefined;
|
|
100
|
+
}, {}, {}>;
|
|
101
|
+
};
|
|
102
|
+
dialect: "sqlite";
|
|
103
|
+
}>;
|
|
104
|
+
export type SqliteIdempotencyTable = ReturnType<typeof sqliteIdempotencyTable>;
|
|
105
|
+
/** Any drizzle SQLite database, sync (bun:sqlite) or async (D1, libsql), whatever its schema. */
|
|
106
|
+
export type SqliteIdempotencyDatabase = BaseSQLiteDatabase<'sync' | 'async', any, any>;
|
|
107
|
+
/** The store on a drizzle SQLite database. */
|
|
108
|
+
export declare const sqliteStore: ({ db, table, }: {
|
|
109
|
+
db: SqliteIdempotencyDatabase;
|
|
110
|
+
table: SqliteIdempotencyTable;
|
|
111
|
+
}) => IdempotencyStore;
|
|
112
|
+
//# sourceMappingURL=sqlite.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../src/sqlite.ts"],"names":[],"mappings":"AAGA,OAAO,EAKL,KAAK,kBAAkB,EACxB,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAqB,gBAAgB,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAErF,wDAAwD;AACxD,eAAO,MAAM,sBAAsB,GAAI,aAAyB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAW7D,CAAC;AAEJ,MAAM,MAAM,sBAAsB,GAAG,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAE/E,iGAAiG;AAEjG,MAAM,MAAM,yBAAyB,GAAG,kBAAkB,CAAC,MAAM,GAAG,OAAO,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AAEvF,8CAA8C;AAC9C,eAAO,MAAM,WAAW,GAAI,gBAGzB;IACD,EAAE,EAAE,yBAAyB,CAAC;IAC9B,KAAK,EAAE,sBAAsB,CAAC;CAC/B,KAAG,gBAqCF,CAAC"}
|
package/dist/sqlite.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/sqlite.ts
|
|
3
|
+
import { and, eq, lt } from "drizzle-orm";
|
|
4
|
+
import {
|
|
5
|
+
index,
|
|
6
|
+
integer,
|
|
7
|
+
sqliteTable,
|
|
8
|
+
text
|
|
9
|
+
} from "drizzle-orm/sqlite-core";
|
|
10
|
+
var sqliteIdempotencyTable = (name = "idempotency_keys") => sqliteTable(name, {
|
|
11
|
+
key: text("key").primaryKey(),
|
|
12
|
+
fingerprint: text("fingerprint").notNull(),
|
|
13
|
+
status: text("status", { enum: ["pending", "completed"] }).notNull(),
|
|
14
|
+
response: text("response", { mode: "json" }).$type(),
|
|
15
|
+
createdAt: integer("created_at", { mode: "timestamp_ms" }).notNull()
|
|
16
|
+
}, (table) => [index(`${name}_created_at_idx`).on(table.createdAt)]);
|
|
17
|
+
var sqliteStore = ({
|
|
18
|
+
db,
|
|
19
|
+
table
|
|
20
|
+
}) => ({
|
|
21
|
+
async get(key) {
|
|
22
|
+
const [row] = await db.select().from(table).where(eq(table.key, key)).limit(1);
|
|
23
|
+
if (!row)
|
|
24
|
+
return;
|
|
25
|
+
const record = {
|
|
26
|
+
key: row.key,
|
|
27
|
+
fingerprint: row.fingerprint,
|
|
28
|
+
status: row.status,
|
|
29
|
+
createdAt: row.createdAt
|
|
30
|
+
};
|
|
31
|
+
if (row.response)
|
|
32
|
+
record.response = row.response;
|
|
33
|
+
return record;
|
|
34
|
+
},
|
|
35
|
+
async reserve(key, fingerprint, createdAt) {
|
|
36
|
+
const inserted = await db.insert(table).values({ key, fingerprint, status: "pending", createdAt }).onConflictDoNothing().returning({ key: table.key });
|
|
37
|
+
return inserted.length > 0;
|
|
38
|
+
},
|
|
39
|
+
async complete(key, createdAt, response) {
|
|
40
|
+
await db.update(table).set({ status: "completed", response }).where(and(eq(table.key, key), eq(table.createdAt, createdAt)));
|
|
41
|
+
},
|
|
42
|
+
async remove(key, createdAt) {
|
|
43
|
+
await db.delete(table).where(and(eq(table.key, key), eq(table.createdAt, createdAt)));
|
|
44
|
+
},
|
|
45
|
+
async purge(before) {
|
|
46
|
+
const gone = await db.delete(table).where(lt(table.createdAt, before)).returning({ key: table.key });
|
|
47
|
+
return gone.length;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
export {
|
|
51
|
+
sqliteStore,
|
|
52
|
+
sqliteIdempotencyTable
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
//# debugId=DC8FB0C3152DE52264756E2164756E21
|
|
56
|
+
//# sourceMappingURL=sqlite.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/sqlite.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"// Keys kept in SQLite through drizzle-orm: Cloudflare D1, bun:sqlite, libsql. Times are whole\n// milliseconds, which is all a Date holds.\nimport { and, eq, lt } from 'drizzle-orm';\nimport {\n index,\n integer,\n sqliteTable,\n text,\n type BaseSQLiteDatabase,\n} from 'drizzle-orm/sqlite-core';\nimport type { IdempotencyRecord, IdempotencyStore, StoredResponse } from './core.ts';\n\n/** The table of keys, for the host's drizzle schema. */\nexport const sqliteIdempotencyTable = (name = 'idempotency_keys') =>\n sqliteTable(\n name,\n {\n key: text('key').primaryKey(),\n fingerprint: text('fingerprint').notNull(),\n status: text('status', { enum: ['pending', 'completed'] }).notNull(),\n response: text('response', { mode: 'json' }).$type<StoredResponse>(),\n createdAt: integer('created_at', { mode: 'timestamp_ms' }).notNull(),\n },\n (table) => [index(`${name}_created_at_idx`).on(table.createdAt)],\n );\n\nexport type SqliteIdempotencyTable = ReturnType<typeof sqliteIdempotencyTable>;\n\n/** Any drizzle SQLite database, sync (bun:sqlite) or async (D1, libsql), whatever its schema. */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any -- the host's driver and schema types are its own; no query here reads them\nexport type SqliteIdempotencyDatabase = BaseSQLiteDatabase<'sync' | 'async', any, any>;\n\n/** The store on a drizzle SQLite database. */\nexport const sqliteStore = ({\n db,\n table,\n}: {\n db: SqliteIdempotencyDatabase;\n table: SqliteIdempotencyTable;\n}): IdempotencyStore => ({\n async get(key) {\n const [row] = await db.select().from(table).where(eq(table.key, key)).limit(1);\n if (!row) return undefined;\n const record: IdempotencyRecord = {\n key: row.key,\n fingerprint: row.fingerprint,\n status: row.status,\n createdAt: row.createdAt,\n };\n if (row.response) record.response = row.response;\n return record;\n },\n async reserve(key, fingerprint, createdAt) {\n const inserted = await db\n .insert(table)\n .values({ key, fingerprint, status: 'pending', createdAt })\n .onConflictDoNothing()\n .returning({ key: table.key });\n return inserted.length > 0;\n },\n async complete(key, createdAt, response) {\n await db\n .update(table)\n .set({ status: 'completed', response })\n .where(and(eq(table.key, key), eq(table.createdAt, createdAt)));\n },\n async remove(key, createdAt) {\n await db.delete(table).where(and(eq(table.key, key), eq(table.createdAt, createdAt)));\n },\n async purge(before) {\n const gone = await db\n .delete(table)\n .where(lt(table.createdAt, before))\n .returning({ key: table.key });\n return gone.length;\n },\n});\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;AAEA;AACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUO,IAAM,yBAAyB,CAAC,OAAO,uBAC5C,YACE,MACA;AAAA,EACE,KAAK,KAAK,KAAK,EAAE,WAAW;AAAA,EAC5B,aAAa,KAAK,aAAa,EAAE,QAAQ;AAAA,EACzC,QAAQ,KAAK,UAAU,EAAE,MAAM,CAAC,WAAW,WAAW,EAAE,CAAC,EAAE,QAAQ;AAAA,EACnE,UAAU,KAAK,YAAY,EAAE,MAAM,OAAO,CAAC,EAAE,MAAsB;AAAA,EACnE,WAAW,QAAQ,cAAc,EAAE,MAAM,eAAe,CAAC,EAAE,QAAQ;AACrE,GACA,CAAC,UAAU,CAAC,MAAM,GAAG,qBAAqB,EAAE,GAAG,MAAM,SAAS,CAAC,CACjE;AASK,IAAM,cAAc;AAAA,EACzB;AAAA,EACA;AAAA,OAIuB;AAAA,OACjB,IAAG,CAAC,KAAK;AAAA,IACb,OAAO,OAAO,MAAM,GAAG,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,GAAG,MAAM,KAAK,GAAG,CAAC,EAAE,MAAM,CAAC;AAAA,IAC7E,IAAI,CAAC;AAAA,MAAK;AAAA,IACV,MAAM,SAA4B;AAAA,MAChC,KAAK,IAAI;AAAA,MACT,aAAa,IAAI;AAAA,MACjB,QAAQ,IAAI;AAAA,MACZ,WAAW,IAAI;AAAA,IACjB;AAAA,IACA,IAAI,IAAI;AAAA,MAAU,OAAO,WAAW,IAAI;AAAA,IACxC,OAAO;AAAA;AAAA,OAEH,QAAO,CAAC,KAAK,aAAa,WAAW;AAAA,IACzC,MAAM,WAAW,MAAM,GACpB,OAAO,KAAK,EACZ,OAAO,EAAE,KAAK,aAAa,QAAQ,WAAW,UAAU,CAAC,EACzD,oBAAoB,EACpB,UAAU,EAAE,KAAK,MAAM,IAAI,CAAC;AAAA,IAC/B,OAAO,SAAS,SAAS;AAAA;AAAA,OAErB,SAAQ,CAAC,KAAK,WAAW,UAAU;AAAA,IACvC,MAAM,GACH,OAAO,KAAK,EACZ,IAAI,EAAE,QAAQ,aAAa,SAAS,CAAC,EACrC,MAAM,IAAI,GAAG,MAAM,KAAK,GAAG,GAAG,GAAG,MAAM,WAAW,SAAS,CAAC,CAAC;AAAA;AAAA,OAE5D,OAAM,CAAC,KAAK,WAAW;AAAA,IAC3B,MAAM,GAAG,OAAO,KAAK,EAAE,MAAM,IAAI,GAAG,MAAM,KAAK,GAAG,GAAG,GAAG,MAAM,WAAW,SAAS,CAAC,CAAC;AAAA;AAAA,OAEhF,MAAK,CAAC,QAAQ;AAAA,IAClB,MAAM,OAAO,MAAM,GAChB,OAAO,KAAK,EACZ,MAAM,GAAG,MAAM,WAAW,MAAM,CAAC,EACjC,UAAU,EAAE,KAAK,MAAM,IAAI,CAAC;AAAA,IAC/B,OAAO,KAAK;AAAA;AAEhB;",
|
|
8
|
+
"debugId": "DC8FB0C3152DE52264756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,80 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@20baht/elysia-idempotency",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Idempotency-Key for Elysia routes: a retried POST gets the first response back instead of writing twice. Keys kept in memory, Postgres or SQLite (D1) through drizzle-orm, or a store of your own.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./pg": {
|
|
17
|
+
"types": "./dist/pg.d.ts",
|
|
18
|
+
"import": "./dist/pg.js",
|
|
19
|
+
"default": "./dist/pg.js"
|
|
20
|
+
},
|
|
21
|
+
"./sqlite": {
|
|
22
|
+
"types": "./dist/sqlite.d.ts",
|
|
23
|
+
"import": "./dist/sqlite.js",
|
|
24
|
+
"default": "./dist/sqlite.js"
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"README.md",
|
|
31
|
+
"CHANGELOG.md"
|
|
32
|
+
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "bun run scripts/build.ts",
|
|
35
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
36
|
+
"test": "bun test",
|
|
37
|
+
"test:coverage": "bun test --coverage"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"elysia",
|
|
44
|
+
"idempotency",
|
|
45
|
+
"idempotency-key",
|
|
46
|
+
"retry",
|
|
47
|
+
"drizzle",
|
|
48
|
+
"postgres",
|
|
49
|
+
"sqlite",
|
|
50
|
+
"d1",
|
|
51
|
+
"bun"
|
|
52
|
+
],
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"drizzle-orm": ">=0.44.0 <1.0.0",
|
|
55
|
+
"elysia": "^1.4.0"
|
|
56
|
+
},
|
|
57
|
+
"peerDependenciesMeta": {
|
|
58
|
+
"drizzle-orm": {
|
|
59
|
+
"optional": true
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"@electric-sql/pglite": "^0.3.0",
|
|
64
|
+
"@types/bun": "^1.4.5",
|
|
65
|
+
"drizzle-kit": "^0.31.11",
|
|
66
|
+
"drizzle-orm": "^0.44.0",
|
|
67
|
+
"elysia": "^1.4.30",
|
|
68
|
+
"typescript": "^5.9.3"
|
|
69
|
+
},
|
|
70
|
+
"repository": {
|
|
71
|
+
"type": "git",
|
|
72
|
+
"url": "git+https://github.com/bank20baht/20baht.git",
|
|
73
|
+
"directory": "packages/elysia/idempotency"
|
|
74
|
+
},
|
|
75
|
+
"homepage": "https://github.com/bank20baht/20baht/tree/main/packages/elysia/idempotency#readme",
|
|
76
|
+
"bugs": {
|
|
77
|
+
"url": "https://github.com/bank20baht/20baht/issues"
|
|
78
|
+
},
|
|
79
|
+
"author": "Nattapong Promthong"
|
|
80
|
+
}
|