envio 3.0.0-alpha.19 → 3.0.0-alpha.19-main-node-pg-client
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/package.json +6 -6
- package/src/Config.res +1 -0
- package/src/Config.res.mjs +2 -2
- package/src/Env.res +1 -1
- package/src/Env.res.mjs +2 -2
- package/src/Internal.res.mjs +1 -1
- package/src/PgStorage.res +192 -124
- package/src/PgStorage.res.mjs +219 -112
- package/src/bindings/ClickHouse.res +1 -1
- package/src/bindings/ClickHouse.res.mjs +1 -1
- package/src/bindings/Pg.res +146 -0
- package/src/bindings/{Postgres.res.d.mts → Pg.res.d.mts} +1 -1
- package/src/bindings/Pg.res.mjs +110 -0
- package/src/db/EntityHistory.res +21 -21
- package/src/db/EntityHistory.res.mjs +16 -3
- package/src/db/InternalTable.res +47 -49
- package/src/db/InternalTable.res.mjs +57 -23
- package/src/db/Table.res +45 -33
- package/src/db/Table.res.mjs +43 -41
- package/src/PgStorage.gen.ts +0 -10
- package/src/bindings/Postgres.gen.ts +0 -8
- package/src/bindings/Postgres.res +0 -120
- package/src/bindings/Postgres.res.mjs +0 -17
|
@@ -26,7 +26,7 @@ var fields = [
|
|
|
26
26
|
"_is_hyper_sync"
|
|
27
27
|
];
|
|
28
28
|
|
|
29
|
-
var table = Table.mkTable("envio_chains", undefined, [
|
|
29
|
+
var table = Table.mkTable("envio_chains", undefined, undefined, [
|
|
30
30
|
Table.mkField("id", "Int32", S$RescriptSchema.$$int, undefined, undefined, undefined, true, undefined, undefined),
|
|
31
31
|
Table.mkField("start_block", "Int32", S$RescriptSchema.$$int, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
32
32
|
Table.mkField("end_block", "Int32", S$RescriptSchema.$$null(S$RescriptSchema.$$int), undefined, undefined, true, undefined, undefined, undefined),
|
|
@@ -110,7 +110,11 @@ function makeGetInitialStateQuery(pgSchema) {
|
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
function getInitialState(sql, pgSchema) {
|
|
113
|
-
return sql.
|
|
113
|
+
return sql.query({
|
|
114
|
+
text: makeGetInitialStateQuery(pgSchema)
|
|
115
|
+
}).then(function (r) {
|
|
116
|
+
return r.rows;
|
|
117
|
+
});
|
|
114
118
|
}
|
|
115
119
|
|
|
116
120
|
var progressFields = [
|
|
@@ -137,7 +141,11 @@ function setMeta(sql, pgSchema, chainsData) {
|
|
|
137
141
|
var value = data[field];
|
|
138
142
|
params.push(value);
|
|
139
143
|
});
|
|
140
|
-
promises.push(sql.
|
|
144
|
+
promises.push(sql.query({
|
|
145
|
+
text: query,
|
|
146
|
+
values: params,
|
|
147
|
+
name: "chains_set_meta"
|
|
148
|
+
}));
|
|
141
149
|
}));
|
|
142
150
|
return Promise.all(promises);
|
|
143
151
|
}
|
|
@@ -153,7 +161,11 @@ function setProgressedChains(sql, pgSchema, progressedChains) {
|
|
|
153
161
|
field === "progress_block" ? data.progressBlockNumber : data.totalEventsProcessed
|
|
154
162
|
));
|
|
155
163
|
});
|
|
156
|
-
promises.push(sql.
|
|
164
|
+
promises.push(sql.query({
|
|
165
|
+
text: query,
|
|
166
|
+
values: params,
|
|
167
|
+
name: "chains_set_progress"
|
|
168
|
+
}));
|
|
157
169
|
});
|
|
158
170
|
return Promise.all(promises);
|
|
159
171
|
}
|
|
@@ -173,7 +185,7 @@ var Chains = {
|
|
|
173
185
|
setProgressedChains: setProgressedChains
|
|
174
186
|
};
|
|
175
187
|
|
|
176
|
-
var table$1 = Table.mkTable("persisted_state", undefined, [
|
|
188
|
+
var table$1 = Table.mkTable("persisted_state", undefined, undefined, [
|
|
177
189
|
Table.mkField("id", "Serial", S$RescriptSchema.$$int, undefined, undefined, undefined, true, undefined, undefined),
|
|
178
190
|
Table.mkField("envio_version", "String", S$RescriptSchema.string, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
179
191
|
Table.mkField("config_hash", "String", S$RescriptSchema.string, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
@@ -198,7 +210,7 @@ var dbSchema = S$RescriptSchema.object(function (s) {
|
|
|
198
210
|
};
|
|
199
211
|
});
|
|
200
212
|
|
|
201
|
-
var table$2 = Table.mkTable("envio_checkpoints", undefined, [
|
|
213
|
+
var table$2 = Table.mkTable("envio_checkpoints", undefined, undefined, [
|
|
202
214
|
Table.mkField("id", "UInt64", S$RescriptSchema.bigint, undefined, undefined, undefined, true, undefined, undefined),
|
|
203
215
|
Table.mkField("chain_id", "Int32", S$RescriptSchema.$$int, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
204
216
|
Table.mkField("block_number", "Int32", S$RescriptSchema.$$int, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
@@ -221,17 +233,25 @@ function makeInsertCheckpointQuery(pgSchema) {
|
|
|
221
233
|
function insert(sql, pgSchema, checkpointIds, checkpointChainIds, checkpointBlockNumbers, checkpointBlockHashes, checkpointEventsProcessed) {
|
|
222
234
|
var query = makeInsertCheckpointQuery(pgSchema);
|
|
223
235
|
var checkpointIdStrings = $$BigInt.arrayToStringArray(checkpointIds);
|
|
224
|
-
return sql.
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
236
|
+
return sql.query({
|
|
237
|
+
text: query,
|
|
238
|
+
values: [
|
|
239
|
+
checkpointIdStrings,
|
|
240
|
+
checkpointChainIds,
|
|
241
|
+
checkpointBlockNumbers,
|
|
242
|
+
checkpointBlockHashes,
|
|
243
|
+
checkpointEventsProcessed
|
|
244
|
+
],
|
|
245
|
+
name: "checkpoints_insert"
|
|
246
|
+
});
|
|
231
247
|
}
|
|
232
248
|
|
|
233
249
|
function rollback(sql, pgSchema, rollbackTargetCheckpointId) {
|
|
234
|
-
return sql.
|
|
250
|
+
return sql.query({
|
|
251
|
+
text: "DELETE FROM \"" + pgSchema + "\".\"" + table$2.tableName + "\" WHERE \"" + "id" + "\" > $1;",
|
|
252
|
+
values: [rollbackTargetCheckpointId.toString()],
|
|
253
|
+
name: "checkpoints_rollback"
|
|
254
|
+
});
|
|
235
255
|
}
|
|
236
256
|
|
|
237
257
|
function makePruneStaleCheckpointsQuery(pgSchema) {
|
|
@@ -239,7 +259,11 @@ function makePruneStaleCheckpointsQuery(pgSchema) {
|
|
|
239
259
|
}
|
|
240
260
|
|
|
241
261
|
function pruneStaleCheckpoints(sql, pgSchema, safeCheckpointId) {
|
|
242
|
-
return sql.
|
|
262
|
+
return sql.query({
|
|
263
|
+
text: makePruneStaleCheckpointsQuery(pgSchema),
|
|
264
|
+
values: [safeCheckpointId.toString()],
|
|
265
|
+
name: "checkpoints_prune"
|
|
266
|
+
});
|
|
243
267
|
}
|
|
244
268
|
|
|
245
269
|
function makeGetRollbackTargetCheckpointQuery(pgSchema) {
|
|
@@ -247,11 +271,15 @@ function makeGetRollbackTargetCheckpointQuery(pgSchema) {
|
|
|
247
271
|
}
|
|
248
272
|
|
|
249
273
|
function getRollbackTargetCheckpoint(sql, pgSchema, reorgChainId, lastKnownValidBlockNumber) {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
274
|
+
return sql.query({
|
|
275
|
+
text: makeGetRollbackTargetCheckpointQuery(pgSchema),
|
|
276
|
+
values: [
|
|
277
|
+
reorgChainId,
|
|
278
|
+
lastKnownValidBlockNumber
|
|
279
|
+
],
|
|
280
|
+
name: "checkpoints_rollback_target"
|
|
281
|
+
}).then(function (r) {
|
|
282
|
+
var rows = r.rows;
|
|
255
283
|
return Belt_Option.map(Belt_Array.get(rows, 0), (function (row) {
|
|
256
284
|
return BigInt(row.id);
|
|
257
285
|
}));
|
|
@@ -259,11 +287,17 @@ function getRollbackTargetCheckpoint(sql, pgSchema, reorgChainId, lastKnownValid
|
|
|
259
287
|
}
|
|
260
288
|
|
|
261
289
|
function makeGetRollbackProgressDiffQuery(pgSchema) {
|
|
262
|
-
return "SELECT
|
|
290
|
+
return "SELECT\n \"" + "chain_id" + "\",\n SUM(\"" + "events_processed" + "\") as events_processed_diff,\n MIN(\"" + "block_number" + "\") - 1 as new_progress_block_number\nFROM \"" + pgSchema + "\".\"" + table$2.tableName + "\"\nWHERE \"" + "id" + "\" > $1\nGROUP BY \"" + "chain_id" + "\";";
|
|
263
291
|
}
|
|
264
292
|
|
|
265
293
|
function getRollbackProgressDiff(sql, pgSchema, rollbackTargetCheckpointId) {
|
|
266
|
-
return sql.
|
|
294
|
+
return sql.query({
|
|
295
|
+
text: makeGetRollbackProgressDiffQuery(pgSchema),
|
|
296
|
+
values: [rollbackTargetCheckpointId.toString()],
|
|
297
|
+
name: "checkpoints_rollback_diff"
|
|
298
|
+
}).then(function (r) {
|
|
299
|
+
return r.rows;
|
|
300
|
+
});
|
|
267
301
|
}
|
|
268
302
|
|
|
269
303
|
var Checkpoints = {
|
|
@@ -300,7 +334,7 @@ var schema = S$RescriptSchema.schema(function (s) {
|
|
|
300
334
|
};
|
|
301
335
|
});
|
|
302
336
|
|
|
303
|
-
var table$3 = Table.mkTable("raw_events", undefined, [
|
|
337
|
+
var table$3 = Table.mkTable("raw_events", undefined, undefined, [
|
|
304
338
|
Table.mkField("chain_id", "Int32", S$RescriptSchema.$$int, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
305
339
|
Table.mkField("event_id", "UInt64", S$RescriptSchema.bigint, undefined, undefined, undefined, undefined, undefined, undefined),
|
|
306
340
|
Table.mkField("event_name", "String", S$RescriptSchema.string, undefined, undefined, undefined, undefined, undefined, undefined),
|
package/src/db/Table.res
CHANGED
|
@@ -109,43 +109,42 @@ let getPgFieldType = (
|
|
|
109
109
|
~pgSchema,
|
|
110
110
|
~isArray,
|
|
111
111
|
~isNumericArrayAsText,
|
|
112
|
-
~isNullable,
|
|
112
|
+
~isNullable as _,
|
|
113
113
|
) => {
|
|
114
114
|
let columnType = switch fieldType {
|
|
115
|
-
| String => (
|
|
116
|
-
| Boolean => (
|
|
117
|
-
| Int32 => (
|
|
118
|
-
| Uint32 => (
|
|
119
|
-
| UInt52 => (
|
|
120
|
-
| UInt64 => (
|
|
121
|
-
| Number => (
|
|
115
|
+
| String => (Pg.Text :> string)
|
|
116
|
+
| Boolean => (Pg.Boolean :> string)
|
|
117
|
+
| Int32 => (Pg.Integer :> string)
|
|
118
|
+
| Uint32 => (Pg.BigInt :> string)
|
|
119
|
+
| UInt52 => (Pg.BigInt :> string)
|
|
120
|
+
| UInt64 => (Pg.BigInt :> string)
|
|
121
|
+
| Number => (Pg.DoublePrecision :> string)
|
|
122
122
|
| BigInt({?precision}) =>
|
|
123
|
-
(
|
|
123
|
+
(Pg.Numeric :> string) ++
|
|
124
124
|
switch precision {
|
|
125
125
|
| Some(precision) => `(${precision->Int.toString}, 0)` // scale is always 0 for BigInt
|
|
126
126
|
| None => ""
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
| BigDecimal({?config}) =>
|
|
130
|
-
(
|
|
130
|
+
(Pg.Numeric :> string) ++
|
|
131
131
|
switch config {
|
|
132
132
|
| Some((precision, scale)) => `(${precision->Int.toString}, ${scale->Int.toString})`
|
|
133
133
|
| None => ""
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
| Serial => (
|
|
137
|
-
| BigSerial => (
|
|
138
|
-
| Json => (
|
|
139
|
-
| Date =>
|
|
140
|
-
(isNullable ? Postgres.TimestampWithTimezoneNull : Postgres.TimestampWithTimezone :> string)
|
|
136
|
+
| Serial => (Pg.Serial :> string)
|
|
137
|
+
| BigSerial => (Pg.BigSerial :> string)
|
|
138
|
+
| Json => (Pg.JsonB :> string)
|
|
139
|
+
| Date => (Pg.TimestampWithTimezone :> string)
|
|
141
140
|
| Enum({config}) => `"${pgSchema}".${config.name}`
|
|
142
|
-
| Entity(_) => (
|
|
141
|
+
| Entity(_) => (Pg.Text :> string) // FIXME: Will it work correctly if id is not a text column?
|
|
143
142
|
}
|
|
144
143
|
|
|
145
144
|
// Workaround for Hasura bug https://github.com/enviodev/hyperindex/issues/788
|
|
146
145
|
let isNumericAsText = isArray && isNumericArrayAsText
|
|
147
|
-
let columnType = if columnType == (
|
|
148
|
-
(
|
|
146
|
+
let columnType = if columnType == (Pg.Numeric :> string) && isNumericAsText {
|
|
147
|
+
(Pg.Text :> string)
|
|
149
148
|
} else {
|
|
150
149
|
columnType
|
|
151
150
|
}
|
|
@@ -162,12 +161,17 @@ type compositeIndexField = {
|
|
|
162
161
|
|
|
163
162
|
type table = {
|
|
164
163
|
tableName: string,
|
|
164
|
+
stmtId: string,
|
|
165
165
|
fields: array<fieldOrDerived>,
|
|
166
166
|
compositeIndices: array<array<compositeIndexField>>,
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
let mkTable = (tableName, ~compositeIndices=[], ~fields) => {
|
|
169
|
+
let mkTable = (tableName, ~stmtId=?, ~compositeIndices=[], ~fields) => {
|
|
170
170
|
tableName,
|
|
171
|
+
stmtId: switch stmtId {
|
|
172
|
+
| Some(id) => id
|
|
173
|
+
| None => tableName
|
|
174
|
+
},
|
|
171
175
|
fields,
|
|
172
176
|
compositeIndices,
|
|
173
177
|
}
|
|
@@ -257,6 +261,7 @@ type sqlParams<'entity> = {
|
|
|
257
261
|
quotedNonPrimaryFieldNames: array<string>,
|
|
258
262
|
arrayFieldTypes: array<string>,
|
|
259
263
|
hasArrayField: bool,
|
|
264
|
+
jsonFieldIndices: array<int>,
|
|
260
265
|
}
|
|
261
266
|
|
|
262
267
|
let toSqlParams = (table: table, ~schema, ~pgSchema) => {
|
|
@@ -264,6 +269,8 @@ let toSqlParams = (table: table, ~schema, ~pgSchema) => {
|
|
|
264
269
|
let quotedNonPrimaryFieldNames = []
|
|
265
270
|
let arrayFieldTypes = []
|
|
266
271
|
let hasArrayField = ref(false)
|
|
272
|
+
let jsonFieldIndices = []
|
|
273
|
+
let fieldIndex = ref(0)
|
|
267
274
|
|
|
268
275
|
let dbSchema: S.t<Js.Dict.t<unknown>> = S.schema(s =>
|
|
269
276
|
switch schema->S.classify {
|
|
@@ -280,16 +287,7 @@ let toSqlParams = (table: table, ~schema, ~pgSchema) => {
|
|
|
280
287
|
hasArrayField := true
|
|
281
288
|
S.array(child->coerceSchema)->S.toUnknown
|
|
282
289
|
}
|
|
283
|
-
| JSON(_) =>
|
|
284
|
-
hasArrayField := true
|
|
285
|
-
schema
|
|
286
|
-
}
|
|
287
|
-
| Bool =>
|
|
288
|
-
// Workaround for https://github.com/porsager/postgres/issues/471
|
|
289
|
-
S.union([
|
|
290
|
-
S.literal(1)->S.shape(_ => true),
|
|
291
|
-
S.literal(0)->S.shape(_ => false),
|
|
292
|
-
])->S.toUnknown
|
|
290
|
+
| JSON(_) => schema
|
|
293
291
|
| _ => schema
|
|
294
292
|
}
|
|
295
293
|
|
|
@@ -298,6 +296,19 @@ let toSqlParams = (table: table, ~schema, ~pgSchema) => {
|
|
|
298
296
|
| None => raise(NonExistingTableField(location))
|
|
299
297
|
}
|
|
300
298
|
|
|
299
|
+
// pg driver doesn't auto-serialize JSONB values like postgres.js did.
|
|
300
|
+
// Track JSON field indices for post-hoc serialization in PgStorage.
|
|
301
|
+
// For JSON fields, use S.unknown in dbSchema to avoid S.unnest+S.compile issues
|
|
302
|
+
// with complex schema types, and handle serialization externally.
|
|
303
|
+
let coercedSchema = switch field {
|
|
304
|
+
| Field({fieldType: Json}) => {
|
|
305
|
+
jsonFieldIndices->Js.Array2.push(fieldIndex.contents)->ignore
|
|
306
|
+
S.unknown
|
|
307
|
+
}
|
|
308
|
+
| _ => schema->coerceSchema
|
|
309
|
+
}
|
|
310
|
+
fieldIndex := fieldIndex.contents + 1
|
|
311
|
+
|
|
301
312
|
quotedFieldNames
|
|
302
313
|
->Js.Array2.push(inlinedLocation)
|
|
303
314
|
->ignore
|
|
@@ -321,15 +332,15 @@ let toSqlParams = (table: table, ~schema, ~pgSchema) => {
|
|
|
321
332
|
~isNumericArrayAsText=false, // TODO: Test whether it should be passed via args and match the column type
|
|
322
333
|
)
|
|
323
334
|
switch f.fieldType {
|
|
324
|
-
| Enum(_) => `${(Text:
|
|
325
|
-
| Boolean =>
|
|
335
|
+
| Enum(_) => `${(Text: Pg.columnType :> string)}[]::${pgFieldType}`
|
|
336
|
+
| Boolean => pgFieldType
|
|
326
337
|
| _ => pgFieldType
|
|
327
338
|
}
|
|
328
|
-
| DerivedFrom(_) => (Text:
|
|
339
|
+
| DerivedFrom(_) => (Text: Pg.columnType :> string) ++ "[]"
|
|
329
340
|
},
|
|
330
341
|
)
|
|
331
342
|
->ignore
|
|
332
|
-
dict->Js.Dict.set(location, s.matches(
|
|
343
|
+
dict->Js.Dict.set(location, s.matches(coercedSchema))
|
|
333
344
|
})
|
|
334
345
|
dict
|
|
335
346
|
| _ => Js.Exn.raiseError("Failed creating db schema. Expected an object schema for table")
|
|
@@ -342,6 +353,7 @@ let toSqlParams = (table: table, ~schema, ~pgSchema) => {
|
|
|
342
353
|
quotedNonPrimaryFieldNames,
|
|
343
354
|
arrayFieldTypes,
|
|
344
355
|
hasArrayField: hasArrayField.contents,
|
|
356
|
+
jsonFieldIndices,
|
|
345
357
|
}
|
|
346
358
|
}
|
|
347
359
|
|
package/src/db/Table.res.mjs
CHANGED
|
@@ -73,7 +73,7 @@ function getFieldName(fieldOrDerived) {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
function getPgFieldType(fieldType, pgSchema, isArray, isNumericArrayAsText,
|
|
76
|
+
function getPgFieldType(fieldType, pgSchema, isArray, isNumericArrayAsText, param) {
|
|
77
77
|
var columnType;
|
|
78
78
|
if (typeof fieldType !== "object") {
|
|
79
79
|
switch (fieldType) {
|
|
@@ -99,7 +99,7 @@ function getPgFieldType(fieldType, pgSchema, isArray, isNumericArrayAsText, isNu
|
|
|
99
99
|
columnType = "JSONB";
|
|
100
100
|
break;
|
|
101
101
|
case "Date" :
|
|
102
|
-
columnType =
|
|
102
|
+
columnType = "TIMESTAMP WITH TIME ZONE";
|
|
103
103
|
break;
|
|
104
104
|
default:
|
|
105
105
|
columnType = "BIGINT";
|
|
@@ -134,10 +134,11 @@ function getPgFieldType(fieldType, pgSchema, isArray, isNumericArrayAsText, isNu
|
|
|
134
134
|
);
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
function mkTable(tableName, compositeIndicesOpt, fields) {
|
|
137
|
+
function mkTable(tableName, stmtId, compositeIndicesOpt, fields) {
|
|
138
138
|
var compositeIndices = compositeIndicesOpt !== undefined ? compositeIndicesOpt : [];
|
|
139
139
|
return {
|
|
140
140
|
tableName: tableName,
|
|
141
|
+
stmtId: stmtId !== undefined ? stmtId : tableName,
|
|
141
142
|
fields: fields,
|
|
142
143
|
compositeIndices: compositeIndices
|
|
143
144
|
};
|
|
@@ -258,6 +259,10 @@ function toSqlParams(table, schema, pgSchema) {
|
|
|
258
259
|
var hasArrayField = {
|
|
259
260
|
contents: false
|
|
260
261
|
};
|
|
262
|
+
var jsonFieldIndices = [];
|
|
263
|
+
var fieldIndex = {
|
|
264
|
+
contents: 0
|
|
265
|
+
};
|
|
261
266
|
var dbSchema = S$RescriptSchema.schema(function (s) {
|
|
262
267
|
var match = schema.t;
|
|
263
268
|
if (typeof match !== "object") {
|
|
@@ -270,39 +275,26 @@ function toSqlParams(table, schema, pgSchema) {
|
|
|
270
275
|
Belt_Array.forEach(match.items, (function (param) {
|
|
271
276
|
var inlinedLocation = param.inlinedLocation;
|
|
272
277
|
var $$location = param.location;
|
|
278
|
+
var schema = param.schema;
|
|
273
279
|
var coerceSchema = function (schema) {
|
|
274
280
|
var child = schema.t;
|
|
275
281
|
if (typeof child !== "object") {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
return S$RescriptSchema.union([
|
|
281
|
-
S$RescriptSchema.shape(S$RescriptSchema.literal(1), (function (param) {
|
|
282
|
-
return true;
|
|
283
|
-
})),
|
|
284
|
-
S$RescriptSchema.shape(S$RescriptSchema.literal(0), (function (param) {
|
|
285
|
-
return false;
|
|
286
|
-
}))
|
|
287
|
-
]);
|
|
288
|
-
default:
|
|
289
|
-
return schema;
|
|
290
|
-
}
|
|
291
|
-
} else {
|
|
292
|
-
switch (child.TAG) {
|
|
293
|
-
case "option" :
|
|
294
|
-
case "null" :
|
|
295
|
-
return S$RescriptSchema.$$null(coerceSchema(child._0));
|
|
296
|
-
case "array" :
|
|
297
|
-
hasArrayField.contents = true;
|
|
298
|
-
return S$RescriptSchema.array(coerceSchema(child._0));
|
|
299
|
-
case "JSON" :
|
|
300
|
-
hasArrayField.contents = true;
|
|
301
|
-
return schema;
|
|
302
|
-
default:
|
|
303
|
-
return schema;
|
|
282
|
+
if (child === "bigint") {
|
|
283
|
+
return $$BigInt.schema;
|
|
284
|
+
} else {
|
|
285
|
+
return schema;
|
|
304
286
|
}
|
|
305
287
|
}
|
|
288
|
+
switch (child.TAG) {
|
|
289
|
+
case "option" :
|
|
290
|
+
case "null" :
|
|
291
|
+
return S$RescriptSchema.$$null(coerceSchema(child._0));
|
|
292
|
+
case "array" :
|
|
293
|
+
hasArrayField.contents = true;
|
|
294
|
+
return S$RescriptSchema.array(coerceSchema(child._0));
|
|
295
|
+
default:
|
|
296
|
+
return schema;
|
|
297
|
+
}
|
|
306
298
|
};
|
|
307
299
|
var field = getFieldByDbName(table, $$location);
|
|
308
300
|
var field$1;
|
|
@@ -315,6 +307,19 @@ function toSqlParams(table, schema, pgSchema) {
|
|
|
315
307
|
Error: new Error()
|
|
316
308
|
};
|
|
317
309
|
}
|
|
310
|
+
var coercedSchema;
|
|
311
|
+
if (field$1.TAG === "Field") {
|
|
312
|
+
var tmp = field$1._0.fieldType;
|
|
313
|
+
if (typeof tmp !== "object" && tmp === "Json") {
|
|
314
|
+
jsonFieldIndices.push(fieldIndex.contents);
|
|
315
|
+
coercedSchema = S$RescriptSchema.unknown;
|
|
316
|
+
} else {
|
|
317
|
+
coercedSchema = coerceSchema(schema);
|
|
318
|
+
}
|
|
319
|
+
} else {
|
|
320
|
+
coercedSchema = coerceSchema(schema);
|
|
321
|
+
}
|
|
322
|
+
fieldIndex.contents = fieldIndex.contents + 1 | 0;
|
|
318
323
|
quotedFieldNames.push(inlinedLocation);
|
|
319
324
|
if (field$1.TAG === "Field") {
|
|
320
325
|
if (field$1._0.isPrimaryKey) {
|
|
@@ -323,21 +328,17 @@ function toSqlParams(table, schema, pgSchema) {
|
|
|
323
328
|
quotedNonPrimaryFieldNames.push(inlinedLocation);
|
|
324
329
|
}
|
|
325
330
|
}
|
|
326
|
-
var tmp;
|
|
331
|
+
var tmp$1;
|
|
327
332
|
if (field$1.TAG === "Field") {
|
|
328
333
|
var f = field$1._0;
|
|
329
334
|
var pgFieldType = getPgFieldType(f.fieldType, pgSchema, true, false, f.isNullable);
|
|
330
335
|
var match = f.fieldType;
|
|
331
|
-
tmp = typeof match !== "object" ?
|
|
332
|
-
match === "Boolean" ? "INTEGER[]::" + pgFieldType : pgFieldType
|
|
333
|
-
) : (
|
|
334
|
-
match.type === "Enum" ? "TEXT[]::" + pgFieldType : pgFieldType
|
|
335
|
-
);
|
|
336
|
+
tmp$1 = typeof match !== "object" || match.type !== "Enum" ? pgFieldType : "TEXT[]::" + pgFieldType;
|
|
336
337
|
} else {
|
|
337
|
-
tmp = "TEXT[]";
|
|
338
|
+
tmp$1 = "TEXT[]";
|
|
338
339
|
}
|
|
339
|
-
arrayFieldTypes.push(tmp);
|
|
340
|
-
dict[$$location] = s.m(
|
|
340
|
+
arrayFieldTypes.push(tmp$1);
|
|
341
|
+
dict[$$location] = s.m(coercedSchema);
|
|
341
342
|
}));
|
|
342
343
|
return dict;
|
|
343
344
|
});
|
|
@@ -346,7 +347,8 @@ function toSqlParams(table, schema, pgSchema) {
|
|
|
346
347
|
quotedFieldNames: quotedFieldNames,
|
|
347
348
|
quotedNonPrimaryFieldNames: quotedNonPrimaryFieldNames,
|
|
348
349
|
arrayFieldTypes: arrayFieldTypes,
|
|
349
|
-
hasArrayField: hasArrayField.contents
|
|
350
|
+
hasArrayField: hasArrayField.contents,
|
|
351
|
+
jsonFieldIndices: jsonFieldIndices
|
|
350
352
|
};
|
|
351
353
|
}
|
|
352
354
|
|
package/src/PgStorage.gen.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/* TypeScript file generated from PgStorage.res by genType. */
|
|
2
|
-
|
|
3
|
-
/* eslint-disable */
|
|
4
|
-
/* tslint:disable */
|
|
5
|
-
|
|
6
|
-
import * as PgStorageJS from './PgStorage.res.mjs';
|
|
7
|
-
|
|
8
|
-
import type {sql as Postgres_sql} from '../src/bindings/Postgres.gen.js';
|
|
9
|
-
|
|
10
|
-
export const makeClient: () => Postgres_sql = PgStorageJS.makeClient as any;
|
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
// Only needed for some old tests
|
|
2
|
-
// Remove @genType in the future
|
|
3
|
-
@genType.import(("postgres", "Sql"))
|
|
4
|
-
type sql
|
|
5
|
-
|
|
6
|
-
type undefinedTransform = | @as(undefined) Undefined | @as(null) Null
|
|
7
|
-
|
|
8
|
-
type transformConfig = {
|
|
9
|
-
undefined?: undefinedTransform, // Transforms undefined values (eg. to null) (default: undefined)
|
|
10
|
-
// column?: 'c => 'd, // Transforms incoming column names (default: fn)
|
|
11
|
-
// value?: 'e => 'f, // Transforms incoming row values (default: fn)
|
|
12
|
-
// row?: 'g => 'h, // Transforms entire rows (default: fn)
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
type connectionConfig = {
|
|
16
|
-
applicationName?: string, // Default application_name (default: 'postgres.js')
|
|
17
|
-
// Other connection parameters, see https://www.postgresql.org/docs/current/runtime-config-client.html
|
|
18
|
-
}
|
|
19
|
-
type streamDuplex
|
|
20
|
-
type buffer
|
|
21
|
-
type secureContext
|
|
22
|
-
|
|
23
|
-
type onread = {
|
|
24
|
-
buffer: Js.Nullable.t<array<int>> => array<int>,
|
|
25
|
-
callback: (int, array<int>) => unit,
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
type tlsConnectOptions = {
|
|
29
|
-
enableTrace?: bool,
|
|
30
|
-
host?: string /* Default: "localhost" */,
|
|
31
|
-
port?: int,
|
|
32
|
-
path?: string,
|
|
33
|
-
socket?: streamDuplex,
|
|
34
|
-
allowHalfOpen?: bool /* Default: false */,
|
|
35
|
-
rejectUnauthorized?: bool /* Default: true */,
|
|
36
|
-
pskCallback?: unit => unit,
|
|
37
|
-
@as("ALPNProtocols") alpnProtocols?: array<string>, //| array<Buffer> | array<typedArray> | array<DataView> | Buffer | typedArray | DataView,
|
|
38
|
-
servername?: string,
|
|
39
|
-
checkServerIdentity?: 'a. (string, 'a) => option<Js.Exn.t>,
|
|
40
|
-
session?: buffer,
|
|
41
|
-
minDHSize?: int /* Default: 1024 */,
|
|
42
|
-
highWaterMark?: int /* Default: 16 * 1024 */,
|
|
43
|
-
secureContext?: secureContext,
|
|
44
|
-
onread?: onread,
|
|
45
|
-
/* Additional properties from tls.createSecureContext() and socket.connect() */
|
|
46
|
-
// [key: string]: Js.Json.t,
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
@unboxed
|
|
50
|
-
type sslOptions =
|
|
51
|
-
| Bool(bool)
|
|
52
|
-
| TLSConnectOptions(tlsConnectOptions)
|
|
53
|
-
| @as("require") Require
|
|
54
|
-
| @as("allow") Allow
|
|
55
|
-
| @as("prefer") Prefer
|
|
56
|
-
| @as("verify-full") VerifyFull
|
|
57
|
-
|
|
58
|
-
let sslOptionsSchema: S.schema<sslOptions> = S.enum([
|
|
59
|
-
Bool(true),
|
|
60
|
-
Bool(false),
|
|
61
|
-
Require,
|
|
62
|
-
Allow,
|
|
63
|
-
Prefer,
|
|
64
|
-
VerifyFull,
|
|
65
|
-
//No schema created for tlsConnectOptions obj
|
|
66
|
-
])
|
|
67
|
-
|
|
68
|
-
type poolConfig = {
|
|
69
|
-
host?: string, // Postgres ip address[es] or domain name[s] (default: '')
|
|
70
|
-
port?: int, // Postgres server port[s] (default: 5432)
|
|
71
|
-
path?: string, // unix socket path (usually '/tmp') (default: '')
|
|
72
|
-
database?: string, // Name of database to connect to (default: '')
|
|
73
|
-
username?: string, // Username of database user (default: '')
|
|
74
|
-
password?: string, // Password of database user (default: '')
|
|
75
|
-
ssl?: sslOptions, // true, prefer, require, tls.connect options (default: false)
|
|
76
|
-
max?: int, // Max number of connections (default: 10)
|
|
77
|
-
maxLifetime?: option<int>, // Max lifetime in seconds (more info below) (default: null)
|
|
78
|
-
idleTimeout?: int, // Idle connection timeout in seconds (default: 0)
|
|
79
|
-
connectTimeout?: int, // Connect timeout in seconds (default: 30)
|
|
80
|
-
prepare?: bool, // Automatic creation of prepared statements (default: true)
|
|
81
|
-
onnotice?: string => unit, // Default console.log, set false to silence NOTICE (default: fn)
|
|
82
|
-
onParameter?: (string, string) => unit, // (key, value) when server param change (default: fn)
|
|
83
|
-
debug?: (~connection: unknown, ~query: unknown, ~params: unknown, ~types: unknown) => unit, // Is called with (connection, query, params, types)
|
|
84
|
-
socket?: unit => unit, // fn returning custom socket to use (default: fn)
|
|
85
|
-
transform?: transformConfig,
|
|
86
|
-
connection?: connectionConfig,
|
|
87
|
-
targetSessionAttrs?: option<string>, // Use 'read-write' with multiple hosts to ensure only connecting to primary (default: null)
|
|
88
|
-
fetchTypes?: bool, // Automatically fetches types on connect on initial connection. (default: true)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
@module("postgres")
|
|
92
|
-
external makeSql: (~config: poolConfig) => sql = "default"
|
|
93
|
-
|
|
94
|
-
@send external beginSql: (sql, sql => promise<'result>) => promise<'result> = "begin"
|
|
95
|
-
|
|
96
|
-
// TODO: can explore this approach (https://forum.rescript-lang.org/t/rfc-support-for-tagged-template-literals/3744)
|
|
97
|
-
// @send @variadic
|
|
98
|
-
// external sql: array<string> => (sql, array<string>) => int = "sql"
|
|
99
|
-
|
|
100
|
-
@send external unsafe: (sql, string) => promise<'a> = "unsafe"
|
|
101
|
-
@send external unpreparedUnsafe: (sql, string, unknown) => promise<'a> = "unsafe"
|
|
102
|
-
@send
|
|
103
|
-
external preparedUnsafe: (sql, string, unknown, @as(json`{prepare: true}`) _) => promise<'a> =
|
|
104
|
-
"unsafe"
|
|
105
|
-
|
|
106
|
-
@unboxed
|
|
107
|
-
type columnType =
|
|
108
|
-
| @as("INTEGER") Integer
|
|
109
|
-
| @as("BIGINT") BigInt
|
|
110
|
-
| @as("BOOLEAN") Boolean
|
|
111
|
-
| @as("NUMERIC") Numeric
|
|
112
|
-
| @as("DOUBLE PRECISION") DoublePrecision
|
|
113
|
-
| @as("TEXT") Text
|
|
114
|
-
| @as("SERIAL") Serial
|
|
115
|
-
| @as("BIGSERIAL") BigSerial
|
|
116
|
-
| @as("JSONB") JsonB
|
|
117
|
-
| @as("TIMESTAMP WITH TIME ZONE") TimestampWithTimezone
|
|
118
|
-
| @as("TIMESTAMP WITH TIME ZONE NULL") TimestampWithTimezoneNull
|
|
119
|
-
| @as("TIMESTAMP") TimestampWithoutTimezone
|
|
120
|
-
| Custom(string)
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
-
|
|
3
|
-
import * as S$RescriptSchema from "rescript-schema/src/S.res.mjs";
|
|
4
|
-
|
|
5
|
-
var sslOptionsSchema = S$RescriptSchema.$$enum([
|
|
6
|
-
true,
|
|
7
|
-
false,
|
|
8
|
-
"require",
|
|
9
|
-
"allow",
|
|
10
|
-
"prefer",
|
|
11
|
-
"verify-full"
|
|
12
|
-
]);
|
|
13
|
-
|
|
14
|
-
export {
|
|
15
|
-
sslOptionsSchema ,
|
|
16
|
-
}
|
|
17
|
-
/* sslOptionsSchema Not a pure module */
|