orange-orm 3.10.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 (459) hide show
  1. package/.dockerignore +1 -0
  2. package/.eslintignore +2 -0
  3. package/.eslintrc.js +45 -0
  4. package/.gitattributes +3 -0
  5. package/CNAME +1 -0
  6. package/CONTRIBUTING.md +43 -0
  7. package/LICENSE +7 -0
  8. package/README.md +1969 -0
  9. package/SECURITY.md +12 -0
  10. package/bin/build.js +127 -0
  11. package/bin/compile.js +26 -0
  12. package/bin/rdb.js +4 -0
  13. package/docs/CODE_OF_CONDUCT.md +18 -0
  14. package/docs/changelog.md +461 -0
  15. package/docs/docs.md +2373 -0
  16. package/docs/orange.svg +32 -0
  17. package/docs/relations.png +0 -0
  18. package/package.json +139 -0
  19. package/src/applyPatch.js +105 -0
  20. package/src/client/axiosInterceptor.js +46 -0
  21. package/src/client/clientMap.js +72 -0
  22. package/src/client/createPatch.js +122 -0
  23. package/src/client/createProviders.js +121 -0
  24. package/src/client/index.js +997 -0
  25. package/src/client/index.mjs +6295 -0
  26. package/src/client/map.js +110 -0
  27. package/src/client/merge.js +9 -0
  28. package/src/client/netAdapter.js +133 -0
  29. package/src/client/rollup.config.js +12 -0
  30. package/src/client/self.js +4 -0
  31. package/src/client/stringify.js +22 -0
  32. package/src/client/toKeyPositionMap.js +30 -0
  33. package/src/clsTest.js +79 -0
  34. package/src/createDomain/negotiateForwardProperty.js +23 -0
  35. package/src/createDomain.js +31 -0
  36. package/src/dateToISOString.js +25 -0
  37. package/src/emitEvent.js +46 -0
  38. package/src/emptyFilter.js +35 -0
  39. package/src/flags.js +6 -0
  40. package/src/fromCompareObject.js +24 -0
  41. package/src/generic-pool.js +641 -0
  42. package/src/getManyDto/newQuery.js +18 -0
  43. package/src/getManyDto/query/newColumnSql.js +8 -0
  44. package/src/getManyDto/query/newSingleQuery.js +19 -0
  45. package/src/getManyDto/query/singleQuery/newJoinedColumnSql.js +25 -0
  46. package/src/getManyDto/query/singleQuery/newShallowColumnSql.js +25 -0
  47. package/src/getManyDto.js +255 -0
  48. package/src/getTSDefinition.js +510 -0
  49. package/src/hostExpress/executePath.js +433 -0
  50. package/src/hostExpress/getMeta.js +49 -0
  51. package/src/hostExpress.js +125 -0
  52. package/src/hostLocal.js +110 -0
  53. package/src/index.d.ts +324 -0
  54. package/src/index.js +116 -0
  55. package/src/jsconfig.json +8 -0
  56. package/src/lock/toIntKey.js +35 -0
  57. package/src/lock.js +26 -0
  58. package/src/map.d.ts +1729 -0
  59. package/src/mssql/encodeBuffer.js +7 -0
  60. package/src/mssql/newDatabase.js +129 -0
  61. package/src/mssql/newPool.js +19 -0
  62. package/src/mssql/newTransaction.js +69 -0
  63. package/src/mssql/pool/defaults.js +45 -0
  64. package/src/mssql/pool/end.js +13 -0
  65. package/src/mssql/pool/newGenericPool.js +62 -0
  66. package/src/mssql/wrapQuery.js +34 -0
  67. package/src/mySql/deleteFromSql.js +8 -0
  68. package/src/mySql/encodeBoolean.js +7 -0
  69. package/src/mySql/insert.js +21 -0
  70. package/src/mySql/insertSql.js +62 -0
  71. package/src/mySql/lastInsertedSql.js +11 -0
  72. package/src/mySql/limitAndOffset.js +18 -0
  73. package/src/mySql/newDatabase.js +127 -0
  74. package/src/mySql/newPool.js +19 -0
  75. package/src/mySql/newTransaction.js +54 -0
  76. package/src/mySql/pool/defaults.js +16 -0
  77. package/src/mySql/pool/end.js +13 -0
  78. package/src/mySql/pool/negotiatePoolOptions.js +12 -0
  79. package/src/mySql/pool/newGenericPool.js +67 -0
  80. package/src/mySql/selectForUpdateSql.js +3 -0
  81. package/src/mySql/wrapQuery.js +16 -0
  82. package/src/mySql/wrapQueryStream.js +9 -0
  83. package/src/newArray.js +3 -0
  84. package/src/newCollection.js +55 -0
  85. package/src/newId.js +1 -0
  86. package/src/newImmutable.js +22 -0
  87. package/src/newObject.js +5 -0
  88. package/src/oracle/deleteFromSql.js +8 -0
  89. package/src/oracle/encodeBoolean.js +7 -0
  90. package/src/oracle/encodeBuffer.js +7 -0
  91. package/src/oracle/encodeDate.js +7 -0
  92. package/src/oracle/formatDateIn.js +5 -0
  93. package/src/oracle/formatDateOut.js +5 -0
  94. package/src/oracle/insert.js +23 -0
  95. package/src/oracle/insertSql.js +73 -0
  96. package/src/oracle/lastInsertedSql.js +14 -0
  97. package/src/oracle/limitAndOffset.js +18 -0
  98. package/src/oracle/mergeSql.js +79 -0
  99. package/src/oracle/newDatabase.js +128 -0
  100. package/src/oracle/newInsertCommandCore.js +27 -0
  101. package/src/oracle/newPool.js +19 -0
  102. package/src/oracle/newTransaction.js +63 -0
  103. package/src/oracle/outputInsertedSql.js +5 -0
  104. package/src/oracle/pool/defaults.js +45 -0
  105. package/src/oracle/pool/end.js +13 -0
  106. package/src/oracle/pool/newGenericPool.js +64 -0
  107. package/src/oracle/replaceParamChar.js +14 -0
  108. package/src/oracle/selectForUpdateSql.js +3 -0
  109. package/src/oracle/wrapQuery.js +38 -0
  110. package/src/patchRow.js +13 -0
  111. package/src/patchTable.js +317 -0
  112. package/src/pg/deleteFromSql.js +8 -0
  113. package/src/pg/encodeBoolean.js +5 -0
  114. package/src/pg/encodeDate.js +7 -0
  115. package/src/pg/encodeJSON.js +15 -0
  116. package/src/pg/formatDateOut.js +5 -0
  117. package/src/pg/insert.js +14 -0
  118. package/src/pg/insertSql.js +70 -0
  119. package/src/pg/lastInsertedSql.js +11 -0
  120. package/src/pg/limitAndOffset.js +18 -0
  121. package/src/pg/newDatabase.js +153 -0
  122. package/src/pg/newPool.js +19 -0
  123. package/src/pg/newStreamableQuery.js +7 -0
  124. package/src/pg/newTransaction.js +58 -0
  125. package/src/pg/pool/defaults.js +45 -0
  126. package/src/pg/pool/end.js +13 -0
  127. package/src/pg/pool/newPgPool.js +82 -0
  128. package/src/pg/replaceParamChar.js +14 -0
  129. package/src/pg/schema.js +11 -0
  130. package/src/pg/selectForUpdateSql.js +3 -0
  131. package/src/pg/wrapQuery.js +33 -0
  132. package/src/pg/wrapQueryStream.js +20 -0
  133. package/src/pools.js +17 -0
  134. package/src/query/negotiateParameters.js +10 -0
  135. package/src/query/negotiateSql.js +14 -0
  136. package/src/query/wrapQuery.js +17 -0
  137. package/src/query.js +13 -0
  138. package/src/sap/deleteFromSql.js +8 -0
  139. package/src/sap/encodeBoolean.js +7 -0
  140. package/src/sap/encodeBuffer.js +7 -0
  141. package/src/sap/formatDateOut.js +5 -0
  142. package/src/sap/insert.js +21 -0
  143. package/src/sap/insertSql.js +64 -0
  144. package/src/sap/lastInsertedSql.js +12 -0
  145. package/src/sap/limitAndOffset.js +16 -0
  146. package/src/sap/mergeSql.js +73 -0
  147. package/src/sap/newDatabase.js +129 -0
  148. package/src/sap/newPool.js +19 -0
  149. package/src/sap/newTransaction.js +69 -0
  150. package/src/sap/pool/end.js +13 -0
  151. package/src/sap/selectForUpdateSql.js +3 -0
  152. package/src/sqlite/deleteFromSql.js +8 -0
  153. package/src/sqlite/encodeBoolean.js +7 -0
  154. package/src/sqlite/encodeBuffer.js +7 -0
  155. package/src/sqlite/insert.js +21 -0
  156. package/src/sqlite/insertSql.js +64 -0
  157. package/src/sqlite/lastInsertedSql.js +12 -0
  158. package/src/sqlite/limitAndOffset.js +18 -0
  159. package/src/sqlite/newDatabase.js +128 -0
  160. package/src/sqlite/newPool.js +19 -0
  161. package/src/sqlite/newTransaction.js +58 -0
  162. package/src/sqlite/pool/defaults.js +45 -0
  163. package/src/sqlite/pool/end.js +13 -0
  164. package/src/sqlite/pool/newGenericPool.js +59 -0
  165. package/src/sqlite/selectForUpdateSql.js +3 -0
  166. package/src/sqlite/wrapQuery.js +24 -0
  167. package/src/table/aggregate.js +9 -0
  168. package/src/table/begin.js +10 -0
  169. package/src/table/cascadeDelete.js +11 -0
  170. package/src/table/cloneStrategy.js +18 -0
  171. package/src/table/column/binary/newDecode.js +15 -0
  172. package/src/table/column/binary/newEncode.js +23 -0
  173. package/src/table/column/binary/purify.js +12 -0
  174. package/src/table/column/binary.js +12 -0
  175. package/src/table/column/boolean/newDecode.js +12 -0
  176. package/src/table/column/boolean/newEncode.js +36 -0
  177. package/src/table/column/boolean/purify.js +7 -0
  178. package/src/table/column/boolean.js +14 -0
  179. package/src/table/column/date/formatOut.js +11 -0
  180. package/src/table/column/date/newDecode.js +17 -0
  181. package/src/table/column/date/newEncode.js +47 -0
  182. package/src/table/column/date/purify.js +17 -0
  183. package/src/table/column/date/toISOString.js +25 -0
  184. package/src/table/column/date/tryParseISO.js +14 -0
  185. package/src/table/column/date.js +14 -0
  186. package/src/table/column/dateWithTimeZone/newEncode.js +41 -0
  187. package/src/table/column/dateWithTimeZone.js +14 -0
  188. package/src/table/column/encodeFilterArg.js +8 -0
  189. package/src/table/column/equal.js +15 -0
  190. package/src/table/column/extractAlias.js +7 -0
  191. package/src/table/column/greaterThan.js +12 -0
  192. package/src/table/column/greaterThanOrEqual.js +12 -0
  193. package/src/table/column/guid/newDecode.js +10 -0
  194. package/src/table/column/guid/newEncode.js +30 -0
  195. package/src/table/column/guid/purify.js +10 -0
  196. package/src/table/column/guid.js +12 -0
  197. package/src/table/column/in.js +24 -0
  198. package/src/table/column/json/formatOut.js +11 -0
  199. package/src/table/column/json/newDecode.js +21 -0
  200. package/src/table/column/json/newEncode.js +41 -0
  201. package/src/table/column/json/purify.js +7 -0
  202. package/src/table/column/json.js +22 -0
  203. package/src/table/column/lessThan.js +12 -0
  204. package/src/table/column/lessThanOrEqual.js +12 -0
  205. package/src/table/column/negotiateNextAndFilter.js +7 -0
  206. package/src/table/column/negotiateNextOrFilter.js +7 -0
  207. package/src/table/column/negotiateRawSqlFilter.js +74 -0
  208. package/src/table/column/newBoolean.js +54 -0
  209. package/src/table/column/newColumn.js +89 -0
  210. package/src/table/column/newDecodeCore.js +10 -0
  211. package/src/table/column/notEqual.js +15 -0
  212. package/src/table/column/numeric/negotiateDefault.js +5 -0
  213. package/src/table/column/numeric/newDecode.js +16 -0
  214. package/src/table/column/numeric/newEncode.js +25 -0
  215. package/src/table/column/numeric/purify.js +12 -0
  216. package/src/table/column/numeric.js +15 -0
  217. package/src/table/column/string/contains.js +3 -0
  218. package/src/table/column/string/containsCore.js +16 -0
  219. package/src/table/column/string/endsWith.js +3 -0
  220. package/src/table/column/string/endsWithCore.js +16 -0
  221. package/src/table/column/string/iContains.js +3 -0
  222. package/src/table/column/string/iEndsWith.js +3 -0
  223. package/src/table/column/string/iEqual.js +15 -0
  224. package/src/table/column/string/iStartsWith.js +3 -0
  225. package/src/table/column/string/newEncode.js +31 -0
  226. package/src/table/column/string/purify.js +7 -0
  227. package/src/table/column/string/startsWith.js +3 -0
  228. package/src/table/column/string/startsWithCore.js +16 -0
  229. package/src/table/column/string.js +55 -0
  230. package/src/table/column.js +154 -0
  231. package/src/table/commands/beginCommand.js +13 -0
  232. package/src/table/commands/commitCommand.js +9 -0
  233. package/src/table/commands/compressChanges.js +25 -0
  234. package/src/table/commands/delete/createAlias.js +6 -0
  235. package/src/table/commands/delete/newSingleCommand.js +17 -0
  236. package/src/table/commands/delete/singleCommand/joinSql.js +26 -0
  237. package/src/table/commands/delete/singleCommand/newSingleCommandCore.js +19 -0
  238. package/src/table/commands/delete/singleCommand/selectSql.js +11 -0
  239. package/src/table/commands/delete/singleCommand/subFilter.js +19 -0
  240. package/src/table/commands/delete/singleCommand/whereSql.js +25 -0
  241. package/src/table/commands/getChangeSet.js +6 -0
  242. package/src/table/commands/insert/getSqlTemplate.js +68 -0
  243. package/src/table/commands/lastCommandMatches.js +11 -0
  244. package/src/table/commands/negotiateEndEdit.js +7 -0
  245. package/src/table/commands/newDeleteCommand.js +23 -0
  246. package/src/table/commands/newGetLastInsertedCommand.js +49 -0
  247. package/src/table/commands/newGetLastInsertedCommandCore.js +42 -0
  248. package/src/table/commands/newInsertAndForgetCommand.js +55 -0
  249. package/src/table/commands/newInsertCommand.js +56 -0
  250. package/src/table/commands/newInsertCommandCore.js +27 -0
  251. package/src/table/commands/newRow.js +51 -0
  252. package/src/table/commands/newUpdateCommand.js +62 -0
  253. package/src/table/commands/newUpdateCommandCore.js +45 -0
  254. package/src/table/commands/pushCommand.js +10 -0
  255. package/src/table/commands/rollbackCommand.js +13 -0
  256. package/src/table/commit.js +30 -0
  257. package/src/table/count.js +29 -0
  258. package/src/table/createJSONReadStream.js +7 -0
  259. package/src/table/createJSONReadStreamDefault.js +33 -0
  260. package/src/table/createJSONReadStreamNative.js +31 -0
  261. package/src/table/createReadStream.js +24 -0
  262. package/src/table/createReadStreamCoreNative.js +40 -0
  263. package/src/table/createReadStreamDefault.js +102 -0
  264. package/src/table/createReadStreamNative.js +17 -0
  265. package/src/table/delete.js +20 -0
  266. package/src/table/deleteSessionContext.js +17 -0
  267. package/src/table/encodeDbSpecific.js +10 -0
  268. package/src/table/executeQueries/executeChanges.js +35 -0
  269. package/src/table/executeQueries/executeQueriesCore.js +12 -0
  270. package/src/table/executeQueries/executeQuery.js +8 -0
  271. package/src/table/executeQueries/resolveExecuteQuery.js +57 -0
  272. package/src/table/executeQueries.js +15 -0
  273. package/src/table/extractDeleteStrategy.js +9 -0
  274. package/src/table/fuzzyPromise.js +18 -0
  275. package/src/table/getById.js +33 -0
  276. package/src/table/getFirstFromDb.js +10 -0
  277. package/src/table/getFromDbById.js +17 -0
  278. package/src/table/getIdArgs.js +14 -0
  279. package/src/table/getMany.js +26 -0
  280. package/src/table/getManyDto/newQuery.js +32 -0
  281. package/src/table/getManyDto/pg/newQuery.js +8 -0
  282. package/src/table/getManyDto.js +10 -0
  283. package/src/table/getManyDtoFast/getSubRows.js +172 -0
  284. package/src/table/getManyDtoFast/newQuery.js +8 -0
  285. package/src/table/getManyDtoFast/newQueryCore.js +18 -0
  286. package/src/table/getManyDtoFast/query/newSingleQuery.js +20 -0
  287. package/src/table/getManyDtoFast/query/newSubQueries/joinLegToQuery.js +18 -0
  288. package/src/table/getManyDtoFast/query/newSubQueries/manyLegToQuery.js +20 -0
  289. package/src/table/getManyDtoFast/query/newSubQueries/oneLegToQuery.js +21 -0
  290. package/src/table/getManyDtoFast/query/newSubQueries.js +47 -0
  291. package/src/table/getManyDtoFast/query/singleQuery/newShallowColumnSql.js +21 -0
  292. package/src/table/getSessionContext.js +10 -0
  293. package/src/table/getSessionSingleton.js +5 -0
  294. package/src/table/groupBy/newQuery.js +25 -0
  295. package/src/table/groupBy.js +65 -0
  296. package/src/table/hasMany.js +33 -0
  297. package/src/table/hasOne.js +33 -0
  298. package/src/table/insert.js +91 -0
  299. package/src/table/insertDefault.js +88 -0
  300. package/src/table/join.js +42 -0
  301. package/src/table/joinRelation/getRelatives.js +64 -0
  302. package/src/table/log.js +39 -0
  303. package/src/table/negotiateAddStrategy.js +20 -0
  304. package/src/table/negotiateExpandInverse.js +13 -0
  305. package/src/table/newCache.js +96 -0
  306. package/src/table/newCascadeDeleteStrategy.js +30 -0
  307. package/src/table/newGetRelated.js +21 -0
  308. package/src/table/newJoinRelation.js +117 -0
  309. package/src/table/newManyRelation.js +61 -0
  310. package/src/table/newOneRelation.js +61 -0
  311. package/src/table/newPrimaryKeyFilter.js +13 -0
  312. package/src/table/newQuery.js +18 -0
  313. package/src/table/newRelatedTable.js +100 -0
  314. package/src/table/newRowCache.js +106 -0
  315. package/src/table/newThrow.js +11 -0
  316. package/src/table/nullPromise.js +1 -0
  317. package/src/table/objectToCallback.js +7 -0
  318. package/src/table/oneRelation/getRelatives.js +45 -0
  319. package/src/table/popChanges.js +19 -0
  320. package/src/table/promise.js +14 -0
  321. package/src/table/purifyStrategy.js +49 -0
  322. package/src/table/query/extractFilter.js +9 -0
  323. package/src/table/query/extractLimit.js +11 -0
  324. package/src/table/query/extractLimitQuery.js +23 -0
  325. package/src/table/query/extractOffset.js +11 -0
  326. package/src/table/query/extractOrderBy.js +57 -0
  327. package/src/table/query/extractSql.js +10 -0
  328. package/src/table/query/newParameterized.js +38 -0
  329. package/src/table/query/newSingleQuery.js +23 -0
  330. package/src/table/query/parameterized/extractParameters.js +8 -0
  331. package/src/table/query/singleQuery/columnSql/joinLegToColumnSql.js +16 -0
  332. package/src/table/query/singleQuery/columnSql/newJoinedColumnSql.js +27 -0
  333. package/src/table/query/singleQuery/columnSql/newShallowColumnSql.js +37 -0
  334. package/src/table/query/singleQuery/joinSql/joinLegToJoinSql.js +13 -0
  335. package/src/table/query/singleQuery/joinSql/joinLegToShallowJoinSql.js +9 -0
  336. package/src/table/query/singleQuery/joinSql/newDiscriminatorSql.js +11 -0
  337. package/src/table/query/singleQuery/joinSql/newShallowJoinSql.js +9 -0
  338. package/src/table/query/singleQuery/joinSql/newShallowJoinSqlCore.js +25 -0
  339. package/src/table/query/singleQuery/joinSql/oneLegToJoinSql.js +13 -0
  340. package/src/table/query/singleQuery/joinSql/oneLegToShallowJoinSql.js +10 -0
  341. package/src/table/query/singleQuery/negotiateExclusive.js +11 -0
  342. package/src/table/query/singleQuery/negotiateLimit.js +10 -0
  343. package/src/table/query/singleQuery/newColumnSql.js +8 -0
  344. package/src/table/query/singleQuery/newDiscriminatorSql.js +33 -0
  345. package/src/table/query/singleQuery/newJoinSql.js +41 -0
  346. package/src/table/query/singleQuery/newQueryContext.js +20 -0
  347. package/src/table/query/singleQuery/newWhereSql.js +19 -0
  348. package/src/table/readStream/extractLimit.js +7 -0
  349. package/src/table/readStream/extractOrderBy.js +55 -0
  350. package/src/table/readStream/mySql/newQuery.js +16 -0
  351. package/src/table/readStream/mySql/query/newSingleQuery.js +21 -0
  352. package/src/table/readStream/mySql/query/newSubQueries/joinLegToQuery.js +19 -0
  353. package/src/table/readStream/mySql/query/newSubQueries/manyLegToQuery.js +22 -0
  354. package/src/table/readStream/mySql/query/newSubQueries/newQueryCore.js +9 -0
  355. package/src/table/readStream/mySql/query/newSubQueries/newSingleQueryCore.js +18 -0
  356. package/src/table/readStream/mySql/query/newSubQueries/oneLegToQuery.js +22 -0
  357. package/src/table/readStream/mySql/query/newSubQueries.js +47 -0
  358. package/src/table/readStream/mySql/query/singleQuery/newShallowColumnSql.js +17 -0
  359. package/src/table/readStream/newQuery.js +32 -0
  360. package/src/table/readStream/newQueryStream.js +8 -0
  361. package/src/table/readStream/pg/newQuery.js +8 -0
  362. package/src/table/readStream/pg/newQueryCore.js +17 -0
  363. package/src/table/readStream/pg/query/newSingleQuery.js +19 -0
  364. package/src/table/readStream/pg/query/newSubQueries/joinLegToQuery.js +19 -0
  365. package/src/table/readStream/pg/query/newSubQueries/manyLegToQuery.js +22 -0
  366. package/src/table/readStream/pg/query/newSubQueries/oneLegToQuery.js +19 -0
  367. package/src/table/readStream/pg/query/newSubQueries.js +47 -0
  368. package/src/table/readStream/pg/query/singleQuery/newShallowColumnSql.js +19 -0
  369. package/src/table/readStreamDefault/createBatchFilter.js +39 -0
  370. package/src/table/relatedTable/aggregate/aggregateJoinSql.js +37 -0
  371. package/src/table/relatedTable/aggregate/joinSql.js +38 -0
  372. package/src/table/relatedTable/aggregate.js +48 -0
  373. package/src/table/relatedTable/all.js +24 -0
  374. package/src/table/relatedTable/any.js +70 -0
  375. package/src/table/relatedTable/childColumn.js +61 -0
  376. package/src/table/relatedTable/columnAggregate.js +16 -0
  377. package/src/table/relatedTable/columnAggregateGroup.js +59 -0
  378. package/src/table/relatedTable/joinSql.js +40 -0
  379. package/src/table/relatedTable/joinSqlArray.js +32 -0
  380. package/src/table/relatedTable/none.js +22 -0
  381. package/src/table/relatedTable/relatedColumn.js +49 -0
  382. package/src/table/relatedTable/relationFilter.js +16 -0
  383. package/src/table/relatedTable/selectSql.js +11 -0
  384. package/src/table/relatedTable/subFilter.js +16 -0
  385. package/src/table/relatedTable/where.js +50 -0
  386. package/src/table/relatedTable/whereSql.js +41 -0
  387. package/src/table/relation/manyCache/extractParentKey.js +17 -0
  388. package/src/table/relation/manyCache/synchronizeAdded.js +13 -0
  389. package/src/table/relation/manyCache/synchronizeChanged.js +31 -0
  390. package/src/table/relation/manyCache/synchronizeRemoved.js +13 -0
  391. package/src/table/relation/newExpanderCache.js +7 -0
  392. package/src/table/relation/newForeignKeyFilter.js +20 -0
  393. package/src/table/relation/newJoinLeg.js +23 -0
  394. package/src/table/relation/newManyCache.js +57 -0
  395. package/src/table/relation/newManyCacheCore.js +53 -0
  396. package/src/table/relation/newManyLeg.js +15 -0
  397. package/src/table/relation/newOneCache.js +28 -0
  398. package/src/table/relation/newOneLeg.js +25 -0
  399. package/src/table/releaseDbClient.js +15 -0
  400. package/src/table/resultToPromise.js +8 -0
  401. package/src/table/resultToRows/dbRowToRow.js +51 -0
  402. package/src/table/resultToRows/dbRowsToRows.js +13 -0
  403. package/src/table/resultToRows/decodeDbRow.js +25 -0
  404. package/src/table/resultToRows/delete/removeFromCache.js +33 -0
  405. package/src/table/resultToRows/delete.js +31 -0
  406. package/src/table/resultToRows/negotiateQueryContext.js +6 -0
  407. package/src/table/resultToRows/newDecodeDbRow.js +262 -0
  408. package/src/table/resultToRows/toDto/createDto.js +20 -0
  409. package/src/table/resultToRows/toDto/extractStrategy.js +41 -0
  410. package/src/table/resultToRows/toDto.js +92 -0
  411. package/src/table/resultToRows.js +37 -0
  412. package/src/table/rollback.js +31 -0
  413. package/src/table/rowArray/negotiateNextTick.js +11 -0
  414. package/src/table/rowArray/orderBy.js +51 -0
  415. package/src/table/rowArray.js +62 -0
  416. package/src/table/setSessionSingleton.js +5 -0
  417. package/src/table/strategyToSpan.js +49 -0
  418. package/src/table/tryGetById.js +13 -0
  419. package/src/table/tryGetFirstFromDb.js +23 -0
  420. package/src/table/tryGetFromCacheById.js +12 -0
  421. package/src/table/tryGetFromDbById/extractStrategy.js +9 -0
  422. package/src/table/tryGetFromDbById.js +19 -0
  423. package/src/table/tryGetSessionContext.js +16 -0
  424. package/src/table/tryReleaseDbClient.js +14 -0
  425. package/src/table/updateField.js +12 -0
  426. package/src/table/where.js +12 -0
  427. package/src/table.js +174 -0
  428. package/src/tedious/deleteFromSql.js +8 -0
  429. package/src/tedious/encodeBoolean.js +7 -0
  430. package/src/tedious/encodeBuffer.js +7 -0
  431. package/src/tedious/formatDateOut.js +5 -0
  432. package/src/tedious/formatJSONOut.js +5 -0
  433. package/src/tedious/getManyDto/newQuery.js +8 -0
  434. package/src/tedious/getManyDto/newQueryCore.js +18 -0
  435. package/src/tedious/getManyDto/query/newSingleQuery.js +29 -0
  436. package/src/tedious/getManyDto/query/newSubQueries/joinLegToQuery.js +16 -0
  437. package/src/tedious/getManyDto/query/newSubQueries/manyLegToQuery.js +16 -0
  438. package/src/tedious/getManyDto/query/newSubQueries/oneLegToQuery.js +19 -0
  439. package/src/tedious/getManyDto/query/newSubQueries.js +48 -0
  440. package/src/tedious/getManyDto/query/singleQuery/newShallowColumnSql.js +34 -0
  441. package/src/tedious/getManyDto.js +27 -0
  442. package/src/tedious/insert.js +13 -0
  443. package/src/tedious/insertSql.js +70 -0
  444. package/src/tedious/limitAndOffset.js +16 -0
  445. package/src/tedious/mergeSql.js +76 -0
  446. package/src/tedious/newDatabase.js +129 -0
  447. package/src/tedious/newPool.js +19 -0
  448. package/src/tedious/newTransaction.js +72 -0
  449. package/src/tedious/outputInsertedSql.js +11 -0
  450. package/src/tedious/pool/defaults.js +45 -0
  451. package/src/tedious/pool/end.js +13 -0
  452. package/src/tedious/pool/newGenericPool.js +71 -0
  453. package/src/tedious/pool/parseConnectionString.js +40 -0
  454. package/src/tedious/selectForUpdateSql.js +3 -0
  455. package/src/tedious/wrapQuery.js +112 -0
  456. package/src/toCompareObject.js +41 -0
  457. package/src/useHook.js +9 -0
  458. package/src/validateDeleteAllowed.js +49 -0
  459. package/src/validateDeleteConflict.js +92 -0
