@mastra/libsql 1.17.0-alpha.1 → 1.17.0-alpha.4

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.js CHANGED
@@ -13363,8 +13363,10 @@ function assertIdentifier(kind, name) {
13363
13363
  }
13364
13364
  }
13365
13365
  function isUniqueViolation(error) {
13366
+ const code = typeof error === "object" && error !== null ? error.code : void 0;
13367
+ if (code === "SQLITE_CONSTRAINT_UNIQUE" || code === "SQLITE_CONSTRAINT_PRIMARYKEY") return true;
13366
13368
  const message = error instanceof Error ? error.message : String(error);
13367
- return message.includes("UNIQUE constraint failed") || message.includes("SQLITE_CONSTRAINT");
13369
+ return message.includes("UNIQUE constraint failed") || message.includes("SQLITE_CONSTRAINT_UNIQUE") || message.includes("SQLITE_CONSTRAINT_PRIMARYKEY");
13368
13370
  }
13369
13371
  function primaryKeyOf(schema) {
13370
13372
  const pks = Object.entries(schema.columns).filter(([, spec]) => spec.type === "uuid-pk" || spec.primaryKey);
@@ -13674,6 +13676,20 @@ var LibSQLFactoryStorage = class extends FactoryStorage {
13674
13676
  async initStorage() {
13675
13677
  await this.#client.execute("SELECT 1");
13676
13678
  }
13679
+ async withTransaction(fn) {
13680
+ if (this.#config.url.includes(":memory:")) return fn(this.ops);
13681
+ const transaction = await this.#client.transaction("write");
13682
+ try {
13683
+ const result = await fn(new LibSQLFactoryStorageOps(transaction, this.#schemas));
13684
+ await transaction.commit();
13685
+ return result;
13686
+ } catch (error) {
13687
+ await transaction.rollback();
13688
+ throw error;
13689
+ } finally {
13690
+ transaction.close();
13691
+ }
13692
+ }
13677
13693
  async ensureCollections(schemas) {
13678
13694
  for (const schema of schemas) {
13679
13695
  await this.#ensureCollection(schema);
@@ -13694,6 +13710,35 @@ var LibSQLFactoryStorage = class extends FactoryStorage {
13694
13710
  if (spec.default !== void 0) ddl += ` DEFAULT ${serializeDefault(spec.default)}`;
13695
13711
  return ddl;
13696
13712
  }
13713
+ /**
13714
+ * A table created by an older schema may still say NOT NULL on a column the
13715
+ * current schema declares nullable — inserts of null then fail on databases
13716
+ * that predate the change. SQLite has no `ALTER COLUMN DROP NOT NULL`, so
13717
+ * relaxation swaps in a table rebuilt from the current schema (create shadow
13718
+ * → copy → drop → rename) atomically.
13719
+ */
13720
+ async #relaxDriftedNotNulls(schema) {
13721
+ const info = await this.#client.execute(`PRAGMA table_info("${schema.name}")`);
13722
+ const hasDrift = info.rows.some((row) => {
13723
+ const spec = schema.columns[String(row.name)];
13724
+ if (!spec || spec.type === "uuid-pk" || spec.primaryKey) return false;
13725
+ return spec.nullable === true && Number(row.notnull) === 1;
13726
+ });
13727
+ if (!hasDrift) return;
13728
+ const ddl = Object.entries(schema.columns).map(([name, spec]) => this.#columnDdl(name, spec));
13729
+ const shadow = `${schema.name}__nullable_rebuild`;
13730
+ const cols = Object.keys(schema.columns).map((name) => `"${name}"`).join(", ");
13731
+ await this.#client.batch(
13732
+ [
13733
+ `DROP TABLE IF EXISTS "${shadow}"`,
13734
+ `CREATE TABLE "${shadow}" (${ddl.join(", ")})`,
13735
+ `INSERT INTO "${shadow}" (${cols}) SELECT ${cols} FROM "${schema.name}"`,
13736
+ `DROP TABLE "${schema.name}"`,
13737
+ `ALTER TABLE "${shadow}" RENAME TO "${schema.name}"`
13738
+ ],
13739
+ "write"
13740
+ );
13741
+ }
13697
13742
  async #ensureCollection(schema) {
13698
13743
  assertIdentifier("collection", schema.name);
13699
13744
  primaryKeyOf(schema);
@@ -13705,6 +13750,7 @@ var LibSQLFactoryStorage = class extends FactoryStorage {
13705
13750
  if (existing.has(name)) continue;
13706
13751
  await this.#client.execute(`ALTER TABLE "${schema.name}" ADD COLUMN ${this.#columnDdl(name, spec)}`);
13707
13752
  }
13753
+ await this.#relaxDriftedNotNulls(schema);
13708
13754
  for (const index of schema.uniqueIndexes ?? []) {
13709
13755
  assertIdentifier("index", index.name);
13710
13756
  index.columns.forEach((column) => assertIdentifier("column", column));