From 9a5ea406f946a92bfb6c78a6ec852538bb6bb657 Mon Sep 17 00:00:00 2001 From: chenos Date: Mon, 15 Nov 2021 23:31:24 +0800 Subject: [PATCH] feat(docs): update collection api doc --- docs/reference/collection.md | 242 +++++++++++++++++++++++++++++++++-- 1 file changed, 231 insertions(+), 11 deletions(-) diff --git a/docs/reference/collection.md b/docs/reference/collection.md index 5a71d32dc..d2337c819 100644 --- a/docs/reference/collection.md +++ b/docs/reference/collection.md @@ -4,14 +4,234 @@ toc: menu # Collection -## collection.constructor() -## collection.mergeOptions() -## collection.sync() -## collection.hasField() -## collection.getField() -## collection.addField() -## collection.setFields() -## collection.mergeField() -## collection.removeField() -## collection.forEachField() -## collection.findField() \ No newline at end of file +## `collection.addField()` + +添加字段,如果字段 name 已存在,直接替换。 + +##### Definition + +```ts +interface addField { + (name: string, options: FieldOptions): Field; +} +``` + +##### Examples + +```ts +const field = collection.addField('name1', { + type: 'string', +}); +await field.sync(); +``` + +## `collection.findField()` + +##### Definition + +```ts +interface findField { + (fn: (field: Field) => boolean): Field; +} +``` + +##### Examples + +```ts +const field = collection.findField((field) => { + return field.name === 'name1'; +}); +``` + +## `collection.forEachField()` + +##### Definition + +```ts +interface forEachField { + (fn: (field: Field) => void): void; +} +``` + +##### Examples + +```ts +collection.forEachField((field) => { + +}); +``` + +## `collection.getField()` + +获取字段 + +##### Definition + +```ts +interface getField { + (name: string): Field; +} +``` + +##### Examples + +```ts +const field = collection.getField('name'); +``` + +## `collection.hasField()` + +是否存在某字段 + +##### Definition + +```ts +interface hasField { + (name: string): boolean; +} +``` + +##### Examples + +```ts +collection.hasField('name'); +``` + +## `collection.updateOptions()` + +更改当前 collection 的 options + +##### Definition + +```ts +interface updateOptions { + (options: CollectionOptions): void; +} +``` + +##### Examples + +```ts +const collection = db.collection({ + name: 'tests', +}); + +collection.updateOptions({ + createdAt: true, + updatedAt: true, + sortable: true, + fields: [], +}); +``` + +## `collection.removeField()` + +移除字段 + +##### Definition + +```ts +interface removeField { + (name: string): Field; +} +``` + +##### Examples + +```ts +collection.removeField('name'); +``` + +## `collection.setFields()` + +设置 fields,会清空之前配置的 fields。 + +##### Definition + +```ts +interface setFields { + (fields: FieldOptions[]): void; +} +``` + +##### Examples + +```ts +const collection = db.collection({ + name: 'tests', +}); +collection.setFields([ + { type: 'string', name: 'name1' }, + { type: 'string', name: 'name2' }, +]); +``` + +## `collection.sync()` + +##### Definition + +```ts +interface sync { + (options?: SyncOptions): Promise; +} +``` + +##### Examples + +如果关联表不存在,不处理 + +```ts +const Post = db.collection({ + name: 'posts', + fields: [ + { type: 'string', name: 'name' }, + // tags 跳过 + { type: 'belongsToMany', name: 'tags' }, + ], +}); +await Post.sync(); +``` + +如果关系表存在,一起处理 + +```ts +const Post = db.collection({ + name: 'posts', + fields: [ + { type: 'string', name: 'name' }, + { type: 'belongsToMany', name: 'tags' }, + ], +}); +const Tag = db.collection({ + name: 'tags', + fields: [{ type: 'string', name: 'name' }], +}); +await Post.sync(); +// 等同于 +await db.sequelize.models.posts.sync(); +await db.sequelize.models.tags.sync(); +await db.sequelize.models.posts_tags.sync(); +``` + +## `collection.updateField()` + +更新字段配置 + +##### Definition + +```ts +interface updateField { + (name: string, options: FieldOptions): Field; +} +``` + +##### Examples + +更改 name + +```ts +const field = collection.updateField('name1', { + name: 'name2', +}); +await field.sync(); +```