orange-orm 4.9.0 → 4.10.0-beta.0

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 (42) hide show
  1. package/README.md +120 -0
  2. package/deno.lock +76 -0
  3. package/dist/index.browser.mjs +192 -56
  4. package/dist/index.mjs +292 -179
  5. package/docs/changelog.md +2 -0
  6. package/other.db +0 -0
  7. package/package.json +1 -1
  8. package/src/bunPg/newDatabase.js +3 -14
  9. package/src/bunPg/newTransaction.js +1 -0
  10. package/src/bunSqlite/newDatabase.js +22 -13
  11. package/src/bunSqlite/newTransaction.js +1 -0
  12. package/src/client/index.js +8 -1
  13. package/src/client/netAdapter.js +13 -2
  14. package/src/d1/newDatabase.js +3 -13
  15. package/src/d1/newTransaction.js +1 -0
  16. package/src/getTSDefinition.js +14 -1
  17. package/src/hostExpress.js +8 -3
  18. package/src/hostLocal.js +66 -6
  19. package/src/map2.d.ts +18 -1
  20. package/src/mssql/newDatabase.js +3 -13
  21. package/src/mssql/newTransaction.js +1 -0
  22. package/src/mySql/newDatabase.js +3 -13
  23. package/src/mySql/newTransaction.js +1 -0
  24. package/src/nodeSqlite/newDatabase.js +29 -18
  25. package/src/nodeSqlite/newTransaction.js +1 -0
  26. package/src/oracle/newDatabase.js +3 -13
  27. package/src/oracle/newTransaction.js +1 -0
  28. package/src/pg/newDatabase.js +4 -16
  29. package/src/pg/newTransaction.js +1 -0
  30. package/src/pglite/newDatabase.js +3 -14
  31. package/src/pglite/newTransaction.js +1 -0
  32. package/src/sap/newDatabase.js +3 -13
  33. package/src/sap/newTransaction.js +1 -0
  34. package/src/sqlite3/newDatabase.js +22 -13
  35. package/src/sqlite3/newTransaction.js +1 -0
  36. package/src/sqliteFunction.js +20 -0
  37. package/src/table/begin.js +0 -1
  38. package/src/table/commit.js +21 -1
  39. package/src/table/query/singleQuery/joinSql/newShallowJoinSqlCore.js +6 -4
  40. package/src/table/rollback.js +22 -2
  41. package/src/tedious/newDatabase.js +3 -13
  42. package/src/tedious/newTransaction.js +1 -0
@@ -465,6 +465,7 @@ export interface ExpressConfig {
465
465
  concurrency?: Concurrency;
466
466
  readonly?: boolean;
467
467
  disableBulkDeletes?: boolean;
468
+ hooks?: ExpressHooks;
468
469
  }
469
470
 
