feat(docs): update doc

This commit is contained in:
chenos 2021-11-24 15:46:26 +08:00
parent a0b374cea5
commit 126a7e1f92
5 changed files with 179 additions and 123 deletions

View File

@ -1,3 +1,7 @@
---
toc: menu
---
# Application
## `app.db`

View File

@ -24,47 +24,22 @@ class Database {
db.sequelize.close();
```
## `db.constructor()`
## `db.close()`
断开数据库连接
##### Definition
```ts
class Database {
constructor (options: DatabaseOptions) => void;
close(): Promise<void>;
}
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<Database>;
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<void>;
}
```
##### 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<CustomModel>.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<CustomRepository>.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<CustomRepository>.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<Database>;
}
```
##### Examples
```ts
await db.sync();
await db.sync({force: true});
```

View File

@ -2,7 +2,7 @@
toc: menu
---
# Operators <Badge>待完善</Badge>
# Filter Operators <Badge>待完善</Badge>
## string

View File

@ -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

View File

@ -105,7 +105,7 @@ await Post.repository.find({
});
```
支持多种 Operators`$` 开头。[更多内容,查阅 Operators 章节](operators.md)
支持多种 Operators`$` 开头。[更多内容,查阅 Operators 章节](filter-operators.md)
```ts
await Post.repository.find({