@ocap/indexdb-sqlite 1.29.5

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.
Files changed (42) hide show
  1. package/README.md +84 -0
  2. package/esm/_virtual/rolldown_runtime.mjs +21 -0
  3. package/esm/db/base.d.mts +76 -0
  4. package/esm/db/base.mjs +666 -0
  5. package/esm/db/index.d.mts +80 -0
  6. package/esm/db/index.mjs +551 -0
  7. package/esm/index.d.mts +3 -0
  8. package/esm/index.mjs +8 -0
  9. package/esm/interfaces.d.mts +328 -0
  10. package/esm/interfaces.mjs +1 -0
  11. package/esm/kysely.d.mts +43 -0
  12. package/esm/kysely.mjs +62 -0
  13. package/esm/migrations/001-genesis.d.mts +14 -0
  14. package/esm/migrations/001-genesis.mjs +107 -0
  15. package/esm/migrations/index.d.mts +16 -0
  16. package/esm/migrations/index.mjs +44 -0
  17. package/esm/package.mjs +70 -0
  18. package/esm/table/base.d.mts +107 -0
  19. package/esm/table/base.mjs +262 -0
  20. package/esm/table/transaction.d.mts +18 -0
  21. package/esm/table/transaction.mjs +22 -0
  22. package/lib/_virtual/rolldown_runtime.cjs +43 -0
  23. package/lib/db/base.cjs +670 -0
  24. package/lib/db/base.d.cts +76 -0
  25. package/lib/db/index.cjs +552 -0
  26. package/lib/db/index.d.cts +80 -0
  27. package/lib/index.cjs +10 -0
  28. package/lib/index.d.cts +3 -0
  29. package/lib/interfaces.cjs +0 -0
  30. package/lib/interfaces.d.cts +328 -0
  31. package/lib/kysely.cjs +63 -0
  32. package/lib/kysely.d.cts +43 -0
  33. package/lib/migrations/001-genesis.cjs +114 -0
  34. package/lib/migrations/001-genesis.d.cts +14 -0
  35. package/lib/migrations/index.cjs +46 -0
  36. package/lib/migrations/index.d.cts +16 -0
  37. package/lib/package.cjs +76 -0
  38. package/lib/table/base.cjs +265 -0
  39. package/lib/table/base.d.cts +107 -0
  40. package/lib/table/transaction.cjs +24 -0
  41. package/lib/table/transaction.d.cts +18 -0
  42. package/package.json +75 -0
