web3ql-client 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/README.md +66 -0
  2. package/contracts/PublicKeyRegistry.sol +87 -0
  3. package/dist/src/access.d.ts +176 -0
  4. package/dist/src/access.d.ts.map +1 -0
  5. package/dist/src/access.js +283 -0
  6. package/dist/src/access.js.map +1 -0
  7. package/dist/src/batch.d.ts +107 -0
  8. package/dist/src/batch.d.ts.map +1 -0
  9. package/dist/src/batch.js +188 -0
  10. package/dist/src/batch.js.map +1 -0
  11. package/dist/src/cli.d.ts +40 -0
  12. package/dist/src/cli.d.ts.map +1 -0
  13. package/dist/src/cli.js +361 -0
  14. package/dist/src/cli.js.map +1 -0
  15. package/dist/src/constraints.d.ts +126 -0
  16. package/dist/src/constraints.d.ts.map +1 -0
  17. package/dist/src/constraints.js +192 -0
  18. package/dist/src/constraints.js.map +1 -0
  19. package/dist/src/crypto.d.ts +118 -0
  20. package/dist/src/crypto.d.ts.map +1 -0
  21. package/dist/src/crypto.js +192 -0
  22. package/dist/src/crypto.js.map +1 -0
  23. package/dist/src/factory-client.d.ts +106 -0
  24. package/dist/src/factory-client.d.ts.map +1 -0
  25. package/dist/src/factory-client.js +202 -0
  26. package/dist/src/factory-client.js.map +1 -0
  27. package/dist/src/index-cache.d.ts +156 -0
  28. package/dist/src/index-cache.d.ts.map +1 -0
  29. package/dist/src/index-cache.js +265 -0
  30. package/dist/src/index-cache.js.map +1 -0
  31. package/dist/src/index.d.ts +60 -0
  32. package/dist/src/index.d.ts.map +1 -0
  33. package/dist/src/index.js +60 -0
  34. package/dist/src/index.js.map +1 -0
  35. package/dist/src/migrations.d.ts +114 -0
  36. package/dist/src/migrations.d.ts.map +1 -0
  37. package/dist/src/migrations.js +173 -0
  38. package/dist/src/migrations.js.map +1 -0
  39. package/dist/src/model.d.ts +198 -0
  40. package/dist/src/model.d.ts.map +1 -0
  41. package/dist/src/model.js +379 -0
  42. package/dist/src/model.js.map +1 -0
  43. package/dist/src/query.d.ts +155 -0
  44. package/dist/src/query.d.ts.map +1 -0
  45. package/dist/src/query.js +386 -0
  46. package/dist/src/query.js.map +1 -0
  47. package/dist/src/registry.d.ts +45 -0
  48. package/dist/src/registry.d.ts.map +1 -0
  49. package/dist/src/registry.js +80 -0
  50. package/dist/src/registry.js.map +1 -0
  51. package/dist/src/schema-manager.d.ts +109 -0
  52. package/dist/src/schema-manager.d.ts.map +1 -0
  53. package/dist/src/schema-manager.js +259 -0
  54. package/dist/src/schema-manager.js.map +1 -0
  55. package/dist/src/table-client.d.ts +156 -0
  56. package/dist/src/table-client.d.ts.map +1 -0
  57. package/dist/src/table-client.js +292 -0
  58. package/dist/src/table-client.js.map +1 -0
  59. package/dist/src/typed-table.d.ts +159 -0
  60. package/dist/src/typed-table.d.ts.map +1 -0
  61. package/dist/src/typed-table.js +246 -0
  62. package/dist/src/typed-table.js.map +1 -0
  63. package/dist/src/types.d.ts +48 -0
  64. package/dist/src/types.d.ts.map +1 -0
  65. package/dist/src/types.js +222 -0
  66. package/dist/src/types.js.map +1 -0
  67. package/keyManager.js +337 -0
  68. package/package.json +38 -0
  69. package/src/access.ts +421 -0
  70. package/src/batch.ts +259 -0
  71. package/src/cli.ts +349 -0
  72. package/src/constraints.ts +283 -0
  73. package/src/crypto.ts +239 -0
  74. package/src/factory-client.ts +237 -0
  75. package/src/index-cache.ts +351 -0
  76. package/src/index.ts +171 -0
  77. package/src/migrations.ts +215 -0
  78. package/src/model.ts +538 -0
  79. package/src/query.ts +508 -0
  80. package/src/registry.ts +100 -0
  81. package/src/schema-manager.ts +301 -0
  82. package/src/table-client.ts +393 -0
  83. package/src/typed-table.ts +340 -0
  84. package/src/types.ts +284 -0
  85. package/tsconfig.json +22 -0
  86. package/walletUtils.js +204 -0
