Skip to content

Commit 0d18fb6

Browse files
author
Lars-Erik Roald
committed
fixed tests
1 parent 35e5b16 commit 0d18fb6

38 files changed

+1078
-116
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ npm-debug.log
66
.vscode
77
demo.db
88
demo*.db*
9+
other.db
910
pglite.db*
1011
coverage
11-
.env
12+
.env
13+
deno.lock

dist/index.browser.mjs

Lines changed: 93 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,35 @@ function requireQuery () {
22002200
return query;
22012201
}
22022202

2203+
var sqliteFunction;
2204+
var hasRequiredSqliteFunction;
2205+
2206+
function requireSqliteFunction () {
2207+
if (hasRequiredSqliteFunction) return sqliteFunction;
2208+
hasRequiredSqliteFunction = 1;
2209+
const executeChanges = requireExecuteChanges();
2210+
const popChanges = requirePopChanges();
2211+
const getSessionSingleton = requireGetSessionSingleton();
2212+
2213+
function executeQueries(context, ...rest) {
2214+
var changes = popChanges(context);
2215+
2216+
return executeChanges(context, changes).then(onDoneChanges);
2217+
2218+
function onDoneChanges() {
2219+
var client = getSessionSingleton(context, 'dbClient');
2220+
if (client && typeof client.function === 'function')
2221+
return client.function.apply(client, rest);
2222+
if (client && typeof client.createFunction === 'function')
2223+
return client.createFunction.apply(client, rest);
2224+
throw new Error('SQLite client does not support user-defined functions');
2225+
}
2226+
}
2227+
2228+
sqliteFunction = executeQueries;
2229+
return sqliteFunction;
2230+
}
2231+
22032232
var hostLocal_1;
22042233
var hasRequiredHostLocal;
22052234

