mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 07:56:01 +00:00
feat: libsql basic support
This commit is contained in:
174
plugins/dbgate-plugin-sqlite/src/backend/driver.libsql.js
Normal file
174
plugins/dbgate-plugin-sqlite/src/backend/driver.libsql.js
Normal file
@@ -0,0 +1,174 @@
|
||||
// @ts-check
|
||||
const _ = require('lodash');
|
||||
const stream = require('stream');
|
||||
const sqliteDriver = require('./driver.sqlite');
|
||||
const driverBases = require('../frontend/drivers');
|
||||
const Analyser = require('./Analyser');
|
||||
const { splitQuery, sqliteSplitterOptions } = require('dbgate-query-splitter');
|
||||
const { runStreamItem, waitForDrain } = require('./helpers');
|
||||
const { getLogger, createBulkInsertStreamBase, extractErrorLogData } = global.DBGATE_PACKAGES['dbgate-tools'];
|
||||
|
||||
const logger = getLogger('sqliteDriver');
|
||||
|
||||
let libsqlValue;
|
||||
function getLibsql() {
|
||||
if (!libsqlValue) {
|
||||
libsqlValue = require('libsql');
|
||||
}
|
||||
return libsqlValue;
|
||||
}
|
||||
|
||||
function extractColumns(row) {
|
||||
if (!row) return [];
|
||||
|
||||
const columns = Object.keys(row).map((columnName) => ({ columnName }));
|
||||
return columns;
|
||||
}
|
||||
|
||||
/** @type {import('dbgate-types').EngineDriver<import('libsql').Database>} */
|
||||
const libsqlDriver = {
|
||||
...driverBases[1],
|
||||
analyserClass: Analyser,
|
||||
async connect({ databaseFile, isReadOnly, authToken, databaseUrl, ...rest }) {
|
||||
console.log('connect', databaseFile, isReadOnly, authToken, databaseUrl, rest);
|
||||
const Database = getLibsql();
|
||||
const client = databaseFile
|
||||
? new Database(databaseFile, { readonly: !!isReadOnly })
|
||||
: new Database(databaseUrl, { authToken, readonly: !!isReadOnly });
|
||||
|
||||
return {
|
||||
client,
|
||||
};
|
||||
},
|
||||
async close(dbhan) {
|
||||
// sqlite close is sync, returns this
|
||||
dbhan.client.close();
|
||||
},
|
||||
// @ts-ignore
|
||||
async query(dbhan, sql) {
|
||||
const stmt = dbhan.client.prepare(sql);
|
||||
|
||||
const rows = stmt.all();
|
||||
const stmtColumns = stmt.columns();
|
||||
const columns = stmtColumns.length > 0 ? stmtColumns : extractColumns(rows[0]);
|
||||
|
||||
return {
|
||||
rows,
|
||||
columns: columns.map((col) => ({
|
||||
columnName: col.name,
|
||||
dataType: col.type,
|
||||
})),
|
||||
};
|
||||
},
|
||||
async stream(dbhan, sql, options) {
|
||||
const sqlSplitted = splitQuery(sql, sqliteSplitterOptions);
|
||||
|
||||
const rowCounter = { count: 0, date: null };
|
||||
|
||||
console.log('#stream', sql);
|
||||
const inTransaction = dbhan.client.transaction(() => {
|
||||
for (const sqlItem of sqlSplitted) {
|
||||
runStreamItem(dbhan, sqlItem, options, rowCounter);
|
||||
}
|
||||
|
||||
if (rowCounter.date) {
|
||||
options.info({
|
||||
message: `${rowCounter.count} rows affected`,
|
||||
time: new Date(),
|
||||
severity: 'info',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
inTransaction();
|
||||
} catch (error) {
|
||||
logger.error(extractErrorLogData(error), 'Stream error');
|
||||
const { message, procName } = error;
|
||||
options.info({
|
||||
message,
|
||||
line: 0,
|
||||
procedure: procName,
|
||||
time: new Date(),
|
||||
severity: 'error',
|
||||
});
|
||||
}
|
||||
|
||||
options.done();
|
||||
// return stream;
|
||||
},
|
||||
async script(dbhan, sql) {
|
||||
const inTransaction = dbhan.client.transaction(() => {
|
||||
for (const sqlItem of splitQuery(sql, this.getQuerySplitterOptions('script'))) {
|
||||
const stmt = dbhan.client.prepare(sqlItem);
|
||||
stmt.run();
|
||||
}
|
||||
});
|
||||
inTransaction();
|
||||
},
|
||||
|
||||
async readQueryTask(stmt, pass) {
|
||||
// let sent = 0;
|
||||
for (const row of stmt.iterate()) {
|
||||
// sent++;
|
||||
if (!pass.write(row)) {
|
||||
// console.log('WAIT DRAIN', sent);
|
||||
await waitForDrain(pass);
|
||||
}
|
||||
}
|
||||
pass.end();
|
||||
},
|
||||
async readQuery(dbhan, sql, structure) {
|
||||
const pass = new stream.PassThrough({
|
||||
objectMode: true,
|
||||
highWaterMark: 100,
|
||||
});
|
||||
|
||||
const stmt = dbhan.client.prepare(sql);
|
||||
const columns = stmt.columns();
|
||||
|
||||
pass.write({
|
||||
__isStreamHeader: true,
|
||||
...(structure || {
|
||||
columns: columns.map((col) => ({
|
||||
columnName: col.name,
|
||||
dataType: col.type,
|
||||
})),
|
||||
}),
|
||||
});
|
||||
this.readQueryTask(stmt, pass);
|
||||
|
||||
return pass;
|
||||
},
|
||||
async writeTable(dbhan, name, options) {
|
||||
return createBulkInsertStreamBase(this, stream, dbhan, name, options);
|
||||
},
|
||||
async getVersion(dbhan) {
|
||||
const { rows } = await this.query(dbhan, 'select sqlite_version() as version');
|
||||
const { version } = rows[0];
|
||||
|
||||
return {
|
||||
version,
|
||||
versionText: `SQLite ${version}`,
|
||||
};
|
||||
},
|
||||
|
||||
getAuthTypes() {
|
||||
const res = [
|
||||
{
|
||||
title: 'File',
|
||||
name: 'file',
|
||||
disabledFields: ['databaseUrl', 'authToken'],
|
||||
},
|
||||
{
|
||||
title: 'URL',
|
||||
name: 'url',
|
||||
disabledFields: ['databaseFile'],
|
||||
},
|
||||
];
|
||||
|
||||
return res;
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = libsqlDriver;
|
||||
@@ -1,9 +1,11 @@
|
||||
// @ts-check
|
||||
const _ = require('lodash');
|
||||
const stream = require('stream');
|
||||
const driverBase = require('../frontend/driver');
|
||||
const Analyser = require('./Analyser');
|
||||
const driverBases = require('../frontend/drivers');
|
||||
const { splitQuery, sqliteSplitterOptions } = require('dbgate-query-splitter');
|
||||
const { getLogger, createBulkInsertStreamBase, extractErrorLogData } = global.DBGATE_PACKAGES['dbgate-tools'];
|
||||
const { runStreamItem, waitForDrain } = require('./helpers');
|
||||
|
||||
const logger = getLogger('sqliteDriver');
|
||||
|
||||
@@ -15,50 +17,9 @@ function getBetterSqlite() {
|
||||
return betterSqliteValue;
|
||||
}
|
||||
|
||||
async function waitForDrain(stream) {
|
||||
return new Promise((resolve) => {
|
||||
stream.once('drain', () => {
|
||||
// console.log('CONTINUE DRAIN');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function runStreamItem(dbhan, sql, options, rowCounter) {
|
||||
const stmt = dbhan.client.prepare(sql);
|
||||
if (stmt.reader) {
|
||||
const columns = stmt.columns();
|
||||
// const rows = stmt.all();
|
||||
|
||||
options.recordset(
|
||||
columns.map((col) => ({
|
||||
columnName: col.name,
|
||||
dataType: col.type,
|
||||
}))
|
||||
);
|
||||
|
||||
for (const row of stmt.iterate()) {
|
||||
options.row(row);
|
||||
}
|
||||
} else {
|
||||
const info = stmt.run();
|
||||
rowCounter.count += info.changes;
|
||||
if (!rowCounter.date) rowCounter.date = new Date().getTime();
|
||||
if (new Date().getTime() > rowCounter.date > 1000) {
|
||||
options.info({
|
||||
message: `${rowCounter.count} rows affected`,
|
||||
time: new Date(),
|
||||
severity: 'info',
|
||||
});
|
||||
rowCounter.count = 0;
|
||||
rowCounter.date = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @type {import('dbgate-types').EngineDriver} */
|
||||
const driver = {
|
||||
...driverBase,
|
||||
...driverBases[0],
|
||||
analyserClass: Analyser,
|
||||
async connect({ databaseFile, isReadOnly }) {
|
||||
const Database = getBetterSqlite();
|
||||
@@ -186,6 +147,4 @@ const driver = {
|
||||
},
|
||||
};
|
||||
|
||||
driver.initialize = (dbgateEnv) => {};
|
||||
|
||||
module.exports = driver;
|
||||
9
plugins/dbgate-plugin-sqlite/src/backend/drivers.js
Normal file
9
plugins/dbgate-plugin-sqlite/src/backend/drivers.js
Normal file
@@ -0,0 +1,9 @@
|
||||
//R@ts-check
|
||||
const sqliteDriver = require('./driver.sqlite');
|
||||
const libsqlDriver = require('./driver.libsql');
|
||||
|
||||
const drivers = [sqliteDriver, libsqlDriver];
|
||||
|
||||
drivers.initialize = (dbgateEnv) => {};
|
||||
|
||||
module.exports = drivers;
|
||||
49
plugins/dbgate-plugin-sqlite/src/backend/helpers.js
Normal file
49
plugins/dbgate-plugin-sqlite/src/backend/helpers.js
Normal file
@@ -0,0 +1,49 @@
|
||||
// @ts-check
|
||||
|
||||
function runStreamItem(dbhan, sql, options, rowCounter) {
|
||||
const stmt = dbhan.client.prepare(sql);
|
||||
console.log(stmt);
|
||||
console.log(stmt.reader);
|
||||
if (stmt.reader) {
|
||||
const columns = stmt.columns();
|
||||
// const rows = stmt.all();
|
||||
|
||||
options.recordset(
|
||||
columns.map((col) => ({
|
||||
columnName: col.name,
|
||||
dataType: col.type,
|
||||
}))
|
||||
);
|
||||
|
||||
for (const row of stmt.iterate()) {
|
||||
options.row(row);
|
||||
}
|
||||
} else {
|
||||
const info = stmt.run();
|
||||
rowCounter.count += info.changes;
|
||||
if (!rowCounter.date) rowCounter.date = new Date().getTime();
|
||||
if (new Date().getTime() - rowCounter.date > 1000) {
|
||||
options.info({
|
||||
message: `${rowCounter.count} rows affected`,
|
||||
time: new Date(),
|
||||
severity: 'info',
|
||||
});
|
||||
rowCounter.count = 0;
|
||||
rowCounter.date = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForDrain(stream) {
|
||||
return new Promise((resolve) => {
|
||||
stream.once('drain', () => {
|
||||
// console.log('CONTINUE DRAIN');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
runStreamItem,
|
||||
waitForDrain,
|
||||
};
|
||||
@@ -1,9 +1,9 @@
|
||||
const driver = require('./driver');
|
||||
const drivers = require('./drivers');
|
||||
|
||||
module.exports = {
|
||||
packageName: 'dbgate-plugin-sqlite',
|
||||
drivers: [driver],
|
||||
drivers: drivers,
|
||||
initialize(dbgateEnv) {
|
||||
driver.initialize(dbgateEnv);
|
||||
drivers.initialize(dbgateEnv);
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user