@@ -0,0 +1,215 @@
1
+ /**
2
+ * @file migrations.ts
3
+ * @notice Web3QL v1.1 — schema migration framework.
4
+ *
5
+ * Because schemas are stored as bytes on-chain, adding or removing columns
6
+ * is an off-chain SDK concern:
7
+ *
8
+ * • ADD COLUMN → new field appears with its DEFAULT value for old records
9
+ * • DROP COLUMN → field is silently ignored on read, omitted on write
10
+ * • RENAME → read uses old name, write uses new name
11
+ * • CHANGE TYPE → codec changes; old wire values are transparently converted
12
+ *
13
+ * Migrations are run client-side during read/write. They do NOT require any
14
+ * contract call or gas. The on-chain ciphertext is re-encrypted with the new
15
+ * schema on the next write (lazy migration).
16
+ *
17
+ * Usage:
18
+ * ─────────────────────────────────────────────────────────────
19
+ * const migrations = new MigrationRunner([
20
+ * {
21
+ * version: 1,
22
+ * description: 'Add role field',
23
+ * up: addColumn({ name: 'role', type: 'ENUM', enumValues: ['user','admin'], default: 'user' }),
24
+ * },
25
+ * {
26
+ * version: 2,
27
+ * description: 'Rename email → emailAddress',
28
+ * up: renameColumn('email', 'emailAddress'),
29
+ * },
30
+ * {
31
+ * version: 3,
32
+ * description: 'Drop legacy field',
33
+ * up: dropColumn('legacyField'),
34
+ * },
35
+ * ]);
36
+ *
37
+ * // Apply to a record read from chain
38
+ * const migratedData = migrations.applyToRecord(rawDecoded, fromVersion = 0);
39
+ *
40
+ * // Apply to a schema definition (for TypedTableClient)
41
+ * const latestSchema = migrations.applyToSchema(v0Schema);
42
+ * ─────────────────────────────────────────────────────────────
43
+ */
44
+
45
+ import type { SchemaDefinition, FieldDescriptor } from './types.js';
46
+
47
+ // ─────────────────────────────────────────────────────────────
48
+ // Row transformer type
49
+ // ─────────────────────────────────────────────────────────────
50
+
51
+ export type RowTransformer = (row: Record<string, unknown>) => Record<string, unknown>;
52
+ export type SchemaTransformer = (schema: SchemaDefinition) => SchemaDefinition;
53
+
54
+ export interface Migration {
55
+ /** Monotonically increasing version number (1, 2, 3…) */
56
+ version : number;
57
+ description: string;
58
+ /** Transform the row data from previous version to this version */
59
+ up : RowTransformer;
60
+ /** Transform the schema definition from previous version to this version */
61
+ schema? : SchemaTransformer;
62
+ }
63
+
64
+ // ─────────────────────────────────────────────────────────────
65
+ // Built-in migration helpers
66
+ // ─────────────────────────────────────────────────────────────
67
+
68
+ /**
69
+ * ADD COLUMN — inserts the field with its default value into records
70
+ * that don't have it yet.
71
+ */
72
+ export function addColumn(field: FieldDescriptor): RowTransformer {
73
+ return (row) => {
74
+ if (row[field.name] !== undefined && row[field.name] !== null) return row;
75
+ const defaultVal = field.default != null
76
+ ? (typeof field.default === 'function' ? (field.default as () => unknown)() : field.default)
77
+ : null;
78
+ return { ...row, [field.name]: defaultVal };
79
+ };
80
+ }
81
+
82
+ /** Schema variant of addColumn */
83
+ export function addColumnSchema(field: FieldDescriptor): SchemaTransformer {
84
+ return (schema) => {
85
+ if (schema.some((f) => f.name === field.name)) return schema;
86
+ return [...schema, field];
87
+ };
88
+ }
89
+
90
+ /**
91
+ * DROP COLUMN — removes the field from records.
92
+ * Old ciphertext data is discarded on next write.
93
+ */
94
+ export function dropColumn(fieldName: string): RowTransformer {
95
+ return (row) => {
96
+ const out = { ...row };
97
+ delete out[fieldName];
98
+ return out;
99
+ };
100
+ }
101
+
102
+ /** Schema variant of dropColumn */
103
+ export function dropColumnSchema(fieldName: string): SchemaTransformer {
104
+ return (schema) => schema.filter((f) => f.name !== fieldName);
105
+ }
106
+
107
+ /**
108
+ * RENAME COLUMN — moves value from oldName to newName.
109
+ */
110
+ export function renameColumn(oldName: string, newName: string): RowTransformer {
111
+ return (row) => {
112
+ if (!(oldName in row)) return row;
113
+ const out = { ...row, [newName]: row[oldName] };
114
+ delete out[oldName];
115
+ return out;
116
+ };
117
+ }
118
+
119
+ /** Schema variant of renameColumn */
120
+ export function renameColumnSchema(oldName: string, newName: string): SchemaTransformer {
121
+ return (schema) =>
122
+ schema.map((f) => (f.name === oldName ? { ...f, name: newName } : f));
123
+ }
124
+
125
+ /**
126
+ * CHANGE TYPE — re-encode a field's value through a transform function.
127
+ * Supply a function that converts the old wire value to the new wire value.
128
+ *
129
+ * @example
130
+ * // Convert TEXT → TIMESTAMP (ISO string → unix ms)
131
+ * changeType('createdAt', (v) => new Date(String(v)).getTime())
132
+ */
133
+ export function changeType(
134
+ fieldName: string,
135
+ transform: (oldValue: unknown) => unknown,
136
+ ): RowTransformer {
137
+ return (row) => {
138
+ if (!(fieldName in row)) return row;
139
+ return { ...row, [fieldName]: transform(row[fieldName]) };
140
+ };
141
+ }
142
+
143
+ /**
144
+ * COMPUTE COLUMN — derive a new field from existing fields.
145
+ * Called only if the field is absent (so it doesn't overwrite on re-migrations).
146
+ */
147
+ export function computeColumn(
148
+ fieldName: string,
149
+ compute : (row: Record<string, unknown>) => unknown,
150
+ ): RowTransformer {
151
+ return (row) => {
152
+ if (row[fieldName] !== undefined && row[fieldName] !== null) return row;
153
+ return { ...row, [fieldName]: compute(row) };
154
+ };
155
+ }
156
+
157
+ // ─────────────────────────────────────────────────────────────
158
+ // MigrationRunner
159
+ // ─────────────────────────────────────────────────────────────
160
+
161
+ export class MigrationRunner {
162
+ private migrations: Migration[];
163
+
164
+ constructor(migrations: Migration[]) {
165
+ // Sort ascending by version
166
+ this.migrations = [...migrations].sort((a, b) => a.version - b.version);
167
+ }
168
+
169
+ /**
170
+ * Apply all migrations with version > `fromVersion` to a single record row.
171
+ *
172
+ * @param row Decoded record from the chain (before migration)
173
+ * @param fromVersion The schema version the record was written at. Use the
174
+ * VERSION field stored in the record, or 0 if absent.
175
+ */
176
+ applyToRecord(
177
+ row : Record<string, unknown>,
178
+ fromVersion: number = 0,
179
+ ): Record<string, unknown> {
180
+ let current = { ...row };
181
+ for (const m of this.migrations) {
182
+ if (m.version > fromVersion) {
183
+ current = m.up(current);
184
+ }
185
+ }
186
+ return current;
187
+ }
188
+
189
+ /**
190
+ * Apply all schema migrations from `fromVersion` to the latest version,
191
+ * producing an up-to-date SchemaDefinition for TypedTableClient.
192
+ */
193
+ applyToSchema(
194
+ baseSchema : SchemaDefinition,
195
+ fromVersion: number = 0,
196
+ ): SchemaDefinition {
197
+ let schema = [...baseSchema];
198
+ for (const m of this.migrations) {
199
+ if (m.version > fromVersion && m.schema) {
200
+ schema = m.schema(schema);
201
+ }
202
+ }
203
+ return schema;
204
+ }
205
+
206
+ /** The latest migration version number. */
207
+ get latestVersion(): number {
208
+ return this.migrations.at(-1)?.version ?? 0;
209
+ }
210
+
211
+ /** List all registered migrations in version order. */
212
+ list(): { version: number; description: string }[] {
213
+ return this.migrations.map(({ version, description }) => ({ version, description }));
214
+ }
215
+ }