edinburgh 0.5.0 → 0.6.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.
Files changed (72) hide show
  1. package/README.md +322 -262
  2. package/build/src/datapack.d.ts +9 -9
  3. package/build/src/datapack.js +9 -9
  4. package/build/src/edinburgh.d.ts +18 -7
  5. package/build/src/edinburgh.js +30 -51
  6. package/build/src/edinburgh.js.map +1 -1
  7. package/build/src/indexes.d.ts +85 -205
  8. package/build/src/indexes.js +150 -503
  9. package/build/src/indexes.js.map +1 -1
  10. package/build/src/migrate.js +8 -10
  11. package/build/src/migrate.js.map +1 -1
  12. package/build/src/models.d.ts +152 -107
  13. package/build/src/models.js +433 -144
  14. package/build/src/models.js.map +1 -1
  15. package/build/src/types.d.ts +30 -48
  16. package/build/src/types.js +25 -24
  17. package/build/src/types.js.map +1 -1
  18. package/build/src/utils.d.ts +4 -4
  19. package/build/src/utils.js +4 -4
  20. package/package.json +1 -1
  21. package/skill/AnyModelClass.md +7 -0
  22. package/skill/FindOptions.md +37 -0
  23. package/skill/Lifecycle Hooks.md +24 -0
  24. package/skill/{Model_delete.md → Lifecycle Hooks_delete.md } +1 -1
  25. package/skill/{Model_getPrimaryKeyHash.md → Lifecycle Hooks_getPrimaryKeyHash.md } +1 -1
  26. package/skill/{Model_isValid.md → Lifecycle Hooks_isValid.md } +1 -1
  27. package/skill/Lifecycle Hooks_migrate.md +26 -0
  28. package/skill/{Model_preCommit.md → Lifecycle Hooks_preCommit.md } +2 -2
  29. package/skill/{Model_preventPersist.md → Lifecycle Hooks_preventPersist.md } +1 -1
  30. package/skill/{Model_validate.md → Lifecycle Hooks_validate.md } +2 -2
  31. package/skill/ModelBase.md +7 -0
  32. package/skill/ModelClass.md +8 -0
  33. package/skill/SKILL.md +180 -132
  34. package/skill/Schema Evolution.md +19 -0
  35. package/skill/TypeWrapper_containsNull.md +11 -0
  36. package/skill/TypeWrapper_deserialize.md +9 -0
  37. package/skill/TypeWrapper_getError.md +11 -0
  38. package/skill/TypeWrapper_serialize.md +10 -0
  39. package/skill/TypeWrapper_serializeType.md +9 -0
  40. package/skill/array.md +2 -2
  41. package/skill/defineModel.md +3 -2
  42. package/skill/deleteEverything.md +8 -0
  43. package/skill/field.md +3 -3
  44. package/skill/link.md +3 -3
  45. package/skill/literal.md +1 -1
  46. package/skill/opt.md +1 -1
  47. package/skill/or.md +1 -1
  48. package/skill/record.md +1 -1
  49. package/skill/set.md +2 -2
  50. package/skill/setOnSaveCallback.md +2 -2
  51. package/skill/transact.md +1 -1
  52. package/src/datapack.ts +9 -9
  53. package/src/edinburgh.ts +43 -52
  54. package/src/indexes.ts +251 -599
  55. package/src/migrate.ts +9 -10
  56. package/src/models.ts +528 -231
  57. package/src/types.ts +36 -34
  58. package/src/utils.ts +4 -4
  59. package/skill/BaseIndex.md +0 -16
  60. package/skill/BaseIndex_batchProcess.md +0 -10
  61. package/skill/BaseIndex_find.md +0 -7
  62. package/skill/BaseIndex_find_2.md +0 -7
  63. package/skill/BaseIndex_find_3.md +0 -7
  64. package/skill/BaseIndex_find_4.md +0 -7
  65. package/skill/Model.md +0 -20
  66. package/skill/Model_batchProcess.md +0 -8
  67. package/skill/Model_migrate.md +0 -32
  68. package/skill/Model_replaceInto.md +0 -16
  69. package/skill/NonPrimaryIndex.md +0 -10
  70. package/skill/SecondaryIndex.md +0 -9
  71. package/skill/UniqueIndex.md +0 -9
  72. package/skill/dump.md +0 -8
