Skip to content

Instantly share code, notes, and snippets.

@timreichen
Last active April 3, 2021 12:49
Show Gist options
  • Save timreichen/c6282e6725fade0afdceb91f46b80075 to your computer and use it in GitHub Desktop.
Save timreichen/c6282e6725fade0afdceb91f46b80075 to your computer and use it in GitHub Desktop.
Deno database driver suggestions

Deno database driver suggestions

An incomplete list of suggestion to unify deno database drivers (more suggestions are wellcome).

Concerning the following database types

  • mysql
  • postgres
  • redis
  • sqlite
  • mongo

Suggestions

Rep name

Would be nice to have reps have the actual db name instead of name creation (mango -> mongo)

Instantiation

There are different instantiation systems at the moment. Some pass the config during instantiation, others have a connect function that creates the instance and await the connection.

import { Mango } from "https://deno.land/x/mango/mod.ts"
const client = new Mango({ /* ... configs */ })
const db = await client.connect()
import { connect } from "https://deno.land/x/redis/mod.ts"
const redis = await connect({ hostname: "127.0.0.1", port: 6379 })

let mod.ts export an connect function that creates an instance internally. I think in most usecases method calling is done to the connection instance, not to the db instance, no?

import { connect } from "https://deno.land/x/mysql/mod.ts"
const mysql = await connect({ /* config */ })

query() vs execute()

let connection have an execute method instead of query or this.executor.exec. Since query calls call execute internally and look if result.rows is defined, we easily can do that with

connection.execute().then(data => data.rows)

I think there is no need to have that as a separate method.

I am also pretty sure there is a nicer way mapping to array/object than having the methods queryObject and queryArray in a similar way.

Transactions

let startTransaction be a async method that returns an instance instead of callback function

import { connect } from "https://deno.land/x/mysql/mod.ts"

const connection = connect({ /* config */ })

const transaction = await connection.startTransaction()

await transaction.execute()

await transaction.execute()

const result = await transaction.commit() /* or */ await transaction.rollback()

Logger

let connection have the logger built in and have a logLevel and quiet property instead of a configLogger function or something similar.

import { connect } from "https://deno.land/x/mysql/mod.ts"
const connection = connect({ /* config */ })
connection.logLevel = "debug"
connection.quiet = true

I am aware that not all these suggestions are appliable on every driver and that some drivers have different methods and functionality (like mongo db). But I think the effort to have a common structure between them would pay off.

What do you think?

@dyedgreen
Copy link

Have a look at some of the discussion in jeremyBanks/database#3 😃

@timreichen
Copy link
Author

@dyedgreen hey this is kinda what I was looking for, though I would change some things. Is this proposal definite or would you consider changing/discussiong some things to give it more a ts/js feel?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment