@peerbit/indexer-sqlite3 2.1.0-369b236 → 2.1.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.
package/dist/index.min.js CHANGED
@@ -20355,54 +20355,6 @@ 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
- };
20406
20358
  var getSQLTable = (ctor, path, primary, inline, addJoinField, fromOptionalField = false) => {
20407
20359
  let clazzes = getDependencies(ctor, 0);
20408
20360
  if (!clazzes || clazzes.length === 0) {
@@ -20486,20 +20438,10 @@ var getSQLFields = (tableName, path, ctor, primary, addJoinFieldFromParent, tabl
20486
20438
  const sqlFields = [];
20487
20439
  const sqlConstraints = [];
20488
20440
  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
- };
20502
20441
  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";
20503
20445
  fields2.unshift(
20504
20446
  {
20505
20447
  name: CHILD_TABLE_ID,
@@ -20515,17 +20457,17 @@ var getSQLFields = (tableName, path, ctor, primary, addJoinFieldFromParent, tabl
20515
20457
  {
20516
20458
  name: PARENT_TABLE_ID,
20517
20459
  key: PARENT_TABLE_ID,
20518
- definition: `${PARENT_TABLE_ID} ${parentPrimaryFieldInfo.type}`,
20519
- type: parentPrimaryFieldInfo.type,
20520
- from: parentPrimaryFieldInfo.from,
20521
- unwrappedType: parentPrimaryFieldInfo.unwrappedType,
20460
+ definition: `${PARENT_TABLE_ID} ${parentPrimaryFieldType}`,
20461
+ type: parentPrimaryFieldType,
20462
+ from: parentPrimaryField?.from,
20463
+ unwrappedType: parentPrimaryField?.unwrappedType,
20522
20464
  isPrimary: false,
20523
20465
  path: [PARENT_TABLE_ID]
20524
20466
  }
20525
20467
  );
20526
20468
  contstraints.push({
20527
20469
  name: `${PARENT_TABLE_ID}_fk`,
20528
- definition: `CONSTRAINT ${PARENT_TABLE_ID}_fk FOREIGN KEY(${PARENT_TABLE_ID}) REFERENCES ${tableName}(${parentPrimaryFieldInfo.name}) ON DELETE CASCADE`
20470
+ definition: `CONSTRAINT ${PARENT_TABLE_ID}_fk FOREIGN KEY(${PARENT_TABLE_ID}) REFERENCES ${tableName}(${parentPrimaryFieldName}) ON DELETE CASCADE`
20529
20471
  });
20530
20472
  };
20531
20473
  const handleNestedType = (key, field2) => {
@@ -20604,17 +20546,28 @@ var getSQLFields = (tableName, path, ctor, primary, addJoinFieldFromParent, tabl
20604
20546
  }
20605
20547
  }
20606
20548
  };
20607
- const handleSimpleField = (field2, type, isOptional2) => {
20608
- const sqlField = createScalarSQLField(path, field2, type, primary, isOptional2);
20609
- foundPrimary = foundPrimary || sqlField.isPrimary;
20610
- sqlFields.push(sqlField);
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
20564
  };
20612
20565
  const handleField = (key, field2, type, isOptional2) => {
20613
20566
  if (type instanceof FixedArrayKind && type.elementType === "u8") {
20614
20567
  type = Uint8Array;
20615
20568
  }
20616
20569
  if (typeof type === "string" || type === Uint8Array) {
20617
- handleSimpleField(field2, type, true);
20570
+ handleSimpleField(key, field2, type, true);
20618
20571
  } else if (typeof type === "function" && clazzCanBeInlined(type)) {
20619
20572
  const subPath = [...path, key];
20620
20573
  const subtables = getSQLTable(type, subPath, false, true, addJoinFields, isOptional2);