@@ -0,0 +1,328 @@
1
+ import { Transaction } from "kysely";
2
+
3
+ //#region src/interfaces.d.ts
4
+
5
+ /**
6
+ * Database schema types for Kysely IndexDB
7
+ * Each table is defined with its column types
8
+ */
9
+ interface Database {
10
+ tx: TxTable;
11
+ account: AccountTable;
12
+ asset: AssetTable;
13
+ token: TokenTable;
14
+ factory: FactoryTable;
15
+ stake: StakeTable;
16
+ delegation: DelegationTable;
17
+ rollup: RollupTable;
18
+ rollupBlock: RollupBlockTable;
19
+ rollupValidator: RollupValidatorTable;
20
+ tokenDistribution: TokenDistributionTable;
21
+ tokenFactory: TokenFactoryTable;
22
+ balance: BalanceTable;
23
+ }
24
+ /**
25
+ * Transaction table for indexed transactions
26
+ */
27
+ interface TxTable {
28
+ hash: string;
29
+ code: string | null;
30
+ height: number | null;
31
+ index_: number | null;
32
+ receipts: string | null;
33
+ receiptsVerified: number | null;
34
+ receiver: string | null;
35
+ sender: string | null;
36
+ time: string | null;
37
+ tx: string | null;
38
+ type: string | null;
39
+ finalized: number | null;
40
+ valid: number | null;
41
+ accounts: string | null;
42
+ assets: string | null;
43
+ tokens: string | null;
44
+ tokenSymbols: string | null;
45
+ factories: string | null;
46
+ rollups: string | null;
47
+ stakes: string | null;
48
+ delegations: string | null;
49
+ tokenFactories: string | null;
50
+ genesisTime: string | null;
51
+ renaissanceTime: string | null;
52
+ data: string | null;
53
+ }
54
+ /**
55
+ * Account table for indexed accounts
56
+ */
57
+ interface AccountTable {
58
+ address: string;
59
+ balance: string;
60
+ balanceNum: string | null;
61
+ gasBalance: string | null;
62
+ moniker: string | null;
63
+ pk: string | null;
64
+ nonce: number | null;
65
+ numTxs: number | null;
66
+ numAssets: number | null;
67
+ issuer: string | null;
68
+ migratedTo: string | null;
69
+ migratedFrom: string | null;
70
+ stake: string | null;
71
+ tokens: string | null;
72
+ data: string | null;
73
+ genesisTime: string | null;
74
+ renaissanceTime: string | null;
75
+ }
76
+ /**
77
+ * Asset table for indexed assets
78
+ */
79
+ interface AssetTable {
80
+ address: string;
81
+ owner: string | null;
82
+ moniker: string | null;
83
+ readonly: number | null;
84
+ transferrable: number | null;
85
+ issuer: string | null;
86
+ parent: string | null;
87
+ ttl: number | null;
88
+ consumedTime: string | null;
89
+ endpoint: string | null;
90
+ display: string | null;
91
+ tags: string | null;
92
+ stake: string | null;
93
+ data: string | null;
94
+ genesisTime: string | null;
95
+ renaissanceTime: string | null;
96
+ }
97
+ /**
98
+ * Token table for indexed tokens
99
+ */
100
+ interface TokenTable {
101
+ address: string;
102
+ symbol: string | null;
103
+ name: string | null;
104
+ decimal: number | null;
105
+ unit: string | null;
106
+ description: string | null;
107
+ icon: string | null;
108
+ totalSupply: string | null;
109
+ totalSupplyNum: string | null;
110
+ initialSupply: string | null;
111
+ maxTotalSupply: string | null;
112
+ foreignToken: string | null;
113
+ issuer: string | null;
114
+ website: string | null;
115
+ metadata: string | null;
116
+ tokenFactoryAddress: string | null;
117
+ spenders: string | null;
118
+ minters: string | null;
119
+ type: string | null;
120
+ data: string | null;
121
+ genesisTime: string | null;
122
+ renaissanceTime: string | null;
123
+ }
124
+ /**
125
+ * Factory table for indexed asset factories
126
+ */
127
+ interface FactoryTable {
128
+ address: string;
129
+ owner: string | null;
130
+ name: string | null;
131
+ description: string | null;
132
+ settlement: string | null;
133
+ limit_: number | null;
134
+ trustedIssuers: string | null;
135
+ tokens: string | null;
136
+ numMinted: number | null;
137
+ lastSettlement: string | null;
138
+ input: string | null;
139
+ output: string | null;
140
+ display: string | null;
141
+ hooks: string | null;
142
+ data: string | null;
143
+ genesisTime: string | null;
144
+ renaissanceTime: string | null;
145
+ }
146
+ /**
147
+ * TokenFactory table for indexed token factories (bonding curve)
148
+ */
149
+ interface TokenFactoryTable {
150
+ address: string;
151
+ owner: string | null;
152
+ name: string | null;
153
+ description: string | null;
154
+ moniker: string | null;
155
+ status: string | null;
156
+ curve: string | null;
157
+ tokenAddress: string | null;
158
+ reserveAddress: string | null;
159
+ reserveBalance: string | null;
160
+ reserveBalanceNum: string | null;
161
+ currentSupply: string | null;
162
+ currentSupplyNum: string | null;
163
+ feeRate: string | null;
164
+ token: string | null;
165
+ reserveToken: string | null;
166
+ input: string | null;
167
+ output: string | null;
168
+ data: string | null;
169
+ genesisTime: string | null;
170
+ renaissanceTime: string | null;
171
+ }
172
+ /**
173
+ * Stake table for indexed stakes
174
+ */
175
+ interface StakeTable {
176
+ address: string;
177
+ sender: string | null;
178
+ receiver: string | null;
179
+ revocable: number | null;
180
+ tokens: string | null;
181
+ revokedTokens: string | null;
182
+ assets: string | null;
183
+ revokedAssets: string | null;
184
+ slashers: string | null;
185
+ nonce: string | null;
186
+ revokeWaitingPeriod: number | null;
187
+ message: string | null;
188
+ data: string | null;
189
+ genesisTime: string | null;
190
+ renaissanceTime: string | null;
191
+ }
192
+ /**
193
+ * Delegation table for indexed delegations
194
+ */
195
+ interface DelegationTable {
196
+ address: string;
197
+ from_: string | null;
198
+ to_: string | null;
199
+ ops: string | null;
200
+ context: string | null;
201
+ data: string | null;
202
+ genesisTime: string | null;
203
+ renaissanceTime: string | null;
204
+ }
205
+ /**
206
+ * Rollup table for indexed rollups
207
+ */
208
+ interface RollupTable {
209
+ address: string;
210
+ tokenAddress: string | null;
211
+ vaultAddress: string | null;
212
+ contractAddress: string | null;
213
+ paused: number | null;
214
+ closed: number | null;
215
+ seedValidators: string | null;
216
+ validators: string | null;
217
+ blockHash: string | null;
218
+ issuer: string | null;
219
+ foreignToken: string | null;
220
+ minStakeAmount: string | null;
221
+ maxStakeAmount: string | null;
222
+ minSignerCount: number | null;
223
+ maxSignerCount: number | null;
224
+ minBlockSize: number | null;
225
+ maxBlockSize: number | null;
226
+ minBlockInterval: number | null;
227
+ leaveWaitingPeriod: number | null;
228
+ publishWaitingPeriod: number | null;
229
+ publishSlashRate: number | null;
230
+ tokenInfo: string | null;
231
+ depositFeeRate: number | null;
232
+ withdrawFeeRate: number | null;
233
+ proposerFeeShare: number | null;
234
+ publisherFeeShare: number | null;
235
+ minDepositAmount: string | null;
236
+ minWithdrawAmount: string | null;
237
+ totalDepositAmount: string | null;
238
+ totalWithdrawAmount: string | null;
239
+ migrateHistory: string | null;
240
+ vaultHistory: string | null;
241
+ data: string | null;
242
+ genesisTime: string | null;
243
+ renaissanceTime: string | null;
244
+ }
245
+ /**
246
+ * RollupBlock table for indexed rollup blocks
247
+ */
248
+ interface RollupBlockTable {
249
+ hash: string;
250
+ rollup: string | null;
251
+ height: string | null;
252
+ merkleRoot: string | null;
253
+ previousHash: string | null;
254
+ txsHash: string | null;
255
+ txs: string | null;
256
+ proposer: string | null;
257
+ signatures: string | null;
258
+ rewardAmount: string | null;
259
+ mintedAmount: string | null;
260
+ burnedAmount: string | null;
261
+ minReward: string | null;
262
+ validators: string | null;
263
+ tokenInfo: string | null;
264
+ governance: number | null;
265
+ data: string | null;
266
+ genesisTime: string | null;
267
+ renaissanceTime: string | null;
268
+ }
269
+ /**
270
+ * RollupValidator table for indexed validators
271
+ */
272
+ interface RollupValidatorTable {
273
+ address: string;
274
+ rollup: string | null;
275
+ pk: string | null;
276
+ moniker: string | null;
277
+ endpoint: string | null;
278
+ joinTime: string | null;
279
+ leaveTime: string | null;
280
+ genesisTime: string | null;
281
+ renaissanceTime: string | null;
282
+ totalStake: string | null;
283
+ availableStake: string | null;
284
+ revokedStake: string | null;
285
+ totalGain: string | null;
286
+ proposedBlockCount: number | null;
287
+ verifiedBlockCount: number | null;
288
+ latestBlockHeight: number | null;
289
+ latestBlockHash: string | null;
290
+ data: string | null;
291
+ }
292
+ /**
293
+ * TokenDistribution table for token holder distribution
294
+ */
295
+ interface TokenDistributionTable {
296
+ tokenAddress: string;
297
+ holders: number | null;
298
+ distribution: string | null;
299
+ stake: string | null;
300
+ gasStake: string | null;
301
+ revokedStake: string | null;
302
+ gas: string | null;
303
+ fee: string | null;
304
+ slashedVault: string | null;
305
+ account: string | null;
306
+ txTime: string | null;
307
+ genesisTime: string | null;
308
+ renaissanceTime: string | null;
309
+ data: string | null;
310
+ }
311
+ /**
312
+ * Balance table for token balances (composite key)
313
+ */
314
+ interface BalanceTable {
315
+ address: string;
316
+ tokenAddress: string;
317
+ balance: string;
318
+ balanceNum: string | null;
319
+ data: string | null;
320
+ }
321
+ /**
322
+ * SQLite-specific operation context with optional transaction
323
+ */
324
+ interface SqliteOperationContext {
325
+ txn?: Transaction<Database>;
326
+ }
327
+ //#endregion
328
+ export { AccountTable, AssetTable, BalanceTable, Database, DelegationTable, FactoryTable, RollupBlockTable, RollupTable, RollupValidatorTable, SqliteOperationContext, StakeTable, TokenDistributionTable, TokenFactoryTable, TokenTable, TxTable };
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,43 @@
1
+ import { Database } from "./interfaces.mjs";
2
+ import { Kysely } from "kysely";
3
+
4
+ //#region src/kysely.d.ts
5
+
6
+ /**
7
+ * SQLite configuration options
8
+ */
9
+ interface SqliteConfig {
10
+ /** Database file path. Use ':memory:' for in-memory database */
11
+ filename: string;
12
+ /** Open database in readonly mode */
13
+ readonly?: boolean;
14
+ /** Throw error if file doesn't exist (default: false, creates new file) */
15
+ create?: boolean;
16
+ /** Enable WAL mode (default: true) */
17
+ walMode?: boolean;
18
+ }
19
+ /**
20
+ * Create a Kysely instance with SQLite dialect
21
+ * Automatically uses kysely-bun-sqlite in Bun or better-sqlite3 in Node.js
22
+ *
23
+ * @param config - SQLite configuration
24
+ * @returns Kysely instance configured for the database
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * // In-memory database for testing
29
+ * const db = createKysely({ filename: ':memory:' });
30
+ *
31
+ * // File-based database for production
32
+ * const db = createKysely({ filename: './data/index.sqlite' });
33
+ * ```
34
+ */
35
+ declare function createKysely(config: SqliteConfig): Kysely<Database>;
36
+ /**
37
+ * Get the underlying SQLite database instance from Kysely
38
+ * Useful for executing raw SQL or pragmas
39
+ * Returns either bun:sqlite Database or better-sqlite3 Database depending on runtime
40
+ */
41
+ declare function getRawDatabase(db: Kysely<Database>): any;
42
+ //#endregion
43
+ export { SqliteConfig, createKysely, getRawDatabase };
package/esm/kysely.mjs ADDED
@@ -0,0 +1,62 @@
1
+ import { __require } from "./_virtual/rolldown_runtime.mjs";
2
+ import { Kysely, SqliteDialect } from "kysely";
3
+
4
+ //#region src/kysely.ts
5
+ function isBunRuntime() {
6
+ return typeof globalThis.Bun !== "undefined";
7
+ }
8
+ /**
9
+ * Create a Kysely instance with SQLite dialect
10
+ * Automatically uses kysely-bun-sqlite in Bun or better-sqlite3 in Node.js
11
+ *
12
+ * @param config - SQLite configuration
13
+ * @returns Kysely instance configured for the database
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * // In-memory database for testing
18
+ * const db = createKysely({ filename: ':memory:' });
19
+ *
20
+ * // File-based database for production
21
+ * const db = createKysely({ filename: './data/index.sqlite' });
22
+ * ```
23
+ */
24
+ function createKysely(config) {
25
+ if (isBunRuntime()) {
26
+ const { BunSqliteDialect } = import.meta.require("kysely-bun-sqlite");
27
+ const { Database: BunDatabase } = import.meta.require("bun:sqlite");
28
+ const sqlite$1 = new BunDatabase(config.filename, {
29
+ create: config.create !== false,
30
+ readonly: config.readonly === true
31
+ });
32
+ if (config.walMode !== false && !config.readonly && config.filename !== ":memory:") {
33
+ sqlite$1.exec("PRAGMA journal_mode = WAL");
34
+ sqlite$1.exec("PRAGMA synchronous = NORMAL");
35
+ }
36
+ sqlite$1.exec("PRAGMA foreign_keys = ON");
37
+ sqlite$1.exec("PRAGMA busy_timeout = 5000");
38
+ return new Kysely({ dialect: new BunSqliteDialect({ database: sqlite$1 }) });
39
+ }
40
+ const sqlite = new (__require("better-sqlite3"))(config.filename, {
41
+ readonly: config.readonly === true,
42
+ fileMustExist: config.create === false
43
+ });
44
+ if (config.walMode !== false && !config.readonly && config.filename !== ":memory:") {
45
+ sqlite.pragma("journal_mode = WAL");
46
+ sqlite.pragma("synchronous = NORMAL");
47
+ }
48
+ sqlite.pragma("foreign_keys = ON");
49
+ sqlite.pragma("busy_timeout = 5000");
50
+ return new Kysely({ dialect: new SqliteDialect({ database: sqlite }) });
51
+ }
52
+ /**
53
+ * Get the underlying SQLite database instance from Kysely
54
+ * Useful for executing raw SQL or pragmas
55
+ * Returns either bun:sqlite Database or better-sqlite3 Database depending on runtime
56
+ */
57
+ function getRawDatabase(db) {
58
+ return db.getExecutor().adapter.db;
59
+ }
60
+
61
+ //#endregion
62
+ export { createKysely, getRawDatabase };
@@ -0,0 +1,14 @@
1
+ import { Kysely } from "kysely";
2
+
3
+ //#region src/migrations/001-genesis.d.ts
4
+
5
+ /**
6
+ * Genesis migration - creates all IndexDB tables
7
+ */
8
+ declare function up(db: Kysely<unknown>): Promise<void>;
9
+ /**
10
+ * Rollback migration - drops all tables
11
+ */
12
+ declare function down(db: Kysely<unknown>): Promise<void>;
13
+ //#endregion
14
+ export { down, up };
@@ -0,0 +1,107 @@
1
+ import { __exportAll } from "../_virtual/rolldown_runtime.mjs";
2
+ import { sql } from "kysely";
3
+
4
+ //#region src/migrations/001-genesis.ts
5
+ var _001_genesis_exports = /* @__PURE__ */ __exportAll({
6
+ down: () => down,
7
+ up: () => up
8
+ });
9
+ /**
10
+ * Genesis migration - creates all IndexDB tables
11
+ */
12
+ async function up(db) {
13
+ await db.schema.createTable("tx").ifNotExists().addColumn("hash", "text", (col) => col.primaryKey()).addColumn("code", "text").addColumn("height", "integer").addColumn("index_", "integer").addColumn("receipts", "text").addColumn("receiptsVerified", "integer").addColumn("receiver", "text").addColumn("sender", "text").addColumn("time", "text").addColumn("tx", "text").addColumn("type", "text").addColumn("finalized", "integer").addColumn("valid", "integer").addColumn("accounts", "text").addColumn("assets", "text").addColumn("tokens", "text").addColumn("tokenSymbols", "text").addColumn("factories", "text").addColumn("rollups", "text").addColumn("stakes", "text").addColumn("delegations", "text").addColumn("tokenFactories", "text").addColumn("genesisTime", "text").addColumn("renaissanceTime", "text").addColumn("data", "text").execute();
14
+ await sql`CREATE INDEX IF NOT EXISTS idx_tx_sender ON tx(sender)`.execute(db);
15
+ await sql`CREATE INDEX IF NOT EXISTS idx_tx_receiver ON tx(receiver)`.execute(db);
16
+ await sql`CREATE INDEX IF NOT EXISTS idx_tx_type ON tx(type)`.execute(db);
17
+ await sql`CREATE INDEX IF NOT EXISTS idx_tx_time ON tx(time)`.execute(db);
18
+ await sql`CREATE INDEX IF NOT EXISTS idx_tx_height ON tx(height)`.execute(db);
19
+ await sql`CREATE INDEX IF NOT EXISTS idx_tx_valid ON tx(valid)`.execute(db);
20
+ await db.schema.createTable("account").ifNotExists().addColumn("address", "text", (col) => col.primaryKey()).addColumn("balance", "text", (col) => col.notNull().defaultTo("0")).addColumn("balanceNum", "text").addColumn("gasBalance", "text").addColumn("moniker", "text").addColumn("pk", "text").addColumn("nonce", "integer").addColumn("numTxs", "integer").addColumn("numAssets", "integer").addColumn("issuer", "text").addColumn("migratedTo", "text").addColumn("migratedFrom", "text").addColumn("stake", "text").addColumn("tokens", "text").addColumn("data", "text").addColumn("genesisTime", "text").addColumn("renaissanceTime", "text").execute();
21
+ await sql`CREATE INDEX IF NOT EXISTS idx_account_balance ON account(balanceNum)`.execute(db);
22
+ await sql`CREATE INDEX IF NOT EXISTS idx_account_genesis ON account(genesisTime)`.execute(db);
23
+ await sql`CREATE INDEX IF NOT EXISTS idx_account_renaissance ON account(renaissanceTime)`.execute(db);
24
+ await sql`CREATE INDEX IF NOT EXISTS idx_account_moniker ON account(moniker)`.execute(db);
25
+ await db.schema.createTable("asset").ifNotExists().addColumn("address", "text", (col) => col.primaryKey()).addColumn("owner", "text").addColumn("moniker", "text").addColumn("readonly", "integer").addColumn("transferrable", "integer").addColumn("issuer", "text").addColumn("parent", "text").addColumn("ttl", "integer").addColumn("consumedTime", "text").addColumn("endpoint", "text").addColumn("display", "text").addColumn("tags", "text").addColumn("stake", "text").addColumn("data", "text").addColumn("genesisTime", "text").addColumn("renaissanceTime", "text").execute();
26
+ await sql`CREATE INDEX IF NOT EXISTS idx_asset_owner ON asset(owner)`.execute(db);
27
+ await sql`CREATE INDEX IF NOT EXISTS idx_asset_parent ON asset(parent)`.execute(db);
28
+ await sql`CREATE INDEX IF NOT EXISTS idx_asset_genesis ON asset(genesisTime)`.execute(db);
29
+ await sql`CREATE INDEX IF NOT EXISTS idx_asset_renaissance ON asset(renaissanceTime)`.execute(db);
30
+ await sql`CREATE INDEX IF NOT EXISTS idx_asset_consumed ON asset(consumedTime)`.execute(db);
31
+ await db.schema.createTable("token").ifNotExists().addColumn("address", "text", (col) => col.primaryKey()).addColumn("symbol", "text").addColumn("name", "text").addColumn("decimal", "integer").addColumn("unit", "text").addColumn("description", "text").addColumn("icon", "text").addColumn("totalSupply", "text").addColumn("totalSupplyNum", "text").addColumn("initialSupply", "text").addColumn("maxTotalSupply", "text").addColumn("foreignToken", "text").addColumn("issuer", "text").addColumn("website", "text").addColumn("metadata", "text").addColumn("tokenFactoryAddress", "text").addColumn("spenders", "text").addColumn("minters", "text").addColumn("type", "text").addColumn("data", "text").addColumn("genesisTime", "text").addColumn("renaissanceTime", "text").execute();
32
+ await sql`CREATE UNIQUE INDEX IF NOT EXISTS idx_token_symbol ON token(symbol) WHERE symbol IS NOT NULL`.execute(db);
33
+ await sql`CREATE INDEX IF NOT EXISTS idx_token_issuer ON token(issuer)`.execute(db);
34
+ await sql`CREATE INDEX IF NOT EXISTS idx_token_supply ON token(totalSupplyNum)`.execute(db);
35
+ await sql`CREATE INDEX IF NOT EXISTS idx_token_genesis ON token(genesisTime)`.execute(db);
36
+ await sql`CREATE INDEX IF NOT EXISTS idx_token_renaissance ON token(renaissanceTime)`.execute(db);
37
+ await db.schema.createTable("factory").ifNotExists().addColumn("address", "text", (col) => col.primaryKey()).addColumn("owner", "text").addColumn("name", "text").addColumn("description", "text").addColumn("settlement", "text").addColumn("limit_", "integer").addColumn("trustedIssuers", "text").addColumn("tokens", "text").addColumn("numMinted", "integer").addColumn("lastSettlement", "text").addColumn("input", "text").addColumn("output", "text").addColumn("display", "text").addColumn("hooks", "text").addColumn("data", "text").addColumn("genesisTime", "text").addColumn("renaissanceTime", "text").execute();
38
+ await sql`CREATE INDEX IF NOT EXISTS idx_factory_owner ON factory(owner)`.execute(db);
39
+ await sql`CREATE INDEX IF NOT EXISTS idx_factory_genesis ON factory(genesisTime)`.execute(db);
40
+ await sql`CREATE INDEX IF NOT EXISTS idx_factory_renaissance ON factory(renaissanceTime)`.execute(db);
41
+ await sql`CREATE INDEX IF NOT EXISTS idx_factory_minted ON factory(numMinted)`.execute(db);
42
+ await sql`CREATE INDEX IF NOT EXISTS idx_factory_limit ON factory(limit_)`.execute(db);
43
+ await sql`CREATE INDEX IF NOT EXISTS idx_factory_name ON factory(name)`.execute(db);
44
+ await db.schema.createTable("tokenFactory").ifNotExists().addColumn("address", "text", (col) => col.primaryKey()).addColumn("owner", "text").addColumn("name", "text").addColumn("description", "text").addColumn("moniker", "text").addColumn("status", "text").addColumn("curve", "text").addColumn("tokenAddress", "text").addColumn("reserveAddress", "text").addColumn("reserveBalance", "text").addColumn("reserveBalanceNum", "text").addColumn("currentSupply", "text").addColumn("currentSupplyNum", "text").addColumn("feeRate", "text").addColumn("token", "text").addColumn("reserveToken", "text").addColumn("input", "text").addColumn("output", "text").addColumn("data", "text").addColumn("genesisTime", "text").addColumn("renaissanceTime", "text").execute();
45
+ await sql`CREATE INDEX IF NOT EXISTS idx_tokenfactory_owner ON tokenFactory(owner)`.execute(db);
46
+ await sql`CREATE INDEX IF NOT EXISTS idx_tokenfactory_token ON tokenFactory(tokenAddress)`.execute(db);
47
+ await sql`CREATE INDEX IF NOT EXISTS idx_tokenfactory_reserve ON tokenFactory(reserveAddress)`.execute(db);
48
+ await sql`CREATE INDEX IF NOT EXISTS idx_tokenfactory_genesis ON tokenFactory(genesisTime)`.execute(db);
49
+ await sql`CREATE INDEX IF NOT EXISTS idx_tokenfactory_renaissance ON tokenFactory(renaissanceTime)`.execute(db);
50
+ await sql`CREATE INDEX IF NOT EXISTS idx_tokenfactory_reserve_balance ON tokenFactory(reserveBalanceNum)`.execute(db);
51
+ await sql`CREATE INDEX IF NOT EXISTS idx_tokenfactory_supply ON tokenFactory(currentSupplyNum)`.execute(db);
52
+ await db.schema.createTable("stake").ifNotExists().addColumn("address", "text", (col) => col.primaryKey()).addColumn("sender", "text").addColumn("receiver", "text").addColumn("revocable", "integer").addColumn("tokens", "text").addColumn("revokedTokens", "text").addColumn("assets", "text").addColumn("revokedAssets", "text").addColumn("slashers", "text").addColumn("nonce", "text").addColumn("revokeWaitingPeriod", "integer").addColumn("message", "text").addColumn("data", "text").addColumn("genesisTime", "text").addColumn("renaissanceTime", "text").execute();
53
+ await sql`CREATE INDEX IF NOT EXISTS idx_stake_sender ON stake(sender)`.execute(db);
54
+ await sql`CREATE INDEX IF NOT EXISTS idx_stake_receiver ON stake(receiver)`.execute(db);
55
+ await sql`CREATE INDEX IF NOT EXISTS idx_stake_genesis ON stake(genesisTime)`.execute(db);
56
+ await sql`CREATE INDEX IF NOT EXISTS idx_stake_renaissance ON stake(renaissanceTime)`.execute(db);
57
+ await db.schema.createTable("delegation").ifNotExists().addColumn("address", "text", (col) => col.primaryKey()).addColumn("from_", "text").addColumn("to_", "text").addColumn("ops", "text").addColumn("context", "text").addColumn("data", "text").addColumn("genesisTime", "text").addColumn("renaissanceTime", "text").execute();
58
+ await sql`CREATE INDEX IF NOT EXISTS idx_delegation_from ON delegation(from_)`.execute(db);
59
+ await sql`CREATE INDEX IF NOT EXISTS idx_delegation_to ON delegation(to_)`.execute(db);
60
+ await sql`CREATE INDEX IF NOT EXISTS idx_delegation_genesis ON delegation(genesisTime)`.execute(db);
61
+ await sql`CREATE INDEX IF NOT EXISTS idx_delegation_renaissance ON delegation(renaissanceTime)`.execute(db);
62
+ await db.schema.createTable("rollup").ifNotExists().addColumn("address", "text", (col) => col.primaryKey()).addColumn("tokenAddress", "text").addColumn("vaultAddress", "text").addColumn("contractAddress", "text").addColumn("paused", "integer").addColumn("closed", "integer").addColumn("seedValidators", "text").addColumn("validators", "text").addColumn("blockHash", "text").addColumn("issuer", "text").addColumn("foreignToken", "text").addColumn("minStakeAmount", "text").addColumn("maxStakeAmount", "text").addColumn("minSignerCount", "integer").addColumn("maxSignerCount", "integer").addColumn("minBlockSize", "integer").addColumn("maxBlockSize", "integer").addColumn("minBlockInterval", "integer").addColumn("leaveWaitingPeriod", "integer").addColumn("publishWaitingPeriod", "integer").addColumn("publishSlashRate", "integer").addColumn("tokenInfo", "text").addColumn("depositFeeRate", "integer").addColumn("withdrawFeeRate", "integer").addColumn("proposerFeeShare", "integer").addColumn("publisherFeeShare", "integer").addColumn("minDepositAmount", "text").addColumn("minWithdrawAmount", "text").addColumn("totalDepositAmount", "text").addColumn("totalWithdrawAmount", "text").addColumn("migrateHistory", "text").addColumn("vaultHistory", "text").addColumn("data", "text").addColumn("genesisTime", "text").addColumn("renaissanceTime", "text").execute();
63
+ await sql`DROP INDEX IF EXISTS idx_rollup_token`.execute(db);
64
+ await sql`CREATE INDEX IF NOT EXISTS idx_rollup_token ON rollup(tokenAddress) WHERE tokenAddress IS NOT NULL`.execute(db);
65
+ await sql`CREATE INDEX IF NOT EXISTS idx_rollup_genesis ON rollup(genesisTime)`.execute(db);
66
+ await sql`CREATE INDEX IF NOT EXISTS idx_rollup_renaissance ON rollup(renaissanceTime)`.execute(db);
67
+ await db.schema.createTable("rollupBlock").ifNotExists().addColumn("hash", "text", (col) => col.primaryKey()).addColumn("rollup", "text").addColumn("height", "text").addColumn("merkleRoot", "text").addColumn("previousHash", "text").addColumn("txsHash", "text").addColumn("txs", "text").addColumn("proposer", "text").addColumn("signatures", "text").addColumn("rewardAmount", "text").addColumn("mintedAmount", "text").addColumn("burnedAmount", "text").addColumn("minReward", "text").addColumn("validators", "text").addColumn("tokenInfo", "text").addColumn("governance", "integer").addColumn("data", "text").addColumn("genesisTime", "text").addColumn("renaissanceTime", "text").execute();
68
+ await sql`CREATE INDEX IF NOT EXISTS idx_rollupblock_rollup ON rollupBlock(rollup)`.execute(db);
69
+ await sql`CREATE INDEX IF NOT EXISTS idx_rollupblock_height ON rollupBlock(height)`.execute(db);
70
+ await sql`CREATE INDEX IF NOT EXISTS idx_rollupblock_proposer ON rollupBlock(proposer)`.execute(db);
71
+ await sql`CREATE INDEX IF NOT EXISTS idx_rollupblock_genesis ON rollupBlock(genesisTime)`.execute(db);
72
+ await sql`CREATE INDEX IF NOT EXISTS idx_rollupblock_renaissance ON rollupBlock(renaissanceTime)`.execute(db);
73
+ await db.schema.createTable("rollupValidator").ifNotExists().addColumn("address", "text", (col) => col.primaryKey()).addColumn("rollup", "text").addColumn("pk", "text").addColumn("moniker", "text").addColumn("endpoint", "text").addColumn("joinTime", "text").addColumn("leaveTime", "text").addColumn("genesisTime", "text").addColumn("renaissanceTime", "text").addColumn("totalStake", "text").addColumn("availableStake", "text").addColumn("revokedStake", "text").addColumn("totalGain", "text").addColumn("proposedBlockCount", "integer").addColumn("verifiedBlockCount", "integer").addColumn("latestBlockHeight", "integer").addColumn("latestBlockHash", "text").addColumn("data", "text").execute();
74
+ await sql`CREATE INDEX IF NOT EXISTS idx_rollupvalidator_rollup ON rollupValidator(rollup)`.execute(db);
75
+ await sql`CREATE INDEX IF NOT EXISTS idx_rollupvalidator_join ON rollupValidator(joinTime)`.execute(db);
76
+ await sql`CREATE INDEX IF NOT EXISTS idx_rollupvalidator_leave ON rollupValidator(leaveTime)`.execute(db);
77
+ await sql`CREATE INDEX IF NOT EXISTS idx_rollupvalidator_genesis ON rollupValidator(genesisTime)`.execute(db);
78
+ await sql`CREATE INDEX IF NOT EXISTS idx_rollupvalidator_renaissance ON rollupValidator(renaissanceTime)`.execute(db);
79
+ await db.schema.createTable("tokenDistribution").ifNotExists().addColumn("tokenAddress", "text", (col) => col.primaryKey()).addColumn("holders", "integer").addColumn("distribution", "text").addColumn("stake", "text").addColumn("gasStake", "text").addColumn("revokedStake", "text").addColumn("gas", "text").addColumn("fee", "text").addColumn("slashedVault", "text").addColumn("account", "text").addColumn("txTime", "text").addColumn("genesisTime", "text").addColumn("renaissanceTime", "text").addColumn("data", "text").execute();
80
+ await db.schema.createTable("balance").ifNotExists().addColumn("address", "text", (col) => col.notNull()).addColumn("tokenAddress", "text", (col) => col.notNull()).addColumn("balance", "text", (col) => col.notNull().defaultTo("0")).addColumn("balanceNum", "text").addColumn("data", "text").addPrimaryKeyConstraint("balance_pk", ["address", "tokenAddress"]).execute();
81
+ await sql`CREATE INDEX IF NOT EXISTS idx_balance_address ON balance(address)`.execute(db);
82
+ await sql`CREATE INDEX IF NOT EXISTS idx_balance_token ON balance(tokenAddress)`.execute(db);
83
+ await sql`CREATE INDEX IF NOT EXISTS idx_balance_amount ON balance(balanceNum)`.execute(db);
84
+ }
85
+ /**
86
+ * Rollback migration - drops all tables
87
+ */
88
+ async function down(db) {
89
+ for (const table of [
90
+ "balance",
91
+ "tokenDistribution",
92
+ "rollupValidator",
93
+ "rollupBlock",
94
+ "rollup",
95
+ "delegation",
96
+ "stake",
97
+ "tokenFactory",
98
+ "factory",
99
+ "token",
100
+ "asset",
101
+ "account",
102
+ "tx"
103
+ ]) await db.schema.dropTable(table).ifExists().execute();
104
+ }
105
+
106
+ //#endregion
107
+ export { _001_genesis_exports, down, up };
@@ -0,0 +1,16 @@
1
+ import { Kysely } from "kysely";
2
+
3
+ //#region src/migrations/index.d.ts
4
+
5
+ /**
6
+ * Run all pending migrations
7
+ * @param db - Kysely instance
8
+ */
9
+ declare function runMigrations(db: Kysely<unknown>): Promise<void>;
10
+ /**
11
+ * Rollback all migrations
12
+ * @param db - Kysely instance
13
+ */
14
+ declare function rollbackMigrations(db: Kysely<unknown>): Promise<void>;
15
+ //#endregion
16
+ export { rollbackMigrations, runMigrations };
@@ -0,0 +1,44 @@
1
+ import { _001_genesis_exports } from "./001-genesis.mjs";
2
+ import { Migrator } from "kysely";
3
+
4
+ //#region src/migrations/index.ts
5
+ /**
6
+ * Custom migration provider that returns migrations in memory
7
+ * This avoids file system operations and works well with Bun
8
+ */
9
+ var InMemoryMigrationProvider = class {
10
+ async getMigrations() {
11
+ return { "001-genesis": _001_genesis_exports };
12
+ }
13
+ };
14
+ /**
15
+ * Run all pending migrations
16
+ * @param db - Kysely instance
17
+ */
18
+ async function runMigrations(db) {
19
+ const { error, results } = await new Migrator({
20
+ db,
21
+ provider: new InMemoryMigrationProvider()
22
+ }).migrateToLatest();
23
+ if (error) throw error;
24
+ for (const result of results ?? []) if (result.status === "Error") throw new Error(`Migration "${result.migrationName}" failed`);
25
+ }
26
+ /**
27
+ * Rollback all migrations
28
+ * @param db - Kysely instance
29
+ */
30
+ async function rollbackMigrations(db) {
31
+ const migrator = new Migrator({
32
+ db,
33
+ provider: new InMemoryMigrationProvider()
34
+ });
35
+ const { results } = await migrator.getMigrations();
36
+ const executedMigrations = results?.filter((r) => r.executedAt) ?? [];
37
+ for (let i = executedMigrations.length - 1; i >= 0; i--) {
38
+ const { error } = await migrator.migrateDown();
39
+ if (error) throw error;
40
+ }
41
+ }
42
+
43
+ //#endregion
44
+ export { rollbackMigrations, runMigrations };