@frogbotai/db-sqlite 0.30.0 → 0.31.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.
- package/dist/index.d.ts +9 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +62 -5
- package/dist/index.js.map +1 -1
- package/dist/writeLock.d.ts +19 -0
- package/dist/writeLock.d.ts.map +1 -0
- package/dist/writeLock.js +215 -0
- package/dist/writeLock.js.map +1 -0
- package/package.json +4 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
import { type MigrateDownArgs, type MigrateUpArgs, type SQLiteAdapter, type SQLiteAdapterArgs } from '@payloadcms/db-sqlite';
|
|
1
|
+
import { type MigrateDownArgs, type MigrateUpArgs, type SQLiteAdapter, type SQLiteAdapterArgs as PayloadSQLiteAdapterArgs } from '@payloadcms/db-sqlite';
|
|
2
2
|
export { sql } from '@payloadcms/db-sqlite';
|
|
3
|
-
export type { MigrateDownArgs, MigrateUpArgs, SQLiteAdapter
|
|
4
|
-
export
|
|
3
|
+
export type { MigrateDownArgs, MigrateUpArgs, SQLiteAdapter };
|
|
4
|
+
export type SQLiteAdapterArgs = PayloadSQLiteAdapterArgs & {
|
|
5
|
+
/**
|
|
6
|
+
* Milliseconds a write waits for the write or transaction ahead of it before it fails. Defaults to `5000`.
|
|
7
|
+
*/
|
|
8
|
+
writeLockTimeout?: number;
|
|
9
|
+
};
|
|
10
|
+
export declare function sqliteAdapter({ writeLockTimeout, ...args }: SQLiteAdapterArgs): {
|
|
5
11
|
init(initArgs: Parameters<(args: {
|
|
6
12
|
payload: import("payload").Payload;
|
|
7
13
|
}) => SQLiteAdapter>[0]): SQLiteAdapter;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,aAAa,EAElB,KAAK,iBAAiB,IAAI,wBAAwB,EACnD,MAAM,uBAAuB,CAAC;AAM/B,OAAO,EAAE,GAAG,EAAE,MAAM,uBAAuB,CAAC;AAC5C,YAAY,EAAE,eAAe,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC;AAE9D,MAAM,MAAM,iBAAiB,GAAG,wBAAwB,GAAG;IACzD;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF,wBAAgB,aAAa,CAAC,EAAE,gBAAuB,EAAE,GAAG,IAAI,EAAE,EAAE,iBAAiB;mBAWlE,UAAU;;wBAAqB,CAAC,CAAC,CAAC;;;;;EA+CpD"}
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,46 @@
|
|
|
1
|
+
import { createClient } from '@libsql/client';
|
|
1
2
|
import { sqliteAdapter as createSQLiteAdapter } from '@payloadcms/db-sqlite';
|
|
2
3
|
import { installSQLJobOperations } from 'frogbot/jobs';
|
|
3
4
|
import { sqliteSearchAdapter } from 'frogbot/search';
|
|
5
|
+
import { connectionPragmas, installWriteLock, takeCommitError } from './writeLock.js';
|
|
4
6
|
export { sql } from '@payloadcms/db-sqlite';
|
|
5
|
-
export function sqliteAdapter(args) {
|
|
6
|
-
const
|
|
7
|
-
...args
|
|
8
|
-
|
|
7
|
+
export function sqliteAdapter({ writeLockTimeout = 5000, ...args }) {
|
|
8
|
+
const adapterArgs = {
|
|
9
|
+
...args,
|
|
10
|
+
busyTimeout: args.busyTimeout ?? 1000,
|
|
11
|
+
wal: args.wal ?? isFileDatabase(args.client?.url)
|
|
12
|
+
};
|
|
13
|
+
const adapter = createSQLiteAdapter(adapterArgs);
|
|
9
14
|
return {
|
|
10
15
|
...adapter,
|
|
11
16
|
init (initArgs) {
|
|
12
17
|
const signIn = initArgs.payload.config?.collections.some((collection)=>collection.custom?.frogbot?.signIn?.length);
|
|
13
18
|
const database = signIn && args.transactionOptions === undefined ? createSQLiteAdapter({
|
|
14
|
-
...
|
|
19
|
+
...adapterArgs,
|
|
15
20
|
transactionOptions: {}
|
|
16
21
|
}).init(initArgs) : adapter.init(initArgs);
|
|
17
22
|
database.packageName = '@frogbotai/db-sqlite';
|
|
23
|
+
const { commitTransaction, connect } = database;
|
|
24
|
+
database.commitTransaction = async function(id) {
|
|
25
|
+
const transactionID = id instanceof Promise ? await id : id;
|
|
26
|
+
const transaction = libsqlTransaction(this.sessions[transactionID]);
|
|
27
|
+
await commitTransaction.call(this, transactionID);
|
|
28
|
+
const error = takeCommitError(transaction);
|
|
29
|
+
if (error) throw error;
|
|
30
|
+
};
|
|
31
|
+
if (connect) {
|
|
32
|
+
database.connect = async function(options) {
|
|
33
|
+
if (!this.client && !this.wal && isFileDatabase(this.clientConfig.url)) {
|
|
34
|
+
await useRollbackJournal(this);
|
|
35
|
+
}
|
|
36
|
+
await connect.call(this, options);
|
|
37
|
+
installWriteLock({
|
|
38
|
+
client: this.client,
|
|
39
|
+
pragmas: connectionPragmas(this),
|
|
40
|
+
timeout: writeLockTimeout
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
}
|
|
18
44
|
installSQLJobOperations({
|
|
19
45
|
adapter: database,
|
|
20
46
|
dialect: 'sqlite'
|
|
@@ -24,5 +50,36 @@ export function sqliteAdapter(args) {
|
|
|
24
50
|
search: sqliteSearchAdapter
|
|
25
51
|
};
|
|
26
52
|
}
|
|
53
|
+
function isFileDatabase(url) {
|
|
54
|
+
return Boolean(url?.startsWith('file:') && !/:memory:|mode=memory/.test(url));
|
|
55
|
+
}
|
|
56
|
+
function libsqlTransaction(session) {
|
|
57
|
+
return session?.db?.session?.tx;
|
|
58
|
+
}
|
|
59
|
+
async function useRollbackJournal(adapter) {
|
|
60
|
+
try {
|
|
61
|
+
adapter.client = createClient(adapter.clientConfig);
|
|
62
|
+
for (const pragma of connectionPragmas(adapter))await adapter.client.execute(pragma);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
65
|
+
adapter.payload.logger.error({
|
|
66
|
+
err: error,
|
|
67
|
+
msg: `Error: cannot connect to SQLite: ${message}`
|
|
68
|
+
});
|
|
69
|
+
adapter.rejectInitializing?.();
|
|
70
|
+
throw new Error(`Error: cannot connect to SQLite: ${message}`);
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const { rows } = await adapter.client.execute('PRAGMA journal_mode');
|
|
74
|
+
if (rows[0]?.journal_mode !== 'wal') return;
|
|
75
|
+
adapter.payload.logger.info('[db-sqlite] Disabling WAL mode: `wal` is false.');
|
|
76
|
+
await adapter.client.execute('PRAGMA journal_mode = DELETE');
|
|
77
|
+
} catch (error) {
|
|
78
|
+
adapter.payload.logger.warn({
|
|
79
|
+
err: error,
|
|
80
|
+
msg: `[db-sqlite] Could not disable WAL mode, so the database stays in WAL: ${error instanceof Error ? error.message : String(error)}`
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
27
84
|
|
|
28
85
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":["sqliteAdapter","createSQLiteAdapter","installSQLJobOperations","sqliteSearchAdapter","sql","args","adapter","init","initArgs","signIn","payload","config","collections","some","collection","custom","frogbot","length","database","transactionOptions","undefined","packageName","dialect","search"],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["createClient","sqliteAdapter","createSQLiteAdapter","installSQLJobOperations","sqliteSearchAdapter","connectionPragmas","installWriteLock","takeCommitError","sql","writeLockTimeout","args","adapterArgs","busyTimeout","wal","isFileDatabase","client","url","adapter","init","initArgs","signIn","payload","config","collections","some","collection","custom","frogbot","length","database","transactionOptions","undefined","packageName","commitTransaction","connect","id","transactionID","Promise","transaction","libsqlTransaction","sessions","call","error","options","clientConfig","useRollbackJournal","pragmas","timeout","dialect","search","Boolean","startsWith","test","session","db","tx","pragma","execute","message","Error","String","logger","err","msg","rejectInitializing","rows","journal_mode","info","warn"],"mappings":"AAAA,SAASA,YAAY,QAAQ,iBAAiB;AAC9C,SAIEC,iBAAiBC,mBAAmB,QAE/B,wBAAwB;AAC/B,SAASC,uBAAuB,QAAQ,eAAe;AACvD,SAASC,mBAAmB,QAAQ,iBAAiB;AAErD,SAASC,iBAAiB,EAAEC,gBAAgB,EAAEC,eAAe,QAAQ,iBAAiB;AAEtF,SAASC,GAAG,QAAQ,wBAAwB;AAU5C,OAAO,SAASP,cAAc,EAAEQ,mBAAmB,IAAI,EAAE,GAAGC,MAAyB;IACnF,MAAMC,cAAc;QAClB,GAAGD,IAAI;QACPE,aAAaF,KAAKE,WAAW,IAAI;QACjCC,KAAKH,KAAKG,GAAG,IAAIC,eAAeJ,KAAKK,MAAM,EAAEC;IAC/C;IAEA,MAAMC,UAAUf,oBAAoBS;IAEpC,OAAO;QACL,GAAGM,OAAO;QACVC,MAAKC,QAA4C;YAC/C,MAAMC,SAASD,SAASE,OAAO,CAACC,MAAM,EAAEC,YAAYC,KAClD,CAACC,aAAeA,WAAWC,MAAM,EAAEC,SAASP,QAAQQ;YAGtD,MAAMC,WACJT,UAAUV,KAAKoB,kBAAkB,KAAKC,YAClC7B,oBAAoB;gBAAE,GAAGS,WAAW;gBAAEmB,oBAAoB,CAAC;YAAE,GAAGZ,IAAI,CAACC,YACrEF,QAAQC,IAAI,CAACC;YAEnBU,SAASG,WAAW,GAAG;YAEvB,MAAM,EAAEC,iBAAiB,EAAEC,OAAO,EAAE,GAAGL;YAEvCA,SAASI,iBAAiB,GAAG,eAAqCE,EAAE;gBAClE,MAAMC,gBAAgBD,cAAcE,UAAU,MAAMF,KAAKA;gBACzD,MAAMG,cAAcC,kBAAkB,IAAI,CAACC,QAAQ,CAACJ,cAAc;gBAElE,MAAMH,kBAAkBQ,IAAI,CAAC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EA,IAAI;QACF,MAAM,EAAEO,IAAI,EAAE,GAAG,MAAMhD,QAAQF,MAAM,CAAC0C,OAAO,CAAC;QAE9C,IAAIQ,IAAI,CAAC,EAAE,EAAEC,iBAAiB,OAAO;QAErCjD,QAAQI,OAAO,CAACwC,MAAM,CAACM,IAAI,CAAC;QAC5B,MAAMlD,QAAQF,MAAM,CAAC0C,OAAO,CAAC;IAC/B,EAAE,OAAOf,OAAO;QACdzB,QAAQI,OAAO,CAACwC,MAAM,CAACO,IAAI,CAAC;YAC1BN,KAAKpB;YACLqB,KAAK,CAAC,sEAAsE,EAAErB,iBAAiBiB,QAAQjB,MAAMgB,OAAO,GAAGE,OAAOlB,QAAQ;QACxI;IACF;AACF","file":"index.js"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { SQLiteAdapter } from '@payloadcms/db-sqlite';
|
|
2
|
+
type Client = SQLiteAdapter['client'];
|
|
3
|
+
/**
|
|
4
|
+
* Returns and forgets the error of this transaction's failed commit. Payload's
|
|
5
|
+
* `commitTransaction` swallows commit errors, so the adapter rethrows this one.
|
|
6
|
+
*/
|
|
7
|
+
export declare function takeCommitError(transaction: object | undefined): Error | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* The connection-level pragmas Payload's `connect` sets. libsql opens a fresh connection after
|
|
10
|
+
* each `transaction()`, so they're set again on every one.
|
|
11
|
+
*/
|
|
12
|
+
export declare function connectionPragmas({ busyTimeout, wal, }: Pick<SQLiteAdapter, 'busyTimeout' | 'wal'>): string[];
|
|
13
|
+
export declare function installWriteLock({ client, pragmas, timeout, }: {
|
|
14
|
+
client: Client;
|
|
15
|
+
pragmas: string[];
|
|
16
|
+
timeout: number;
|
|
17
|
+
}): void;
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=writeLock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writeLock.d.ts","sourceRoot":"","sources":["../src/writeLock.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D,KAAK,MAAM,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;AAStC;;;GAGG;AACH,wBAAgB,eAAe,CAAC,WAAW,EAAE,MAAM,GAAG,SAAS,GAAG,KAAK,GAAG,SAAS,CAQlF;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,EAChC,WAAW,EACX,GAAG,GACJ,EAAE,IAAI,CAAC,aAAa,EAAE,aAAa,GAAG,KAAK,CAAC,GAAG,MAAM,EAAE,CAUvD;AAED,wBAAgB,gBAAgB,CAAC,EAC/B,MAAM,EACN,OAAO,EACP,OAAO,GACR,EAAE;IACD,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB,GAAG,IAAI,CAyFP"}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
const locked = new WeakSet();
|
|
2
|
+
const commitErrors = new WeakMap();
|
|
3
|
+
/**
|
|
4
|
+
* Returns and forgets the error of this transaction's failed commit. Payload's
|
|
5
|
+
* `commitTransaction` swallows commit errors, so the adapter rethrows this one.
|
|
6
|
+
*/ export function takeCommitError(transaction) {
|
|
7
|
+
if (!transaction) return undefined;
|
|
8
|
+
const error = commitErrors.get(transaction);
|
|
9
|
+
commitErrors.delete(transaction);
|
|
10
|
+
return error;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The connection-level pragmas Payload's `connect` sets. libsql opens a fresh connection after
|
|
14
|
+
* each `transaction()`, so they're set again on every one.
|
|
15
|
+
*/ export function connectionPragmas({ busyTimeout, wal }) {
|
|
16
|
+
return [
|
|
17
|
+
...busyTimeout > 0 ? [
|
|
18
|
+
`PRAGMA busy_timeout = ${busyTimeout}`
|
|
19
|
+
] : [],
|
|
20
|
+
...wal ? [
|
|
21
|
+
`PRAGMA journal_size_limit = ${wal.journalSizeLimit}`,
|
|
22
|
+
`PRAGMA synchronous = ${wal.synchronous}`
|
|
23
|
+
] : []
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
export function installWriteLock({ client, pragmas, timeout }) {
|
|
27
|
+
if (client.protocol !== 'file' || locked.has(client)) return;
|
|
28
|
+
locked.add(client);
|
|
29
|
+
const execute = client.execute.bind(client);
|
|
30
|
+
const transaction = client.transaction.bind(client);
|
|
31
|
+
const lock = createLock(timeout);
|
|
32
|
+
const replaceConnection = (mode)=>{
|
|
33
|
+
const begun = transaction(mode);
|
|
34
|
+
for (const pragma of pragmas)execute(pragma).catch(()=>undefined);
|
|
35
|
+
return begun;
|
|
36
|
+
};
|
|
37
|
+
const recover = (error)=>{
|
|
38
|
+
if (isBusy(error)) {
|
|
39
|
+
replaceConnection('deferred').then((stale)=>stale.close(), ()=>undefined);
|
|
40
|
+
}
|
|
41
|
+
throw error;
|
|
42
|
+
};
|
|
43
|
+
const run = (call)=>call().catch(recover);
|
|
44
|
+
const batch = client.batch.bind(client);
|
|
45
|
+
const executeMultiple = client.executeMultiple.bind(client);
|
|
46
|
+
const migrate = client.migrate.bind(client);
|
|
47
|
+
client.batch = (...args)=>lock.run(()=>run(()=>batch(...args)));
|
|
48
|
+
client.executeMultiple = (...args)=>lock.run(()=>run(()=>executeMultiple(...args)));
|
|
49
|
+
client.migrate = (...args)=>lock.run(()=>run(()=>migrate(...args)));
|
|
50
|
+
client.execute = (...args)=>isRead(args[0]) ? run(()=>execute(...args)) : lock.run(()=>run(()=>execute(...args)));
|
|
51
|
+
client.transaction = async (mode)=>{
|
|
52
|
+
const release = await lock.acquire();
|
|
53
|
+
let begun;
|
|
54
|
+
try {
|
|
55
|
+
begun = await replaceConnection(mode);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
release();
|
|
58
|
+
return recover(error);
|
|
59
|
+
}
|
|
60
|
+
const { rollback, close } = begun;
|
|
61
|
+
begun.commit = async ()=>{
|
|
62
|
+
try {
|
|
63
|
+
await begun.executeMultiple('COMMIT');
|
|
64
|
+
} catch (error) {
|
|
65
|
+
commitErrors.set(begun, error instanceof Error ? error : new Error(String(error), {
|
|
66
|
+
cause: error
|
|
67
|
+
}));
|
|
68
|
+
if (!begun.closed) await begun.executeMultiple('ROLLBACK').catch(()=>undefined);
|
|
69
|
+
throw error;
|
|
70
|
+
} finally{
|
|
71
|
+
if (begun.closed) release();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
begun.rollback = async ()=>{
|
|
75
|
+
try {
|
|
76
|
+
if (!begun.closed) await rollback.call(begun);
|
|
77
|
+
} finally{
|
|
78
|
+
release();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
begun.close = ()=>{
|
|
82
|
+
try {
|
|
83
|
+
close.call(begun);
|
|
84
|
+
} finally{
|
|
85
|
+
release();
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
return begun;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function createLock(timeout) {
|
|
92
|
+
const waiting = [];
|
|
93
|
+
let held = false;
|
|
94
|
+
const release = ()=>{
|
|
95
|
+
const next = waiting.shift();
|
|
96
|
+
if (next) next();
|
|
97
|
+
else held = false;
|
|
98
|
+
};
|
|
99
|
+
const acquire = ()=>new Promise((resolve, reject)=>{
|
|
100
|
+
const started = Date.now();
|
|
101
|
+
let released = false;
|
|
102
|
+
let timer;
|
|
103
|
+
const grant = ()=>{
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
held = true;
|
|
106
|
+
resolve(()=>{
|
|
107
|
+
if (released) return;
|
|
108
|
+
released = true;
|
|
109
|
+
release();
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
if (!held) return grant();
|
|
113
|
+
timer = setTimeout(()=>{
|
|
114
|
+
waiting.splice(waiting.indexOf(grant), 1);
|
|
115
|
+
reject(new Error(`SQLite write waited ${Date.now() - started} ms for the write lock and gave up (\`writeLockTimeout\` is ${timeout} ms). Either a write ran without \`req\` while a transaction was open, so it waited on the transaction it belongs to (pass \`req\` so it joins the transaction), or a long transaction held the lock (shorten it, or raise \`writeLockTimeout\`).`));
|
|
116
|
+
}, timeout);
|
|
117
|
+
waiting.push(grant);
|
|
118
|
+
});
|
|
119
|
+
return {
|
|
120
|
+
acquire,
|
|
121
|
+
async run (fn) {
|
|
122
|
+
const done = await acquire();
|
|
123
|
+
try {
|
|
124
|
+
return await fn();
|
|
125
|
+
} finally{
|
|
126
|
+
done();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
const readPragmasWithArgument = new Set([
|
|
132
|
+
'foreign_key_check',
|
|
133
|
+
'foreign_key_list',
|
|
134
|
+
'index_info',
|
|
135
|
+
'index_list',
|
|
136
|
+
'index_xinfo',
|
|
137
|
+
'integrity_check',
|
|
138
|
+
'quick_check',
|
|
139
|
+
'table_info',
|
|
140
|
+
'table_list',
|
|
141
|
+
'table_xinfo'
|
|
142
|
+
]);
|
|
143
|
+
const writePragmas = new Set([
|
|
144
|
+
'incremental_vacuum',
|
|
145
|
+
'optimize',
|
|
146
|
+
'wal_checkpoint'
|
|
147
|
+
]);
|
|
148
|
+
function isRead(statement) {
|
|
149
|
+
const text = typeof statement === 'string' ? statement : statement?.sql;
|
|
150
|
+
if (typeof text !== 'string') return false;
|
|
151
|
+
const sql = mask(text);
|
|
152
|
+
if (sql === undefined) return false;
|
|
153
|
+
const pragma = /^pragma\s+(?:\w+\s*\.\s*)?(\w+)\s*(\(|=|;|$)/.exec(sql);
|
|
154
|
+
if (pragma) {
|
|
155
|
+
const [, name, next] = pragma;
|
|
156
|
+
if (next === '(') return readPragmasWithArgument.has(name);
|
|
157
|
+
return next !== '=' && !writePragmas.has(name);
|
|
158
|
+
}
|
|
159
|
+
if (/^explain\b/.test(sql)) return true;
|
|
160
|
+
return /^(?:select|values)\b/.test(sql.startsWith('with') ? mainStatement(sql) : sql);
|
|
161
|
+
}
|
|
162
|
+
function mask(text) {
|
|
163
|
+
let out = '';
|
|
164
|
+
for(let index = 0; index < text.length;){
|
|
165
|
+
const char = text[index];
|
|
166
|
+
const pair = text.slice(index, index + 2);
|
|
167
|
+
if (pair === '--') {
|
|
168
|
+
const end = text.indexOf('\n', index);
|
|
169
|
+
index = end === -1 ? text.length : end + 1;
|
|
170
|
+
out += ' ';
|
|
171
|
+
} else if (pair === '/*') {
|
|
172
|
+
const end = text.indexOf('*/', index + 2);
|
|
173
|
+
if (end === -1) return undefined;
|
|
174
|
+
index = end + 2;
|
|
175
|
+
out += ' ';
|
|
176
|
+
} else if (char === "'" || char === '"' || char === '`' || char === '[') {
|
|
177
|
+
const close = char === '[' ? ']' : char;
|
|
178
|
+
let end = index + 1;
|
|
179
|
+
for(;;){
|
|
180
|
+
end = text.indexOf(close, end);
|
|
181
|
+
if (end === -1) return undefined;
|
|
182
|
+
if (close !== ']' && text[end + 1] === close) end += 2;
|
|
183
|
+
else break;
|
|
184
|
+
}
|
|
185
|
+
index = end + 1;
|
|
186
|
+
out += char === "'" ? " '' " : ' _ ';
|
|
187
|
+
} else {
|
|
188
|
+
out += char.toLowerCase();
|
|
189
|
+
index += 1;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return out.trim();
|
|
193
|
+
}
|
|
194
|
+
function mainStatement(sql) {
|
|
195
|
+
let rest = sql.replace(/^with\s+(?:recursive\s+)?/, '');
|
|
196
|
+
for(;;){
|
|
197
|
+
const head = /^\w+\s*(?:\([^()]*\)\s*)?as\s+(?:not\s+)?(?:materialized\s+)?\(/.exec(rest);
|
|
198
|
+
if (!head) return '';
|
|
199
|
+
let depth = 1;
|
|
200
|
+
let index = head[0].length;
|
|
201
|
+
for(; index < rest.length && depth > 0; index += 1){
|
|
202
|
+
if (rest[index] === '(') depth += 1;
|
|
203
|
+
else if (rest[index] === ')') depth -= 1;
|
|
204
|
+
}
|
|
205
|
+
if (depth > 0) return '';
|
|
206
|
+
rest = rest.slice(index).trimStart();
|
|
207
|
+
if (!rest.startsWith(',')) return rest;
|
|
208
|
+
rest = rest.slice(1).trimStart();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function isBusy(error) {
|
|
212
|
+
return error instanceof Error && 'code' in error && typeof error.code === 'string' && error.code.startsWith('SQLITE_BUSY');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
//# sourceMappingURL=writeLock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/writeLock.ts"],"names":["locked","WeakSet","commitErrors","WeakMap","takeCommitError","transaction","undefined","error","get","delete","connectionPragmas","busyTimeout","wal","journalSizeLimit","synchronous","installWriteLock","client","pragmas","timeout","protocol","has","add","execute","bind","lock","createLock","replaceConnection","mode","begun","pragma","catch","recover","isBusy","then","stale","close","run","call","batch","executeMultiple","migrate","args","isRead","release","acquire","rollback","commit","set","Error","String","cause","closed","waiting","held","next","shift","Promise","resolve","reject","started","Date","now","released","timer","grant","clearTimeout","setTimeout","splice","indexOf","push","fn","done","readPragmasWithArgument","Set","writePragmas","statement","text","sql","mask","exec","name","test","startsWith","mainStatement","out","index","length","char","pair","slice","end","toLowerCase","trim","rest","replace","head","depth","trimStart","code"],"mappings":"AAQA,MAAMA,SAAS,IAAIC;AACnB,MAAMC,eAAe,IAAIC;AAEzB;;;CAGC,GACD,OAAO,SAASC,gBAAgBC,WAA+B;IAC7D,IAAI,CAACA,aAAa,OAAOC;IAEzB,MAAMC,QAAQL,aAAaM,GAAG,CAACH;IAE/BH,aAAaO,MAAM,CAACJ;IAEpB,OAAOE;AACT;AAEA;;;CAGC,GACD,OAAO,SAASG,kBAAkB,EAChCC,WAAW,EACXC,GAAG,EACwC;IAC3C,OAAO;WACDD,cAAc,IAAI;YAAC,CAAC,sBAAsB,EAAEA,aAAa;SAAC,GAAG,EAAE;WAC/DC,MACA;YACE,CAAC,4BAA4B,EAAEA,IAAIC,gBAAgB,EAAE;YACrD,CAAC,qBAAqB,EAAED,IAAIE,WAAW,EAAE;SAC1C,GACD,EAAE;KACP;AACH;AA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,IAAIU,QAAQ,GAAGX,SAAS,EAAG;YACnD,IAAIQ,IAAI,CAACR,MAAM,KAAK,KAAKW,SAAS;iBAC7B,IAAIH,IAAI,CAACR,MAAM,KAAK,KAAKW,SAAS;QACzC;QAEA,IAAIA,QAAQ,GAAG,OAAO;QAEtBH,OAAOA,KAAKJ,KAAK,CAACJ,OAAOY,SAAS;QAElC,IAAI,CAACJ,KAAKX,UAAU,CAAC,MAAM,OAAOW;QAElCA,OAAOA,KAAKJ,KAAK,CAAC,GAAGQ,SAAS;IAChC;AACF;AAEA,SAASjE,OAAOzB,KAAc;IAC5B,OACEA,iBAAiByC,SACjB,UAAUzC,SACV,OAAOA,MAAM2F,IAAI,KAAK,YACtB3F,MAAM2F,IAAI,CAAChB,UAAU,CAAC;AAE1B","file":"writeLock.js"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frogbotai/db-sqlite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"description": "FrogBot SQLite adapter.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Colby Gilbert",
|
|
@@ -30,15 +30,16 @@
|
|
|
30
30
|
],
|
|
31
31
|
"sideEffects": false,
|
|
32
32
|
"dependencies": {
|
|
33
|
+
"@libsql/client": "0.14.0",
|
|
33
34
|
"@payloadcms/db-sqlite": "3.90.1",
|
|
34
35
|
"drizzle-orm": "0.45.2"
|
|
35
36
|
},
|
|
36
37
|
"peerDependencies": {
|
|
37
|
-
"frogbot": "0.
|
|
38
|
+
"frogbot": "0.31.0"
|
|
38
39
|
},
|
|
39
40
|
"devDependencies": {
|
|
40
41
|
"typescript": "5.6.2",
|
|
41
|
-
"frogbot": "0.
|
|
42
|
+
"frogbot": "0.31.0"
|
|
42
43
|
},
|
|
43
44
|
"publishConfig": {
|
|
44
45
|
"access": "public"
|