Skip to content

Commit

Permalink
feat(driver): implement sqlite driver
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Nov 5, 2023
1 parent 87f2afd commit 7ebb1f0
Show file tree
Hide file tree
Showing 16 changed files with 4,197 additions and 4 deletions.
1,222 changes: 1,222 additions & 0 deletions src/drivers/SqliteDriver.ts

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/factories/ConnectionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class ConnectionFactory {
switch (driver) {
case 'fake':
case 'mysql':
case 'sqlite':
case 'postgres':
await client.destroy()
break
Expand Down Expand Up @@ -52,6 +53,13 @@ export class ConnectionFactory {
return this.mongoose(con)
}

/**
* Create the connection with a sqlite database.
*/
public static sqlite(con: string): Knex {
return this.knex(con, 'better-sqlite3')
}

/**
* Create the connection with a postgres database.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/factories/DriverFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Exec, Options } from '@athenna/common'
import type { Driver } from '#src/drivers/Driver'
import type { DriverKey } from '#src/types/DriverKey'
import { MySqlDriver } from '#src/drivers/MySqlDriver'
import { SqliteDriver } from '#src/drivers/SqliteDriver'
import { PostgresDriver } from '#src/drivers/PostgresDriver'
import type { CreateConOptions } from '#src/types/CreateConOptions'
import { ConnectionFactory } from '#src/factories/ConnectionFactory'
Expand All @@ -30,6 +31,10 @@ export class DriverFactory {
Driver: MySqlDriver,
client: null
})
.set('sqlite', {
Driver: SqliteDriver,
client: null
})
.set('postgres', {
Driver: PostgresDriver,
client: null
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/config/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export default {
},
debug: false
},
'sqlite-memory': {
driver: 'sqlite',
connection: {
filename: ':memory:'
},
debug: false
},
'postgres-docker': {
driver: 'postgres',
connection: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { DatabaseImpl, Migration } from '#src'

export class UserMigration extends Migration {
public static connection() {
return 'sqlite-memory'
}

public tableName = 'users'

public async up(db: DatabaseImpl) {
return db.createTable(this.tableName, table => {
table.increments('id')
table.string('name')
table.string('email').unique()
table.timestamps(true, true, true)
table.dateTime('deletedAt').nullable().defaultTo(null)
})
}

public async down(db: DatabaseImpl) {
return db.dropTable(this.tableName)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { DatabaseImpl, Migration } from '#src'

export class ProductMigration extends Migration {
public static connection() {
return 'sqlite-memory'
}

public tableName = 'products'

public async up(db: DatabaseImpl) {
return db.createTable(this.tableName, table => {
table.increments('id')
table.string('name', 255)
table.integer('price').defaultTo(0)
table.integer('userId').unsigned().index().references('id').inTable('users')

table.timestamps(true, true, true)
table.dateTime('deletedAt').nullable().defaultTo(null)
})
}

public async down(db: DatabaseImpl) {
return db.dropTable(this.tableName)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { DatabaseImpl, Migration } from '#src'

export class ProductDetailsMigration extends Migration {
public static connection() {
return 'sqlite-memory'
}

public tableName = 'product_details'

public async up(db: DatabaseImpl) {
return db.createTable(this.tableName, table => {
table.increments('id')
table.string('content', 255)
table.integer('productId').unsigned().index().references('id').inTable('products')

table.timestamps(true, true, true)
table.dateTime('deletedAt').nullable().defaultTo(null)
})
}

public async down(db: DatabaseImpl) {
return db.dropTable(this.tableName)
}
}
6 changes: 3 additions & 3 deletions tests/unit/database/migrations/MigrationSourceTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export default class MigrationSourceTest {

const migrations = await new MigrationSource('fake').getMigrations()

assert.deepEqual(migrations[0].name, '2022_10_08_000005_create_countries_table.ts')
assert.deepEqual(migrations[1].name, '2022_10_08_000006_create_capitals_table.ts')
assert.deepEqual(migrations[0].name, '2022_10_08_0000012_create_countries_table.ts')
assert.deepEqual(migrations[1].name, '2022_10_08_0000013_create_capitals_table.ts')
}

@Test()
Expand All @@ -50,7 +50,7 @@ export default class MigrationSourceTest {
const migrations = await migrationSource.getMigrations()
const name = migrationSource.getMigrationName(migrations[0])

assert.deepEqual(name, '2022_10_08_000005_create_countries_table.ts')
assert.deepEqual(name, '2022_10_08_0000012_create_countries_table.ts')
}

@Test()
Expand Down
Loading

0 comments on commit 7ebb1f0

Please sign in to comment.