tinybase 10.0.0-beta.0 → 10.0.0-beta.2

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 (185) hide show
  1. package/@types/common/index.d.ts +1 -0
  2. package/@types/common/with-schemas/index.d.ts +1 -0
  3. package/@types/indexes/index.d.ts +4 -3
  4. package/@types/indexes/with-schemas/index.d.ts +4 -3
  5. package/@types/metrics/index.d.ts +1 -0
  6. package/@types/metrics/with-schemas/index.d.ts +1 -0
  7. package/@types/omni/index.d.ts +1 -0
  8. package/@types/omni/with-schemas/index.d.ts +1 -0
  9. package/@types/persisters/index.d.ts +1 -0
  10. package/@types/persisters/persister-partykit-server/index.d.ts +17 -0
  11. package/@types/persisters/persister-partykit-server/with-schemas/index.d.ts +17 -0
  12. package/@types/persisters/persister-tinyjoin/index.d.ts +197 -0
  13. package/@types/persisters/persister-tinyjoin/with-schemas/index.d.ts +221 -0
  14. package/@types/persisters/with-schemas/index.d.ts +1 -0
  15. package/@types/queries/index.d.ts +3 -0
  16. package/@types/queries/with-schemas/index.d.ts +3 -0
  17. package/@types/relationships/index.d.ts +1 -0
  18. package/@types/relationships/with-schemas/index.d.ts +1 -0
  19. package/@types/store/index.d.ts +4 -0
  20. package/@types/store/with-schemas/index.d.ts +4 -0
  21. package/@types/ui-react/index.d.ts +2 -0
  22. package/@types/ui-react/with-schemas/index.d.ts +1 -0
  23. package/@types/ui-react-dom-charts/index.d.ts +7 -0
  24. package/@types/ui-react-dom-charts/with-schemas/index.d.ts +7 -0
  25. package/@types/ui-solid/index.d.ts +2 -0
  26. package/@types/ui-solid/with-schemas/index.d.ts +1 -0
  27. package/@types/ui-svelte/index.d.ts +23 -0
  28. package/@types/ui-svelte/with-schemas/index.d.ts +23 -0
  29. package/@types/ui-svelte-dom/index.d.ts +10 -0
  30. package/@types/ui-svelte-dom/with-schemas/index.d.ts +10 -0
  31. package/@types/ui-svelte-inspector/index.d.ts +1 -0
  32. package/@types/ui-svelte-inspector/with-schemas/index.d.ts +1 -0
  33. package/checkpoints/index.js +4 -2
  34. package/checkpoints/with-schemas/index.js +4 -2
  35. package/index.js +37 -4
  36. package/metrics/index.js +2 -1
  37. package/metrics/with-schemas/index.js +2 -1
  38. package/min/checkpoints/index.js +1 -1
  39. package/min/checkpoints/index.js.gz +0 -0
  40. package/min/checkpoints/with-schemas/index.js +1 -1
  41. package/min/checkpoints/with-schemas/index.js.gz +0 -0
  42. package/min/index.js +1 -1
  43. package/min/index.js.gz +0 -0
  44. package/min/metrics/index.js +1 -1
  45. package/min/metrics/index.js.gz +0 -0
  46. package/min/metrics/with-schemas/index.js +1 -1
  47. package/min/metrics/with-schemas/index.js.gz +0 -0
  48. package/min/omni/index.js +1 -1
  49. package/min/omni/index.js.gz +0 -0
  50. package/min/omni/with-schemas/index.js +1 -1
  51. package/min/omni/with-schemas/index.js.gz +0 -0
  52. package/min/persisters/index.js +1 -1
  53. package/min/persisters/index.js.gz +0 -0
  54. package/min/persisters/persister-better-sqlite3/index.js +1 -1
  55. package/min/persisters/persister-better-sqlite3/index.js.gz +0 -0
  56. package/min/persisters/persister-better-sqlite3/with-schemas/index.js +1 -1
  57. package/min/persisters/persister-better-sqlite3/with-schemas/index.js.gz +0 -0
  58. package/min/persisters/persister-capacitor-sqlite/index.js +1 -1
  59. package/min/persisters/persister-capacitor-sqlite/index.js.gz +0 -0
  60. package/min/persisters/persister-capacitor-sqlite/with-schemas/index.js +1 -1
  61. package/min/persisters/persister-capacitor-sqlite/with-schemas/index.js.gz +0 -0
  62. package/min/persisters/persister-cr-sqlite-wasm/index.js +1 -1
  63. package/min/persisters/persister-cr-sqlite-wasm/index.js.gz +0 -0
  64. package/min/persisters/persister-cr-sqlite-wasm/with-schemas/index.js +1 -1
  65. package/min/persisters/persister-cr-sqlite-wasm/with-schemas/index.js.gz +0 -0
  66. package/min/persisters/persister-durable-object-sql-storage/index.js +1 -1
  67. package/min/persisters/persister-durable-object-sql-storage/index.js.gz +0 -0
  68. package/min/persisters/persister-durable-object-sql-storage/with-schemas/index.js +1 -1
  69. package/min/persisters/persister-durable-object-sql-storage/with-schemas/index.js.gz +0 -0
  70. package/min/persisters/persister-electric-sql/index.js +1 -1
  71. package/min/persisters/persister-electric-sql/index.js.gz +0 -0
  72. package/min/persisters/persister-electric-sql/with-schemas/index.js +1 -1
  73. package/min/persisters/persister-electric-sql/with-schemas/index.js.gz +0 -0
  74. package/min/persisters/persister-expo-sqlite/index.js +1 -1
  75. package/min/persisters/persister-expo-sqlite/index.js.gz +0 -0
  76. package/min/persisters/persister-expo-sqlite/with-schemas/index.js +1 -1
  77. package/min/persisters/persister-expo-sqlite/with-schemas/index.js.gz +0 -0
  78. package/min/persisters/persister-libsql/index.js +1 -1
  79. package/min/persisters/persister-libsql/index.js.gz +0 -0
  80. package/min/persisters/persister-libsql/with-schemas/index.js +1 -1
  81. package/min/persisters/persister-libsql/with-schemas/index.js.gz +0 -0
  82. package/min/persisters/persister-pg/index.js +1 -1
  83. package/min/persisters/persister-pg/index.js.gz +0 -0
  84. package/min/persisters/persister-pg/with-schemas/index.js +1 -1
  85. package/min/persisters/persister-pg/with-schemas/index.js.gz +0 -0
  86. package/min/persisters/persister-pglite/index.js +1 -1
  87. package/min/persisters/persister-pglite/index.js.gz +0 -0
  88. package/min/persisters/persister-pglite/with-schemas/index.js +1 -1
  89. package/min/persisters/persister-pglite/with-schemas/index.js.gz +0 -0
  90. package/min/persisters/persister-postgres/index.js +1 -1
  91. package/min/persisters/persister-postgres/index.js.gz +0 -0
  92. package/min/persisters/persister-postgres/with-schemas/index.js +1 -1
  93. package/min/persisters/persister-postgres/with-schemas/index.js.gz +0 -0
  94. package/min/persisters/persister-powersync/index.js +1 -1
  95. package/min/persisters/persister-powersync/index.js.gz +0 -0
  96. package/min/persisters/persister-powersync/with-schemas/index.js +1 -1
  97. package/min/persisters/persister-powersync/with-schemas/index.js.gz +0 -0
  98. package/min/persisters/persister-react-native-sqlite/index.js +1 -1
  99. package/min/persisters/persister-react-native-sqlite/index.js.gz +0 -0
  100. package/min/persisters/persister-react-native-sqlite/with-schemas/index.js +1 -1
  101. package/min/persisters/persister-react-native-sqlite/with-schemas/index.js.gz +0 -0
  102. package/min/persisters/persister-sqlite-bun/index.js +1 -1
  103. package/min/persisters/persister-sqlite-bun/index.js.gz +0 -0
  104. package/min/persisters/persister-sqlite-bun/with-schemas/index.js +1 -1
  105. package/min/persisters/persister-sqlite-bun/with-schemas/index.js.gz +0 -0
  106. package/min/persisters/persister-sqlite-node/index.js +1 -1
  107. package/min/persisters/persister-sqlite-node/index.js.gz +0 -0
  108. package/min/persisters/persister-sqlite-node/with-schemas/index.js +1 -1
  109. package/min/persisters/persister-sqlite-node/with-schemas/index.js.gz +0 -0
  110. package/min/persisters/persister-sqlite-wasm/index.js +1 -1
  111. package/min/persisters/persister-sqlite-wasm/index.js.gz +0 -0
  112. package/min/persisters/persister-sqlite-wasm/with-schemas/index.js +1 -1
  113. package/min/persisters/persister-sqlite-wasm/with-schemas/index.js.gz +0 -0
  114. package/min/persisters/persister-sqlite3/index.js +1 -1
  115. package/min/persisters/persister-sqlite3/index.js.gz +0 -0
  116. package/min/persisters/persister-sqlite3/with-schemas/index.js +1 -1
  117. package/min/persisters/persister-sqlite3/with-schemas/index.js.gz +0 -0
  118. package/min/persisters/persister-tinyjoin/index.js +1 -0
  119. package/min/persisters/persister-tinyjoin/index.js.gz +0 -0
  120. package/min/persisters/persister-tinyjoin/with-schemas/index.js +1 -0
  121. package/min/persisters/persister-tinyjoin/with-schemas/index.js.gz +0 -0
  122. package/min/persisters/with-schemas/index.js +1 -1
  123. package/min/persisters/with-schemas/index.js.gz +0 -0
  124. package/min/queries/index.js +1 -1
  125. package/min/queries/index.js.gz +0 -0
  126. package/min/queries/with-schemas/index.js +1 -1
  127. package/min/queries/with-schemas/index.js.gz +0 -0
  128. package/min/relationships/index.js +1 -1
  129. package/min/relationships/index.js.gz +0 -0
  130. package/min/relationships/with-schemas/index.js +1 -1
  131. package/min/relationships/with-schemas/index.js.gz +0 -0
  132. package/min/synchronizers/synchronizer-local/index.js +1 -1
  133. package/min/synchronizers/synchronizer-local/index.js.gz +0 -0
  134. package/min/synchronizers/synchronizer-local/with-schemas/index.js +1 -1
  135. package/min/synchronizers/synchronizer-local/with-schemas/index.js.gz +0 -0
  136. package/min/with-schemas/index.js +1 -1
  137. package/min/with-schemas/index.js.gz +0 -0
  138. package/omni/index.js +205 -94
  139. package/omni/with-schemas/index.js +205 -94
  140. package/package.json +54 -14
  141. package/persisters/index.js +28 -13
  142. package/persisters/persister-better-sqlite3/index.js +70 -55
  143. package/persisters/persister-better-sqlite3/with-schemas/index.js +70 -55
  144. package/persisters/persister-capacitor-sqlite/index.js +70 -55
  145. package/persisters/persister-capacitor-sqlite/with-schemas/index.js +70 -55
  146. package/persisters/persister-cr-sqlite-wasm/index.js +70 -55
  147. package/persisters/persister-cr-sqlite-wasm/with-schemas/index.js +70 -55
  148. package/persisters/persister-durable-object-sql-storage/index.js +28 -13
  149. package/persisters/persister-durable-object-sql-storage/with-schemas/index.js +28 -13
  150. package/persisters/persister-electric-sql/index.js +70 -55
  151. package/persisters/persister-electric-sql/with-schemas/index.js +70 -55
  152. package/persisters/persister-expo-sqlite/index.js +70 -55
  153. package/persisters/persister-expo-sqlite/with-schemas/index.js +70 -55
  154. package/persisters/persister-libsql/index.js +91 -78
  155. package/persisters/persister-libsql/with-schemas/index.js +91 -78
  156. package/persisters/persister-pg/index.js +77 -62
  157. package/persisters/persister-pg/with-schemas/index.js +77 -62
  158. package/persisters/persister-pglite/index.js +28 -13
  159. package/persisters/persister-pglite/with-schemas/index.js +28 -13
  160. package/persisters/persister-postgres/index.js +28 -13
  161. package/persisters/persister-postgres/with-schemas/index.js +28 -13
  162. package/persisters/persister-powersync/index.js +81 -66
  163. package/persisters/persister-powersync/with-schemas/index.js +81 -66
  164. package/persisters/persister-react-native-sqlite/index.js +70 -55
  165. package/persisters/persister-react-native-sqlite/with-schemas/index.js +70 -55
  166. package/persisters/persister-sqlite-bun/index.js +70 -55
  167. package/persisters/persister-sqlite-bun/with-schemas/index.js +70 -55
  168. package/persisters/persister-sqlite-node/index.js +70 -55
  169. package/persisters/persister-sqlite-node/with-schemas/index.js +70 -55
  170. package/persisters/persister-sqlite-wasm/index.js +70 -55
  171. package/persisters/persister-sqlite-wasm/with-schemas/index.js +70 -55
  172. package/persisters/persister-sqlite3/index.js +70 -55
  173. package/persisters/persister-sqlite3/with-schemas/index.js +70 -55
  174. package/persisters/persister-tinyjoin/index.js +1710 -0
  175. package/persisters/persister-tinyjoin/with-schemas/index.js +1710 -0
  176. package/persisters/with-schemas/index.js +28 -13
  177. package/queries/index.js +2 -1
  178. package/queries/with-schemas/index.js +2 -1
  179. package/readme.md +3 -3
  180. package/relationships/index.js +39 -9
  181. package/relationships/with-schemas/index.js +39 -9
  182. package/releases.md +20 -1
  183. package/synchronizers/synchronizer-local/index.js +1 -1
  184. package/synchronizers/synchronizer-local/with-schemas/index.js +1 -1
  185. package/with-schemas/index.js +37 -4
@@ -105,50 +105,6 @@ const arrayClear = (array, to = size(array)) => array.splice(0, to);
105
105
  const arrayPush = (array, ...values) => array.push(...values);
106
106
  const arrayShift = (array) => array.shift();
107
107
 
108
- const SINGLE_ROW_ID = '_';
109
- const DEFAULT_ROW_ID_COLUMN_NAME = '_id';
110
- const SELECT = 'SELECT';
111
- const WHERE = 'WHERE';
112
- const TABLE = 'TABLE';
113
- const INSERT = 'INSERT';
114
- const DELETE = 'DELETE';
115
- const UPDATE = 'UPDATE';
116
- const ALTER_TABLE = 'ALTER ' + TABLE;
117
- const FROM = 'FROM';
118
- const DELETE_FROM = DELETE + ' ' + FROM;
119
- const SELECT_STAR_FROM = SELECT + '*' + FROM;
120
- const PRAGMA = 'pragma_';
121
- const DATA_VERSION = 'data_version';
122
- const SCHEMA_VERSION = 'schema_version';
123
- const PRAGMA_TABLE = 'pragma_table_';
124
- const CREATE = 'CREATE ';
125
- const CREATE_TABLE = CREATE + TABLE;
126
- const TABLE_NAME_PLACEHOLDER_REGEX = /\$tableName/g;
127
- const getWrappedCommand = (executeCommand, onSqlCommand) =>
128
- onSqlCommand
129
- ? async (sql, params) => {
130
- onSqlCommand(sql, params);
131
- return await executeCommand(sql, params);
132
- }
133
- : executeCommand;
134
- const escapeId = (str) =>
135
- arrayJoin(
136
- arrayMap(strSplit(str, DOT), (part) => `"${strReplace(part, /"/g, '""')}"`),
137
- DOT,
138
- );
139
- const escapeColumnNames = (...columnNames) =>
140
- arrayJoin(arrayMap(columnNames, escapeId), COMMA);
141
- const anonymousPlaceholder = () => '?';
142
- const getPlaceholders = (array, getPlaceholder, offset = [1]) =>
143
- arrayJoin(
144
- arrayMap(array, () => getPlaceholder(offset)),
145
- COMMA,
146
- );
147
- const getWhereCondition = (tableName, condition = TRUE) =>
148
- WHERE + `(${replaceTableName(condition, escapeId(tableName))})`;
149
- const replaceTableName = (condition, tableName) =>
150
- strReplace(condition, TABLE_NAME_PLACEHOLDER_REGEX, tableName);
151
-
152
108
  const object = Object;
