@peerbit/indexer-sqlite3 2.0.2-e209d2e → 2.1.0-369b236

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,54 @@ 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
+ fieldType = unwrapNestedType(fieldType);
20384
+ if (fieldType instanceof VecKind) {
20385
+ continue;
20386
+ }
20387
+ if (typeof fieldType === "string" || isUint8ArrayType(fieldType)) {
20388
+ const sqlField = createScalarSQLField(path, field2, fieldType, primary, true);
20389
+ if (sqlField.isPrimary) {
20390
+ return {
20391
+ name: sqlField.name,
20392
+ type: sqlField.type,
20393
+ from: sqlField.from,
20394
+ unwrappedType: sqlField.unwrappedType
20395
+ };
20396
+ }
20397
+ } else if (typeof fieldType === "function" && clazzCanBeInlined(fieldType)) {
20398
+ const nested = resolvePrimaryFieldInfoFromSchema(fieldType, [...path, field2.key], primary);
20399
+ if (nested) {
20400
+ return nested;
20401
+ }
20402
+ }
20403
+ }
20404
+ return void 0;
20405
+ };
20358
20406
  var getSQLTable = (ctor, path, primary, inline, addJoinField, fromOptionalField = false) => {
20359
20407
  let clazzes = getDependencies(ctor, 0);
20360
20408
  if (!clazzes || clazzes.length === 0) {
@@ -20438,10 +20486,20 @@ var getSQLFields = (tableName, path, ctor, primary, addJoinFieldFromParent, tabl
20438
20486
  const sqlFields = [];
20439
20487
  const sqlConstraints = [];
20440
20488
  let foundPrimary = false;
20489
+ const parentPrimaryFieldInfo = primary === false || primary === CHILD_TABLE_ID ? {
20490
+ name: CHILD_TABLE_ID,
20491
+ type: "INTEGER",
20492
+ from: void 0,
20493
+ unwrappedType: void 0
20494
+ } : resolvePrimaryFieldInfoFromSchema(ctor, path, primary) || {
20495
+ // Fallback: nested tables use synthetic integer ids, and we allow that
20496
+ // primary to be unresolved here.
20497
+ name: primary,
20498
+ type: "INTEGER",
20499
+ from: void 0,
20500
+ unwrappedType: void 0
20501
+ };
20441
20502
  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
20503
  fields2.unshift(
20446
20504
  {
20447
20505
  name: CHILD_TABLE_ID,
@@ -20457,17 +20515,17 @@ var getSQLFields = (tableName, path, ctor, primary, addJoinFieldFromParent, tabl
20457
20515
  {
20458
20516
  name: PARENT_TABLE_ID,
20459
20517
  key: PARENT_TABLE_ID,
20460
- definition: `${PARENT_TABLE_ID} ${parentPrimaryFieldType}`,
20461
- type: parentPrimaryFieldType,
20462
- from: parentPrimaryField?.from,
20463
- unwrappedType: parentPrimaryField?.unwrappedType,
20518
+ definition: `${PARENT_TABLE_ID} ${parentPrimaryFieldInfo.type}`,
20519
+ type: parentPrimaryFieldInfo.type,
20520
+ from: parentPrimaryFieldInfo.from,
20521
+ unwrappedType: parentPrimaryFieldInfo.unwrappedType,
20464
20522
  isPrimary: false,
20465
20523
  path: [PARENT_TABLE_ID]
20466
20524
  }
20467
20525
  );
20468
20526
  contstraints.push({
20469
20527
  name: `${PARENT_TABLE_ID}_fk`,
20470
- definition: `CONSTRAINT ${PARENT_TABLE_ID}_fk FOREIGN KEY(${PARENT_TABLE_ID}) REFERENCES ${tableName}(${parentPrimaryFieldName}) ON DELETE CASCADE`
20528
+ definition: `CONSTRAINT ${PARENT_TABLE_ID}_fk FOREIGN KEY(${PARENT_TABLE_ID}) REFERENCES ${tableName}(${parentPrimaryFieldInfo.name}) ON DELETE CASCADE`
20471
20529
  });
20472
20530
  };
20473
20531
  const handleNestedType = (key, field2) => {
@@ -20546,28 +20604,17 @@ var getSQLFields = (tableName, path, ctor, primary, addJoinFieldFromParent, tabl
20546
20604
  }
20547
20605
  }
20548
20606
  };
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
- });
20607
+ const handleSimpleField = (field2, type, isOptional2) => {
20608
+ const sqlField = createScalarSQLField(path, field2, type, primary, isOptional2);
20609
+ foundPrimary = foundPrimary || sqlField.isPrimary;
20610
+ sqlFields.push(sqlField);
20564
20611
  };
20565
20612
  const handleField = (key, field2, type, isOptional2) => {
20566
20613
  if (type instanceof FixedArrayKind && type.elementType === "u8") {
20567
20614
  type = Uint8Array;
20568
20615
  }
20569
20616
  if (typeof type === "string" || type === Uint8Array) {
20570
- handleSimpleField(key, field2, type, true);
20617
+ handleSimpleField(field2, type, true);
20571
20618
  } else if (typeof type === "function" && clazzCanBeInlined(type)) {
20572
20619
  const subPath = [...path, key];
20573
20620
  const subtables = getSQLTable(type, subPath, false, true, addJoinFields, isOptional2);