trustline 0.0.1 → 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.
- package/LICENSE +21 -0
- package/README.md +339 -0
- package/dist/adapters/mysql/index.cjs +199 -0
- package/dist/adapters/mysql/index.cjs.map +1 -0
- package/dist/adapters/mysql/index.d.cts +6 -0
- package/dist/adapters/mysql/index.d.ts +6 -0
- package/dist/adapters/mysql/index.js +21 -0
- package/dist/adapters/mysql/index.js.map +1 -0
- package/dist/adapters/postgres/index.cjs +199 -0
- package/dist/adapters/postgres/index.cjs.map +1 -0
- package/dist/adapters/postgres/index.d.cts +6 -0
- package/dist/adapters/postgres/index.d.ts +6 -0
- package/dist/adapters/postgres/index.js +21 -0
- package/dist/adapters/postgres/index.js.map +1 -0
- package/dist/adapters/sqlite/index.cjs +216 -0
- package/dist/adapters/sqlite/index.cjs.map +1 -0
- package/dist/adapters/sqlite/index.d.cts +6 -0
- package/dist/adapters/sqlite/index.d.ts +6 -0
- package/dist/adapters/sqlite/index.js +28 -0
- package/dist/adapters/sqlite/index.js.map +1 -0
- package/dist/chunk-CTPFKR4O.js +157 -0
- package/dist/chunk-CTPFKR4O.js.map +1 -0
- package/dist/chunk-GF3NKEEK.js +18 -0
- package/dist/chunk-GF3NKEEK.js.map +1 -0
- package/dist/client/index.cjs +141 -0
- package/dist/client/index.cjs.map +1 -0
- package/dist/client/index.d.cts +18 -0
- package/dist/client/index.d.ts +18 -0
- package/dist/client/index.js +104 -0
- package/dist/client/index.js.map +1 -0
- package/dist/frameworks/express/index.cjs +121 -0
- package/dist/frameworks/express/index.cjs.map +1 -0
- package/dist/frameworks/express/index.d.cts +18 -0
- package/dist/frameworks/express/index.d.ts +18 -0
- package/dist/frameworks/express/index.js +83 -0
- package/dist/frameworks/express/index.js.map +1 -0
- package/dist/frameworks/fastify/index.cjs +158 -0
- package/dist/frameworks/fastify/index.cjs.map +1 -0
- package/dist/frameworks/fastify/index.d.cts +25 -0
- package/dist/frameworks/fastify/index.d.ts +25 -0
- package/dist/frameworks/fastify/index.js +120 -0
- package/dist/frameworks/fastify/index.js.map +1 -0
- package/dist/frameworks/hono/index.cjs +117 -0
- package/dist/frameworks/hono/index.cjs.map +1 -0
- package/dist/frameworks/hono/index.d.cts +17 -0
- package/dist/frameworks/hono/index.d.ts +17 -0
- package/dist/frameworks/hono/index.js +79 -0
- package/dist/frameworks/hono/index.js.map +1 -0
- package/dist/index-Dc4GFume.d.ts +34 -0
- package/dist/index-DqkKZOlH.d.cts +34 -0
- package/dist/index.cjs +571 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +537 -0
- package/dist/index.js.map +1 -0
- package/dist/interface-BzT_DC3u.d.cts +38 -0
- package/dist/interface-BzT_DC3u.d.ts +38 -0
- package/dist/token-BtfYGd9K.d.cts +33 -0
- package/dist/token-BtfYGd9K.d.ts +33 -0
- package/package.json +125 -3
- package/index.js +0 -1
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/adapters/postgres/index.ts
|
|
21
|
+
var postgres_exports = {};
|
|
22
|
+
__export(postgres_exports, {
|
|
23
|
+
postgresStorage: () => postgresStorage
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(postgres_exports);
|
|
26
|
+
|
|
27
|
+
// src/storage/postgres.ts
|
|
28
|
+
var import_kysely2 = require("kysely");
|
|
29
|
+
|
|
30
|
+
// src/storage/sql.ts
|
|
31
|
+
var import_kysely = require("kysely");
|
|
32
|
+
function createSqlStorage(database, dialect, options) {
|
|
33
|
+
const tables = resolveTables(options);
|
|
34
|
+
let schemaPromise = null;
|
|
35
|
+
async function ensureSchema() {
|
|
36
|
+
if (!schemaPromise) {
|
|
37
|
+
schemaPromise = createSchema(database, dialect, tables);
|
|
38
|
+
}
|
|
39
|
+
await schemaPromise;
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
async findClient(clientId) {
|
|
43
|
+
await ensureSchema();
|
|
44
|
+
const row = await database.selectFrom(tables.clients).selectAll().where("client_id", "=", clientId).executeTakeFirst();
|
|
45
|
+
return row ? mapClientRow(row) : null;
|
|
46
|
+
},
|
|
47
|
+
async createClient(client) {
|
|
48
|
+
await ensureSchema();
|
|
49
|
+
await database.insertInto(tables.clients).values({
|
|
50
|
+
id: client.id,
|
|
51
|
+
client_id: client.clientId,
|
|
52
|
+
client_secret: client.clientSecret,
|
|
53
|
+
name: client.name,
|
|
54
|
+
scopes: JSON.stringify(client.scopes),
|
|
55
|
+
created_at: client.createdAt.toISOString(),
|
|
56
|
+
last_seen_at: client.lastSeenAt?.toISOString() ?? null
|
|
57
|
+
}).execute();
|
|
58
|
+
},
|
|
59
|
+
async deleteClient(clientId) {
|
|
60
|
+
await ensureSchema();
|
|
61
|
+
await database.deleteFrom(tables.clients).where("client_id", "=", clientId).execute();
|
|
62
|
+
},
|
|
63
|
+
async listClients() {
|
|
64
|
+
await ensureSchema();
|
|
65
|
+
const rows = await database.selectFrom(tables.clients).selectAll().orderBy("created_at", "asc").execute();
|
|
66
|
+
return rows.map(mapClientRow);
|
|
67
|
+
},
|
|
68
|
+
async touchClient(clientId, lastSeenAt) {
|
|
69
|
+
await ensureSchema();
|
|
70
|
+
await database.updateTable(tables.clients).set({
|
|
71
|
+
last_seen_at: lastSeenAt.toISOString()
|
|
72
|
+
}).where("client_id", "=", clientId).execute();
|
|
73
|
+
},
|
|
74
|
+
async getSigningKeys() {
|
|
75
|
+
await ensureSchema();
|
|
76
|
+
const rows = await database.selectFrom(tables.signingKeys).selectAll().orderBy("created_at", "asc").execute();
|
|
77
|
+
return rows.map(mapSigningKeyRow);
|
|
78
|
+
},
|
|
79
|
+
async addSigningKey(key) {
|
|
80
|
+
await ensureSchema();
|
|
81
|
+
await database.insertInto(tables.signingKeys).values({
|
|
82
|
+
key_id: key.keyId,
|
|
83
|
+
algorithm: key.algorithm,
|
|
84
|
+
private_key: key.privateKey,
|
|
85
|
+
public_key: key.publicKey,
|
|
86
|
+
created_at: key.createdAt.toISOString(),
|
|
87
|
+
retired_at: key.retiredAt?.toISOString() ?? null
|
|
88
|
+
}).execute();
|
|
89
|
+
},
|
|
90
|
+
async retireKey(keyId) {
|
|
91
|
+
await ensureSchema();
|
|
92
|
+
await database.updateTable(tables.signingKeys).set({
|
|
93
|
+
retired_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
94
|
+
}).where("key_id", "=", keyId).execute();
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function resolveTables(options) {
|
|
99
|
+
const prefix = options?.tablePrefix ?? "trustline_";
|
|
100
|
+
return {
|
|
101
|
+
clients: options?.tables?.clients ?? `${prefix}clients`,
|
|
102
|
+
signingKeys: options?.tables?.signingKeys ?? `${prefix}signing_keys`
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async function createSchema(database, dialect, tables) {
|
|
106
|
+
await createClientsTable(database, dialect, tables.clients);
|
|
107
|
+
await createSigningKeysTable(database, dialect, tables.signingKeys);
|
|
108
|
+
}
|
|
109
|
+
async function createClientsTable(database, dialect, tableName) {
|
|
110
|
+
if (dialect === "mysql") {
|
|
111
|
+
await import_kysely.sql`
|
|
112
|
+
create table if not exists ${import_kysely.sql.table(tableName)} (
|
|
113
|
+
id varchar(255) not null,
|
|
114
|
+
client_id varchar(255) not null primary key,
|
|
115
|
+
client_secret text not null,
|
|
116
|
+
name varchar(255) not null,
|
|
117
|
+
scopes text not null,
|
|
118
|
+
created_at varchar(64) not null,
|
|
119
|
+
last_seen_at varchar(64) null
|
|
120
|
+
)
|
|
121
|
+
`.execute(database);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
await import_kysely.sql`
|
|
125
|
+
create table if not exists ${import_kysely.sql.table(tableName)} (
|
|
126
|
+
id varchar(255) not null,
|
|
127
|
+
client_id varchar(255) not null primary key,
|
|
128
|
+
client_secret text not null,
|
|
129
|
+
name varchar(255) not null,
|
|
130
|
+
scopes text not null,
|
|
131
|
+
created_at varchar(64) not null,
|
|
132
|
+
last_seen_at varchar(64)
|
|
133
|
+
)
|
|
134
|
+
`.execute(database);
|
|
135
|
+
}
|
|
136
|
+
async function createSigningKeysTable(database, dialect, tableName) {
|
|
137
|
+
if (dialect === "mysql") {
|
|
138
|
+
await import_kysely.sql`
|
|
139
|
+
create table if not exists ${import_kysely.sql.table(tableName)} (
|
|
140
|
+
key_id varchar(255) not null primary key,
|
|
141
|
+
algorithm varchar(32) not null,
|
|
142
|
+
private_key text not null,
|
|
143
|
+
public_key text not null,
|
|
144
|
+
created_at varchar(64) not null,
|
|
145
|
+
retired_at varchar(64) null
|
|
146
|
+
)
|
|
147
|
+
`.execute(database);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
await import_kysely.sql`
|
|
151
|
+
create table if not exists ${import_kysely.sql.table(tableName)} (
|
|
152
|
+
key_id varchar(255) not null primary key,
|
|
153
|
+
algorithm varchar(32) not null,
|
|
154
|
+
private_key text not null,
|
|
155
|
+
public_key text not null,
|
|
156
|
+
created_at varchar(64) not null,
|
|
157
|
+
retired_at varchar(64)
|
|
158
|
+
)
|
|
159
|
+
`.execute(database);
|
|
160
|
+
}
|
|
161
|
+
function mapClientRow(row) {
|
|
162
|
+
return {
|
|
163
|
+
id: row.id,
|
|
164
|
+
clientId: row.client_id,
|
|
165
|
+
clientSecret: row.client_secret,
|
|
166
|
+
name: row.name,
|
|
167
|
+
scopes: JSON.parse(row.scopes),
|
|
168
|
+
createdAt: new Date(row.created_at),
|
|
169
|
+
lastSeenAt: row.last_seen_at ? new Date(row.last_seen_at) : null
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function mapSigningKeyRow(row) {
|
|
173
|
+
return {
|
|
174
|
+
keyId: row.key_id,
|
|
175
|
+
algorithm: row.algorithm,
|
|
176
|
+
privateKey: row.private_key,
|
|
177
|
+
publicKey: row.public_key,
|
|
178
|
+
createdAt: new Date(row.created_at),
|
|
179
|
+
retiredAt: row.retired_at ? new Date(row.retired_at) : null
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// src/storage/postgres.ts
|
|
184
|
+
function postgresStorage(pool, options) {
|
|
185
|
+
return createSqlStorage(
|
|
186
|
+
new import_kysely2.Kysely({
|
|
187
|
+
dialect: new import_kysely2.PostgresDialect({
|
|
188
|
+
pool
|
|
189
|
+
})
|
|
190
|
+
}),
|
|
191
|
+
"postgres",
|
|
192
|
+
options
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
196
|
+
0 && (module.exports = {
|
|
197
|
+
postgresStorage
|
|
198
|
+
});
|
|
199
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/adapters/postgres/index.ts","../../../src/storage/postgres.ts","../../../src/storage/sql.ts"],"sourcesContent":["export { postgresStorage } from \"../../storage/postgres\";\nexport type { SqlStorageOptions, StorageAdapter } from \"../../storage/interface\";\n","import { Kysely, PostgresDialect } from \"kysely\";\nimport type { Pool } from \"pg\";\n\nimport type { SqlStorageOptions, StorageAdapter } from \"./interface\";\nimport { createSqlStorage } from \"./sql\";\n\nexport function postgresStorage(\n pool: Pool,\n options?: SqlStorageOptions,\n): StorageAdapter {\n return createSqlStorage(\n new Kysely({\n dialect: new PostgresDialect({\n pool,\n }),\n }),\n \"postgres\",\n options,\n );\n}\n","import { type Kysely, sql } from \"kysely\";\n\nimport type {\n ServiceClient,\n SigningKey,\n SqlStorageOptions,\n StorageAdapter,\n} from \"./interface\";\n\ntype SqlDialect = \"mysql\" | \"postgres\" | \"sqlite\";\n\ninterface SqlTables {\n clients: string;\n signingKeys: string;\n}\n\ninterface SqlDatabase {\n [tableName: string]: SqlRow;\n}\n\ninterface SqlRow {\n algorithm?: SigningKey[\"algorithm\"];\n client_id?: string;\n client_secret?: string;\n created_at: string;\n id?: string;\n key_id?: string;\n last_seen_at?: string | null;\n name?: string;\n private_key?: string;\n public_key?: string;\n retired_at?: string | null;\n scopes?: string;\n}\n\ninterface ClientRow {\n id: string;\n client_id: string;\n client_secret: string;\n name: string;\n scopes: string;\n created_at: string;\n last_seen_at: string | null;\n}\n\ninterface SigningKeyRow {\n key_id: string;\n algorithm: SigningKey[\"algorithm\"];\n private_key: string;\n public_key: string;\n created_at: string;\n retired_at: string | null;\n}\n\nexport function createSqlStorage(\n database: Kysely<SqlDatabase>,\n dialect: SqlDialect,\n options?: SqlStorageOptions,\n): StorageAdapter {\n const tables = resolveTables(options);\n let schemaPromise: Promise<void> | null = null;\n\n async function ensureSchema(): Promise<void> {\n if (!schemaPromise) {\n schemaPromise = createSchema(database, dialect, tables);\n }\n\n await schemaPromise;\n }\n\n return {\n async findClient(clientId) {\n await ensureSchema();\n\n const row = (await database\n .selectFrom(tables.clients)\n .selectAll()\n .where(\"client_id\", \"=\", clientId)\n .executeTakeFirst()) as ClientRow | undefined;\n\n return row ? mapClientRow(row) : null;\n },\n async createClient(client) {\n await ensureSchema();\n\n await database\n .insertInto(tables.clients)\n .values({\n id: client.id,\n client_id: client.clientId,\n client_secret: client.clientSecret,\n name: client.name,\n scopes: JSON.stringify(client.scopes),\n created_at: client.createdAt.toISOString(),\n last_seen_at: client.lastSeenAt?.toISOString() ?? null,\n })\n .execute();\n },\n async deleteClient(clientId) {\n await ensureSchema();\n\n await database\n .deleteFrom(tables.clients)\n .where(\"client_id\", \"=\", clientId)\n .execute();\n },\n async listClients() {\n await ensureSchema();\n\n const rows = (await database\n .selectFrom(tables.clients)\n .selectAll()\n .orderBy(\"created_at\", \"asc\")\n .execute()) as ClientRow[];\n\n return rows.map(mapClientRow);\n },\n async touchClient(clientId, lastSeenAt) {\n await ensureSchema();\n\n await database\n .updateTable(tables.clients)\n .set({\n last_seen_at: lastSeenAt.toISOString(),\n })\n .where(\"client_id\", \"=\", clientId)\n .execute();\n },\n async getSigningKeys() {\n await ensureSchema();\n\n const rows = (await database\n .selectFrom(tables.signingKeys)\n .selectAll()\n .orderBy(\"created_at\", \"asc\")\n .execute()) as SigningKeyRow[];\n\n return rows.map(mapSigningKeyRow);\n },\n async addSigningKey(key) {\n await ensureSchema();\n\n await database\n .insertInto(tables.signingKeys)\n .values({\n key_id: key.keyId,\n algorithm: key.algorithm,\n private_key: key.privateKey,\n public_key: key.publicKey,\n created_at: key.createdAt.toISOString(),\n retired_at: key.retiredAt?.toISOString() ?? null,\n })\n .execute();\n },\n async retireKey(keyId) {\n await ensureSchema();\n\n await database\n .updateTable(tables.signingKeys)\n .set({\n retired_at: new Date().toISOString(),\n })\n .where(\"key_id\", \"=\", keyId)\n .execute();\n },\n };\n}\n\nfunction resolveTables(options?: SqlStorageOptions): SqlTables {\n const prefix = options?.tablePrefix ?? \"trustline_\";\n\n return {\n clients: options?.tables?.clients ?? `${prefix}clients`,\n signingKeys: options?.tables?.signingKeys ?? `${prefix}signing_keys`,\n };\n}\n\nasync function createSchema(\n database: Kysely<SqlDatabase>,\n dialect: SqlDialect,\n tables: SqlTables,\n): Promise<void> {\n await createClientsTable(database, dialect, tables.clients);\n await createSigningKeysTable(database, dialect, tables.signingKeys);\n}\n\nasync function createClientsTable(\n database: Kysely<SqlDatabase>,\n dialect: SqlDialect,\n tableName: string,\n): Promise<void> {\n if (dialect === \"mysql\") {\n await sql`\n create table if not exists ${sql.table(tableName)} (\n id varchar(255) not null,\n client_id varchar(255) not null primary key,\n client_secret text not null,\n name varchar(255) not null,\n scopes text not null,\n created_at varchar(64) not null,\n last_seen_at varchar(64) null\n )\n `.execute(database);\n return;\n }\n\n await sql`\n create table if not exists ${sql.table(tableName)} (\n id varchar(255) not null,\n client_id varchar(255) not null primary key,\n client_secret text not null,\n name varchar(255) not null,\n scopes text not null,\n created_at varchar(64) not null,\n last_seen_at varchar(64)\n )\n `.execute(database);\n}\n\nasync function createSigningKeysTable(\n database: Kysely<SqlDatabase>,\n dialect: SqlDialect,\n tableName: string,\n): Promise<void> {\n if (dialect === \"mysql\") {\n await sql`\n create table if not exists ${sql.table(tableName)} (\n key_id varchar(255) not null primary key,\n algorithm varchar(32) not null,\n private_key text not null,\n public_key text not null,\n created_at varchar(64) not null,\n retired_at varchar(64) null\n )\n `.execute(database);\n return;\n }\n\n await sql`\n create table if not exists ${sql.table(tableName)} (\n key_id varchar(255) not null primary key,\n algorithm varchar(32) not null,\n private_key text not null,\n public_key text not null,\n created_at varchar(64) not null,\n retired_at varchar(64)\n )\n `.execute(database);\n}\n\nfunction mapClientRow(row: ClientRow): ServiceClient {\n return {\n id: row.id,\n clientId: row.client_id,\n clientSecret: row.client_secret,\n name: row.name,\n scopes: JSON.parse(row.scopes) as string[],\n createdAt: new Date(row.created_at),\n lastSeenAt: row.last_seen_at ? new Date(row.last_seen_at) : null,\n };\n}\n\nfunction mapSigningKeyRow(row: SigningKeyRow): SigningKey {\n return {\n keyId: row.key_id,\n algorithm: row.algorithm,\n privateKey: row.private_key,\n publicKey: row.public_key,\n createdAt: new Date(row.created_at),\n retiredAt: row.retired_at ? new Date(row.retired_at) : null,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,iBAAwC;;;ACAxC,oBAAiC;AAsD1B,SAAS,iBACd,UACA,SACA,SACgB;AAChB,QAAM,SAAS,cAAc,OAAO;AACpC,MAAI,gBAAsC;AAE1C,iBAAe,eAA8B;AAC3C,QAAI,CAAC,eAAe;AAClB,sBAAgB,aAAa,UAAU,SAAS,MAAM;AAAA,IACxD;AAEA,UAAM;AAAA,EACR;AAEA,SAAO;AAAA,IACL,MAAM,WAAW,UAAU;AACzB,YAAM,aAAa;AAEnB,YAAM,MAAO,MAAM,SAChB,WAAW,OAAO,OAAO,EACzB,UAAU,EACV,MAAM,aAAa,KAAK,QAAQ,EAChC,iBAAiB;AAEpB,aAAO,MAAM,aAAa,GAAG,IAAI;AAAA,IACnC;AAAA,IACA,MAAM,aAAa,QAAQ;AACzB,YAAM,aAAa;AAEnB,YAAM,SACH,WAAW,OAAO,OAAO,EACzB,OAAO;AAAA,QACN,IAAI,OAAO;AAAA,QACX,WAAW,OAAO;AAAA,QAClB,eAAe,OAAO;AAAA,QACtB,MAAM,OAAO;AAAA,QACb,QAAQ,KAAK,UAAU,OAAO,MAAM;AAAA,QACpC,YAAY,OAAO,UAAU,YAAY;AAAA,QACzC,cAAc,OAAO,YAAY,YAAY,KAAK;AAAA,MACpD,CAAC,EACA,QAAQ;AAAA,IACb;AAAA,IACA,MAAM,aAAa,UAAU;AAC3B,YAAM,aAAa;AAEnB,YAAM,SACH,WAAW,OAAO,OAAO,EACzB,MAAM,aAAa,KAAK,QAAQ,EAChC,QAAQ;AAAA,IACb;AAAA,IACA,MAAM,cAAc;AAClB,YAAM,aAAa;AAEnB,YAAM,OAAQ,MAAM,SACjB,WAAW,OAAO,OAAO,EACzB,UAAU,EACV,QAAQ,cAAc,KAAK,E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AU,IAAI;AAAA,EACzD;AACF;;;ADzQO,SAAS,gBACd,MACA,SACgB;AAChB,SAAO;AAAA,IACL,IAAI,sBAAO;AAAA,MACT,SAAS,IAAI,+BAAgB;AAAA,QAC3B;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAAA,IACD;AAAA,IACA;AAAA,EACF;AACF;","names":["import_kysely"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Pool } from 'pg';
|
|
2
|
+
import { S as SqlStorageOptions, a as StorageAdapter } from '../../interface-BzT_DC3u.cjs';
|
|
3
|
+
|
|
4
|
+
declare function postgresStorage(pool: Pool, options?: SqlStorageOptions): StorageAdapter;
|
|
5
|
+
|
|
6
|
+
export { SqlStorageOptions, StorageAdapter, postgresStorage };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Pool } from 'pg';
|
|
2
|
+
import { S as SqlStorageOptions, a as StorageAdapter } from '../../interface-BzT_DC3u.js';
|
|
3
|
+
|
|
4
|
+
declare function postgresStorage(pool: Pool, options?: SqlStorageOptions): StorageAdapter;
|
|
5
|
+
|
|
6
|
+
export { SqlStorageOptions, StorageAdapter, postgresStorage };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSqlStorage
|
|
3
|
+
} from "../../chunk-CTPFKR4O.js";
|
|
4
|
+
|
|
5
|
+
// src/storage/postgres.ts
|
|
6
|
+
import { Kysely, PostgresDialect } from "kysely";
|
|
7
|
+
function postgresStorage(pool, options) {
|
|
8
|
+
return createSqlStorage(
|
|
9
|
+
new Kysely({
|
|
10
|
+
dialect: new PostgresDialect({
|
|
11
|
+
pool
|
|
12
|
+
})
|
|
13
|
+
}),
|
|
14
|
+
"postgres",
|
|
15
|
+
options
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
export {
|
|
19
|
+
postgresStorage
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/storage/postgres.ts"],"sourcesContent":["import { Kysely, PostgresDialect } from \"kysely\";\nimport type { Pool } from \"pg\";\n\nimport type { SqlStorageOptions, StorageAdapter } from \"./interface\";\nimport { createSqlStorage } from \"./sql\";\n\nexport function postgresStorage(\n pool: Pool,\n options?: SqlStorageOptions,\n): StorageAdapter {\n return createSqlStorage(\n new Kysely({\n dialect: new PostgresDialect({\n pool,\n }),\n }),\n \"postgres\",\n options,\n );\n}\n"],"mappings":";;;;;AAAA,SAAS,QAAQ,uBAAuB;AAMjC,SAAS,gBACd,MACA,SACgB;AAChB,SAAO;AAAA,IACL,IAAI,OAAO;AAAA,MACT,SAAS,IAAI,gBAAgB;AAAA,QAC3B;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAAA,IACD;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/adapters/sqlite/index.ts
|
|
31
|
+
var sqlite_exports = {};
|
|
32
|
+
__export(sqlite_exports, {
|
|
33
|
+
sqliteStorage: () => sqliteStorage
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(sqlite_exports);
|
|
36
|
+
|
|
37
|
+
// src/storage/sqlite.ts
|
|
38
|
+
var import_better_sqlite3 = __toESM(require("better-sqlite3"), 1);
|
|
39
|
+
var import_kysely2 = require("kysely");
|
|
40
|
+
|
|
41
|
+
// src/storage/sql.ts
|
|
42
|
+
var import_kysely = require("kysely");
|
|
43
|
+
function createSqlStorage(database, dialect, options) {
|
|
44
|
+
const tables = resolveTables(options);
|
|
45
|
+
let schemaPromise = null;
|
|
46
|
+
async function ensureSchema() {
|
|
47
|
+
if (!schemaPromise) {
|
|
48
|
+
schemaPromise = createSchema(database, dialect, tables);
|
|
49
|
+
}
|
|
50
|
+
await schemaPromise;
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
async findClient(clientId) {
|
|
54
|
+
await ensureSchema();
|
|
55
|
+
const row = await database.selectFrom(tables.clients).selectAll().where("client_id", "=", clientId).executeTakeFirst();
|
|
56
|
+
return row ? mapClientRow(row) : null;
|
|
57
|
+
},
|
|
58
|
+
async createClient(client) {
|
|
59
|
+
await ensureSchema();
|
|
60
|
+
await database.insertInto(tables.clients).values({
|
|
61
|
+
id: client.id,
|
|
62
|
+
client_id: client.clientId,
|
|
63
|
+
client_secret: client.clientSecret,
|
|
64
|
+
name: client.name,
|
|
65
|
+
scopes: JSON.stringify(client.scopes),
|
|
66
|
+
created_at: client.createdAt.toISOString(),
|
|
67
|
+
last_seen_at: client.lastSeenAt?.toISOString() ?? null
|
|
68
|
+
}).execute();
|
|
69
|
+
},
|
|
70
|
+
async deleteClient(clientId) {
|
|
71
|
+
await ensureSchema();
|
|
72
|
+
await database.deleteFrom(tables.clients).where("client_id", "=", clientId).execute();
|
|
73
|
+
},
|
|
74
|
+
async listClients() {
|
|
75
|
+
await ensureSchema();
|
|
76
|
+
const rows = await database.selectFrom(tables.clients).selectAll().orderBy("created_at", "asc").execute();
|
|
77
|
+
return rows.map(mapClientRow);
|
|
78
|
+
},
|
|
79
|
+
async touchClient(clientId, lastSeenAt) {
|
|
80
|
+
await ensureSchema();
|
|
81
|
+
await database.updateTable(tables.clients).set({
|
|
82
|
+
last_seen_at: lastSeenAt.toISOString()
|
|
83
|
+
}).where("client_id", "=", clientId).execute();
|
|
84
|
+
},
|
|
85
|
+
async getSigningKeys() {
|
|
86
|
+
await ensureSchema();
|
|
87
|
+
const rows = await database.selectFrom(tables.signingKeys).selectAll().orderBy("created_at", "asc").execute();
|
|
88
|
+
return rows.map(mapSigningKeyRow);
|
|
89
|
+
},
|
|
90
|
+
async addSigningKey(key) {
|
|
91
|
+
await ensureSchema();
|
|
92
|
+
await database.insertInto(tables.signingKeys).values({
|
|
93
|
+
key_id: key.keyId,
|
|
94
|
+
algorithm: key.algorithm,
|
|
95
|
+
private_key: key.privateKey,
|
|
96
|
+
public_key: key.publicKey,
|
|
97
|
+
created_at: key.createdAt.toISOString(),
|
|
98
|
+
retired_at: key.retiredAt?.toISOString() ?? null
|
|
99
|
+
}).execute();
|
|
100
|
+
},
|
|
101
|
+
async retireKey(keyId) {
|
|
102
|
+
await ensureSchema();
|
|
103
|
+
await database.updateTable(tables.signingKeys).set({
|
|
104
|
+
retired_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
105
|
+
}).where("key_id", "=", keyId).execute();
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function resolveTables(options) {
|
|
110
|
+
const prefix = options?.tablePrefix ?? "trustline_";
|
|
111
|
+
return {
|
|
112
|
+
clients: options?.tables?.clients ?? `${prefix}clients`,
|
|
113
|
+
signingKeys: options?.tables?.signingKeys ?? `${prefix}signing_keys`
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
async function createSchema(database, dialect, tables) {
|
|
117
|
+
await createClientsTable(database, dialect, tables.clients);
|
|
118
|
+
await createSigningKeysTable(database, dialect, tables.signingKeys);
|
|
119
|
+
}
|
|
120
|
+
async function createClientsTable(database, dialect, tableName) {
|
|
121
|
+
if (dialect === "mysql") {
|
|
122
|
+
await import_kysely.sql`
|
|
123
|
+
create table if not exists ${import_kysely.sql.table(tableName)} (
|
|
124
|
+
id varchar(255) not null,
|
|
125
|
+
client_id varchar(255) not null primary key,
|
|
126
|
+
client_secret text not null,
|
|
127
|
+
name varchar(255) not null,
|
|
128
|
+
scopes text not null,
|
|
129
|
+
created_at varchar(64) not null,
|
|
130
|
+
last_seen_at varchar(64) null
|
|
131
|
+
)
|
|
132
|
+
`.execute(database);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
await import_kysely.sql`
|
|
136
|
+
create table if not exists ${import_kysely.sql.table(tableName)} (
|
|
137
|
+
id varchar(255) not null,
|
|
138
|
+
client_id varchar(255) not null primary key,
|
|
139
|
+
client_secret text not null,
|
|
140
|
+
name varchar(255) not null,
|
|
141
|
+
scopes text not null,
|
|
142
|
+
created_at varchar(64) not null,
|
|
143
|
+
last_seen_at varchar(64)
|
|
144
|
+
)
|
|
145
|
+
`.execute(database);
|
|
146
|
+
}
|
|
147
|
+
async function createSigningKeysTable(database, dialect, tableName) {
|
|
148
|
+
if (dialect === "mysql") {
|
|
149
|
+
await import_kysely.sql`
|
|
150
|
+
create table if not exists ${import_kysely.sql.table(tableName)} (
|
|
151
|
+
key_id varchar(255) not null primary key,
|
|
152
|
+
algorithm varchar(32) not null,
|
|
153
|
+
private_key text not null,
|
|
154
|
+
public_key text not null,
|
|
155
|
+
created_at varchar(64) not null,
|
|
156
|
+
retired_at varchar(64) null
|
|
157
|
+
)
|
|
158
|
+
`.execute(database);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
await import_kysely.sql`
|
|
162
|
+
create table if not exists ${import_kysely.sql.table(tableName)} (
|
|
163
|
+
key_id varchar(255) not null primary key,
|
|
164
|
+
algorithm varchar(32) not null,
|
|
165
|
+
private_key text not null,
|
|
166
|
+
public_key text not null,
|
|
167
|
+
created_at varchar(64) not null,
|
|
168
|
+
retired_at varchar(64)
|
|
169
|
+
)
|
|
170
|
+
`.execute(database);
|
|
171
|
+
}
|
|
172
|
+
function mapClientRow(row) {
|
|
173
|
+
return {
|
|
174
|
+
id: row.id,
|
|
175
|
+
clientId: row.client_id,
|
|
176
|
+
clientSecret: row.client_secret,
|
|
177
|
+
name: row.name,
|
|
178
|
+
scopes: JSON.parse(row.scopes),
|
|
179
|
+
createdAt: new Date(row.created_at),
|
|
180
|
+
lastSeenAt: row.last_seen_at ? new Date(row.last_seen_at) : null
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
function mapSigningKeyRow(row) {
|
|
184
|
+
return {
|
|
185
|
+
keyId: row.key_id,
|
|
186
|
+
algorithm: row.algorithm,
|
|
187
|
+
privateKey: row.private_key,
|
|
188
|
+
publicKey: row.public_key,
|
|
189
|
+
createdAt: new Date(row.created_at),
|
|
190
|
+
retiredAt: row.retired_at ? new Date(row.retired_at) : null
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// src/storage/sqlite.ts
|
|
195
|
+
function sqliteStorage(pathOrDatabase, options) {
|
|
196
|
+
const database = typeof pathOrDatabase === "string" ? createSqliteDatabase(pathOrDatabase) : pathOrDatabase;
|
|
197
|
+
return createSqlStorage(
|
|
198
|
+
new import_kysely2.Kysely({
|
|
199
|
+
dialect: new import_kysely2.SqliteDialect({
|
|
200
|
+
database
|
|
201
|
+
})
|
|
202
|
+
}),
|
|
203
|
+
"sqlite",
|
|
204
|
+
options
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
function createSqliteDatabase(path) {
|
|
208
|
+
const database = new import_better_sqlite3.default(path);
|
|
209
|
+
database.pragma("journal_mode = WAL");
|
|
210
|
+
return database;
|
|
211
|
+
}
|
|
212
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
213
|
+
0 && (module.exports = {
|
|
214
|
+
sqliteStorage
|
|
215
|
+
});
|
|
216
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/adapters/sqlite/index.ts","../../../src/storage/sqlite.ts","../../../src/storage/sql.ts"],"sourcesContent":["export { sqliteStorage } from \"../../storage/sqlite\";\nexport type { SqlStorageOptions, StorageAdapter } from \"../../storage/interface\";\n","import BetterSqlite3 from \"better-sqlite3\";\nimport { Kysely, SqliteDialect } from \"kysely\";\n\nimport type { SqlStorageOptions, StorageAdapter } from \"./interface\";\nimport { createSqlStorage } from \"./sql\";\n\nexport function sqliteStorage(\n pathOrDatabase: string | BetterSqlite3.Database,\n options?: SqlStorageOptions,\n): StorageAdapter {\n const database =\n typeof pathOrDatabase === \"string\"\n ? createSqliteDatabase(pathOrDatabase)\n : pathOrDatabase;\n\n return createSqlStorage(\n new Kysely({\n dialect: new SqliteDialect({\n database,\n }),\n }),\n \"sqlite\",\n options,\n );\n}\n\nfunction createSqliteDatabase(path: string): BetterSqlite3.Database {\n const database = new BetterSqlite3(path);\n database.pragma(\"journal_mode = WAL\");\n return database;\n}\n","import { type Kysely, sql } from \"kysely\";\n\nimport type {\n ServiceClient,\n SigningKey,\n SqlStorageOptions,\n StorageAdapter,\n} from \"./interface\";\n\ntype SqlDialect = \"mysql\" | \"postgres\" | \"sqlite\";\n\ninterface SqlTables {\n clients: string;\n signingKeys: string;\n}\n\ninterface SqlDatabase {\n [tableName: string]: SqlRow;\n}\n\ninterface SqlRow {\n algorithm?: SigningKey[\"algorithm\"];\n client_id?: string;\n client_secret?: string;\n created_at: string;\n id?: string;\n key_id?: string;\n last_seen_at?: string | null;\n name?: string;\n private_key?: string;\n public_key?: string;\n retired_at?: string | null;\n scopes?: string;\n}\n\ninterface ClientRow {\n id: string;\n client_id: string;\n client_secret: string;\n name: string;\n scopes: string;\n created_at: string;\n last_seen_at: string | null;\n}\n\ninterface SigningKeyRow {\n key_id: string;\n algorithm: SigningKey[\"algorithm\"];\n private_key: string;\n public_key: string;\n created_at: string;\n retired_at: string | null;\n}\n\nexport function createSqlStorage(\n database: Kysely<SqlDatabase>,\n dialect: SqlDialect,\n options?: SqlStorageOptions,\n): StorageAdapter {\n const tables = resolveTables(options);\n let schemaPromise: Promise<void> | null = null;\n\n async function ensureSchema(): Promise<void> {\n if (!schemaPromise) {\n schemaPromise = createSchema(database, dialect, tables);\n }\n\n await schemaPromise;\n }\n\n return {\n async findClient(clientId) {\n await ensureSchema();\n\n const row = (await database\n .selectFrom(tables.clients)\n .selectAll()\n .where(\"client_id\", \"=\", clientId)\n .executeTakeFirst()) as ClientRow | undefined;\n\n return row ? mapClientRow(row) : null;\n },\n async createClient(client) {\n await ensureSchema();\n\n await database\n .insertInto(tables.clients)\n .values({\n id: client.id,\n client_id: client.clientId,\n client_secret: client.clientSecret,\n name: client.name,\n scopes: JSON.stringify(client.scopes),\n created_at: client.createdAt.toISOString(),\n last_seen_at: client.lastSeenAt?.toISOString() ?? null,\n })\n .execute();\n },\n async deleteClient(clientId) {\n await ensureSchema();\n\n await database\n .deleteFrom(tables.clients)\n .where(\"client_id\", \"=\", clientId)\n .execute();\n },\n async listClients() {\n await ensureSchema();\n\n const rows = (await database\n .selectFrom(tables.clients)\n .selectAll()\n .orderBy(\"created_at\", \"asc\")\n .execute()) as ClientRow[];\n\n return rows.map(mapClientRow);\n },\n async touchClient(clientId, lastSeenAt) {\n await ensureSchema();\n\n await database\n .updateTable(tables.clients)\n .set({\n last_seen_at: lastSeenAt.toISOString(),\n })\n .where(\"client_id\", \"=\", clientId)\n .execute();\n },\n async getSigningKeys() {\n await ensureSchema();\n\n const rows = (await database\n .selectFrom(tables.signingKeys)\n .selectAll()\n .orderBy(\"created_at\", \"asc\")\n .execute()) as SigningKeyRow[];\n\n return rows.map(mapSigningKeyRow);\n },\n async addSigningKey(key) {\n await ensureSchema();\n\n await database\n .insertInto(tables.signingKeys)\n .values({\n key_id: key.keyId,\n algorithm: key.algorithm,\n private_key: key.privateKey,\n public_key: key.publicKey,\n created_at: key.createdAt.toISOString(),\n retired_at: key.retiredAt?.toISOString() ?? null,\n })\n .execute();\n },\n async retireKey(keyId) {\n await ensureSchema();\n\n await database\n .updateTable(tables.signingKeys)\n .set({\n retired_at: new Date().toISOString(),\n })\n .where(\"key_id\", \"=\", keyId)\n .execute();\n },\n };\n}\n\nfunction resolveTables(options?: SqlStorageOptions): SqlTables {\n const prefix = options?.tablePrefix ?? \"trustline_\";\n\n return {\n clients: options?.tables?.clients ?? `${prefix}clients`,\n signingKeys: options?.tables?.signingKeys ?? `${prefix}signing_keys`,\n };\n}\n\nasync function createSchema(\n database: Kysely<SqlDatabase>,\n dialect: SqlDialect,\n tables: SqlTables,\n): Promise<void> {\n await createClientsTable(database, dialect, tables.clients);\n await createSigningKeysTable(database, dialect, tables.signingKeys);\n}\n\nasync function createClientsTable(\n database: Kysely<SqlDatabase>,\n dialect: SqlDialect,\n tableName: string,\n): Promise<void> {\n if (dialect === \"mysql\") {\n await sql`\n create table if not exists ${sql.table(tableName)} (\n id varchar(255) not null,\n client_id varchar(255) not null primary key,\n client_secret text not null,\n name varchar(255) not null,\n scopes text not null,\n created_at varchar(64) not null,\n last_seen_at varchar(64) null\n )\n `.execute(database);\n return;\n }\n\n await sql`\n create table if not exists ${sql.table(tableName)} (\n id varchar(255) not null,\n client_id varchar(255) not null primary key,\n client_secret text not null,\n name varchar(255) not null,\n scopes text not null,\n created_at varchar(64) not null,\n last_seen_at varchar(64)\n )\n `.execute(database);\n}\n\nasync function createSigningKeysTable(\n database: Kysely<SqlDatabase>,\n dialect: SqlDialect,\n tableName: string,\n): Promise<void> {\n if (dialect === \"mysql\") {\n await sql`\n create table if not exists ${sql.table(tableName)} (\n key_id varchar(255) not null primary key,\n algorithm varchar(32) not null,\n private_key text not null,\n public_key text not null,\n created_at varchar(64) not null,\n retired_at varchar(64) null\n )\n `.execute(database);\n return;\n }\n\n await sql`\n create table if not exists ${sql.table(tableName)} (\n key_id varchar(255) not null primary key,\n algorithm varchar(32) not null,\n private_key text not null,\n public_key text not null,\n created_at varchar(64) not null,\n retired_at varchar(64)\n )\n `.execute(database);\n}\n\nfunction mapClientRow(row: ClientRow): ServiceClient {\n return {\n id: row.id,\n clientId: row.client_id,\n clientSecret: row.client_secret,\n name: row.name,\n scopes: JSON.parse(row.scopes) as string[],\n createdAt: new Date(row.created_at),\n lastSeenAt: row.last_seen_at ? new Date(row.last_seen_at) : null,\n };\n}\n\nfunction mapSigningKeyRow(row: SigningKeyRow): SigningKey {\n return {\n keyId: row.key_id,\n algorithm: row.algorithm,\n privateKey: row.private_key,\n publicKey: row.public_key,\n createdAt: new Date(row.created_at),\n retiredAt: row.retired_at ? new Date(row.retired_at) : null,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,4BAA0B;AAC1B,IAAAA,iBAAsC;;;ACDtC,oBAAiC;AAsD1B,SAAS,iBACd,UACA,SACA,SACgB;AAChB,QAAM,SAAS,cAAc,OAAO;AACpC,MAAI,gBAAsC;AAE1C,iBAAe,eAA8B;AAC3C,QAAI,CAAC,eAAe;AAClB,sBAAgB,aAAa,UAAU,SAAS,MAAM;AAAA,IACxD;AAEA,UAAM;AAAA,EACR;AAEA,SAAO;AAAA,IACL,M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 +1,6 @@
|
|
|
1
|
+
import BetterSqlite3 from 'better-sqlite3';
|
|
2
|
+
import { S as SqlStorageOptions, a as StorageAdapter } from '../../interface-BzT_DC3u.cjs';
|
|
3
|
+
|
|
4
|
+
declare function sqliteStorage(pathOrDatabase: string | BetterSqlite3.Database, options?: SqlStorageOptions): StorageAdapter;
|
|
5
|
+
|
|
6
|
+
export { SqlStorageOptions, StorageAdapter, sqliteStorage };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import BetterSqlite3 from 'better-sqlite3';
|
|
2
|
+
import { S as SqlStorageOptions, a as StorageAdapter } from '../../interface-BzT_DC3u.js';
|
|
3
|
+
|
|
4
|
+
declare function sqliteStorage(pathOrDatabase: string | BetterSqlite3.Database, options?: SqlStorageOptions): StorageAdapter;
|
|
5
|
+
|
|
6
|
+
export { SqlStorageOptions, StorageAdapter, sqliteStorage };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSqlStorage
|
|
3
|
+
} from "../../chunk-CTPFKR4O.js";
|
|
4
|
+
|
|
5
|
+
// src/storage/sqlite.ts
|
|
6
|
+
import BetterSqlite3 from "better-sqlite3";
|
|
7
|
+
import { Kysely, SqliteDialect } from "kysely";
|
|
8
|
+
function sqliteStorage(pathOrDatabase, options) {
|
|
9
|
+
const database = typeof pathOrDatabase === "string" ? createSqliteDatabase(pathOrDatabase) : pathOrDatabase;
|
|
10
|
+
return createSqlStorage(
|
|
11
|
+
new Kysely({
|
|
12
|
+
dialect: new SqliteDialect({
|
|
13
|
+
database
|
|
14
|
+
})
|
|
15
|
+
}),
|
|
16
|
+
"sqlite",
|
|
17
|
+
options
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
function createSqliteDatabase(path) {
|
|
21
|
+
const database = new BetterSqlite3(path);
|
|
22
|
+
database.pragma("journal_mode = WAL");
|
|
23
|
+
return database;
|
|
24
|
+
}
|
|
25
|
+
export {
|
|
26
|
+
sqliteStorage
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/storage/sqlite.ts"],"sourcesContent":["import BetterSqlite3 from \"better-sqlite3\";\nimport { Kysely, SqliteDialect } from \"kysely\";\n\nimport type { SqlStorageOptions, StorageAdapter } from \"./interface\";\nimport { createSqlStorage } from \"./sql\";\n\nexport function sqliteStorage(\n pathOrDatabase: string | BetterSqlite3.Database,\n options?: SqlStorageOptions,\n): StorageAdapter {\n const database =\n typeof pathOrDatabase === \"string\"\n ? createSqliteDatabase(pathOrDatabase)\n : pathOrDatabase;\n\n return createSqlStorage(\n new Kysely({\n dialect: new SqliteDialect({\n database,\n }),\n }),\n \"sqlite\",\n options,\n );\n}\n\nfunction createSqliteDatabase(path: string): BetterSqlite3.Database {\n const database = new BetterSqlite3(path);\n database.pragma(\"journal_mode = WAL\");\n return database;\n}\n"],"mappings":";;;;;AAAA,OAAO,mBAAmB;AAC1B,SAAS,QAAQ,qBAAqB;AAK/B,SAAS,cACd,gBACA,SACgB;AAChB,QAAM,WACJ,OAAO,mBAAmB,WACtB,qBAAqB,cAAc,IACnC;AAEN,SAAO;AAAA,IACL,IAAI,OAAO;AAAA,MACT,SAAS,IAAI,cAAc;AAAA,QACzB;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAAA,IACD;AAAA,IACA;AAAA,EACF;AACF;AAEA,SAAS,qBAAqB,MAAsC;AAClE,QAAM,WAAW,IAAI,cAAc,IAAI;AACvC,WAAS,OAAO,oBAAoB;AACpC,SAAO;AACT;","names":[]}
|