153
109
  const getPrototypeOf = (obj) => object.getPrototypeOf(obj);
154
110
  const objEntries = object.entries;
@@ -198,6 +154,62 @@ const objValues = (obj) => object.values(obj);
198
154
  const objSize = (obj) => size(objIds(obj));
199
155
  const objIsEmpty = (obj) => isObject(obj) && objSize(obj) == 0;
200
156
 
157
+ const SINGLE_ROW_ID = '_';
158
+ const DEFAULT_ROW_ID_COLUMN_NAME = '_id';
159
+ const SELECT = 'SELECT';
160
+ const WHERE = 'WHERE';
161
+ const TABLE = 'TABLE';
162
+ const INSERT = 'INSERT';
163
+ const DELETE = 'DELETE';
164
+ const UPDATE = 'UPDATE';
165
+ const ALTER_TABLE = 'ALTER ' + TABLE;
166
+ const FROM = 'FROM';
167
+ const DELETE_FROM = DELETE + ' ' + FROM;
168
+ const SELECT_STAR_FROM = SELECT + '*' + FROM;
169
+ const PRAGMA = 'pragma_';
170
+ const DATA_VERSION = 'data_version';
171
+ const SCHEMA_VERSION = 'schema_version';
172
+ const PRAGMA_TABLE = 'pragma_table_';
173
+ const CREATE = 'CREATE ';
174
+ const CREATE_TABLE = CREATE + TABLE;
175
+ const TABLE_NAME_PLACEHOLDER_REGEX = /\$tableName/g;
176
+ const getWrappedCommand = (executeCommand, onSqlCommand) =>
177
+ onSqlCommand
178
+ ? async (sql, params) => {
179
+ onSqlCommand(sql, params);
180
+ return await executeCommand(sql, params);
181
+ }
182
+ : executeCommand;
183
+ const escapeId = (str) =>
184
+ arrayJoin(
185
+ arrayMap(strSplit(str, DOT), (part) => `"${strReplace(part, /"/g, '""')}"`),
186
+ DOT,
187
+ );
188
+ const escapeColumnNames = (...columnNames) =>
189
+ arrayJoin(arrayMap(columnNames, escapeId), COMMA);
190
+ const anonymousPlaceholder = () => '?';
191
+ const getPlaceholders = (array, getPlaceholder, offset = [1]) =>
192
+ arrayJoin(
193
+ arrayMap(array, () => getPlaceholder(offset)),
194
+ COMMA,
195
+ );
196
+ const getWhere = (tableName, condition = TRUE, extraCondition) => {
197
+ const conditions = arrayFilter(
198
+ [
199
+ condition == TRUE
200
+ ? void 0
201
+ : `(${replaceTableName(condition, escapeId(tableName))})`,
202
+ extraCondition,
203
+ ],
204
+ (condition2) => !isUndefined(condition2),
205
+ );
206
+ return isEmpty(conditions)
207
+ ? EMPTY_STRING
208
+ : WHERE + arrayJoin(conditions, 'AND');
209
+ };
210
+ const replaceTableName = (condition, tableName) =>
211
+ strReplace(condition, TABLE_NAME_PLACEHOLDER_REGEX, tableName);
212
+
201
213
  const map = Map;
