@hot-updater/plugin-core 1.1.13 → 1.1.15
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
|
-
/**
|
|
109
|
-
|
|
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
|
-
/**
|
|
109
|
-
|
|
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)}`;
|
|
@@ -149,8 +149,9 @@ const retentionOrder = (table) => {
|
|
|
149
149
|
* column past `before` keeps. `before` renders the bound each time it
|
|
150
150
|
* appears. MySQL deletes with ORDER BY and LIMIT. Elsewhere the cutoff is
|
|
151
151
|
* the last of the `limit` oldest expired rows in that order, a row value
|
|
152
|
-
* the index ranges over
|
|
153
|
-
* AS, aliases `i` and `b
|
|
152
|
+
* the index ranges over. PostgreSQL's shapes pass Supabase's apply RPC: no
|
|
153
|
+
* IN, no AS, aliases `i` and `b`; SQLite's (D1's) delete each doomed row's
|
|
154
|
+
* entries by the index table's key.
|
|
154
155
|
*/
|
|
155
156
|
const pruneStatements = (dialect, table, tablePrefix, limit) => {
|
|
156
157
|
const quote = (name) => quoteSql(dialect, name);
|
|
@@ -159,9 +160,17 @@ const pruneStatements = (dialect, table, tablePrefix, limit) => {
|
|
|
159
160
|
const order = retentionOrder(table).columns.map(quote);
|
|
160
161
|
const list = (alias, direction = "") => order.map((column) => `${alias}.${column}${direction}`).join(", ");
|
|
161
162
|
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) =>
|
|
163
|
+
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
164
|
const entries = table.indexes.filter((index) => isMultiIndex(table, index)).map((index) => quote(`${tablePrefix}${table.name}__${index.name}`));
|
|
164
165
|
const keyOf = (left, right) => table.key.map((column) => `${left}.${quote(column)} = ${right}.${quote(column)}`).join(" AND ");
|
|
166
|
+
if (dialect === "sqlite") return [...table.indexes.filter((index) => isMultiIndex(table, index)).map((index) => {
|
|
167
|
+
const columns = [...index.eq, ...require_adapter.indexOrderColumns(table, index)];
|
|
168
|
+
const multi = columns.filter((column) => require_adapter.findPhysicalColumn(table, column).multi);
|
|
169
|
+
const values = columns.map((column) => multi.includes(column) ? `j${multi.indexOf(column)}.${quote("value")}` : `b.${quote(column)}`);
|
|
170
|
+
const each = multi.map((column, at) => `, json_each(b.${quote(column)}) j${at}`);
|
|
171
|
+
const into = quote(`${tablePrefix}${table.name}__${index.name}`);
|
|
172
|
+
return (before) => `DELETE FROM ${into} WHERE (${columns.map(quote).join(", ")}) IN (SELECT ${values.join(", ")} FROM ${name} b${each.join("")} WHERE ${doomed("b", before)})`;
|
|
173
|
+
}), (before) => `DELETE FROM ${name} WHERE ${doomed(name, before)}`];
|
|
165
174
|
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
175
|
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
176
|
};
|
|
@@ -149,8 +149,9 @@ const retentionOrder = (table) => {
|
|
|
149
149
|
* column past `before` keeps. `before` renders the bound each time it
|
|
150
150
|
* appears. MySQL deletes with ORDER BY and LIMIT. Elsewhere the cutoff is
|
|
151
151
|
* the last of the `limit` oldest expired rows in that order, a row value
|
|
152
|
-
* the index ranges over
|
|
153
|
-
* AS, aliases `i` and `b
|
|
152
|
+
* the index ranges over. PostgreSQL's shapes pass Supabase's apply RPC: no
|
|
153
|
+
* IN, no AS, aliases `i` and `b`; SQLite's (D1's) delete each doomed row's
|
|
154
|
+
* entries by the index table's key.
|
|
154
155
|
*/
|
|
155
156
|
const pruneStatements = (dialect, table, tablePrefix, limit) => {
|
|
156
157
|
const quote = (name) => quoteSql(dialect, name);
|
|
@@ -159,9 +160,17 @@ const pruneStatements = (dialect, table, tablePrefix, limit) => {
|
|
|
159
160
|
const order = retentionOrder(table).columns.map(quote);
|
|
160
161
|
const list = (alias, direction = "") => order.map((column) => `${alias}.${column}${direction}`).join(", ");
|
|
161
162
|
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) =>
|
|
163
|
+
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
164
|
const entries = table.indexes.filter((index) => isMultiIndex(table, index)).map((index) => quote(`${tablePrefix}${table.name}__${index.name}`));
|
|
164
165
|
const keyOf = (left, right) => table.key.map((column) => `${left}.${quote(column)} = ${right}.${quote(column)}`).join(" AND ");
|
|
166
|
+
if (dialect === "sqlite") return [...table.indexes.filter((index) => isMultiIndex(table, index)).map((index) => {
|
|
167
|
+
const columns = [...index.eq, ...indexOrderColumns(table, index)];
|
|
168
|
+
const multi = columns.filter((column) => findPhysicalColumn(table, column).multi);
|
|
169
|
+
const values = columns.map((column) => multi.includes(column) ? `j${multi.indexOf(column)}.${quote("value")}` : `b.${quote(column)}`);
|
|
170
|
+
const each = multi.map((column, at) => `, json_each(b.${quote(column)}) j${at}`);
|
|
171
|
+
const into = quote(`${tablePrefix}${table.name}__${index.name}`);
|
|
172
|
+
return (before) => `DELETE FROM ${into} WHERE (${columns.map(quote).join(", ")}) IN (SELECT ${values.join(", ")} FROM ${name} b${each.join("")} WHERE ${doomed("b", before)})`;
|
|
173
|
+
}), (before) => `DELETE FROM ${name} WHERE ${doomed(name, before)}`];
|
|
165
174
|
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
175
|
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
176
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hot-updater/plugin-core",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.15",
|
|
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.1.
|
|
48
|
+
"@hot-updater/protocol": "1.1.15"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@electric-sql/pglite": "0.4.1",
|