Skip to content

Commit

Permalink
refa: add driver.dropAll() for clarification
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 8, 2024
1 parent 99b2baf commit fb984b3
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 26 deletions.
10 changes: 6 additions & 4 deletions packages/core/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ Type Driven Database Framework.

| Driver | Version | Notes |
| ------ | ------ | ----- |
| [MySQL](https://github.com/shigma/minato/tree/master/packages/mysql) | [![npm](https://img.shields.io/npm/v/@minatojs/driver-mysql?style=flat-square)](https://www.npmjs.com/package/@minatojs/driver-mysql) | MySQL 5.7, MySQL 8.0, MariaDB 10.5 |
| [Memory](https://github.com/shigma/minato/tree/master/packages/memory) | [![npm](https://img.shields.io/npm/v/@minatojs/driver-memory?style=flat-square)](https://www.npmjs.com/package/@minatojs/driver-memory) | In-memory driver support |
| [MongoDB](https://github.com/shigma/minato/tree/master/packages/mongo) | [![npm](https://img.shields.io/npm/v/@minatojs/driver-mongo?style=flat-square)](https://www.npmjs.com/package/@minatojs/driver-mongo) | |
| [MySQL](https://github.com/shigma/minato/tree/master/packages/mysql) | [![npm](https://img.shields.io/npm/v/@minatojs/driver-mysql?style=flat-square)](https://www.npmjs.com/package/@minatojs/driver-mysql) | MySQL 5.7+, MariaDB 10.5 |
| [PostgreSQL](https://github.com/shigma/minato/tree/master/packages/postgres) | [![npm](https://img.shields.io/npm/v/@minatojs/driver-postgres?style=flat-square)](https://www.npmjs.com/package/@minatojs/driver-postgres) | PostgreSQL 14+ |
| [SQLite](https://github.com/shigma/minato/tree/master/packages/sqlite) | [![npm](https://img.shields.io/npm/v/@minatojs/driver-sqlite?style=flat-square)](https://www.npmjs.com/package/@minatojs/driver-sqlite) | |
| [Memory](https://github.com/shigma/minato/tree/master/packages/memory) | [![npm](https://img.shields.io/npm/v/@minatojs/driver-memory?style=flat-square)](https://www.npmjs.com/package/@minatojs/driver-memory) | In-memory driver support |

## Basic Usage

```ts
import { Database } from 'minato'
import Database from 'minato'
import MySQLDriver from '@minatojs/driver-mysql'

const database = new Database()

await database.connect('mysql', {
await database.connect(MySQLDriver, {
host: 'localhost',
port: 3306,
user: 'root',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class Database<S = any> extends Service {
}

async dropAll() {
await Promise.all(Object.values(this.drivers).map(driver => driver.drop()))
await Promise.all(Object.values(this.drivers).map(driver => driver.dropAll()))
}

async stats() {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export abstract class Driver<C = any> {

abstract start(): Promise<void>
abstract stop(): Promise<void>
abstract drop(table?: string): Promise<void>
abstract drop(table: string): Promise<void>
abstract dropAll(): Promise<void>
abstract stats(): Promise<Partial<Driver.Stats>>
abstract prepare(name: string): Promise<void>
abstract get(sel: Selection.Immutable, modifier: Modifier): Promise<any>
Expand Down
12 changes: 6 additions & 6 deletions packages/memory/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ export class MemoryDriver extends Driver<MemoryDriver.Config> {
}).filter(Boolean)
}

async drop(table?: string) {
if (table) {
delete this.#store[table]
} else {
this.#store = { _fields: [] }
}
async drop(table: string) {
delete this.#store[table]
}

async dropAll() {
this.#store = { _fields: [] }
}

async stats() {
Expand Down
10 changes: 5 additions & 5 deletions packages/mongo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ export class MongoDriver extends Driver<MongoDriver.Config> {
})
}

async drop(table?: string) {
if (table) {
await this.db.dropCollection(table, { session: this.session })
return
}
async drop(table: string) {
await this.db.dropCollection(table, { session: this.session })
}

async dropAll() {
await Promise.all([
'_fields',
...Object.keys(this.database.tables),
Expand Down
7 changes: 5 additions & 2 deletions packages/mysql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,11 @@ INSERT INTO mtt VALUES(json_extract(j, concat('$[', i, ']'))); SET i=i+1; END WH
return this.queue(sql, values)
}

async drop(table?: string) {
if (table) return this.query(`DROP TABLE ${escapeId(table)}`)
async drop(table: string) {
await this.query(`DROP TABLE ${escapeId(table)}`)
}

async dropAll() {
const data = await this._select('information_schema.tables', ['TABLE_NAME'], 'TABLE_SCHEMA = ?', [this.config.database])
if (!data.length) return
await this.query(data.map(({ TABLE_NAME }) => `DROP TABLE ${escapeId(TABLE_NAME)}`).join('; '))
Expand Down
10 changes: 5 additions & 5 deletions packages/postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,11 @@ export class PostgresDriver extends Driver<PostgresDriver.Config> {
})
}

async drop(table?: string) {
if (table) {
await this.query(`DROP TABLE IF EXISTS ${escapeId(table)} CASCADE`)
return
}
async drop(table: string) {
await this.query(`DROP TABLE IF EXISTS ${escapeId(table)} CASCADE`)
}

async dropAll() {
const tables: TableInfo[] = await this.queue(`SELECT * FROM information_schema.tables WHERE table_schema = 'public'`)
if (!tables.length) return
await this.query(`DROP TABLE IF EXISTS ${tables.map(t => escapeId(t.table_name)).join(',')} CASCADE`)
Expand Down
7 changes: 5 additions & 2 deletions packages/sqlite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,11 @@ export class SQLiteDriver extends Driver<SQLiteDriver.Config> {
return result
}

async drop(table?: string) {
if (table) return this.#run(`DROP TABLE ${escapeId(table)}`)
async drop(table: string) {
this.#run(`DROP TABLE ${escapeId(table)}`)
}

async dropAll() {
const tables = Object.keys(this.database.tables)
for (const table of tables) {
this.#run(`DROP TABLE ${escapeId(table)}`)
Expand Down

0 comments on commit fb984b3

Please sign in to comment.