@@ -2210,6 +2239,7 @@ function requireHostLocal () {
22102239
let getMeta = requireGetMeta();
22112240
let setSessionSingleton = requireSetSessionSingleton();
22122241
let executeQuery = requireQuery();
2242+
let executeSqliteFunction = requireSqliteFunction();
22132243
let hostExpress = requireHostExpress();
22142244
const readonlyOps = ['getManyDto', 'getMany', 'aggregate', 'count'];
22152245
// { db, table, defaultConcurrency,
@@ -2220,9 +2250,9 @@ function requireHostLocal () {
22202250
// disableBulkDeletes, isBrowser }
22212251
function hostLocal() {
22222252
const _options = arguments[0];
2223-
let { table, transaction, db, isHttp } = _options;
2253+
let { table, transaction, db, isHttp, hooks } = _options;
22242254

2225-
let c = { get, post, patch, query, express };
2255+
let c = { get, post, patch, query, sqliteFunction, express };
22262256

22272257
function get() {
22282258
return getMeta(table);
@@ -2273,10 +2303,21 @@ function requireHostLocal () {
22732303
else
22742304
db = dbPromise;
22752305
}
2276-
if (readonlyOps.includes(body.path))
2306+
const hasTransactionHooks = !!(hooks?.beforeTransactionBegin
2307+
|| hooks?.afterTransactionBegin
2308+
|| hooks?.beforeTransactionCommit
2309+
|| hooks?.afterTransactionCommit);
2310+
if (!hasTransactionHooks && readonlyOps.includes(body.path))
22772311
await db.transaction({ readonly: true }, fn);
2278-
else
2312+
else {
2313+
if (hooks?.beforeTransactionBegin) {
2314+
await hooks.beforeTransactionBegin(db, request, response);
2315+
2316+
}
2317+
22792318
await db.transaction(fn);
2319+
}
2320+
22802321
}
22812322
return result;
22822323

@@ -2311,6 +2352,31 @@ function requireHostLocal () {
23112352

23122353
}
23132354

2355+
async function sqliteFunction() {
2356+
let args = arguments;
2357+
let result;
2358+
2359+
if (transaction)
2360+
await transaction(fn);
2361+
else {
2362+
if (typeof db === 'function') {
2363+
let dbPromise = db();
2364+
if (dbPromise.then)
2365+
db = await dbPromise;
2366+
else
2367+
db = dbPromise;
2368+
}
2369+
result = await db.sqliteFunction.apply(null, arguments);
2370+
}
2371+
2372+
return result;
2373+
2374+
async function fn(...args1) {
2375+
result = await executeSqliteFunction.apply(null, [...args1, ...args]);
2376+
}
2377+
2378+
}
2379+
23142380
function express(client, options) {
23152381
return hostExpress(hostLocal, client, options);
23162382
}
@@ -2401,6 +2467,7 @@ function requireNetAdapter () {
24012467
post,
24022468
patch,
24032469
query,
2470+
sqliteFunction,
24042471
express
24052472
};
24062473

@@ -2456,6 +2523,10 @@ function requireNetAdapter () {
24562523
throw new Error('Queries are not supported through http');
24572524
}
24582525

2526+
function sqliteFunction() {
2527+
throw new Error('Sqlite Function is not supported through http');
2528+
}
2529+
24592530
function express() {
24602531
throw new Error('Hosting in express is not supported on the client side');
24612532
}
@@ -2469,7 +2540,8 @@ function requireNetAdapter () {
24692540
get,
24702541
post,
24712542
patch,
2472-
query
2543+
query,
2544+
sqliteFunction
24732545
};
24742546

24752547
return c;
@@ -2494,6 +2566,11 @@ function requireNetAdapter () {
24942566
return adapter.query.apply(null, arguments);
24952567
}
24962568

2569+
async function sqliteFunction() {
2570+
const adapter = await getInnerAdapter();
2571+
return adapter.sqliteFunction.apply(null, arguments);
2572+
}
2573+
24972574
async function getInnerAdapter() {
24982575
const db = await getDb();
24992576
if (typeof db === 'string') {
@@ -2782,6 +2859,7 @@ function requireClient () {
27822859
}
27832860
};
27842861
client.query = query;
2862+
client.function = sqliteFunction;
27852863
client.transaction = runInTransaction;
27862864
client.db = baseUrl;
27872865
client.mssql = onProvider.bind(null, 'mssql');
@@ -2870,6 +2948,11 @@ function requireClient () {
28702948
return adapter.query.apply(null, arguments);
28712949
}
28722950

2951+
async function sqliteFunction() {
2952+
const adapter = netAdapter(baseUrl, undefined, { tableOptions: { db: baseUrl, transaction } });
2953+
return adapter.sqliteFunction.apply(null, arguments);
2954+
}
2955+
28732956
function express(arg) {
28742957
if (providers.express) {
28752958
return providers.express(client, { ...options, ...arg });
@@ -3553,6 +3636,7 @@ function requireClient () {
35533636
return;
35543637

35553638
let body = stringify({ patch, options: { ...tableOptions, ...concurrencyOptions, strategy, deduceStrategy } });
3639+
35563640
let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
35573641
let { changed, strategy: newStrategy } = await adapter.patch(body);
35583642
copyInto(changed, [row]);
@@ -13940,6 +14024,7 @@ function requireNewTransaction$2 () {
1394014024
rdb.aggregateCount = 0;
1394114025
rdb.quote = (name) => `"${name}"`;
1394214026
rdb.cache = {};
14027+
rdb.changes = [];
1394314028

1394414029
if (readonly) {
1394514030
rdb.dbClient = {
@@ -14043,7 +14128,6 @@ function requireBegin () {
1404314128
let setSessionSingleton = requireSetSessionSingleton();
1404414129

1404514130
function begin(context, transactionLess) {
14046-
setSessionSingleton(context, 'changes', []);
1404714131
if (transactionLess) {
1404814132
setSessionSingleton(context, 'transactionLess', true);
1404914133
return Promise.resolve();
@@ -14840,7 +14924,6 @@ function requireNewDatabase$2 () {
1484014924
let hostLocal = requireHostLocal();
1484114925
let doQuery = requireQuery();
1484214926
let releaseDbClient = requireReleaseDbClient();
14843-
let setSessionSingleton = requireSetSessionSingleton();
1484414927

1484514928
function newDatabase(d1Database, poolOptions) {
1484614929
if (!d1Database)
@@ -14913,7 +14996,6 @@ function requireNewDatabase$2 () {
1491314996
let domain = createDomain();
1491414997
let transaction = newTransaction(domain, pool);
1491514998
let p = domain.run(() => new Promise(transaction)
14916-
.then(() => setSessionSingleton(domain, 'changes', []))
1491714999
.then(() => doQuery(domain, query).then(onResult, onError)));
1491815000
return p;
1491915001

@@ -15358,6 +15440,7 @@ function requireNewTransaction$1 () {
1535815440
rdb.aggregateCount = 0;
1535915441
rdb.quote = quote;
1536015442
rdb.cache = {};
15443+
rdb.changes = [];
1536115444

1536215445
if (readonly) {
1536315446
rdb.dbClient = {
@@ -15600,7 +15683,6 @@ function requireNewDatabase$1 () {
1560015683
let hostLocal = requireHostLocal();
1560115684
let doQuery = requireQuery();
1560215685
let releaseDbClient = requireReleaseDbClient();
15603-
let setSessionSingleton = requireSetSessionSingleton();
1560415686

1560515687
function newDatabase(connectionString, poolOptions) {
1560615688
poolOptions = poolOptions || { min: 1 };
@@ -15685,7 +15767,6 @@ function requireNewDatabase$1 () {
1568515767
let domain = createDomain();
1568615768
let transaction = newTransaction(domain, pool);
1568715769
let p = domain.run(() => new Promise(transaction)
15688-
.then(() => setSessionSingleton(domain, 'changes', []))
1568915770
.then(() => doQuery(domain, query).then(onResult, onError)));
1569015771
return p;
1569115772

@@ -15887,6 +15968,7 @@ function requireNewTransaction () {
1588715968
rdb.aggregateCount = 0;
1588815969
rdb.quote = quote;
1588915970
rdb.cache = {};
15971+
rdb.changes = [];
1589015972

1589115973
if (readonly) {
1589215974
rdb.dbClient = {
@@ -16152,7 +16234,6 @@ function requireNewDatabase () {
1615216234
let hostLocal = requireHostLocal();
1615316235
let doQuery = requireQuery();
1615416236
let releaseDbClient = requireReleaseDbClient();
16155-
let setSessionSingleton = requireSetSessionSingleton();
1615616237

1615716238
function newDatabase(connectionString, poolOptions) {
1615816239
if (!connectionString)
@@ -16192,12 +16273,7 @@ function requireNewDatabase () {
1619216273
}
1619316274

1619416275
function run() {
16195-
let p;
16196-
let transaction = newTransaction(domain, pool, options);
16197-
p = new Promise(transaction);
16198-
16199-
return p.then(begin)
16200-
.then(negotiateSchema);
16276+
throw new Error('wont happen');
1620116277
}
1620216278

1620316279
function negotiateSchema(previous) {
@@ -16239,7 +16315,6 @@ function requireNewDatabase () {
1623916315
let domain = createDomain();
1624016316
let transaction = newTransaction(domain, pool);
1624116317
let p = domain.run(() => new Promise(transaction)
16242-
.then(() => setSessionSingleton(domain, 'changes', []))
1624316318
.then(() => doQuery(domain, query).then(onResult, onError)));
1624416319
return p;
1624516320

0 commit comments

Comments
 (0)