envio 2.26.0-alpha.9 → 2.26.0-rc.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.
@@ -72,10 +72,11 @@ function getFieldType(field) {
72
72
  );
73
73
  }
74
74
 
75
- function mkTable(tableName, compositeIndicesOpt, fields) {
75
+ function mkTable(tableName, schemaName, compositeIndicesOpt, fields) {
76
76
  var compositeIndices = compositeIndicesOpt !== undefined ? compositeIndicesOpt : [];
77
77
  return {
78
78
  tableName: tableName,
79
+ schemaName: schemaName,
79
80
  fields: fields,
80
81
  compositeIndices: compositeIndices
81
82
  };
@@ -183,7 +184,7 @@ function getUnfilteredCompositeIndicesUnsafe(table) {
183
184
  }));
184
185
  }
185
186
 
186
- function toSqlParams(table, schema, pgSchema) {
187
+ function toSqlParams(table, schema) {
187
188
  var quotedFieldNames = [];
188
189
  var quotedNonPrimaryFieldNames = [];
189
190
  var arrayFieldTypes = [];
@@ -261,7 +262,7 @@ function toSqlParams(table, schema, pgSchema) {
261
262
  var fieldType = f.fieldType;
262
263
  tmp = fieldType === "TIMESTAMP" || fieldType === "TIMESTAMP WITH TIME ZONE" || fieldType === "JSONB" || fieldType === "SERIAL" || fieldType === "TEXT" || fieldType === "DOUBLE PRECISION" || fieldType === "NUMERIC" || fieldType === "BOOLEAN" || fieldType === "INTEGER" || fieldType === "TIMESTAMP WITH TIME ZONE NULL" ? (
263
264
  fieldType === "BOOLEAN" ? "INTEGER[]::" + f.fieldType : fieldType
264
- ) : "TEXT[]::\"" + pgSchema + "\"." + fieldType;
265
+ ) : "TEXT[]::" + fieldType;
265
266
  } else {
266
267
  tmp = "TEXT";
267
268
  }
@@ -301,6 +302,43 @@ function getCompositeIndices(table) {
301
302
  }));
302
303
  }
303
304
 
305
+ function makeBatchSetFnString(table) {
306
+ var fieldNamesInQuotes = Belt_Array.map(getNonDefaultFieldNames(table), (function (fieldName) {
307
+ return "\"" + fieldName + "\"";
308
+ }));
309
+ return "(sql, rows) => {\n return sql\`\n INSERT INTO \"" + table.schemaName + "\".\"" + table.tableName + "\"\n \${sql(rows, " + fieldNamesInQuotes.join(", ") + ")}\n ON CONFLICT(" + getPrimaryKeyFieldNames(table).join(", ") + ") DO UPDATE\n SET\n " + Belt_Array.map(fieldNamesInQuotes, (function (fieldNameInQuotes) {
310
+ return fieldNameInQuotes + " = EXCLUDED." + fieldNameInQuotes;
311
+ })).join(", ") + ";\`\n }";
312
+ }
313
+
314
+ function chunkBatchQuery(sql, entityDataArray, queryToExecute, maxItemsPerQueryOpt) {
315
+ var maxItemsPerQuery = maxItemsPerQueryOpt !== undefined ? maxItemsPerQueryOpt : 500;
316
+ var responses = [];
317
+ var i = 0;
318
+ while(i < entityDataArray.length) {
319
+ var chunk = entityDataArray.slice(i, i + maxItemsPerQuery | 0);
320
+ var response = queryToExecute(sql, chunk);
321
+ responses.push(response);
322
+ i = i + maxItemsPerQuery | 0;
323
+ };
324
+ return Promise.all(responses);
325
+ }
326
+
327
+ function makeBatchSetFn(table, schema) {
328
+ var batchSetFn = eval(makeBatchSetFnString(table));
329
+ var parseOrThrow = S$RescriptSchema.compile(S$RescriptSchema.array(schema), "Output", "Json", "Sync", true);
330
+ return async function (sql, rows) {
331
+ var rowsJson = parseOrThrow(rows);
332
+ await chunkBatchQuery(sql, rowsJson, batchSetFn, undefined);
333
+ };
334
+ }
335
+
336
+ var PostgresInterop = {
337
+ makeBatchSetFnString: makeBatchSetFnString,
338
+ chunkBatchQuery: chunkBatchQuery,
339
+ makeBatchSetFn: makeBatchSetFn
340
+ };
341
+
304
342
  exports.mkField = mkField;
305
343
  exports.mkDerivedFromField = mkDerivedFromField;
306
344
  exports.getUserDefinedFieldName = getUserDefinedFieldName;
@@ -323,4 +361,5 @@ exports.getUnfilteredCompositeIndicesUnsafe = getUnfilteredCompositeIndicesUnsaf
323
361
  exports.toSqlParams = toSqlParams;
324
362
  exports.getSingleIndices = getSingleIndices;
325
363
  exports.getCompositeIndices = getCompositeIndices;
364
+ exports.PostgresInterop = PostgresInterop;
326
365
  /* BigInt Not a pure module */