470
471
  export interface ExpressContext {
@@ -473,6 +474,18 @@ export interface ExpressContext {
473
474
  client: RdbClient;
474
475
  }
475
476
 
477
+ export interface ExpressTransactionHooks {
478
+ beforeBegin?: (db: Pool, request: import('express').Request, response: import('express').Response) => void | Promise<void>;
479
+ afterBegin?: (db: Pool, request: import('express').Request, response: import('express').Response) => void | Promise<void>;
480
+ beforeCommit?: (db: Pool, request: import('express').Request, response: import('express').Response) => void | Promise<void>;
481
+ afterCommit?: (db: Pool, request: import('express').Request, response: import('express').Response) => void | Promise<void>;
482
+ afterRollback?: (db: Pool, request: import('express').Request, response: import('express').Response, error?: unknown) => void | Promise<void>;
483
+ }
484
+
485
+ export interface ExpressHooks extends ExpressTransactionHooks {
486
+ transaction?: ExpressTransactionHooks;
487
+ }
488
+
476
489
  export interface ExpressTables {${getExpressTables()}
477
490
  }
478
491
  `;
@@ -610,13 +623,18 @@ function requireHostExpress () {
610
623
  const dbOptions = { db: options.db || client.db };
611
624
  let c = {};
612
625
  const readonly = { readonly: options.readonly};
626
+ const sharedHooks = options.hooks;
613
627
  for (let tableName in client.tables) {
628
+ const tableOptions = options[tableName] || {};
629
+ const hooks = tableOptions.hooks || sharedHooks;
614
630
  c[tableName] = hostLocal({
615
631
  ...dbOptions,
616
632
  ...readonly,
617
- ...options[tableName],
633
+ ...tableOptions,
618
634
  table: client.tables[tableName],
619
- isHttp: true, client
635
+ isHttp: true,
636
+ client,
637
+ hooks
620
638
 
621
639
  });
622
640
  }
@@ -2200,6 +2218,35 @@ function requireQuery () {
2200
2218
  return query;
2201
2219
  }
2202
2220
 
2221
+ var sqliteFunction;
2222
+ var hasRequiredSqliteFunction;
2223
+
2224
+ function requireSqliteFunction () {
2225
+ if (hasRequiredSqliteFunction) return sqliteFunction;
2226
+ hasRequiredSqliteFunction = 1;
2227
+ const executeChanges = requireExecuteChanges();
2228
+ const popChanges = requirePopChanges();
2229
+ const getSessionSingleton = requireGetSessionSingleton();
2230
+
2231
+ function executeQueries(context, ...rest) {
2232
+ var changes = popChanges(context);
2233
+
2234
+ return executeChanges(context, changes).then(onDoneChanges);
2235
+
2236
+ function onDoneChanges() {
2237
+ var client = getSessionSingleton(context, 'dbClient');
2238
+ if (client && typeof client.function === 'function')
2239
+ return client.function.apply(client, rest);
2240
+ if (client && typeof client.createFunction === 'function')
2241
+ return client.createFunction.apply(client, rest);
2242
+ throw new Error('SQLite client does not support user-defined functions');
2243
+ }
2244
+ }
2245
+
2246
+ sqliteFunction = executeQueries;
2247
+ return sqliteFunction;
2248
+ }
2249
+
2203
2250
  var hostLocal_1;
2204
2251
  var hasRequiredHostLocal;
2205
2252
 
@@ -2210,6 +2257,7 @@ function requireHostLocal () {
2210
2257
  let getMeta = requireGetMeta();
2211
2258
  let setSessionSingleton = requireSetSessionSingleton();
2212
2259
  let executeQuery = requireQuery();
2260
+ let executeSqliteFunction = requireSqliteFunction();
2213
2261
  let hostExpress = requireHostExpress();
2214
2262
  const readonlyOps = ['getManyDto', 'getMany', 'aggregate', 'count'];
2215
2263
  // { db, table, defaultConcurrency,
@@ -2220,9 +2268,12 @@ function requireHostLocal () {
2220
2268
  // disableBulkDeletes, isBrowser }
2221
2269
  function hostLocal() {
2222
2270
  const _options = arguments[0];
2223
- let { table, transaction, db, isHttp } = _options;
2271
+ let { table, transaction, db, isHttp, hooks, client } = _options;
2272
+ const transactionHooks = hooks && hooks.transaction;
2273
+ const getTransactionHook = (name) =>
2274
+ (transactionHooks && transactionHooks[name]) || (hooks && hooks[name]);
2224
2275
 
2225
- let c = { get, post, patch, query, express };
2276
+ let c = { get, post, patch, query, sqliteFunction, express };
2226
2277
 
2227
2278
  function get() {
2228
2279
  return getMeta(table);
@@ -2273,10 +2324,41 @@ function requireHostLocal () {
2273
2324
  else
2274
2325
  db = dbPromise;
2275
2326
  }
2276
- if (readonlyOps.includes(body.path))
2327
+ const beforeBegin = getTransactionHook('beforeBegin');
2328
+ const afterBegin = getTransactionHook('afterBegin');
2329
+ const beforeCommit = getTransactionHook('beforeCommit');
2330
+ const afterCommit = getTransactionHook('afterCommit');
2331
+ const afterRollback = getTransactionHook('afterRollback');
2332
+ const hasTransactionHooks = !!(beforeBegin
2333
+ || afterBegin
2334
+ || beforeCommit
2335
+ || afterCommit
2336
+ || afterRollback);
2337
+ if (!hasTransactionHooks && readonlyOps.includes(body.path))
2277
2338
  await db.transaction({ readonly: true }, fn);
2278
- else
2279
- await db.transaction(fn);
2339
+ else {
2340
+ await db.transaction(async (context) => {
2341
+ const hookDb = typeof client === 'function'
2342
+ ? client({ transaction: (fn) => fn(context) })
2343
+ : (client || db);
2344
+ if (afterCommit)
2345
+ setSessionSingleton(context, 'afterCommitHook', () =>
2346
+ afterCommit(hookDb, request, response)
2347
+ );
2348
+ if (afterRollback)
2349
+ setSessionSingleton(context, 'afterRollbackHook', (error) =>
2350
+ afterRollback(hookDb, request, response, error)
2351
+ );
2352
+ if (beforeBegin)
2353
+ await beforeBegin(hookDb, request, response);
2354
+ if (afterBegin)
2355
+ await afterBegin(hookDb, request, response);
2356
+ await fn(context);
2357
+ if (beforeCommit)
2358
+ await beforeCommit(hookDb, request, response);
2359
+ });
2360
+ }
2361
+
2280
2362
  }
2281
2363
  return result;
2282
2364
 
@@ -2311,6 +2393,31 @@ function requireHostLocal () {
2311
2393
 
2312
2394
  }
2313
2395
 
2396
+ async function sqliteFunction() {
2397
+ let args = arguments;
2398
+ let result;
2399
+
2400
+ if (transaction)
2401
+ await transaction(fn);
2402
+ else {
2403
+ if (typeof db === 'function') {
2404
+ let dbPromise = db();
2405
+ if (dbPromise.then)
2406
+ db = await dbPromise;
2407
+ else
2408
+ db = dbPromise;
2409
+ }
2410
+ result = await db.sqliteFunction.apply(null, arguments);
2411
+ }
2412
+
2413
+ return result;
2414
+
2415
+ async function fn(...args1) {
2416
+ result = await executeSqliteFunction.apply(null, [...args1, ...args]);
2417
+ }
2418
+
2419
+ }
2420
+
2314
2421
  function express(client, options) {
2315
2422
  return hostExpress(hostLocal, client, options);
2316
2423
  }
@@ -2401,6 +2508,7 @@ function requireNetAdapter () {
2401
2508
  post,
2402
2509
  patch,
2403
2510
  query,
2511
+ sqliteFunction,
2404
2512
  express
2405
2513
  };
2406
2514
 
@@ -2456,6 +2564,10 @@ function requireNetAdapter () {
2456
2564
  throw new Error('Queries are not supported through http');
2457
2565
  }
2458
2566
 
2567
+ function sqliteFunction() {
2568
+ throw new Error('Sqlite Function is not supported through http');
2569
+ }
2570
+
2459
2571
  function express() {
2460
2572
  throw new Error('Hosting in express is not supported on the client side');
2461
2573
  }
@@ -2469,7 +2581,8 @@ function requireNetAdapter () {
2469
2581
  get,
2470
2582
  post,
2471
2583
  patch,
2472
- query
2584
+ query,
2585
+ sqliteFunction
2473
2586
  };
2474
2587
 
2475
2588
  return c;
@@ -2494,6 +2607,11 @@ function requireNetAdapter () {
2494
2607
  return adapter.query.apply(null, arguments);
2495
2608
  }
2496
2609
 
2610
+ async function sqliteFunction() {
2611
+ const adapter = await getInnerAdapter();
2612
+ return adapter.sqliteFunction.apply(null, arguments);
2613
+ }
2614
+
2497
2615
  async function getInnerAdapter() {
2498
2616
  const db = await getDb();
2499
2617
  if (typeof db === 'string') {
@@ -2782,6 +2900,7 @@ function requireClient () {
2782
2900
  }
2783
2901
  };
2784
2902
  client.query = query;
2903
+ client.function = sqliteFunction;
2785
2904
  client.transaction = runInTransaction;
2786
2905
  client.db = baseUrl;
2787
2906
  client.mssql = onProvider.bind(null, 'mssql');
@@ -2870,6 +2989,11 @@ function requireClient () {
2870
2989
  return adapter.query.apply(null, arguments);
2871
2990
  }
2872
2991
 
2992
+ async function sqliteFunction() {
2993
+ const adapter = netAdapter(baseUrl, undefined, { tableOptions: { db: baseUrl, transaction } });
2994
+ return adapter.sqliteFunction.apply(null, arguments);
2995
+ }
2996
+
2873
2997
  function express(arg) {
2874
2998
  if (providers.express) {
2875
2999
  return providers.express(client, { ...options, ...arg });
@@ -3553,6 +3677,7 @@ function requireClient () {
3553
3677
  return;
3554
3678
 
3555
3679
  let body = stringify({ patch, options: { ...tableOptions, ...concurrencyOptions, strategy, deduceStrategy } });
3680
+
3556
3681
  let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
3557
3682
  let { changed, strategy: newStrategy } = await adapter.patch(body);
3558
3683
  copyInto(changed, [row]);
@@ -4132,8 +4257,10 @@ function requireNewShallowJoinSqlCore () {
4132
4257
 
4133
4258
  function _new(context, rightTable, leftColumns, rightColumns, leftAlias, rightAlias, filter) {
4134
4259
  const quote = getSessionSingleton(context, 'quote');
4135
- leftAlias = quote(leftAlias);
4136
- rightAlias = quote(rightAlias);
4260
+ const leftAliasRaw = leftAlias;
4261
+ const rightAliasRaw = rightAlias;
4262
+ leftAlias = quote(leftAliasRaw);
4263
+ rightAlias = quote(rightAliasRaw);
4137
4264
  var sql = '';
4138
4265
  var delimiter = '';
4139
4266
  for (var i = 0; i < leftColumns.length; i++) {
@@ -4147,7 +4274,7 @@ function requireNewShallowJoinSqlCore () {
4147
4274
  sql += delimiter + leftAlias + '.' + quote(leftColumn._dbName) + '=' + rightAlias + '.' + quote(rightColumn._dbName);
4148
4275
  }
4149
4276
 
4150
- sql += newDiscriminatorSql(context, rightTable, rightAlias);
4277
+ sql += newDiscriminatorSql(context, rightTable, rightAliasRaw);
4151
4278
  var result = newParameterized(sql);
4152
4279
  if (filter)
4153
4280
  result = result.append(delimiter).append(filter);
@@ -12936,14 +13063,34 @@ function requireCommit () {
12936
13063
  const getSessionSingleton = requireGetSessionSingleton();
12937
13064
 
12938
13065
  function _commit(context, result) {
13066
+ let hookError;
12939
13067
  return popAndPushChanges()
13068
+ .then(callAfterCommit)
12940
13069
  .then(releaseDbClient.bind(null, context))
12941
- .then(onReleased);
13070
+ .then(onReleased)
13071
+ .then(throwHookErrorIfAny);
12942
13072
 
12943
13073
  function onReleased() {
12944
13074
  return result;
12945
13075
  }
12946
13076
 
13077
+ function throwHookErrorIfAny(res) {
13078
+ if (hookError)
13079
+ throw hookError;
13080
+ return res;
13081
+ }
13082
+
13083
+ function callAfterCommit() {
13084
+ const hook = getSessionSingleton(context, 'afterCommitHook');
13085
+ if (!hook)
13086
+ return Promise.resolve();
13087
+ return Promise.resolve()
13088
+ .then(() => hook())
13089
+ .catch((e) => {
13090
+ hookError = e;
13091
+ });
13092
+ }
13093
+
12947
13094
  async function popAndPushChanges() {
12948
13095
  let changes = popChanges(context);
12949
13096
  while (changes.length > 0) {
@@ -13044,10 +13191,13 @@ function requireRollback () {
13044
13191
  const getSessionSingleton = requireGetSessionSingleton();
13045
13192
 
13046
13193
  function _rollback(context, e) {
13194
+ let hookError;
13047
13195
  var chain = resultToPromise()
13048
13196
  .then(() => popChanges(context))
13049
13197
  .then(executeRollback)
13050
- .then(() => releaseDbClient(context));
13198
+ .then(callAfterRollback)
13199
+ .then(() => releaseDbClient(context))
13200
+ .then(throwHookErrorIfAny);
13051
13201
 
13052
13202
 
13053
13203
  function executeRollback() {
@@ -13057,6 +13207,23 @@ function requireRollback () {
13057
13207
  return executeQuery(context, rollbackCommand);
13058
13208
  }
13059
13209
 
13210
+ function callAfterRollback() {
13211
+ const hook = getSessionSingleton(context, 'afterRollbackHook');
13212
+ if (!hook)
13213
+ return Promise.resolve();
13214
+ return Promise.resolve()
13215
+ .then(() => hook(e))
13216
+ .catch((err) => {
13217
+ hookError = err;
13218
+ });
13219
+ }
13220
+
13221
+ function throwHookErrorIfAny(res) {
13222
+ if (hookError)
13223
+ throw hookError;
13224
+ return res;
13225
+ }
13226
+
13060
13227
  if (e) {
13061
13228
  if (e.message?.indexOf('ORA-01476: divisor is equal to zero') > -1)
13062
13229
  return newThrow(context, new Error('Conflict when updating a column'), chain);
@@ -13938,6 +14105,7 @@ function requireNewTransaction$2 () {
13938
14105
  rdb.aggregateCount = 0;
13939
14106
  rdb.quote = (name) => `"${name}"`;
13940
14107
  rdb.cache = {};
14108
+ rdb.changes = [];
13941
14109
 
13942
14110
  if (readonly) {
13943
14111
  rdb.dbClient = {
@@ -14041,7 +14209,6 @@ function requireBegin () {
14041
14209
  let setSessionSingleton = requireSetSessionSingleton();
14042
14210
 
14043
14211
  function begin(context, transactionLess) {
14044
- setSessionSingleton(context, 'changes', []);
14045
14212
  if (transactionLess) {
14046
14213
  setSessionSingleton(context, 'transactionLess', true);
14047
14214
  return Promise.resolve();
@@ -14838,7 +15005,6 @@ function requireNewDatabase$2 () {
14838
15005
  let hostLocal = requireHostLocal();
14839
15006
  let doQuery = requireQuery();
14840
15007
  let releaseDbClient = requireReleaseDbClient();
14841
- let setSessionSingleton = requireSetSessionSingleton();
14842
15008
 
14843
15009
  function newDatabase(d1Database, poolOptions) {
14844
15010
  if (!d1Database)
@@ -14855,10 +15021,9 @@ function requireNewDatabase$2 () {
14855
15021
  }
14856
15022
  let domain = createDomain();
14857
15023
 
14858
- if (fn)
14859
- return domain.run(runInTransaction);
14860
- else
14861
- return domain.run(run);
15024
+ if (!fn)
15025
+ throw new Error('transaction requires a function');
15026
+ return domain.run(runInTransaction);
14862
15027
 
14863
15028
  async function runInTransaction() {
14864
15029
  let result;
@@ -14877,13 +15042,6 @@ function requireNewDatabase$2 () {
14877
15042
  return _begin(domain, transactionLess);
14878
15043
  }
14879
15044
 
14880
- function run() {
14881
- let p;
14882
- let transaction = newTransaction(domain, pool, options);
14883
- p = new Promise(transaction);
14884
-
14885
- return p.then(begin);
14886
- }
14887
15045
 
14888
15046
  };
14889
15047
 
@@ -14911,7 +15069,6 @@ function requireNewDatabase$2 () {
14911
15069
  let domain = createDomain();
14912
15070
  let transaction = newTransaction(domain, pool);
14913
15071
  let p = domain.run(() => new Promise(transaction)
14914
- .then(() => setSessionSingleton(domain, 'changes', []))
14915
15072
  .then(() => doQuery(domain, query).then(onResult, onError)));
14916
15073
  return p;
14917
15074
 
@@ -15356,6 +15513,7 @@ function requireNewTransaction$1 () {
15356
15513
  rdb.aggregateCount = 0;
15357
15514
  rdb.quote = quote;
15358
15515
  rdb.cache = {};
15516
+ rdb.changes = [];
15359
15517
 
15360
15518
  if (readonly) {
15361
15519
  rdb.dbClient = {
@@ -15598,7 +15756,6 @@ function requireNewDatabase$1 () {
15598
15756
  let hostLocal = requireHostLocal();
15599
15757
  let doQuery = requireQuery();
15600
15758
  let releaseDbClient = requireReleaseDbClient();
15601
- let setSessionSingleton = requireSetSessionSingleton();
15602
15759
 
15603
15760
  function newDatabase(connectionString, poolOptions) {
15604
15761
  poolOptions = poolOptions || { min: 1 };
@@ -15613,10 +15770,9 @@ function requireNewDatabase$1 () {
15613
15770
  }
15614
15771
  let domain = createDomain();
15615
15772
 
15616
- if (fn)
15617
- return domain.run(runInTransaction);
15618
- else
15619
- return domain.run(run);
15773
+ if (!fn)
15774
+ throw new Error('transaction requires a function');
15775
+ return domain.run(runInTransaction);
15620
15776
 
15621
15777
  async function runInTransaction() {
15622
15778
  let result;
@@ -15635,14 +15791,6 @@ function requireNewDatabase$1 () {
15635
15791
  return _begin(domain, options);
15636
15792
  }
15637
15793
 
15638
- function run() {
15639
- let p;
15640
- let transaction = newTransaction(domain, pool, options);
15641
- p = new Promise(transaction);
15642
-
15643
- return p.then(begin)
15644
- .then(negotiateSchema);
15645
- }
15646
15794
 
15647
15795
  function negotiateSchema(previous) {
15648
15796
  let schema = options && options.schema;
@@ -15683,7 +15831,6 @@ function requireNewDatabase$1 () {
15683
15831
  let domain = createDomain();
15684
15832
  let transaction = newTransaction(domain, pool);
15685
15833
  let p = domain.run(() => new Promise(transaction)
15686
- .then(() => setSessionSingleton(domain, 'changes', []))
15687
15834
  .then(() => doQuery(domain, query).then(onResult, onError)));
15688
15835
  return p;
15689
15836
 
@@ -15885,6 +16032,7 @@ function requireNewTransaction () {
15885
16032
  rdb.aggregateCount = 0;
15886
16033
  rdb.quote = quote;
15887
16034
  rdb.cache = {};
16035
+ rdb.changes = [];
15888
16036
 
15889
16037
  if (readonly) {
15890
16038
  rdb.dbClient = {
@@ -16150,7 +16298,6 @@ function requireNewDatabase () {
16150
16298
  let hostLocal = requireHostLocal();
16151
16299
  let doQuery = requireQuery();
16152
16300
  let releaseDbClient = requireReleaseDbClient();
16153
- let setSessionSingleton = requireSetSessionSingleton();
16154
16301
 
16155
16302
  function newDatabase(connectionString, poolOptions) {
16156
16303
  if (!connectionString)
@@ -16167,10 +16314,9 @@ function requireNewDatabase () {
16167
16314
  }
16168
16315
  let domain = createDomain();
16169
16316
 
16170
- if (fn)
16171
- return domain.run(runInTransaction);
16172
- else
16173
- return domain.run(run);
16317
+ if (!fn)
16318
+ throw new Error('transaction requires a function');
16319
+ return domain.run(runInTransaction);
16174
16320
 
16175
16321
  async function runInTransaction() {
16176
16322
  let result;
@@ -16189,15 +16335,6 @@ function requireNewDatabase () {
16189
16335
  return _begin(domain, options);
16190
16336
  }
16191
16337
 
16192
- function run() {
16193
- let p;
16194
- let transaction = newTransaction(domain, pool, options);
16195
- p = new Promise(transaction);
16196
-
16197
- return p.then(begin)
16198
- .then(negotiateSchema);
16199
- }
16200
-
16201
16338
  function negotiateSchema(previous) {
16202
16339
  let schema = options && options.schema;
16203
16340
  if (!schema)
@@ -16237,7 +16374,6 @@ function requireNewDatabase () {
16237
16374
  let domain = createDomain();
16238
16375
  let transaction = newTransaction(domain, pool);
16239
16376
  let p = domain.run(() => new Promise(transaction)
16240
- .then(() => setSessionSingleton(domain, 'changes', []))
16241
16377
  .then(() => doQuery(domain, query).then(onResult, onError)));
16242
16378
  return p;
16243
16379