@@ -0,0 +1,997 @@
1
+ const createPatch = require('./createPatch');
2
+ const stringify = require('./stringify');
3
+ const netAdapter = require('./netAdapter');
4
+ const toKeyPositionMap = require('./toKeyPositionMap');
5
+ const rootMap = new WeakMap();
6
+ const fetchingStrategyMap = new WeakMap();
7
+ const targetKey = Symbol();
8
+ const map = require('./clientMap');
9
+ const clone = require('rfdc/default');
10
+ const createAxiosInterceptor = require('./axiosInterceptor');
11
+ const flags = require('../flags');
12
+
13
+ function rdbClient(options = {}) {
14
+ flags.useLazyDefaults = false;
15
+ if (options.pg)
16
+ options = { db: options };
17
+ let transaction = options.transaction;
18
+ let _reactive = options.reactive;
19
+ let providers = options.providers || {};
20
+ let baseUrl = options.db;
21
+ if (typeof providers === 'function')
22
+ baseUrl = typeof options.db === 'function' ? providers(options.db) : options.db;
23
+ const axiosInterceptor = createAxiosInterceptor();
24
+
25
+ function client(_options = {}) {
26
+ if (_options.pg)
27
+ _options = { db: _options };
28
+ return rdbClient({ ...options, ..._options });
29
+ }
30
+
31
+ client.reactive = (cb => _reactive = cb);
32
+ client.map = map.bind(null, client);
33
+ Object.defineProperty(client, 'metaData', {
34
+ get: getMetaData,
35
+ enumerable: true,
36
+ configurable: false
37
+ });
38
+ client.interceptors = axiosInterceptor;
39
+ client.createPatch = _createPatch;
40
+ client.table = table;
41
+ client.or = column('or');
42
+ client.and = column('and');
43
+ client.not = column('not');
44
+ client.filter = {
45
+ or: client.or,
46
+ and: client.and,
47
+ not: client.not,
48
+ toJSON: function() {
49
+ return;
50
+ }
51
+ };
52
+ client.query = query;
53
+ client.transaction = runInTransaction;
54
+ client.db = baseUrl;
55
+ client.mssql = onProvider.bind(null, 'mssql');
56
+ client.mssqlNative = onProvider.bind(null, 'mssqlNative');
57
+ client.pg = onProvider.bind(null, 'pg');
58
+ client.postgres = onProvider.bind(null, 'postgres');
59
+ client.sqlite = onProvider.bind(null, 'sqlite');
60
+ client.sap = onProvider.bind(null, 'sap');
61
+ client.oracle = onProvider.bind(null, 'oracle');
62
+ client.http = onProvider.bind(null, 'http');//todo
63
+ client.mysql = onProvider.bind(null, 'mysql');
64
+ client.express = express;
65
+
66
+ function onProvider(name, ...args) {
67
+ let db = providers[name].apply(null, args);
68
+ return client({ db });
69
+ }
70
+
71
+ if (options.tables) {
72
+ // for (let name in options.tables) {
73
+ // client[name] = table(options.tables[name], name, { ...readonly, ...clone(options[name]) });
74
+ // }
75
+ client.tables = options.tables;
76
+ // return client;
77
+ }
78
+ // else {
79
+ let handler = {
80
+ get(_target, property,) {
81
+ if (property in client)
82
+ return Reflect.get(...arguments);
83
+ else {
84
+ const readonly = { readonly: options.readonly, concurrency: options.concurrency };
85
+ return table(options?.tables?.[property] || baseUrl, property, { ...readonly, ...clone(options[property]) });
86
+ }
87
+ }
88
+
89
+ };
90
+ return new Proxy(client, handler);
91
+ // }
92
+
93
+ function getMetaData() {
94
+ const result = { readonly: options.readonly, concurrency: options.concurrency };
95
+ for (let name in options.tables) {
96
+ result[name] = getMetaDataTable(options.tables[name], inferOptions(options, name));
97
+ }
98
+ return result;
99
+ }
100
+
101
+ function inferOptions(defaults, property) {
102
+ const parent = {};
103
+ if ('readonly' in defaults)
104
+ parent.readonly = defaults.readonly;
105
+ if ('concurrency' in defaults)
106
+ parent.concurrency = defaults.concurrency;
107
+ return { ...parent, ...(defaults[property] || {}) };
108
+ }
109
+
110
+
111
+ function getMetaDataTable(table, options) {
112
+ const result = {};
113
+ for (let i = 0; i < table._columns.length; i++) {
114
+ const name = table._columns[i].alias;
115
+ result[name] = inferOptions(options, name);
116
+ }
117
+ for (let name in table._relations) {
118
+ if (!isJoinRelation(name, table))
119
+ result[name] = getMetaDataTable(table._relations[name].childTable, inferOptions(options, name));
120
+ }
121
+
122
+ return result;
123
+
124
+ function isJoinRelation(name, table) {
125
+ return table[name] && table[name]._relation.columns;
126
+ }
127
+ }
128
+
129
+ async function query() {
130
+ return netAdapter(baseUrl, undefined, { tableOptions: { db: baseUrl, transaction } }).query.apply(null, arguments);
131
+ }
132
+
133
+ function express(arg) {
134
+ if (providers.express) {
135
+ return providers.express(client, { ...options, ...arg });
136
+ }
137
+ else
138
+ throw new Error('Cannot host express clientside');
139
+ }
140
+
141
+
142
+
143
+ function _createPatch(original, modified, ...restArgs) {
144
+ if (!Array.isArray(original)) {
145
+ original = [original];
146
+ modified = [modified];
147
+ }
148
+ let args = [original, modified, ...restArgs];
149
+ return createPatch(...args);
150
+ }
151
+
152
+ async function getDb() {
153
+ let db = baseUrl;
154
+ if (typeof db === 'function') {
155
+ let dbPromise = db();
156
+ if (dbPromise.then)
157
+ db = await dbPromise;
158
+ else
159
+ db = dbPromise;
160
+ }
161
+
162
+ return db;
163
+ }
164
+
165
+ async function runInTransaction(fn, _options) {
166
+ let db = await getDb();
167
+ if (!db.createTransaction)
168
+ throw new Error('Transaction not supported through http');
169
+ const transaction = db.createTransaction(_options);
170
+ try {
171
+ const nextClient = client({ transaction });
172
+ await fn(nextClient);
173
+ await transaction(db.commit);
174
+ }
175
+ catch (e) {
176
+ await transaction(db.rollback.bind(null, e));
177
+ }
178
+ }
179
+
180
+ function table(url, tableName, tableOptions) {
181
+ tableOptions = tableOptions || {};
182
+ tableOptions = { db: baseUrl, ...tableOptions, transaction };
183
+ let meta;
184
+ let c = {
185
+ count,
186
+ getMany,
187
+ aggregate: groupBy,
188
+ getAll,
189
+ getOne,
190
+ getById,
191
+ proxify,
192
+ update,
193
+ updateChanges,
194
+ insert,
195
+ insertAndForget,
196
+ delete: _delete,
197
+ deleteCascade,
198
+ patch,
199
+ expand,
200
+ };
201
+
202
+
203
+ let handler = {
204
+ get(_target, property,) {
205
+ if (property in c)
206
+ return Reflect.get(...arguments);
207
+ else
208
+ return column(property);
209
+ }
210
+
211
+ };
212
+ let _table = new Proxy(c, handler);
213
+ return _table;
214
+
215
+ function expand() {
216
+ return c;
217
+ }
218
+
219
+ async function getAll() {
220
+ let _getMany = getMany.bind(null, undefined);
221
+ return _getMany.apply(null, arguments);
222
+ }
223
+
224
+ async function getMany(_, strategy) {
225
+ let metaPromise = getMeta();
226
+ strategy = extractFetchingStrategy({}, strategy);
227
+ let args = [_, strategy].concat(Array.prototype.slice.call(arguments).slice(2));
228
+ let rows = await getManyCore.apply(null, args);
229
+ await metaPromise;
230
+ return proxify(rows, strategy);
231
+ }
232
+
233
+ async function groupBy(strategy) {
234
+ let args = negotiateGroupBy(null, strategy);
235
+ let body = stringify({
236
+ path: 'aggregate',
237
+ args
238
+ });
239
+ let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
240
+ return adapter.post(body);
241
+ }
242
+
243
+ async function count(_) {
244
+ let args = [_].concat(Array.prototype.slice.call(arguments).slice(1));
245
+ let body = stringify({
246
+ path: 'count',
247
+ args
248
+ });
249
+ let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
250
+ return adapter.post(body);
251
+ }
252
+
253
+ async function getOne(filter, strategy) {
254
+ let metaPromise = getMeta();
255
+ strategy = extractFetchingStrategy({}, strategy);
256
+ let _strategy = { ...strategy, ...{ limit: 1 } };
257
+ let args = [filter, _strategy].concat(Array.prototype.slice.call(arguments).slice(2));
258
+ let rows = await getManyCore.apply(null, args);
259
+ await metaPromise;
260
+ if (rows.length === 0)
261
+ return;
262
+ return proxify(rows[0], strategy);
263
+ }
264
+
265
+ async function getById() {
266
+ if (arguments.length === 0)
267
+ return;
268
+ let meta = await getMeta();
269
+ let keyFilter = client.filter;
270
+ for (let i = 0; i < meta.keys.length; i++) {
271
+ let keyName = meta.keys[i].name;
272
+ let keyValue = arguments[i];
273
+ keyFilter = keyFilter.and(_table[keyName].eq(keyValue));
274
+ }
275
+ let args = [keyFilter].concat(Array.prototype.slice.call(arguments).slice(meta.keys.length));
276
+ return getOne.apply(null, args);
277
+ }
278
+
279
+ async function getManyCore() {
280
+ let args = negotiateWhere.apply(null, arguments);
281
+ let body = stringify({
282
+ path: 'getManyDto',
283
+ args
284
+ });
285
+ let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
286
+ return adapter.post(body);
287
+ }
288
+
289
+ function negotiateWhere(_, strategy, ...rest) {
290
+ const args = Array.prototype.slice.call(arguments);
291
+ if (strategy)
292
+ return [_, where(strategy), ...rest];
293
+ else
294
+ return args;
295
+
296
+ function where(_strategy, path = '') {
297
+ if (typeof _strategy !== 'object' || _strategy === null)
298
+ return _strategy;
299
+
300
+ if (Array.isArray(_strategy)) {
301
+ return _strategy.map(item => where(item, path));
302
+ }
303
+
304
+ const strategy = { ..._strategy };
305
+ for (let name in _strategy) {
306
+ if (name === 'where' && typeof strategy[name] === 'function')
307
+ strategy.where = column(path + 'where')(strategy.where); // Assuming `column` is defined elsewhere.
308
+ else if (typeof strategy[name] === 'function') {
309
+ strategy[name] = aggregate(path, strategy[name]);
310
+ }
311
+ else
312
+ strategy[name] = where(_strategy[name], path + name + '.');
313
+ }
314
+ return strategy;
315
+ }
316
+
317
+ }
318
+
319
+
320
+
321
+ function negotiateGroupBy(_, strategy, ...rest) {
322
+ const args = Array.prototype.slice.call(arguments);
323
+ if (strategy)
324
+ return [_, where(strategy), ...rest];
325
+ else
326
+ return args;
327
+
328
+ function where(_strategy, path = '') {
329
+ if (typeof _strategy !== 'object' || _strategy === null)
330
+ return _strategy;
331
+
332
+ if (Array.isArray(_strategy)) {
333
+ return _strategy.map(item => where(item, path));
334
+ }
335
+
336
+ const strategy = { ..._strategy };
337
+ for (let name in _strategy) {
338
+ if (name === 'where' && typeof strategy[name] === 'function')
339
+ strategy.where = column(path + 'where')(strategy.where); // Assuming `column` is defined elsewhere.
340
+ else if (typeof strategy[name] === 'function') {
341
+ strategy[name] = groupByAggregate(path, strategy[name]);
342
+ }
343
+ else
344
+ strategy[name] = where(_strategy[name], path + name + '.');
345
+ }
346
+ return strategy;
347
+ }
348
+
349
+ }
350
+
351
+
352
+
353
+
354
+
355
+ async function _delete() {
356
+ let args = Array.prototype.slice.call(arguments);
357
+ let body = stringify({
358
+ path: 'delete',
359
+ args
360
+ });
361
+ let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
362
+ return adapter.post(body);
363
+ }
364
+
365
+ async function deleteCascade() {
366
+ let args = Array.prototype.slice.call(arguments);
367
+ let body = stringify({
368
+ path: 'deleteCascade',
369
+ args
370
+ });
371
+ let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
372
+ return adapter.post(body);
373
+ }
374
+
375
+ async function update(rows, ...rest) {
376
+ const concurrency = undefined;
377
+ const args = [concurrency].concat(rest);
378
+ if (Array.isArray(rows)) {
379
+ const proxy = await getMany.apply(null, [rows, ...rest]);
380
+ proxy.splice.apply(proxy, [0, proxy.length, ...rows]);
381
+ await proxy.saveChanges.apply(proxy, args);
382
+ return proxy;
383
+ }
384
+ else {
385
+ const proxy = await getMany.apply(null, [[rows], ...rest]);
386
+ proxy.splice.apply(proxy, [0, 1, rows]);
387
+ await proxy.saveChanges.apply(proxy, args);
388
+ return proxify(proxy[0], args[0]);
389
+ }
390
+ }
391
+
392
+ async function updateChanges(rows, oldRows, ...rest) {
393
+ const concurrency = undefined;
394
+ const args = [concurrency].concat(rest);
395
+ if (Array.isArray(rows)) {
396
+ //todo
397
+ const proxy = await getMany.apply(null, [rows, ...rest]);
398
+ proxy.splice.apply(proxy, [0, proxy.length, ...rows]);
399
+ await proxy.saveChanges.apply(proxy, args);
400
+ return proxy;
401
+ }
402
+ else {
403
+ let proxy = proxify([oldRows], args[0]);
404
+ proxy.splice.apply(proxy, [0, 1, rows]);
405
+ await proxy.saveChanges.apply(proxy, args);
406
+ return proxify(proxy[0], args[0]);
407
+ }
408
+ }
409
+
410
+ async function insert(rows, ...rest) {
411
+ const concurrency = undefined;
412
+ const args = [concurrency].concat(rest);
413
+ if (Array.isArray(rows)) {
414
+ let proxy = proxify([], rest[0]);
415
+ proxy.splice.apply(proxy, [0, 0, ...rows]);
416
+ await proxy.saveChanges.apply(proxy, args);
417
+ return proxy;
418
+ }
419
+ else {
420
+ let proxy = proxify([], args[0]);
421
+ proxy.splice.apply(proxy, [0, 0, rows]);
422
+ await proxy.saveChanges.apply(proxy, args);
423
+ return proxify(proxy[0], rest[0]);
424
+ }
425
+ }
426
+
427
+ async function insertAndForget(rows) {
428
+ const concurrency = undefined;
429
+ let args = [concurrency, { insertAndForget: true }];
430
+ if (Array.isArray(rows)) {
431
+ let proxy = proxify([], args[0]);
432
+ proxy.splice.apply(proxy, [0, 0, ...rows]);
433
+ await proxy.saveChanges.apply(proxy, args);
434
+ }
435
+ else {
436
+ let proxy = proxify([], args[0]);
437
+ proxy.splice.apply(proxy, [0, 0, rows]);
438
+ await proxy.saveChanges.apply(proxy, args);
439
+ }
440
+ }
441
+
442
+
443
+ function proxify(itemOrArray, strategy) {
444
+ if (Array.isArray(itemOrArray))
445
+ return proxifyArray(itemOrArray, strategy);
446
+ else
447
+ return proxifyRow(itemOrArray, strategy);
448
+ }
449
+
450
+ function proxifyArray(array, strategy) {
451
+ let _array = array;
452
+ if (_reactive)
453
+ array = _reactive(array);
454
+ let handler = {
455
+ get(_target, property) {
456
+ if (property === 'toJSON')
457
+ return () => {
458
+ return toJSON(array);
459
+ };
460
+ else if (property === 'save' || property === 'saveChanges')
461
+ return saveArray.bind(null, array);
462
+ else if (property === 'delete')
463
+ return deleteArray.bind(null, array);
464
+ else if (property === 'refresh')
465
+ return refreshArray.bind(null, array);
466
+ else if (property === 'clearChanges')
467
+ return clearChangesArray.bind(null, array);
468
+ else if (property === 'acceptChanges')
469
+ return acceptChangesArray.bind(null, array);
470
+ else if (property === targetKey)
471
+ return _array;
472
+ else
473
+ return Reflect.get.apply(_array, arguments);
474
+ }
475
+
476
+ };
477
+ let innerProxy = new Proxy(array, handler);
478
+ rootMap.set(array, { json: stringify(array), strategy, originalArray: [...array] });
479
+ if (strategy !== undefined) {
480
+ const { limit, ...cleanStrategy } = { ...strategy };
481
+ fetchingStrategyMap.set(array, cleanStrategy);
482
+ }
483
+ return innerProxy;
484
+ }
485
+
486
+ function proxifyRow(row, strategy) {
487
+ let handler = {
488
+ get(_target, property,) {
489
+ if (property === 'save' || property === 'saveChanges') //call server then acceptChanges
490
+ return saveRow.bind(null, row);
491
+ else if (property === 'delete') //call server then remove from jsonMap and original
492
+ return deleteRow.bind(null, row);
493
+ else if (property === 'refresh') //refresh from server then acceptChanges
494
+ return refreshRow.bind(null, row);
495
+ else if (property === 'clearChanges') //refresh from jsonMap, update original if present
496
+ return clearChangesRow.bind(null, row);
497
+ else if (property === 'acceptChanges') //remove from jsonMap
498
+ return acceptChangesRow.bind(null, row);
499
+ else if (property === 'toJSON')
500
+ return () => {
501
+ return toJSON(row);
502
+ };
503
+ else if (property === targetKey)
504
+ return row;
505
+ else
506
+ return Reflect.get(...arguments);
507
+ }
508
+
509
+ };
510
+ let innerProxy = new Proxy(row, handler);
511
+ rootMap.set(row, { json: stringify(row), strategy });
512
+ fetchingStrategyMap.set(row, strategy);
513
+ return innerProxy;
514
+ }
515
+
516
+ function toJSON(row, _meta = meta) {
517
+ if (!row)
518
+ return null;
519
+ if (!_meta)
520
+ return row;
521
+ if (Array.isArray(row)) {
522
+ return row.map(x => toJSON(x, _meta));
523
+ }
524
+ let result = {};
525
+ for (let name in row) {
526
+ if (name in _meta.relations)
527
+ result[name] = toJSON(row[name], _meta.relations[name]);
528
+ else if (name in _meta.columns) {
529
+ if (_meta.columns[name].serializable)
530
+ result[name] = row[name];
531
+ }
532
+ else
533
+ result[name] = row[name];
534
+ }
535
+ return result;
536
+ }
537
+
538
+
539
+
540
+
541
+ async function getMeta() {
542
+ if (meta)
543
+ return meta;
544
+ let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
545
+ meta = await adapter.get();
546
+
547
+ while (hasUnresolved(meta)) {
548
+ meta = parseMeta(meta);
549
+ }
550
+ return meta;
551
+
552
+ function parseMeta(meta, map = new Map()) {
553
+ if (typeof meta === 'number') {
554
+ return map.get(meta) || meta;
555
+ }
556
+ map.set(meta.id, meta);
557
+ for (let p in meta.relations) {
558
+ meta.relations[p] = parseMeta(meta.relations[p], map);
559
+ }
560
+ return meta;
561
+ }
562
+
563
+ function hasUnresolved(meta, set = new WeakSet()) {
564
+ if (typeof meta === 'number')
565
+ return true;
566
+ else if (set.has(meta))
567
+ return false;
568
+ else {
569
+ set.add(meta);
570
+ return Object.values(meta.relations).reduce((prev, current) => {
571
+ return prev || hasUnresolved(current, set);
572
+ }, false);
573
+ }
574
+ }
575
+
576
+
577
+ }
578
+
579
+ async function saveArray(array, concurrencyOptions, strategy) {
580
+ let deduceStrategy = false;
581
+ let { json } = rootMap.get(array);
582
+ strategy = extractStrategy({ strategy }, array);
583
+ strategy = extractFetchingStrategy(array, strategy);
584
+
585
+ let meta = await getMeta();
586
+ const patch = createPatch(JSON.parse(json), array, meta);
587
+ if (patch.length === 0)
588
+ return;
589
+ let body = stringify({ patch, options: { strategy, ...tableOptions, ...concurrencyOptions, deduceStrategy } });
590
+ let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
591
+ let p = adapter.patch(body);
592
+ if (strategy?.insertAndForget) {
593
+ await p;
594
+ return;
595
+ }
596
+
597
+ let updatedPositions = extractChangedRowsPositions(array, patch, meta);
598
+ let insertedPositions = getInsertedRowsPosition(array);
599
+ let { changed, strategy: newStrategy } = await p;
600
+ copyIntoArray(changed, array, [...insertedPositions, ...updatedPositions]);
601
+ rootMap.set(array, { json: stringify(array), strategy: newStrategy, originalArray: [...array] });
602
+ }
603
+
604
+ async function patch(patch, concurrencyOptions, strategy) {
605
+ let deduceStrategy = false;
606
+ if (patch.length === 0)
607
+ return;
608
+ let body = stringify({ patch, options: { strategy, ...tableOptions, ...concurrencyOptions, deduceStrategy } });
609
+ let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
610
+ await adapter.patch(body);
611
+ return;
612
+ }
613
+
614
+ function extractChangedRowsPositions(rows, patch, meta) {
615
+ const positions = [];
616
+ const originalSet = new Set(rootMap.get(rows).originalArray);
617
+ const positionsAdded = {};
618
+ const keyPositionMap = toKeyPositionMap(rows, meta);
619
+ for (let i = 0; i < patch.length; i++) {
620
+ const element = patch[i];
621
+ const pathArray = element.path.split('/');
622
+ const position = keyPositionMap[pathArray[1]];
623
+ if (position >= 0 && originalSet.has(rows[position]) && !positionsAdded[position]) {
624
+ positions.push(position);
625
+ positionsAdded[position] = true;
626
+ }
627
+ }
628
+ return positions;
629
+ }
630
+
631
+ function getInsertedRowsPosition(array) {
632
+ const positions = [];
633
+ const originalSet = new Set(rootMap.get(array).originalArray);
634
+ for (let i = 0; i < array.length; i++) {
635
+ if (!originalSet.has(array[i]))
636
+ positions.push(i);
637
+ }
638
+ return positions;
639
+ }
640
+
641
+ function copyInto(from, to) {
642
+ for (let i = 0; i < from.length; i++) {
643
+ for (let p in from[i]) {
644
+ to[i][p] = from[i][p];
645
+ }
646
+ }
647
+ }
648
+
649
+ function copyIntoArray(from, to, positions) {
650
+ for (let i = 0; i < from.length; i++) {
651
+ to[positions[i]] = from[i];
652
+ }
653
+ }
654
+
655
+
656
+ function extractStrategy(options, obj) {
657
+ if (options?.strategy !== undefined)
658
+ return options.strategy;
659
+ if (obj) {
660
+ let context = rootMap.get(obj);
661
+ if (context.strategy !== undefined) {
662
+ // @ts-ignore
663
+ let { limit, ...strategy } = { ...context.strategy };
664
+ return strategy;
665
+ }
666
+ }
667
+ }
668
+
669
+ function extractFetchingStrategy(obj, strategy) {
670
+ if (strategy !== undefined)
671
+ return strategy;
672
+ else if (fetchingStrategyMap.get(obj) !== undefined) {
673
+ // @ts-ignore
674
+ const { limit, ...strategy } = { ...fetchingStrategyMap.get(obj) };
675
+ return strategy;
676
+ }
677
+ }
678
+
679
+ function clearChangesArray(array) {
680
+ let { json } = rootMap.get(array);
681
+ let old = JSON.parse(json);
682
+ array.splice(0, old.length, ...old);
683
+ }
684
+
685
+ function acceptChangesArray(array) {
686
+ const map = rootMap.get(array);
687
+ map.json = stringify(array);
688
+ map.originalArray = [...array];
689
+ }
690
+
691
+ async function deleteArray(array, options) {
692
+ if (array.length === 0)
693
+ return;
694
+ let meta = await getMeta();
695
+ let patch = createPatch(array, [], meta);
696
+ let body = stringify({ patch, options });
697
+ let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
698
+ let { strategy } = await adapter.patch(body);
699
+ array.length = 0;
700
+ rootMap.set(array, { jsonMap: stringify(array), strategy });
701
+ }
702
+
703
+ function setMapValue(rowsMap, keys, row, index) {
704
+ let keyValue = row[keys[0].name];
705
+ if (keys.length > 1) {
706
+ let subMap = rowsMap.get(keyValue);
707
+ if (!subMap) {
708
+ subMap = new Map();
709
+ rowsMap.set(keyValue, subMap);
710
+ }
711
+ setMapValue(subMap, keys.slice(1), row, index);
712
+ }
713
+ else
714
+ rowsMap.set(keyValue, index);
715
+ }
716
+
717
+ function getMapValue(rowsMap, keys, row) {
718
+ let keyValue = row[keys[0].name];
719
+ if (keys.length > 1)
720
+ return getMapValue(rowsMap.get(keyValue), keys.slice(1));
721
+ else
722
+ return rowsMap.get(keyValue);
723
+ }
724
+
725
+ async function refreshArray(array, strategy) {
726
+ clearChangesArray(array);
727
+ strategy = extractStrategy({ strategy }, array);
728
+ strategy = extractFetchingStrategy(array, strategy);
729
+ if (array.length === 0)
730
+ return;
731
+ let meta = await getMeta();
732
+ let filter = client.filter;
733
+ let rowsMap = new Map();
734
+ for (let rowIndex = 0; rowIndex < array.length; rowIndex++) {
735
+ let row = array[rowIndex];
736
+ let keyFilter = client.filter;
737
+ for (let i = 0; i < meta.keys.length; i++) {
738
+ let keyName = meta.keys[i].name;
739
+ let keyValue = row[keyName];
740
+ keyFilter = keyFilter.and(_table[keyName].eq(keyValue));
741
+ }
742
+ setMapValue(rowsMap, meta.keys, row, rowIndex);
743
+ filter = filter.or(keyFilter);
744
+ }
745
+ let rows = await getManyCore(filter, strategy);
746
+ let removedIndexes = new Set();
747
+ if (array.length !== rows.length)
748
+ for (var i = 0; i < array.length; i++) {
749
+ removedIndexes.add(i);
750
+ }
751
+ for (let i = 0; i < rows.length; i++) {
752
+ let row = rows[i];
753
+ let originalIndex = getMapValue(rowsMap, meta.keys, row);
754
+ if (array.length !== rows.length)
755
+ removedIndexes.delete(originalIndex);
756
+ array[originalIndex] = row;
757
+ }
758
+ let offset = 0;
759
+ for (let i of removedIndexes) {
760
+ array.splice(i + offset, 1);
761
+ offset--;
762
+ }
763
+ rootMap.set(array, { json: stringify(array), strategy, originalArray: [...array] });
764
+ fetchingStrategyMap.set(array, strategy);
765
+ }
766
+
767
+ async function deleteRow(row, options) {
768
+ let strategy = extractStrategy(options, row);
769
+ let meta = await getMeta();
770
+ let patch = createPatch([row], [], meta);
771
+ let body = stringify({ patch, options });
772
+ let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
773
+ await adapter.patch(body);
774
+ rootMap.set(row, { strategy });
775
+ }
776
+
777
+ async function saveRow(row, concurrencyOptions, strategy) {
778
+ let deduceStrategy;
779
+ if (arguments.length < 3)
780
+ deduceStrategy = false;
781
+ strategy = extractStrategy({ strategy }, row);
782
+ strategy = extractFetchingStrategy(row, strategy);
783
+
784
+ let { json } = rootMap.get(row);
785
+ if (!json)
786
+ return;
787
+ let meta = await getMeta();
788
+
789
+ let patch = createPatch([JSON.parse(json)], [row], meta);
790
+ if (patch.length === 0)
791
+ return;
792
+
793
+ let body = stringify({ patch, options: { ...tableOptions, ...concurrencyOptions, strategy, deduceStrategy } });
794
+
795
+ let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
796
+ let { changed, strategy: newStrategy } = await adapter.patch(body);
797
+ copyInto(changed, [row]);
798
+ rootMap.set(row, { json: stringify(row), strategy: newStrategy });
799
+ }
800
+
801
+ async function refreshRow(row, strategy) {
802
+ clearChangesRow(row);
803
+ strategy = extractStrategy({ strategy }, row);
804
+ strategy = extractFetchingStrategy(row, strategy);
805
+
806
+ let meta = await getMeta();
807
+ let keyFilter = client.filter;
808
+ for (let i = 0; i < meta.keys.length; i++) {
809
+ let keyName = meta.keys[i].name;
810
+ let keyValue = row[keyName];
811
+ keyFilter = keyFilter.and(_table[keyName].eq(keyValue));
812
+ }
813
+ let rows = await getManyCore(keyFilter, strategy);
814
+ for (let p in row) {
815
+ delete row[p];
816
+ }
817
+ if (rows.length === 0)
818
+ return;
819
+ for (let p in rows[0]) {
820
+ row[p] = rows[0][p];
821
+ }
822
+ rootMap.set(row, { json: stringify(row), strategy });
823
+ fetchingStrategyMap.set(row, strategy);
824
+ }
825
+
826
+ function acceptChangesRow(row) {
827
+ const { strategy } = rootMap.get(row);
828
+ rootMap.set(row, { json: stringify(row), strategy });
829
+ }
830
+
831
+ function clearChangesRow(row) {
832
+ let { json } = rootMap.get(row);
833
+ if (!json)
834
+ return;
835
+ let old = JSON.parse(json);
836
+ for (let p in row) {
837
+ delete row[p];
838
+ }
839
+ for (let p in old) {
840
+ row[p] = old[p];
841
+ }
842
+ }
843
+ }
844
+ }
845
+
846
+ function tableProxy() {
847
+ let handler = {
848
+ get(_target, property,) {
849
+ return column(property);
850
+ }
851
+
852
+ };
853
+ return new Proxy({}, handler);
854
+ }
855
+
856
+ function aggregate(path, arg) {
857
+
858
+ const c = {
859
+ sum,
860
+ count,
861
+ avg,
862
+ max,
863
+ min
864
+ };
865
+
866
+ let handler = {
867
+ get(_target, property,) {
868
+ if (property in c)
869
+ return Reflect.get(...arguments);
870
+ else {
871
+ subColumn = column(path + '_aggregate');
872
+ return column(property);
873
+ }
874
+ }
875
+
876
+ };
877
+ let subColumn;
878
+ const proxy = new Proxy(c, handler);
879
+
880
+ const result = arg(proxy);
881
+
882
+ if (subColumn)
883
+ return subColumn(result.self());
884
+ else
885
+ return result;
886
+
887
+
888
+ function sum(fn) {
889
+ return column(path + '_aggregate')(fn(column('')).groupSum());
890
+ }
891
+ function avg(fn) {
892
+ return column(path + '_aggregate')(fn(column('')).groupAvg());
893
+ }
894
+ function max(fn) {
895
+ return column(path + '_aggregate')(fn(column('')).groupMax());
896
+ }
897
+ function min(fn) {
898
+ return column(path + '_aggregate')(fn(column('')).groupMin());
899
+ }
900
+ function count(fn) {
901
+ return column(path + '_aggregate')(fn(column('')).groupCount());
902
+ }
903
+ }
904
+
905
+ function groupByAggregate(path, arg) {
906
+
907
+ const c = {
908
+ sum,
909
+ count,
910
+ avg,
911
+ max,
912
+ min
913
+ };
914
+
915
+ let handler = {
916
+ get(_target, property,) {
917
+ if (property in c)
918
+ return Reflect.get(...arguments);
919
+ else {
920
+ subColumn = column(path + '_aggregate');
921
+ return column(property);
922
+ }
923
+ }
924
+
925
+ };
926
+ let subColumn;
927
+ const proxy = new Proxy(c, handler);
928
+
929
+ const result = arg(proxy);
930
+
931
+ if (subColumn)
932
+ return subColumn(result.self());
933
+ else
934
+ return result;
935
+
936
+
937
+ function sum(fn) {
938
+ return column(path + '_aggregate')(fn(column('')).sum());
939
+ }
940
+ function avg(fn) {
941
+ return column(path + '_aggregate')(fn(column('')).avg());
942
+ }
943
+ function max(fn) {
944
+ return column(path + '_aggregate')(fn(column('')).max());
945
+ }
946
+ function min(fn) {
947
+ return column(path + '_aggregate')(fn(column('')).min());
948
+ }
949
+ function count(fn) {
950
+ return column(path + '_aggregate')(fn(column('')).count());
951
+ }
952
+ }
953
+
954
+ function column(path, ...previous) {
955
+ function c() {
956
+ let args = [];
957
+ for (let i = 0; i < arguments.length; i++) {
958
+ if (typeof arguments[i] === 'function')
959
+ args[i] = arguments[i](tableProxy(path.split('.').slice(0, -1).join('.')));
960
+ else
961
+ args[i] = arguments[i];
962
+ }
963
+ args = previous.concat(Array.prototype.slice.call(args));
964
+ let result = { path, args };
965
+ let handler = {
966
+ get(_target, property) {
967
+ if (property === 'toJSON')
968
+ return result.toJSON;
969
+ else if (property === 'then')
970
+ return;
971
+ if (property in result)
972
+ return Reflect.get(...arguments);
973
+ else
974
+ return column(property, result);
975
+
976
+ }
977
+ };
978
+ return new Proxy(result, handler);
979
+ }
980
+ let handler = {
981
+ get(_target, property) {
982
+ if (property === 'toJSON')
983
+ return Reflect.get(...arguments);
984
+ else if (property === 'then')
985
+ return;
986
+ else {
987
+ const nextPath = path ? path + '.' : '';
988
+ return column(nextPath + property);
989
+ }
990
+ }
991
+
992
+ };
993
+ return new Proxy(c, handler);
994
+
995
+ }
996
+
997
+ module.exports = rdbClient();