|
1 | 1 | import timekeeper from 'timekeeper' |
2 | 2 | import { getActiveTest } from '@japa/runner' |
| 3 | +import { BaseModel } from '@adonisjs/lucid/orm' |
| 4 | +import { AppFactory } from '@adonisjs/core/factories/app' |
| 5 | +import { mkdir, rm } from 'node:fs/promises' |
| 6 | +import { join } from 'node:path' |
| 7 | +import { Emitter } from '@adonisjs/core/events' |
| 8 | +import { LoggerFactory } from '@adonisjs/core/factories/logger' |
| 9 | +import { Database } from '@adonisjs/lucid/database' |
3 | 10 |
|
4 | 11 | /** |
5 | 12 | * Travels time by seconds |
@@ -39,3 +46,120 @@ export function freezeTime() { |
39 | 46 | timekeeper.reset() |
40 | 47 | }) |
41 | 48 | } |
| 49 | + |
| 50 | +/** |
| 51 | + * Creates an instance of the database class for making queries |
| 52 | + */ |
| 53 | +export async function createDatabase() { |
| 54 | + const test = getActiveTest() |
| 55 | + if (!test) { |
| 56 | + throw new Error('Cannot use "createDatabase" outside of a Japa test') |
| 57 | + } |
| 58 | + |
| 59 | + const basePath = test.context.fs.basePath |
| 60 | + await mkdir(basePath) |
| 61 | + |
| 62 | + const app = new AppFactory().create(test.context.fs.baseUrl, () => {}) |
| 63 | + const logger = new LoggerFactory().create() |
| 64 | + const emitter = new Emitter(app) |
| 65 | + const db = new Database( |
| 66 | + { |
| 67 | + connection: process.env.DB || 'sqlite', |
| 68 | + connections: { |
| 69 | + sqlite: { |
| 70 | + client: 'sqlite3', |
| 71 | + connection: { |
| 72 | + filename: join(test.context.fs.basePath, 'db.sqlite3'), |
| 73 | + }, |
| 74 | + }, |
| 75 | + pg: { |
| 76 | + client: 'pg', |
| 77 | + connection: { |
| 78 | + host: process.env.PG_HOST as string, |
| 79 | + port: Number(process.env.PG_PORT), |
| 80 | + database: process.env.PG_DATABASE as string, |
| 81 | + user: process.env.PG_USER as string, |
| 82 | + password: process.env.PG_PASSWORD as string, |
| 83 | + }, |
| 84 | + }, |
| 85 | + mssql: { |
| 86 | + client: 'mssql', |
| 87 | + connection: { |
| 88 | + server: process.env.MSSQL_HOST as string, |
| 89 | + port: Number(process.env.MSSQL_PORT! as string), |
| 90 | + user: process.env.MSSQL_USER as string, |
| 91 | + password: process.env.MSSQL_PASSWORD as string, |
| 92 | + database: 'master', |
| 93 | + options: { |
| 94 | + enableArithAbort: true, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + mysql: { |
| 99 | + client: 'mysql2', |
| 100 | + connection: { |
| 101 | + host: process.env.MYSQL_HOST as string, |
| 102 | + port: Number(process.env.MYSQL_PORT), |
| 103 | + database: process.env.MYSQL_DATABASE as string, |
| 104 | + user: process.env.MYSQL_USER as string, |
| 105 | + password: process.env.MYSQL_PASSWORD as string, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + logger, |
| 111 | + emitter |
| 112 | + ) |
| 113 | + |
| 114 | + test.cleanup(async () => { |
| 115 | + db.manager.closeAll() |
| 116 | + await rm(basePath, { force: true, recursive: true, maxRetries: 3 }) |
| 117 | + }) |
| 118 | + BaseModel.useAdapter(db.modelAdapter()) |
| 119 | + return db |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Creates needed database tables |
| 124 | + */ |
| 125 | +export async function createTables(db: Database) { |
| 126 | + const test = getActiveTest() |
| 127 | + if (!test) { |
| 128 | + throw new Error('Cannot use "createTables" outside of a Japa test') |
| 129 | + } |
| 130 | + |
| 131 | + test.cleanup(async () => { |
| 132 | + await db.connection().schema.dropTable('users') |
| 133 | + await db.connection().schema.dropTable('jwt_refresh_tokens') |
| 134 | + await db.connection().schema.dropTable('remember_me_tokens') |
| 135 | + }) |
| 136 | + |
| 137 | + await db.connection().schema.createTable('jwt_refresh_tokens', (table) => { |
| 138 | + table.increments() |
| 139 | + table.integer('tokenable_id').notNullable().unsigned() |
| 140 | + table.string('type').notNullable() |
| 141 | + table.string('name').nullable() |
| 142 | + table.string('hash', 80).notNullable() |
| 143 | + table.text('abilities').notNullable() |
| 144 | + table.timestamp('created_at', { precision: 6, useTz: true }).notNullable() |
| 145 | + table.timestamp('updated_at', { precision: 6, useTz: true }).notNullable() |
| 146 | + table.timestamp('expires_at', { precision: 6, useTz: true }).nullable() |
| 147 | + table.timestamp('last_used_at', { precision: 6, useTz: true }).nullable() |
| 148 | + }) |
| 149 | + |
| 150 | + await db.connection().schema.createTable('users', (table) => { |
| 151 | + table.increments() |
| 152 | + table.string('username').unique().notNullable() |
| 153 | + table.string('email').unique().notNullable() |
| 154 | + table.string('password').nullable() |
| 155 | + }) |
| 156 | + |
| 157 | + await db.connection().schema.createTable('remember_me_tokens', (table) => { |
| 158 | + table.increments() |
| 159 | + table.integer('tokenable_id').notNullable().unsigned() |
| 160 | + table.string('hash', 80).notNullable() |
| 161 | + table.timestamp('created_at', { precision: 6, useTz: true }).notNullable() |
| 162 | + table.timestamp('updated_at', { precision: 6, useTz: true }).notNullable() |
| 163 | + table.timestamp('expires_at', { precision: 6, useTz: true }).notNullable() |
| 164 | + }) |
| 165 | +} |
0 commit comments