202
214
  const mapNew = (entries) => new map(entries);
203
215
  const mapGet = (map2, key) => map2?.get(key);
@@ -1045,7 +1057,7 @@ const getCommandFunctions = (
1045
1057
  await executeCommand(
1046
1058
  SELECT_STAR_FROM +
1047
1059
  escapeId(tableName) +
1048
- getWhereCondition(tableName, condition),
1060
+ getWhere(tableName, condition),
1049
1061
  ),
1050
1062
  (row) => {
1051
1063
  const rowId = row[rowIdColumnName];
@@ -1201,9 +1213,7 @@ const getCommandFunctions = (
1201
1213
  if (isUndefined(content)) {
1202
1214
  if (isUndefined(contentSubIdSet)) {
1203
1215
  await executeCommand(
1204
- DELETE_FROM +
1205
- escapeId(tableName) +
1206
- getWhereCondition(tableName, condition),
1216
+ DELETE_FROM + escapeId(tableName) + getWhere(tableName, condition),
1207
1217
  );
1208
1218
  }
1209
1219
  } else {
@@ -1213,8 +1223,11 @@ const getCommandFunctions = (
1213
1223
  await executeCommand(
1214
1224
  DELETE_FROM +
1215
1225
  escapeId(tableName) +
1216
- getWhereCondition(tableName, condition) +
1217
- `AND(${escapeId(rowIdColumnName)}=${getPlaceholder([1])})`,
1226
+ getWhere(
1227
+ tableName,
1228
+ condition,
1229
+ `(${escapeId(rowIdColumnName)}=${getPlaceholder([1])})`,
1230
+ ),
1218
1231
  [rowId],
1219
1232
  );
1220
1233
  } else if (!isEmpty(settingColumnNames)) {
@@ -1273,8 +1286,12 @@ const getCommandFunctions = (
1273
1286
  await executeCommand(
1274
1287
  DELETE_FROM +
1275
1288
  escapeId(tableName) +
1276
- getWhereCondition(tableName, condition) + // eslint-disable-next-line max-len
1277
- `AND${escapeId(rowIdColumnName)}NOT IN(${getPlaceholders(deleteRowIds, getPlaceholder)})`,
1289
+ getWhere(
1290
+ tableName,
1291
+ condition,
1292
+ // eslint-disable-next-line max-len
1293
+ `${escapeId(rowIdColumnName)}NOT IN(${getPlaceholders(deleteRowIds, getPlaceholder)})`,
1294
+ ),
1278
1295
  deleteRowIds,
1279
1296
  );
1280
1297
  } else if (
@@ -1282,9 +1299,7 @@ const getCommandFunctions = (
1282
1299
  collHas(schemaMap, tableName)
1283
1300
  ) {
1284
1301
  await executeCommand(
1285
- DELETE_FROM +
1286
- escapeId(tableName) +
1287
- getWhereCondition(tableName, condition),
1302
+ DELETE_FROM + escapeId(tableName) + getWhere(tableName, condition),
1288
1303
  );
1289
1304
  }
1290
1305
  }
@@ -1759,30 +1774,28 @@ const createLibSqlPersister = (
1759
1774
  client,
1760
1775
  'getClient',
1761
1776
  void 0,
1762
- client.protocol == 'file'
1763
- ? void 0
1764
- : async (actions) => {
1765
- onSqlCommand?.('BEGIN');
1766
- const transaction = await client.transaction('write');
1767
- return await tryFinallyAsync(
1768
- async () => {
1769
- try {
1770
- const result = await actions(async (sql, args = []) => {
1771
- onSqlCommand?.(sql, args);
1772
- return (await transaction.execute({sql, args})).rows;
1773
- });
1774
- await transaction.commit();
1775
- onSqlCommand?.('END');
1776
- return result;
1777
- } catch (error) {
1778
- await tryCatch(() => transaction.rollback());
1779
- onSqlCommand?.('ROLLBACK');
1780
- throw error;
1781
- }
1782
- },
1783
- () => transaction.close(),
1784
- );
1777
+ async (actions) => {
1778
+ onSqlCommand?.('BEGIN');
1779
+ const transaction = await client.transaction('write');
1780
+ return await tryFinallyAsync(
1781
+ async () => {
1782
+ try {
1783
+ const result = await actions(async (sql, args) => {
1784
+ onSqlCommand?.(sql, args);
1785
+ return (await transaction.execute({sql, args: args ?? []})).rows;
1786
+ });
1787
+ await transaction.commit();
1788
+ onSqlCommand?.('END');
1789
+ return result;
1790
+ } catch (error) {
1791
+ await tryCatch(() => transaction.rollback());
1792
+ onSqlCommand?.('ROLLBACK');
1793
+ throw error;
1794
+ }
1785
1795
  },
1796
+ () => transaction.close(),
1797
+ );
1798
+ },
1786
1799
  );
1787
1800
 
1788
1801
  export {createLibSqlPersister};
@@ -105,50 +105,6 @@ const arrayClear = (array, to = size(array)) => array.splice(0, to);
105
105
  const arrayPush = (array, ...values) => array.push(...values);
106
106
  const arrayShift = (array) => array.shift();
107
107
 
108
- const SINGLE_ROW_ID = '_';
109
- const DEFAULT_ROW_ID_COLUMN_NAME = '_id';
110
- const SELECT = 'SELECT';
111
- const WHERE = 'WHERE';
112
- const TABLE = 'TABLE';
113
- const INSERT = 'INSERT';
114
- const DELETE = 'DELETE';
115
- const UPDATE = 'UPDATE';
116
- const ALTER_TABLE = 'ALTER ' + TABLE;
117
- const FROM = 'FROM';
118
- const DELETE_FROM = DELETE + ' ' + FROM;
119
- const SELECT_STAR_FROM = SELECT + '*' + FROM;
120
- const PRAGMA = 'pragma_';
121
- const DATA_VERSION = 'data_version';
122
- const SCHEMA_VERSION = 'schema_version';
123
- const PRAGMA_TABLE = 'pragma_table_';
124
- const CREATE = 'CREATE ';
125
- const CREATE_TABLE = CREATE + TABLE;
126
- const TABLE_NAME_PLACEHOLDER_REGEX = /\$tableName/g;
127
- const getWrappedCommand = (executeCommand, onSqlCommand) =>
128
- onSqlCommand
129
- ? async (sql, params) => {
130
- onSqlCommand(sql, params);
131
- return await executeCommand(sql, params);
132
- }
133
- : executeCommand;
134
- const escapeId = (str) =>
135
- arrayJoin(
136
- arrayMap(strSplit(str, DOT), (part) => `"${strReplace(part, /"/g, '""')}"`),
137
- DOT,
138
- );
139
- const escapeColumnNames = (...columnNames) =>
140
- arrayJoin(arrayMap(columnNames, escapeId), COMMA);
141
- const anonymousPlaceholder = () => '?';
142
- const getPlaceholders = (array, getPlaceholder, offset = [1]) =>
143
- arrayJoin(
144
- arrayMap(array, () => getPlaceholder(offset)),
145
- COMMA,
146
- );
147
- const getWhereCondition = (tableName, condition = TRUE) =>
148
- WHERE + `(${replaceTableName(condition, escapeId(tableName))})`;
149
- const replaceTableName = (condition, tableName) =>
150
- strReplace(condition, TABLE_NAME_PLACEHOLDER_REGEX, tableName);
151
-
152
108
  const object = Object;
153
109
  const getPrototypeOf = (obj) => object.getPrototypeOf(obj);
154
110
  const objEntries = object.entries;
@@ -198,6 +154,62 @@ const objValues = (obj) => object.values(obj);
198
154
  const objSize = (obj) => size(objIds(obj));
199
155
  const objIsEmpty = (obj) => isObject(obj) && objSize(obj) == 0;
200
156
 
157
+ const SINGLE_ROW_ID = '_';
158
+ const DEFAULT_ROW_ID_COLUMN_NAME = '_id';
159
+ const SELECT = 'SELECT';
160
+ const WHERE = 'WHERE';
161
+ const TABLE = 'TABLE';
162
+ const INSERT = 'INSERT';
163
+ const DELETE = 'DELETE';
164
+ const UPDATE = 'UPDATE';
165
+ const ALTER_TABLE = 'ALTER ' + TABLE;
166
+ const FROM = 'FROM';
167
+ const DELETE_FROM = DELETE + ' ' + FROM;
168
+ const SELECT_STAR_FROM = SELECT + '*' + FROM;
169
+ const PRAGMA = 'pragma_';
170
+ const DATA_VERSION = 'data_version';
171
+ const SCHEMA_VERSION = 'schema_version';
172
+ const PRAGMA_TABLE = 'pragma_table_';
173
+ const CREATE = 'CREATE ';
174
+ const CREATE_TABLE = CREATE + TABLE;
175
+ const TABLE_NAME_PLACEHOLDER_REGEX = /\$tableName/g;
176
+ const getWrappedCommand = (executeCommand, onSqlCommand) =>
177
+ onSqlCommand
178
+ ? async (sql, params) => {
179
+ onSqlCommand(sql, params);
180
+ return await executeCommand(sql, params);
181
+ }
182
+ : executeCommand;
183
+ const escapeId = (str) =>
184
+ arrayJoin(
185
+ arrayMap(strSplit(str, DOT), (part) => `"${strReplace(part, /"/g, '""')}"`),
186
+ DOT,
187
+ );
188
+ const escapeColumnNames = (...columnNames) =>
189
+ arrayJoin(arrayMap(columnNames, escapeId), COMMA);
190
+ const anonymousPlaceholder = () => '?';
191
+ const getPlaceholders = (array, getPlaceholder, offset = [1]) =>
192
+ arrayJoin(
193
+ arrayMap(array, () => getPlaceholder(offset)),
194
+ COMMA,
195
+ );
196
+ const getWhere = (tableName, condition = TRUE, extraCondition) => {
197
+ const conditions = arrayFilter(
198
+ [
199
+ condition == TRUE
200
+ ? void 0
201
+ : `(${replaceTableName(condition, escapeId(tableName))})`,
202
+ extraCondition,
203
+ ],
204
+ (condition2) => !isUndefined(condition2),
205
+ );
206
+ return isEmpty(conditions)
207
+ ? EMPTY_STRING
208
+ : WHERE + arrayJoin(conditions, 'AND');
209
+ };
210
+ const replaceTableName = (condition, tableName) =>
211
+ strReplace(condition, TABLE_NAME_PLACEHOLDER_REGEX, tableName);
212
+
201
213
  const map = Map;
202
214
  const mapNew = (entries) => new map(entries);
203
215
  const mapGet = (map2, key) => map2?.get(key);
@@ -1045,7 +1057,7 @@ const getCommandFunctions = (
1045
1057
  await executeCommand(
1046
1058
  SELECT_STAR_FROM +
1047
1059
  escapeId(tableName) +
1048
- getWhereCondition(tableName, condition),
1060
+ getWhere(tableName, condition),
1049
1061
  ),
1050
1062
  (row) => {
1051
1063
  const rowId = row[rowIdColumnName];
@@ -1201,9 +1213,7 @@ const getCommandFunctions = (
1201
1213
  if (isUndefined(content)) {
1202
1214
  if (isUndefined(contentSubIdSet)) {
1203
1215
  await executeCommand(
1204
- DELETE_FROM +
1205
- escapeId(tableName) +
1206
- getWhereCondition(tableName, condition),
1216
+ DELETE_FROM + escapeId(tableName) + getWhere(tableName, condition),
1207
1217
  );
1208
1218
  }
1209
1219
  } else {
@@ -1213,8 +1223,11 @@ const getCommandFunctions = (
1213
1223
  await executeCommand(
1214
1224
  DELETE_FROM +
1215
1225
  escapeId(tableName) +
1216
- getWhereCondition(tableName, condition) +
1217
- `AND(${escapeId(rowIdColumnName)}=${getPlaceholder([1])})`,
1226
+ getWhere(
1227
+ tableName,
1228
+ condition,
1229
+ `(${escapeId(rowIdColumnName)}=${getPlaceholder([1])})`,
1230
+ ),
1218
1231
  [rowId],
1219
1232
  );
1220
1233
  } else if (!isEmpty(settingColumnNames)) {
@@ -1273,8 +1286,12 @@ const getCommandFunctions = (
1273
1286
  await executeCommand(
1274
1287
  DELETE_FROM +
1275
1288
  escapeId(tableName) +
1276
- getWhereCondition(tableName, condition) + // eslint-disable-next-line max-len
1277
- `AND${escapeId(rowIdColumnName)}NOT IN(${getPlaceholders(deleteRowIds, getPlaceholder)})`,
1289
+ getWhere(
1290
+ tableName,
1291
+ condition,
1292
+ // eslint-disable-next-line max-len
1293
+ `${escapeId(rowIdColumnName)}NOT IN(${getPlaceholders(deleteRowIds, getPlaceholder)})`,
1294
+ ),
1278
1295
  deleteRowIds,
1279
1296
  );
1280
1297
  } else if (
@@ -1282,9 +1299,7 @@ const getCommandFunctions = (
1282
1299
  collHas(schemaMap, tableName)
1283
1300
  ) {
1284
1301
  await executeCommand(
1285
- DELETE_FROM +
1286
- escapeId(tableName) +
1287
- getWhereCondition(tableName, condition),
1302
+ DELETE_FROM + escapeId(tableName) + getWhere(tableName, condition),
1288
1303
  );
1289
1304
  }
1290
1305
  }
@@ -1759,30 +1774,28 @@ const createLibSqlPersister = (
1759
1774
  client,
1760
1775
  'getClient',
1761
1776
  void 0,
1762
- client.protocol == 'file'
1763
- ? void 0
1764
- : async (actions) => {
1765
- onSqlCommand?.('BEGIN');
1766
- const transaction = await client.transaction('write');
1767
- return await tryFinallyAsync(
1768
- async () => {
1769
- try {
1770
- const result = await actions(async (sql, args = []) => {
1771
- onSqlCommand?.(sql, args);
1772
- return (await transaction.execute({sql, args})).rows;
1773
- });
1774
- await transaction.commit();
1775
- onSqlCommand?.('END');
1776
- return result;
1777
- } catch (error) {
1778
- await tryCatch(() => transaction.rollback());
1779
- onSqlCommand?.('ROLLBACK');
1780
- throw error;
1781
- }
1782
- },
1783
- () => transaction.close(),
1784
- );
1777
+ async (actions) => {
1778
+ onSqlCommand?.('BEGIN');
1779
+ const transaction = await client.transaction('write');
1780
+ return await tryFinallyAsync(
1781
+ async () => {
1782
+ try {
1783
+ const result = await actions(async (sql, args) => {
1784
+ onSqlCommand?.(sql, args);
1785
+ return (await transaction.execute({sql, args: args ?? []})).rows;
1786
+ });
1787
+ await transaction.commit();
1788
+ onSqlCommand?.('END');
1789
+ return result;
1790
+ } catch (error) {
1791
+ await tryCatch(() => transaction.rollback());
1792
+ onSqlCommand?.('ROLLBACK');
1793
+ throw error;
1794
+ }
1785
1795
  },
1796
+ () => transaction.close(),
1797
+ );
1798
+ },
1786
1799
  );
1787
1800
 
1788
1801
  export {createLibSqlPersister};
@@ -92,57 +92,6 @@ const arrayClear = (array, to = size(array)) => array.splice(0, to);
92
92
  const arrayPush = (array, ...values) => array.push(...values);
93
93
  const arrayShift = (array) => array.shift();
94
94
 
95
- const SINGLE_ROW_ID = '_';
96
- const DEFAULT_ROW_ID_COLUMN_NAME = '_id';
97
- const SELECT = 'SELECT';
98
- const WHERE = 'WHERE';
99
- const TABLE = 'TABLE';
100
- const INSERT = 'INSERT';
101
- const DELETE = 'DELETE';
102
- const UPDATE = 'UPDATE';
103
- const ALTER_TABLE = 'ALTER ' + TABLE;
104
- const FROM = 'FROM';
105
- const DELETE_FROM = DELETE + ' ' + FROM;
106
- const SELECT_STAR_FROM = SELECT + '*' + FROM;
107
- const CREATE = 'CREATE ';
108
- const CREATE_TABLE = CREATE + TABLE;
109
- const OR_REPLACE = 'OR REPLACE ';
110
- const FUNCTION = 'FUNCTION';
111
- const TABLE_NAME_PLACEHOLDER_REGEX = /\$tableName/g;
112
- const getWrappedCommand = (executeCommand, onSqlCommand) =>
113
- onSqlCommand
114
- ? async (sql, params) => {
115
- onSqlCommand(sql, params);
116
- return await executeCommand(sql, params);
117
- }
118
- : executeCommand;
119
- const escapeId = (str) =>
120
- arrayJoin(
121
- arrayMap(strSplit(str, DOT), (part) => `"${strReplace(part, /"/g, '""')}"`),
122
- DOT,
123
- );
124
- const escapeIds = (...ids) => escapeId(arrayJoin(ids, '_'));
125
- const escapeColumnNames = (...columnNames) =>
126
- arrayJoin(arrayMap(columnNames, escapeId), COMMA);
127
- const numberedPlaceholder = (offset) => '$' + offset[0]++;
128
- const getPlaceholders = (array, getPlaceholder, offset = [1]) =>
129
- arrayJoin(
130
- arrayMap(array, () => getPlaceholder(offset)),
131
- COMMA,
132
- );
133
- const getWhereCondition = (tableName, condition = TRUE) =>
134
- WHERE + `(${replaceTableName(condition, escapeId(tableName))})`;
135
- const replaceTableName = (condition, tableName) =>
136
- strReplace(condition, TABLE_NAME_PLACEHOLDER_REGEX, tableName);
137
-
138
- const collSize = (coll) => coll?.size ?? 0;
139
- const collHas = (coll, keyOrValue) => coll?.has(keyOrValue) ?? false;
140
- const collIsEmpty = (coll) => isUndefined(coll) || collSize(coll) == 0;
141
- const collValues = (coll) => [...(coll?.values() ?? [])];
142
- const collClear = (coll) => coll.clear();
143
- const collForEach = (coll, cb) => coll?.forEach(cb);
144
- const collDel = (coll, keyOrValue) => coll?.delete(keyOrValue);
145
-
146
95
  const object = Object;
147
96
  const getPrototypeOf = (obj) => object.getPrototypeOf(obj);
148
97
  const objEntries = object.entries;
@@ -192,6 +141,69 @@ const objValues = (obj) => object.values(obj);
192
141
  const objSize = (obj) => size(objIds(obj));
193
142
  const objIsEmpty = (obj) => isObject(obj) && objSize(obj) == 0;
194
143
 
144
+ const SINGLE_ROW_ID = '_';
145
+ const DEFAULT_ROW_ID_COLUMN_NAME = '_id';
146
+ const SELECT = 'SELECT';
147
+ const WHERE = 'WHERE';
148
+ const TABLE = 'TABLE';
149
+ const INSERT = 'INSERT';
150
+ const DELETE = 'DELETE';
151
+ const UPDATE = 'UPDATE';
152
+ const ALTER_TABLE = 'ALTER ' + TABLE;
153
+ const FROM = 'FROM';
154
+ const DELETE_FROM = DELETE + ' ' + FROM;
155
+ const SELECT_STAR_FROM = SELECT + '*' + FROM;
156
+ const CREATE = 'CREATE ';
157
+ const CREATE_TABLE = CREATE + TABLE;
158
+ const OR_REPLACE = 'OR REPLACE ';
159
+ const FUNCTION = 'FUNCTION';
160
+ const TABLE_NAME_PLACEHOLDER_REGEX = /\$tableName/g;
161
+ const getWrappedCommand = (executeCommand, onSqlCommand) =>
162
+ onSqlCommand
163
+ ? async (sql, params) => {
164
+ onSqlCommand(sql, params);
165
+ return await executeCommand(sql, params);
166
+ }
167
+ : executeCommand;
168
+ const escapeId = (str) =>
169
+ arrayJoin(
170
+ arrayMap(strSplit(str, DOT), (part) => `"${strReplace(part, /"/g, '""')}"`),
171
+ DOT,
172
+ );
173
+ const escapeIds = (...ids) => escapeId(arrayJoin(ids, '_'));
174
+ const escapeColumnNames = (...columnNames) =>
175
+ arrayJoin(arrayMap(columnNames, escapeId), COMMA);
176
+ const numberedPlaceholder = (offset) => '$' + offset[0]++;
177
+ const getPlaceholders = (array, getPlaceholder, offset = [1]) =>
178
+ arrayJoin(
179
+ arrayMap(array, () => getPlaceholder(offset)),
180
+ COMMA,
181
+ );
182
+ const getWhere = (tableName, condition = TRUE, extraCondition) => {
183
+ const conditions = arrayFilter(
184
+ [
185
+ condition == TRUE
186
+ ? void 0
187
+ : `(${replaceTableName(condition, escapeId(tableName))})`,
188
+ extraCondition,
189
+ ],
190
+ (condition2) => !isUndefined(condition2),
191
+ );
192
+ return isEmpty(conditions)
193
+ ? EMPTY_STRING
194
+ : WHERE + arrayJoin(conditions, 'AND');
195
+ };
196
+ const replaceTableName = (condition, tableName) =>
197
+ strReplace(condition, TABLE_NAME_PLACEHOLDER_REGEX, tableName);
198
+
199
+ const collSize = (coll) => coll?.size ?? 0;
200
+ const collHas = (coll, keyOrValue) => coll?.has(keyOrValue) ?? false;
201
+ const collIsEmpty = (coll) => isUndefined(coll) || collSize(coll) == 0;
202
+ const collValues = (coll) => [...(coll?.values() ?? [])];
203
+ const collClear = (coll) => coll.clear();
204
+ const collForEach = (coll, cb) => coll?.forEach(cb);
205
+ const collDel = (coll, keyOrValue) => coll?.delete(keyOrValue);
206
+
195
207
  const jsonString = JSON.stringify;
196
208
  const jsonParse = JSON.parse;
197
209
  const jsonStringWithUndefined = (obj) =>
@@ -1039,7 +1051,7 @@ const getCommandFunctions = (
1039
1051
  await executeCommand(
1040
1052
  SELECT_STAR_FROM +
1041
1053
  escapeId(tableName) +
1042
- getWhereCondition(tableName, condition),
1054
+ getWhere(tableName, condition),
1043
1055
  ),
1044
1056
  (row) => {
1045
1057
  const rowId = row[rowIdColumnName];
@@ -1195,9 +1207,7 @@ const getCommandFunctions = (
1195
1207
  if (isUndefined(content)) {
1196
1208
  if (isUndefined(contentSubIdSet)) {
1197
1209
  await executeCommand(
1198
- DELETE_FROM +
1199
- escapeId(tableName) +
1200
- getWhereCondition(tableName, condition),
1210
+ DELETE_FROM + escapeId(tableName) + getWhere(tableName, condition),
1201
1211
  );
1202
1212
  }
1203
1213
  } else {
@@ -1207,8 +1217,11 @@ const getCommandFunctions = (
1207
1217
  await executeCommand(
1208
1218
  DELETE_FROM +
1209
1219
  escapeId(tableName) +
1210
- getWhereCondition(tableName, condition) +
1211
- `AND(${escapeId(rowIdColumnName)}=${getPlaceholder([1])})`,
1220
+ getWhere(
1221
+ tableName,
1222
+ condition,
1223
+ `(${escapeId(rowIdColumnName)}=${getPlaceholder([1])})`,
1224
+ ),
1212
1225
  [rowId],
1213
1226
  );
1214
1227
  } else if (!isEmpty(settingColumnNames)) {
@@ -1267,8 +1280,12 @@ const getCommandFunctions = (
1267
1280
  await executeCommand(
1268
1281
  DELETE_FROM +
1269
1282
  escapeId(tableName) +
1270
- getWhereCondition(tableName, condition) + // eslint-disable-next-line max-len
1271
- `AND${escapeId(rowIdColumnName)}NOT IN(${getPlaceholders(deleteRowIds, getPlaceholder)})`,
1283
+ getWhere(
1284
+ tableName,
1285
+ condition,
1286
+ // eslint-disable-next-line max-len
1287
+ `${escapeId(rowIdColumnName)}NOT IN(${getPlaceholders(deleteRowIds, getPlaceholder)})`,
1288
+ ),
1272
1289
  deleteRowIds,
1273
1290
  );
1274
1291
  } else if (
@@ -1276,9 +1293,7 @@ const getCommandFunctions = (
1276
1293
  collHas(schemaMap, tableName)
1277
1294
  ) {
1278
1295
  await executeCommand(
1279
- DELETE_FROM +
1280
- escapeId(tableName) +
1281
- getWhereCondition(tableName, condition),
1296
+ DELETE_FROM + escapeId(tableName) + getWhere(tableName, condition),
1282
1297
  );
1283
1298
  }
1284
1299
  }