diff --git a/docs/reference/application.md b/docs/reference/application.md index 0be033b64..cf6b2131d 100644 --- a/docs/reference/application.md +++ b/docs/reference/application.md @@ -1,3 +1,7 @@ +--- +toc: menu +--- + # Application ## `app.db` diff --git a/docs/reference/database.md b/docs/reference/database.md index 7f060b035..8fb48b3e7 100644 --- a/docs/reference/database.md +++ b/docs/reference/database.md @@ -24,47 +24,22 @@ class Database { db.sequelize.close(); ``` -## `db.constructor()` +## `db.close()` + +断开数据库连接 ##### Definition ```ts class Database { - constructor (options: DatabaseOptions) => void; + close(): Promise; } - -type DatabaseOptions = Sequelize.Options | Sequelize; ``` ##### Examples -配置 options 与 Sequelize.Options 一致,如: - ```ts -const db = new Database({ - dialect: 'sqlite', - storage: 'path/to/database.sqlite' -}); -``` - -也可以直接传 sequelize 实例 - -```ts -const sequelize = new Sequelize('sqlite::memory:'); -const db = new Database(sequelize); -``` - -注意,new Sequelize() 会有针对参数的 try catch 处理,Database 如果要捕获异常,也需要在 new 的时候 try catch,如: - -```ts -try { - const db = new Database({ - dialect: 'sqlite', - storage: 'path/to/database.sqlite' - }); -} catch(error) { - -} +await db.close(); ``` ## `db.collection()` @@ -110,78 +85,99 @@ const Post = db.collection({ 自定义 repository 见 [db.registerRepositories()](#dbregisterrepositories) -## `db.sync()` - -将所有定义的 Collections 同步给数据库。 +## `db.constructor()` ##### Definition ```ts class Database { - sync(options?: Sequelize.SyncOptions): Promise; + constructor (options: DatabaseOptions) => void; } + +type DatabaseOptions = Sequelize.Options | Sequelize; ``` ##### Examples -```ts -await db.sync(); -await db.sync({force: true}); -``` - -## `db.close()` - -断开数据库连接 - -##### Definition +配置 options 与 Sequelize.Options 一致,如: ```ts -class Database { - close(): Promise; -} -``` - -##### Examples - -```ts -await db.close(); -``` - -## `db.registerFieldTypes()` - -自定义字段存储类型,更多字段类型查看 [Field Types](field-types) - -##### Definition - -```ts -class Database { - registerFieldTypes(types: RegisterFieldTypes): void; -} - -interface RegisterFieldTypes { - [key: string]: Field; -} -``` - -##### Examples - -```ts -class CustomField extends Field { - get dataType() { - return DataTypes.STRING; - } -} - -db.registerFieldTypes({ custom: CustomField }); - -db.collection({ - name: 'tests', - fields: [ - { type: 'custom', name: 'customName' }, - ], +const db = new Database({ + dialect: 'sqlite', + storage: 'path/to/database.sqlite' }); ``` +也可以直接传 sequelize 实例 + +```ts +const sequelize = new Sequelize('sqlite::memory:'); +const db = new Database(sequelize); +``` + +注意,new Sequelize() 会有针对参数的 try catch 处理,Database 如果要捕获异常,也需要在 new 的时候 try catch,如: + +```ts +try { + const db = new Database({ + dialect: 'sqlite', + storage: 'path/to/database.sqlite' + }); +} catch(error) { + +} +``` + +## `db.emit()` + +同步事件触发 + +##### Definition + +##### Examples + +## `db.emitAsync()` + +异步事件触发 + +##### Definition + +##### Examples + +## `db.getCollection()` + +##### Definition + +```ts +class Database { + getCollection(name: string): Collection; +} +``` + +##### Examples + +```ts +const collection = db.getCollection('tests'); +``` + +## `db.hasCollection()` + +##### Definition + +```ts +class Database { + hasCollection(name: string): boolean; +} +``` + +##### Examples + +```ts +if (db.hasCollection('tests')) { + +} +``` + ## `db.import()` ##### Definition @@ -217,22 +213,41 @@ db.import({ ##### Examples -## `db.emitAsync()` +## `db.registerFieldTypes()` -异步事件触发 +自定义字段存储类型,更多字段类型查看 [Field Types](field-types) ##### Definition -##### Examples +```ts +class Database { + registerFieldTypes(types: RegisterFieldTypes): void; +} -## `db.emit()` - -同步事件触发 - -##### Definition +interface RegisterFieldTypes { + [key: string]: Field; +} +``` ##### Examples +```ts +class CustomField extends Field { + get dataType() { + return DataTypes.STRING; + } +} + +db.registerFieldTypes({ custom: CustomField }); + +db.collection({ + name: 'tests', + fields: [ + { type: 'custom', name: 'customName' }, + ], +}); +``` + ## `db.registerModels()` 自定义 Model @@ -271,31 +286,6 @@ const test = Test.model.create(); test.customMethod(); ``` -## `db.registerRepositories()` - -自定义 Repository - -##### Examples - -```ts -class CustomRepository extends Repository { - customMethod() { - console.log('custom method'); - } -} - -db.registerModels({ - CustomRepository, -}); - -const Test = db.collection({ - name: 'tests', - repository: 'CustomRepository', -}); - -Test.repository.customMethod(); -``` - ## `db.registerOperators()` 自定义筛选条件 @@ -369,3 +359,65 @@ repository.find({ }, }); ``` + +## `db.registerRepositories()` + +自定义 Repository + +##### Examples + +```ts +class CustomRepository extends Repository { + customMethod() { + console.log('custom method'); + } +} + +db.registerModels({ + CustomRepository, +}); + +const Test = db.collection({ + name: 'tests', + repository: 'CustomRepository', +}); + +Test.repository.customMethod(); +``` + +## `db.removeCollection()` + +移除 collection + +##### Definition + +```ts +class Database { + removeCollection(name: string): Collection; +} +``` + +##### Examples + +```ts +db.removeCollection('tests'); +``` + +## `db.sync()` + +将所有定义的 Collections 同步给数据库。 + +##### Definition + +```ts +class Database { + sync(options?: Sequelize.SyncOptions): Promise; +} +``` + +##### Examples + +```ts +await db.sync(); +await db.sync({force: true}); +``` diff --git a/docs/reference/operators.md b/docs/reference/filter-operators.md similarity index 90% rename from docs/reference/operators.md rename to docs/reference/filter-operators.md index 02faac1ca..e424252e8 100644 --- a/docs/reference/operators.md +++ b/docs/reference/filter-operators.md @@ -2,7 +2,7 @@ toc: menu --- -# Operators 待完善 +# Filter Operators 待完善 ## string diff --git a/docs/reference/overview.md b/docs/reference/overview.md index 66a08e49f..d746d9e1f 100644 --- a/docs/reference/overview.md +++ b/docs/reference/overview.md @@ -16,7 +16,7 @@ order: 0 - [Collection](collection.md) - [Model](model.md) - [Field Types](field-types.md) -- [Operators](operators.md) +- [Filter Operators](filter-operators.md) ## Application diff --git a/docs/reference/repository.md b/docs/reference/repository.md index 28a213b69..46a28ae97 100644 --- a/docs/reference/repository.md +++ b/docs/reference/repository.md @@ -105,7 +105,7 @@ await Post.repository.find({ }); ``` -支持多种 Operators,以 `$` 开头。[更多内容,查阅 Operators 章节](operators.md) +支持多种 Operators,以 `$` 开头。[更多内容,查阅 Operators 章节](filter-operators.md) ```ts await Post.repository.find({