Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/2.connectors/mariadb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
icon: simple-icons:mariadb
---

# MySQL

> Connect DB0 to MariaDB Database using mariadb

## Usage

For this connector, you need to install [`mariadb`](https://www.npmjs.com/package/mariadb) dependency:

:pm-install{name="mariadb"}

Use `mariadb` connector:

```js
import { createDatabase } from "db0";
import mariadb from "db0/connectors/mariadb";

const db = createDatabase(
mariadb({
/* options */
}),
);
```

## Options

:read-more{to="https://github.com/mariadb-corporation/mariadb-connector-nodejs/blob/main/types/share.d.ts#L603"}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"jiti": "^2.6.1",
"mlly": "^1.8.0",
"mysql2": "^3.15.2",
"mariadb": "^3.4.5",
"obuild": "^0.2.1",
"pg": "^8.16.3",
"prettier": "^3.6.2",
Expand Down Expand Up @@ -90,6 +91,9 @@
"mysql2": {
"optional": true
},
"mariadb": {
"optional": true
},
"@electric-sql/pglite": {
"optional": true
},
Expand Down
21 changes: 20 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/_connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { ConnectorOptions as PlanetscaleOptions } from "db0/connectors/plan
import type { ConnectorOptions as PostgreSQLOptions } from "db0/connectors/postgresql";
import type { ConnectorOptions as SQLite3Options } from "db0/connectors/sqlite3";

export type ConnectorName = "better-sqlite3" | "bun-sqlite" | "bun" | "cloudflare-d1" | "cloudflare-hyperdrive-mysql" | "cloudflare-hyperdrive-postgresql" | "libsql-core" | "libsql-http" | "libsql-node" | "libsql" | "libsql-web" | "mysql2" | "node-sqlite" | "sqlite" | "pglite" | "planetscale" | "postgresql" | "sqlite3";
export type ConnectorName = "better-sqlite3" | "bun-sqlite" | "bun" | "cloudflare-d1" | "cloudflare-hyperdrive-mysql" | "cloudflare-hyperdrive-postgresql" | "libsql-core" | "libsql-http" | "libsql-node" | "libsql" | "libsql-web" | "mariadb" | "mysql2" | "node-sqlite" | "sqlite" | "pglite" | "planetscale" | "postgresql" | "sqlite3";

export type ConnectorOptions = {
"better-sqlite3": BetterSQLite3Options;
Expand Down Expand Up @@ -56,6 +56,7 @@ export const connectors: Record<ConnectorName, string> = Object.freeze({
/** alias of libsql-node */
"libsql": "db0/connectors/libsql/node",
"libsql-web": "db0/connectors/libsql/web",
"mariadb": "db0/connectors/mariadb",
"mysql2": "db0/connectors/mysql2",
"node-sqlite": "db0/connectors/node-sqlite",
/** alias of node-sqlite */
Expand Down
70 changes: 70 additions & 0 deletions src/connectors/mariadb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import mariadb from "mariadb";
import type { Connector, Primitive } from "db0";
import { BoundableStatement } from "./_internal/statement.ts";

export type ConnectorConfig = mariadb.ConnectionConfig;

type InternalQuery = (sql: string, params?: unknown[]) => Promise<any>;

export default function mysqlConnector(
opts: ConnectorConfig,
): Connector<mariadb.Connection> {
let _connection: mariadb.Connection | undefined;
const getConnection = async () => {
if (_connection) {
return _connection;
}

_connection = await mariadb.createConnection({
...opts,
});

return _connection;
};

const query: InternalQuery = (sql, params) =>
getConnection()
.then((c) => c.query(sql, params))
.then((res) => res[0]);

return {
name: "mariadb",
dialect: "mariadb",
getInstance: () => getConnection(),
exec: (sql) => query(sql),
prepare: (sql) => new StatementWrapper(sql, query),
dispose: async () => {
await _connection?.end?.();
_connection = undefined;
},
};
}

class StatementWrapper extends BoundableStatement<void> {
#query: InternalQuery;
#sql: string;

constructor(sql: string, query: InternalQuery) {
super();
this.#sql = sql;
this.#query = query;
}

async all(...params: Primitive[]) {
const res = await this.#query(this.#sql, params);
return Array.isArray(res) ? res : [res];
}

async run(...params: Primitive[]) {
const res = (await this.#query(this.#sql, params)) as mariadb.UpsertResult;
return {
success: true,
...res,
};
}

async get(...params: Primitive[]) {
const res = await this.#query(this.#sql, params);
return Array.isArray(res) ? res[0] : res;
}
}
7 changes: 6 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
*/
export type Primitive = string | number | boolean | undefined | null;

export type SQLDialect = "mysql" | "postgresql" | "sqlite" | "libsql";
export type SQLDialect =
| "mysql"
| "postgresql"
| "sqlite"
| "libsql"
| "mariadb";

export type Statement = {
/**
Expand Down
6 changes: 4 additions & 2 deletions test/connectors/_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export function testConnector<TConnector extends Connector = Connector>(opts: {
it("drop and create table", async () => {
await db.sql`DROP TABLE IF EXISTS users`;
switch (opts.dialect) {
case "mysql": {
case "mysql":
case "mariadb": {
await db.sql`CREATE TABLE users (\`id\` VARCHAR(4) PRIMARY KEY, \`firstName\` TEXT, \`lastName\` TEXT, \`email\` TEXT)`;
break;
}
Expand All @@ -53,7 +54,8 @@ export function testConnector<TConnector extends Connector = Connector>(opts: {

it("insert", async () => {
switch (opts.dialect) {
case "mysql": {
case "mysql":
case "mariadb": {
await db.sql`INSERT INTO users VALUES (${userId}, 'John', 'Doe', '')`;
break;
}
Expand Down
15 changes: 15 additions & 0 deletions test/connectors/mariadb.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe } from "vitest";
import connector from "../../src/connectors/mariadb";
import { testConnector } from "./_tests";

describe.runIf(process.env.MYSQL_URL)("connectors: mariadb.test", () => {
testConnector({
dialect: "mariadb",
connector: connector({
host: "localhost",
user: "test",
password: "test",
database: "db0",
}),
});
});