@20baht/elysia-idempotency 0.0.0-stage → 0.1.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.
@@ -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,cAA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B,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,77 @@
1
1
  {
2
2
  "name": "@20baht/elysia-idempotency",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
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
+ },
15
+ "./pg": {
16
+ "types": "./dist/pg.d.ts",
17
+ "import": "./dist/pg.js"
18
+ },
19
+ "./sqlite": {
20
+ "types": "./dist/sqlite.d.ts",
21
+ "import": "./dist/sqlite.js"
22
+ },
23
+ "./package.json": "./package.json"
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "README.md",
28
+ "CHANGELOG.md"
29
+ ],
30
+ "scripts": {
31
+ "build": "bun run scripts/build.ts",
32
+ "typecheck": "tsc -p tsconfig.json --noEmit",
33
+ "test": "bun test",
34
+ "test:coverage": "bun test --coverage"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "keywords": [
40
+ "elysia",
41
+ "idempotency",
42
+ "idempotency-key",
43
+ "retry",
44
+ "drizzle",
45
+ "postgres",
46
+ "sqlite",
47
+ "d1",
48
+ "bun"
49
+ ],
50
+ "peerDependencies": {
51
+ "drizzle-orm": ">=0.44.0 <1.0.0",
52
+ "elysia": "^1.4.0"
53
+ },
54
+ "peerDependenciesMeta": {
55
+ "drizzle-orm": {
56
+ "optional": true
57
+ }
58
+ },
59
+ "devDependencies": {
60
+ "@electric-sql/pglite": "^0.3.0",
61
+ "@types/bun": "^1.4.5",
62
+ "drizzle-kit": "^0.31.11",
63
+ "drizzle-orm": "^0.44.0",
64
+ "elysia": "^1.4.30",
65
+ "typescript": "^5.9.3"
66
+ },
67
+ "repository": {
68
+ "type": "git",
69
+ "url": "git+https://github.com/bank20baht/20baht.git",
70
+ "directory": "packages/elysia/idempotency"
71
+ },
72
+ "homepage": "https://github.com/bank20baht/20baht/tree/main/packages/elysia/idempotency#readme",
73
+ "bugs": {
74
+ "url": "https://github.com/bank20baht/20baht/issues"
75
+ },
76
+ "author": "Nattapong Promthong"
77
+ }