@hot-updater/plugin-core 1.2.0-rc.43 → 1.2.0-rc.44

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.
@@ -105,20 +105,25 @@ const createSqlAdapter = ({ executor, tablePrefix = "", maxOps = 1e3, maxParams
105
105
  const insert = (bind, into, columns, values) => `INSERT INTO ${into} (${columns.map(quote).join(", ")}) ${when ? `SELECT ${values.join(", ")} WHERE ${when(bind)}` : `VALUES (${values.join(", ")})`}`;
106
106
  /** Inserts `row` into the op's table, each value bound as its column's type. */
107
107
  const insertRow = (bind, row) => insert(bind, name, table.columns.map((column) => column.name), table.columns.map((column) => bind(row[column.name], column)));
108
- /** Replaces the row's entries in the index table of every multi-valued index. */
109
- const entries = (key, row, changed) => table.indexes.flatMap((index) => {
108
+ /**
109
+ * Replaces the row's entries in the index table of every multi-valued
110
+ * index. A row being inserted has none to replace (`stale` false), and
111
+ * deleting them would read the whole index table: its key ends with the
112
+ * row's key, so no index leads with it.
113
+ */
114
+ const entries = (key, row, changed, stale = true) => table.indexes.flatMap((index) => {
110
115
  const columns = [...index.eq, ...require_adapter.indexOrderColumns(table, index)];
111
116
  if (!require_sqlSchema.isMultiIndex(table, index) || changed && !columns.some((column) => changed.includes(column))) return [];
112
117
  const into = tableOf(table, index);
113
118
  const found = row ? require_values.indexEntries(table, index, row) : [];
114
119
  const order = found.length > 0 ? require_values.indexOrderTuple(table, index, row) : [];
115
- return [sql((bind) => `DELETE FROM ${into} WHERE ${where(bind, key)}`), ...found.map((eq) => sql((bind) => {
120
+ return [...stale ? [sql((bind) => `DELETE FROM ${into} WHERE ${where(bind, key)}`)] : [], ...found.map((eq) => sql((bind) => {
116
121
  const values = [...eq, ...order].map((value) => bind(value));
117
122
  return insert(bind, into, columns, values);
118
123
  }))];
119
124
  });
120
125
  switch (op.type) {
121
- case "insert": return [sql((bind) => insertRow(bind, op.row)), ...entries(require_adapter.rowKey(table, op.row), op.row)];
126
+ case "insert": return [sql((bind) => insertRow(bind, op.row)), ...entries(require_adapter.rowKey(table, op.row), op.row, void 0, false)];
122
127
  case "patch": return [sql((bind) => {
123
128
  const set = Object.entries(op.set).map(([column, value]) => `${quote(column)} = ${bind(value, require_adapter.findPhysicalColumn(table, column))}`);
124
129
  return `UPDATE ${name} SET ${[...set, `${version} = ${version} + 1`].join(", ")} WHERE ${where(bind, op.key, op.guard.v)}`;
@@ -105,20 +105,25 @@ const createSqlAdapter = ({ executor, tablePrefix = "", maxOps = 1e3, maxParams
105
105
  const insert = (bind, into, columns, values) => `INSERT INTO ${into} (${columns.map(quote).join(", ")}) ${when ? `SELECT ${values.join(", ")} WHERE ${when(bind)}` : `VALUES (${values.join(", ")})`}`;
106
106
  /** Inserts `row` into the op's table, each value bound as its column's type. */
107
107
  const insertRow = (bind, row) => insert(bind, name, table.columns.map((column) => column.name), table.columns.map((column) => bind(row[column.name], column)));
108
- /** Replaces the row's entries in the index table of every multi-valued index. */
109
- const entries = (key, row, changed) => table.indexes.flatMap((index) => {
108
+ /**
109
+ * Replaces the row's entries in the index table of every multi-valued
110
+ * index. A row being inserted has none to replace (`stale` false), and
111
+ * deleting them would read the whole index table: its key ends with the
112
+ * row's key, so no index leads with it.
113
+ */
114
+ const entries = (key, row, changed, stale = true) => table.indexes.flatMap((index) => {
110
115
  const columns = [...index.eq, ...indexOrderColumns(table, index)];
111
116
  if (!isMultiIndex(table, index) || changed && !columns.some((column) => changed.includes(column))) return [];
112
117
  const into = tableOf(table, index);
113
118
  const found = row ? indexEntries(table, index, row) : [];
114
119
  const order = found.length > 0 ? indexOrderTuple(table, index, row) : [];
115
- return [sql((bind) => `DELETE FROM ${into} WHERE ${where(bind, key)}`), ...found.map((eq) => sql((bind) => {
120
+ return [...stale ? [sql((bind) => `DELETE FROM ${into} WHERE ${where(bind, key)}`)] : [], ...found.map((eq) => sql((bind) => {
116
121
  const values = [...eq, ...order].map((value) => bind(value));
117
122
  return insert(bind, into, columns, values);
118
123
  }))];
119
124
  });
120
125
  switch (op.type) {
121
- case "insert": return [sql((bind) => insertRow(bind, op.row)), ...entries(rowKey(table, op.row), op.row)];
126
+ case "insert": return [sql((bind) => insertRow(bind, op.row)), ...entries(rowKey(table, op.row), op.row, void 0, false)];
122
127
  case "patch": return [sql((bind) => {
123
128
  const set = Object.entries(op.set).map(([column, value]) => `${quote(column)} = ${bind(value, findPhysicalColumn(table, column))}`);
124
129
  return `UPDATE ${name} SET ${[...set, `${version} = ${version} + 1`].join(", ")} WHERE ${where(bind, op.key, op.guard.v)}`;
@@ -72,7 +72,12 @@ const sqlTableShapes = (dialect, tables, tablePrefix = "") => tables.map((table)
72
72
  name: `${name}__${index.name}`,
73
73
  columns: entries,
74
74
  key: columns,
75
- indexes: []
75
+ indexes: [{
76
+ kind: "index",
77
+ name: shortSqlName(`${name}__${index.name}__key`),
78
+ unique: false,
79
+ columns: table.key
80
+ }]
76
81
  }
77
82
  }];
78
83
  }
@@ -149,8 +154,9 @@ const retentionOrder = (table) => {
149
154
  * column past `before` keeps. `before` renders the bound each time it
150
155
  * appears. MySQL deletes with ORDER BY and LIMIT. Elsewhere the cutoff is
151
156
  * the last of the `limit` oldest expired rows in that order, a row value
152
- * the index ranges over, and the shapes pass Supabase's apply RPC: no IN, no
153
- * AS, aliases `i` and `b`.
157
+ * the index ranges over. PostgreSQL's shapes pass Supabase's apply RPC: no
158
+ * IN, no AS, aliases `i` and `b`; SQLite's (D1's) delete each doomed row's
159
+ * entries by the index table's key.
154
160
  */
155
161
  const pruneStatements = (dialect, table, tablePrefix, limit) => {
156
162
  const quote = (name) => quoteSql(dialect, name);
@@ -159,9 +165,17 @@ const pruneStatements = (dialect, table, tablePrefix, limit) => {
159
165
  const order = retentionOrder(table).columns.map(quote);
160
166
  const list = (alias, direction = "") => order.map((column) => `${alias}.${column}${direction}`).join(", ");
161
167
  const oldest = (before) => `SELECT ${list("i")} FROM ${name} i WHERE i.${at} <= ${before()} ORDER BY ${list("i", " ASC")} LIMIT ${limit}`;
162
- const doomed = (alias, before) => `${alias}.${at} <= ${before()} AND (${list(alias)}) <= (SELECT ${list("b")} FROM (${oldest(before)}) b ORDER BY ${list("b", " DESC")} LIMIT 1)`;
168
+ const doomed = (alias, before) => `(${list(alias)}) <= (SELECT ${list("b")} FROM (${oldest(before)}) b ORDER BY ${list("b", " DESC")} LIMIT 1) AND ${alias}.${at} <= ${before()}`;
163
169
  const entries = table.indexes.filter((index) => isMultiIndex(table, index)).map((index) => quote(`${tablePrefix}${table.name}__${index.name}`));
164
170
  const keyOf = (left, right) => table.key.map((column) => `${left}.${quote(column)} = ${right}.${quote(column)}`).join(" AND ");
171
+ if (dialect === "sqlite") return [...table.indexes.filter((index) => isMultiIndex(table, index)).map((index) => {
172
+ const columns = [...index.eq, ...require_adapter.indexOrderColumns(table, index)];
173
+ const multi = columns.filter((column) => require_adapter.findPhysicalColumn(table, column).multi);
174
+ const values = columns.map((column) => multi.includes(column) ? `j${multi.indexOf(column)}.${quote("value")}` : `b.${quote(column)}`);
175
+ const each = multi.map((column, at) => `, json_each(b.${quote(column)}) j${at}`);
176
+ const into = quote(`${tablePrefix}${table.name}__${index.name}`);
177
+ return (before) => `DELETE FROM ${into} WHERE (${columns.map(quote).join(", ")}) IN (SELECT ${values.join(", ")} FROM ${name} b${each.join("")} WHERE ${doomed("b", before)})`;
178
+ }), (before) => `DELETE FROM ${name} WHERE ${doomed(name, before)}`];
165
179
  if (dialect === "mysql") return [...entries.map((index) => (before) => `DELETE e FROM ${index} e JOIN (${oldest(before)}) d ON ${keyOf("e", "d")}`), (before) => `DELETE FROM ${name} WHERE ${at} <= ${before()} ORDER BY ${order.join(", ")} LIMIT ${limit}`];
166
180
  return [...entries.map((index) => (before) => `DELETE FROM ${index} WHERE EXISTS (SELECT * FROM ${name} b WHERE ${keyOf("b", index)} AND ${doomed("b", before)})`), (before) => `DELETE FROM ${name} WHERE ${doomed(name, before)}`];
167
181
  };
@@ -72,7 +72,12 @@ const sqlTableShapes = (dialect, tables, tablePrefix = "") => tables.map((table)
72
72
  name: `${name}__${index.name}`,
73
73
  columns: entries,
74
74
  key: columns,
75
- indexes: []
75
+ indexes: [{
76
+ kind: "index",
77
+ name: shortSqlName(`${name}__${index.name}__key`),
78
+ unique: false,
79
+ columns: table.key
80
+ }]
76
81
  }
77
82
  }];
78
83
  }
@@ -149,8 +154,9 @@ const retentionOrder = (table) => {
149
154
  * column past `before` keeps. `before` renders the bound each time it
150
155
  * appears. MySQL deletes with ORDER BY and LIMIT. Elsewhere the cutoff is
151
156
  * the last of the `limit` oldest expired rows in that order, a row value
152
- * the index ranges over, and the shapes pass Supabase's apply RPC: no IN, no
153
- * AS, aliases `i` and `b`.
157
+ * the index ranges over. PostgreSQL's shapes pass Supabase's apply RPC: no
158
+ * IN, no AS, aliases `i` and `b`; SQLite's (D1's) delete each doomed row's
159
+ * entries by the index table's key.
154
160
  */
155
161
  const pruneStatements = (dialect, table, tablePrefix, limit) => {
156
162
  const quote = (name) => quoteSql(dialect, name);
@@ -159,9 +165,17 @@ const pruneStatements = (dialect, table, tablePrefix, limit) => {
159
165
  const order = retentionOrder(table).columns.map(quote);
160
166
  const list = (alias, direction = "") => order.map((column) => `${alias}.${column}${direction}`).join(", ");
161
167
  const oldest = (before) => `SELECT ${list("i")} FROM ${name} i WHERE i.${at} <= ${before()} ORDER BY ${list("i", " ASC")} LIMIT ${limit}`;
162
- const doomed = (alias, before) => `${alias}.${at} <= ${before()} AND (${list(alias)}) <= (SELECT ${list("b")} FROM (${oldest(before)}) b ORDER BY ${list("b", " DESC")} LIMIT 1)`;
168
+ const doomed = (alias, before) => `(${list(alias)}) <= (SELECT ${list("b")} FROM (${oldest(before)}) b ORDER BY ${list("b", " DESC")} LIMIT 1) AND ${alias}.${at} <= ${before()}`;
163
169
  const entries = table.indexes.filter((index) => isMultiIndex(table, index)).map((index) => quote(`${tablePrefix}${table.name}__${index.name}`));
164
170
  const keyOf = (left, right) => table.key.map((column) => `${left}.${quote(column)} = ${right}.${quote(column)}`).join(" AND ");
171
+ if (dialect === "sqlite") return [...table.indexes.filter((index) => isMultiIndex(table, index)).map((index) => {
172
+ const columns = [...index.eq, ...indexOrderColumns(table, index)];
173
+ const multi = columns.filter((column) => findPhysicalColumn(table, column).multi);
174
+ const values = columns.map((column) => multi.includes(column) ? `j${multi.indexOf(column)}.${quote("value")}` : `b.${quote(column)}`);
175
+ const each = multi.map((column, at) => `, json_each(b.${quote(column)}) j${at}`);
176
+ const into = quote(`${tablePrefix}${table.name}__${index.name}`);
177
+ return (before) => `DELETE FROM ${into} WHERE (${columns.map(quote).join(", ")}) IN (SELECT ${values.join(", ")} FROM ${name} b${each.join("")} WHERE ${doomed("b", before)})`;
178
+ }), (before) => `DELETE FROM ${name} WHERE ${doomed(name, before)}`];
165
179
  if (dialect === "mysql") return [...entries.map((index) => (before) => `DELETE e FROM ${index} e JOIN (${oldest(before)}) d ON ${keyOf("e", "d")}`), (before) => `DELETE FROM ${name} WHERE ${at} <= ${before()} ORDER BY ${order.join(", ")} LIMIT ${limit}`];
166
180
  return [...entries.map((index) => (before) => `DELETE FROM ${index} WHERE EXISTS (SELECT * FROM ${name} b WHERE ${keyOf("b", index)} AND ${doomed("b", before)})`), (before) => `DELETE FROM ${name} WHERE ${doomed(name, before)}`];
167
181
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hot-updater/plugin-core",
3
- "version": "1.2.0-rc.43",
3
+ "version": "1.2.0-rc.44",
4
4
  "type": "module",
5
5
  "description": "React Native OTA solution for self-hosted",
6
6
  "sideEffects": false,
@@ -45,7 +45,7 @@
45
45
  "es-toolkit": "^1.32.0",
46
46
  "mime": "^4.0.4",
47
47
  "verkit": "0.4.0",
48
- "@hot-updater/protocol": "1.2.0-rc.43"
48
+ "@hot-updater/protocol": "1.2.0-rc.44"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@electric-sql/pglite": "0.4.1",