Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(minato): impl model.immutable and driver readOnly #119

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
14 changes: 8 additions & 6 deletions packages/core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ export class Database<S = {}, N = {}, C extends Context = Context> extends Servi
static readonly migrate = Symbol('minato.migrate')

public tables: Dict<Model> = Object.create(null)
public drivers: Driver<any, C>[] = []
public drivers: Driver<Driver.Config, C>[] = []
public types: Dict<Field.Transform> = Object.create(null)

private _driver: Driver<any, C> | undefined
private _driver: Driver<Driver.Config, C> | undefined
private stashed = new Set<string>()
private prepareTasks: Dict<Promise<void>> = Object.create(null)
public migrateTasks: Dict<Promise<void>> = Object.create(null)

async connect<T = undefined>(driver: Driver.Constructor<T>, ...args: Spread<T>) {
async connect<T extends Driver.Config = Driver.Config>(driver: Driver.Constructor<T>, ...args: Spread<T>) {
this.ctx.plugin(driver, args[0] as any)
await this.ctx.start()
}
Expand All @@ -109,7 +109,7 @@ export class Database<S = {}, N = {}, C extends Context = Context> extends Servi
await Promise.all(Object.values(this.prepareTasks))
}

private getDriver(table: string | Selection): Driver<any, C> {
private getDriver(table: string | Selection): Driver<Driver.Config, C> {
if (Selection.is(table)) return table.driver as any
const model: Model = this.tables[table]
if (!model) throw new Error(`cannot resolve table "${table}"`)
Expand Down Expand Up @@ -602,12 +602,14 @@ export class Database<S = {}, N = {}, C extends Context = Context> extends Servi

async drop<K extends Keys<S>>(table: K) {
if (this[Database.transact]) throw new Error('cannot drop table in transaction')
await this.getDriver(table).drop(table)
const driver = this.getDriver(table)
if (driver.config.readonly) throw new Error('cannot drop table in read-only mode')
await driver.drop(table)
}

async dropAll() {
if (this[Database.transact]) throw new Error('cannot drop table in transaction')
await Promise.all(Object.values(this.drivers).map(driver => driver.dropAll()))
await Promise.all(Object.values(this.drivers).filter(driver => !driver.config.readonly).map(driver => driver.dropAll()))
}

async stats() {
Expand Down
28 changes: 23 additions & 5 deletions packages/core/src/driver.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Awaitable, deepEqual, defineProperty, Dict, mapValues, remove } from 'cosmokit'
import { Context, Logger, Service } from 'cordis'
import { Context, Logger, Service, z } from 'cordis'
import { Eval, Update } from './eval.ts'
import { Direction, Modifier, Selection } from './selection.ts'
import { Field, Model, Relation } from './model.ts'
import { Database } from './database.ts'
import { Type } from './type.ts'
import { FlatKeys, Keys, Values } from './utils.ts'
import enUS from './locales/en-US.yml'
import zhCN from './locales/zh-CN.yml'

export namespace Driver {
export interface Stats {
Expand Down Expand Up @@ -52,10 +54,10 @@ export namespace Driver {
}

export namespace Driver {
export type Constructor<T> = new (ctx: Context, config: T) => Driver<T>
export type Constructor<T extends Driver.Config> = new (ctx: Context, config: T) => Driver<T>
}

export abstract class Driver<T = any, C extends Context = Context> {
export abstract class Driver<T extends Driver.Config = Driver.Config, C extends Context = Context> {
static inject = ['model']

abstract start(): Promise<void>
Expand Down Expand Up @@ -165,20 +167,36 @@ export abstract class Driver<T = any, C extends Context = Context> {
async _ensureSession() {}

async prepareIndexes(table: string) {
const oldIndexes = await this.getIndexes(table)
const { indexes } = this.model(table)
if (this.config.migrateStrategy === 'never' || this.config.readonly) return
const oldIndexes = await this.getIndexes(table)
for (const index of indexes) {
const oldIndex = oldIndexes.find(info => info.name === index.name)
if (!oldIndex) {
await this.createIndex(table, index)
} else if (!deepEqual(oldIndex, index)) {
} else if (this.config.migrateStrategy === 'auto' && !deepEqual(oldIndex, index)) {
await this.dropIndex(table, index.name!)
await this.createIndex(table, index)
}
}
}
}

export namespace Driver {
export interface Config {
readonly?: boolean
migrateStrategy?: 'auto' | 'create' | 'never'
}

export const Config: z<Config> = z.object({
readonly: z.boolean().default(false),
migrateStrategy: z.union([z.const('auto'), z.const('create'), z.const('never')]).default('auto'),
}).i18n({
'en-US': enUS,
'zh-CN': zhCN,
})
}

export interface MigrationHooks {
before: (keys: string[]) => boolean
after: (keys: string[]) => void
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/locales/en-US.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$description: Access settings
readonly: Connect in read-only mode.
migrateStrategy: Table migration strategy.
3 changes: 3 additions & 0 deletions packages/core/src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$description: 访问设置
readonly: 以只读模式连接。
migrateStrategy: 表迁移策略。
3 changes: 3 additions & 0 deletions packages/core/src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ class Executable<S = any, T = any> {
}

async execute(): Promise<T> {
if (this.driver.config.readonly && !['get', 'eval'].includes(this.type)) {
throw new Error(`database is in read-only mode`)
}
await this.driver.database.prepared()
await this.driver._ensureSession()
return this.driver[this.type as any](this, ...this.args)
Expand Down
52 changes: 31 additions & 21 deletions packages/mongo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@

/** synchronize table schema */
async prepare(table: string) {
if (this.config.migrateStrategy === 'never' || this.config.readonly) {
if (this.config.migrateStrategy === 'never' && this.shouldEnsurePrimary(table)) {
throw new Error(`immutable table ${table} cannot be autoInc`)
}
return
}

Check warning on line 237 in packages/mongo/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/mongo/src/index.ts#L233-L237

Added lines #L233 - L237 were not covered by tests

await Promise.all([
this._createInternalTable(),
this.db.createCollection(table).catch(noop),
Expand Down Expand Up @@ -536,7 +543,7 @@
}

export namespace MongoDriver {
export interface Config extends MongoClientOptions {
export interface Config extends Driver.Config, MongoClientOptions {
username?: string
password?: string
protocol?: string
Expand All @@ -556,27 +563,30 @@
optimizeIndex?: boolean
}

export const Config: z<Config> = z.object({
protocol: z.string().default('mongodb'),
host: z.string().default('localhost'),
port: z.natural().max(65535),
username: z.string(),
password: z.string().role('secret'),
database: z.string().required(),
authDatabase: z.string(),
writeConcern: z.object({
w: z.union([
z.const(undefined),
z.number().required(),
z.const('majority').required(),
]),
wtimeoutMS: z.number(),
journal: z.boolean(),
export const Config: z<Config> = z.intersect([
z.object({
protocol: z.string().default('mongodb'),
host: z.string().default('localhost'),
port: z.natural().max(65535),
username: z.string(),
password: z.string().role('secret'),
database: z.string().required(),
authDatabase: z.string(),
writeConcern: z.object({
w: z.union([
z.const(undefined),
z.number().required(),
z.const('majority').required(),
]),
wtimeoutMS: z.number(),
journal: z.boolean(),
}) as any,
}).i18n({
'en-US': enUS,
'zh-CN': zhCN,
}),
}).i18n({
'en-US': enUS,
'zh-CN': zhCN,
})
Driver.Config,
])
}

export default MongoDriver
3 changes: 3 additions & 0 deletions packages/mongo/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ describe('@minatojs/driver-mongo', () => {
aggregateNull: false,
}
},
migration: {
definition: false,
},
transaction: {
abort: false
}
Expand Down
23 changes: 16 additions & 7 deletions packages/mysql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,20 @@
}

if (!columns.length) {
if (this.config.readonly || this.config.migrateStrategy === 'never') {
throw new Error(`immutable table ${name} cannot be created`)
}

Check warning on line 238 in packages/mysql/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/mysql/src/index.ts#L237-L238

Added lines #L237 - L238 were not covered by tests
this.logger.info('auto creating table %c', name)
return this.query(`CREATE TABLE ${escapeId(name)} (${create.join(', ')}) COLLATE = ${this.sql.escape(this.config.charset ?? 'utf8mb4_general_ci')}`)
}

if (this.config.readonly || this.config.migrateStrategy !== 'auto') {
if (create.length || update.length) {
throw new Error(`immutable table ${name} cannot be migrated`)
}
return
}

const operations = [
...create.map(def => 'ADD ' + def),
...update,
Expand Down Expand Up @@ -593,7 +603,7 @@
}

export namespace MySQLDriver {
export interface Config extends PoolConfig {}
export interface Config extends Driver.Config, PoolConfig {}

export const Config: z<Config> = z.intersect([
z.object({
Expand All @@ -602,8 +612,6 @@
user: z.string().default('root'),
password: z.string().role('secret'),
database: z.string().required(),
}),
z.object({
ssl: z.union([
z.const(undefined),
z.object({
Expand All @@ -630,11 +638,12 @@
sessionTimeout: z.number(),
}),
]) as any,
}).i18n({
'en-US': enUS,
'zh-CN': zhCN,
}),
]).i18n({
'en-US': enUS,
'zh-CN': zhCN,
})
Driver.Config,
])
}

export default MySQLDriver
35 changes: 24 additions & 11 deletions packages/postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,20 @@
}

if (!columns.length) {
if (this.config.readonly || this.config.migrateStrategy === 'never') {
throw new Error(`immutable table ${name} cannot be created`)
}

Check warning on line 242 in packages/postgres/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/postgres/src/index.ts#L241-L242

Added lines #L241 - L242 were not covered by tests
this.logger.info('auto creating table %c', name)
return this.query<any>(`CREATE TABLE ${escapeId(name)} (${create.join(', ')}, _pg_mtime BIGINT)`)
}

if (this.config.readonly || this.config.migrateStrategy !== 'auto') {
if (create.length || update.length) {
throw new Error(`immutable table ${name} cannot be migrated`)
}
return
}

const operations = [
...create.map(def => 'ADD ' + def),
...update,
Expand Down Expand Up @@ -522,24 +532,27 @@
}

export namespace PostgresDriver {
export interface Config<T extends Record<string, postgres.PostgresType> = {}> extends postgres.Options<T> {
export interface Config<T extends Record<string, postgres.PostgresType> = {}> extends Driver.Config, postgres.Options<T> {
host: string
port: number
user: string
password: string
database: string
}

export const Config: z<Config> = z.object({
host: z.string().default('localhost'),
port: z.natural().max(65535).default(5432),
user: z.string().default('root'),
password: z.string().role('secret'),
database: z.string().required(),
}).i18n({
'en-US': enUS,
'zh-CN': zhCN,
})
export const Config: z<Config> = z.intersect([
z.object({
host: z.string().default('localhost'),
port: z.natural().max(65535).default(5432),
user: z.string().default('root'),
password: z.string().role('secret'),
database: z.string().required(),
}).i18n({
'en-US': enUS,
'zh-CN': zhCN,
}),
Driver.Config,
])
}

export default PostgresDriver
26 changes: 19 additions & 7 deletions packages/sqlite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,18 @@
}))
}

if (this.config.readonly || this.config.migrateStrategy === 'never') {
if (!columns.length || shouldMigrate || alter.length) {
throw new Error(`immutable table ${table} cannot be migrated`)
}
return
}

if (!columns.length) {
this.logger.info('auto creating table %c', table)
this._run(`CREATE TABLE ${escapeId(table)} (${[...columnDefs, ...indexDefs].join(', ')})`)
} else if (this.config.migrateStrategy === 'create') {
throw new Error(`immutable table ${table} cannot be migrated`)

Check warning on line 126 in packages/sqlite/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/sqlite/src/index.ts#L126

Added line #L126 was not covered by tests
} else if (shouldMigrate) {
// preserve old columns
for (const { name, type, notnull, pk, dflt_value: value } of columns) {
Expand Down Expand Up @@ -482,16 +491,19 @@
}

export namespace SQLiteDriver {
export interface Config {
export interface Config extends Driver.Config {
path: string
}

export const Config: z<Config> = z.object({
path: z.string().role('path').required(),
}).i18n({
'en-US': enUS,
'zh-CN': zhCN,
})
export const Config: z<Config> = z.intersect([
z.object({
path: z.string().role('path').required(),
}).i18n({
'en-US': enUS,
'zh-CN': zhCN,
}),
Driver.Config,
])
}

export default SQLiteDriver
Loading