package/src/migrate.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import * as lowlevel from "olmdb/lowlevel";
2
2
  import DataPack from "./datapack.js";
3
- import { modelRegistry, currentTxn, Transaction } from "./models.js";
3
+ import { currentTxn, type Transaction, transact } from "./edinburgh.js";
4
+ import { modelRegistry } from "./models.js";
4
5
  import { dbDel, toBuffer, bytesEqual } from "./utils.js";
5
6
  import { deserializeType, TypeWrapper } from "./types.js";
6
- import { transact } from "./edinburgh.js";
7
7
 
8
8
  const INDEX_ID_PREFIX = -2;
9
9
 
@@ -123,7 +123,7 @@ export async function runMigration(options: MigrationOptions = {}): Promise<Migr
123
123
  if (options.tables && !options.tables.includes(model.tableName)) continue;
124
124
  knownIndexIds.add(model._indexId!);
125
125
  modelByPkIndexId.set(model._indexId!, model);
126
- for (const sec of model._secondaries || []) {
126
+ for (const sec of Object.values(model._secondaries || {})) {
127
127
  knownIndexIds.add(sec._indexId!);
128
128
  }
129
129
  }
@@ -157,7 +157,7 @@ export async function runMigration(options: MigrationOptions = {}): Promise<Migr
157
157
  let secondaryCount = 0;
158
158
  let rewrittenCount = 0;
159
159
  const migrateFn = (model as any).migrate as ((record: Record<string, any>) => void) | undefined;
160
- const secondaries = model._secondaries || [];
160
+ const secondaries = Object.values(model._secondaries || {});
161
161
 
162
162
  await forEachRow(indexId, (txn, keyBuf, valueBuf) => {
163
163
  const valuePack = new DataPack(valueBuf);
@@ -170,7 +170,7 @@ export async function runMigration(options: MigrationOptions = {}): Promise<Migr
170
170
  const record: Record<string, any> = {};
171
171
  const keyPack = new DataPack(keyBuf);
172
172
  keyPack.readNumber(); // skip indexId
173
- for (const [name, type] of model._pkFieldTypes.entries()) {
173
+ for (const [name, type] of model._indexFields.entries()) {
174
174
  record[name] = type.deserialize(keyPack);
175
175
  }
176
176
  for (const [name, type] of versionInfo.nonKeyFields.entries()) {
@@ -189,14 +189,14 @@ export async function runMigration(options: MigrationOptions = {}): Promise<Migr
189
189
  sec._write(txn, keyBuf, record as any);
190
190
  secondaryCount++;
191
191
  } else if (preMigrate) {
192
- if (sec._update(txn, keyBuf, record, preMigrate)) secondaryCount++;
192
+ if (sec._update(txn, keyBuf, record as any, preMigrate)) secondaryCount++;
193
193
  }
194
194
  }
195
195
  }
196
196
 
197
197
  // Rewrite primary row data to current version
198
198
  if (rewriteData) {
199
- model._writePrimary(txn, keyBuf, record);
199
+ model._writePK(txn, keyBuf, record);
200
200
  rewrittenCount++;
201
201
  }
202
202
  });
@@ -224,7 +224,6 @@ export async function runMigration(options: MigrationOptions = {}): Promise<Migr
224
224
  const failures: Record<string, number> = {};
225
225
 
226
226
  await forEachRow(oldDef.id, (txn, keyBuf) => {
227
- let instance;
228
227
  try {
229
228
  // Deserialize old key
230
229
  const keyPack = new DataPack(keyBuf);
@@ -239,7 +238,7 @@ export async function runMigration(options: MigrationOptions = {}): Promise<Migr
239
238
  if (migrateFn) migrateFn(record);
240
239
 
241
240
  // _write validates, checks duplicates, writes primary + secondaries
242
- instance = new (model as any)(record, txn);
241
+ let instance = new (model as any)(record, txn);
243
242
  instance._write(txn);
244
243
  dbDel(txn.id, keyBuf);
245
244
  converted++;
@@ -250,7 +249,7 @@ export async function runMigration(options: MigrationOptions = {}): Promise<Migr
250
249
  failures['error'] = (failures['error'] || 0) + 1;
251
250
  }
252
251
  } finally {
253
- if (instance) txn.instances.delete(instance);
252
+ txn.instances.clear(); // We've already handled the _write() ourselves
254
253
  }
255
254
  });
256
255