@peerbit/indexer-sqlite3 2.1.0 → 2.1.1-2ed894b

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/dist/index.min.js CHANGED
@@ -20355,6 +20355,58 @@ var toSQLType = (type, isOptional = false) => {
20355
20355
  }
20356
20356
  return isOptional ? ret : ret + " NOT NULL";
20357
20357
  };
20358
+ var createScalarSQLField = (path, field2, type, primary, isOptional) => {
20359
+ const name = getInlineTableFieldName(path.slice(1), field2.key);
20360
+ const isPrimary = primary !== false && name === primary;
20361
+ const sqlType = toSQLType(type, isOptional);
20362
+ return {
20363
+ name,
20364
+ key: field2.key,
20365
+ definition: `${escapeColumnName(name)} ${sqlType} ${isPrimary ? "PRIMARY KEY" : ""}`,
20366
+ type: sqlType,
20367
+ isPrimary,
20368
+ from: field2,
20369
+ unwrappedType: unwrapNestedType(field2.type),
20370
+ path: [...path.slice(1), field2.key]
20371
+ };
20372
+ };
20373
+ var resolvePrimaryFieldInfoFromSchema = (ctor, path, primary) => {
20374
+ const schema = getSchema(ctor);
20375
+ if (!schema) {
20376
+ return void 0;
20377
+ }
20378
+ for (const field2 of schema.fields) {
20379
+ let fieldType = field2.type;
20380
+ if (fieldType instanceof OptionKind) {
20381
+ fieldType = fieldType.elementType;
20382
+ }
20383
+ if (fieldType instanceof FixedArrayKind && fieldType.elementType === "u8") {
20384
+ fieldType = Uint8Array;
20385
+ } else {
20386
+ fieldType = unwrapNestedType(fieldType);
20387
+ }
20388
+ if (fieldType instanceof VecKind) {
20389
+ continue;
20390
+ }
20391
+ if (typeof fieldType === "string" || isUint8ArrayType(fieldType)) {
20392
+ const sqlField = createScalarSQLField(path, field2, fieldType, primary, true);
20393
+ if (sqlField.isPrimary) {
20394
+ return {
20395
+ name: sqlField.name,
20396
+ type: sqlField.type,
20397
+ from: sqlField.from,
20398
+ unwrappedType: sqlField.unwrappedType
20399
+ };
20400
+ }
20401
+ } else if (typeof fieldType === "function" && clazzCanBeInlined(fieldType)) {
20402
+ const nested = resolvePrimaryFieldInfoFromSchema(fieldType, [...path, field2.key], primary);
20403
+ if (nested) {
20404
+ return nested;
20405
+ }
20406
+ }
20407
+ }
20408
+ return void 0;
20409
+ };
20358
20410
  var getSQLTable = (ctor, path, primary, inline, addJoinField, fromOptionalField = false) => {
20359
20411
  let clazzes = getDependencies(ctor, 0);
20360
20412
  if (!clazzes || clazzes.length === 0) {
@@ -20438,10 +20490,20 @@ var getSQLFields = (tableName, path, ctor, primary, addJoinFieldFromParent, tabl
20438
20490
  const sqlFields = [];
20439
20491
  const sqlConstraints = [];
20440
20492
  let foundPrimary = false;
20493
+ const parentPrimaryFieldInfo = primary === false || primary === CHILD_TABLE_ID ? {
20494
+ name: CHILD_TABLE_ID,
20495
+ type: "INTEGER",
20496
+ from: void 0,
20497
+ unwrappedType: void 0
20498
+ } : resolvePrimaryFieldInfoFromSchema(ctor, path, primary) || {
20499
+ // Fallback: nested tables use synthetic integer ids, and we allow that
20500
+ // primary to be unresolved here.
20501
+ name: primary,
20502
+ type: "INTEGER",
20503
+ from: void 0,
20504
+ unwrappedType: void 0
20505
+ };
20441
20506
  const addJoinFields = primary === false ? addJoinFieldFromParent : (fields2, contstraints) => {
20442
- const parentPrimaryField = primary != null ? sqlFields.find((field2) => field2.name === primary) : void 0;
20443
- const parentPrimaryFieldName = parentPrimaryField?.name || CHILD_TABLE_ID;
20444
- const parentPrimaryFieldType = parentPrimaryField ? parentPrimaryField.type : "INTEGER";
20445
20507
  fields2.unshift(
20446
20508
  {
20447
20509
  name: CHILD_TABLE_ID,
@@ -20457,17 +20519,17 @@ var getSQLFields = (tableName, path, ctor, primary, addJoinFieldFromParent, tabl
20457
20519
  {
20458
20520
  name: PARENT_TABLE_ID,
20459
20521
  key: PARENT_TABLE_ID,
20460
- definition: `${PARENT_TABLE_ID} ${parentPrimaryFieldType}`,
20461
- type: parentPrimaryFieldType,
20462
- from: parentPrimaryField?.from,
20463
- unwrappedType: parentPrimaryField?.unwrappedType,
20522
+ definition: `${PARENT_TABLE_ID} ${parentPrimaryFieldInfo.type}`,
20523
+ type: parentPrimaryFieldInfo.type,
20524
+ from: parentPrimaryFieldInfo.from,
20525
+ unwrappedType: parentPrimaryFieldInfo.unwrappedType,
20464
20526
  isPrimary: false,
20465
20527
  path: [PARENT_TABLE_ID]
20466
20528
  }
20467
20529
  );
20468
20530
  contstraints.push({
20469
20531
  name: `${PARENT_TABLE_ID}_fk`,
20470
- definition: `CONSTRAINT ${PARENT_TABLE_ID}_fk FOREIGN KEY(${PARENT_TABLE_ID}) REFERENCES ${tableName}(${parentPrimaryFieldName}) ON DELETE CASCADE`
20532
+ definition: `CONSTRAINT ${PARENT_TABLE_ID}_fk FOREIGN KEY(${PARENT_TABLE_ID}) REFERENCES ${tableName}(${parentPrimaryFieldInfo.name}) ON DELETE CASCADE`
20471
20533
  });
20472
20534
  };
20473
20535
  const handleNestedType = (key, field2) => {
@@ -20546,28 +20608,17 @@ var getSQLFields = (tableName, path, ctor, primary, addJoinFieldFromParent, tabl
20546
20608
  }
20547
20609
  }
20548
20610
  };
20549
- const handleSimpleField = (key, field2, type, isOptional2) => {
20550
- let keyString = getInlineTableFieldName(path.slice(1), key);
20551
- const isPrimary = primary != null && keyString === primary;
20552
- foundPrimary = foundPrimary || isPrimary;
20553
- const fieldType = toSQLType(type, isOptional2);
20554
- sqlFields.push({
20555
- name: keyString,
20556
- key,
20557
- definition: `${escapeColumnName(keyString)} ${fieldType} ${isPrimary ? "PRIMARY KEY" : ""}`,
20558
- type: fieldType,
20559
- isPrimary,
20560
- from: field2,
20561
- unwrappedType: unwrapNestedType(field2.type),
20562
- path: [...path.slice(1), key]
20563
- });
20611
+ const handleSimpleField = (field2, type, isOptional2) => {
20612
+ const sqlField = createScalarSQLField(path, field2, type, primary, isOptional2);
20613
+ foundPrimary = foundPrimary || sqlField.isPrimary;
20614
+ sqlFields.push(sqlField);
20564
20615
  };
20565
20616
  const handleField = (key, field2, type, isOptional2) => {
20566
20617
  if (type instanceof FixedArrayKind && type.elementType === "u8") {
20567
20618
  type = Uint8Array;
20568
20619
  }
20569
20620
  if (typeof type === "string" || type === Uint8Array) {
20570
- handleSimpleField(key, field2, type, true);
20621
+ handleSimpleField(field2, type, true);
20571
20622
  } else if (typeof type === "function" && clazzCanBeInlined(type)) {
20572
20623
  const subPath = [...path, key];
20573
20624
  const subtables = getSQLTable(type, subPath, false, true, addJoinFields, isOptional2);