@yongdall/organization 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.mts CHANGED
@@ -8,7 +8,7 @@ declare const OrganizationType: _yongdall_model0.ModelTable<{
8
8
  description: imodel10.FieldDefine<"string", false, true>;
9
9
  createdAt: imodel10.FieldDefine<"timestamp", false, false>;
10
10
  updatedAt: imodel10.FieldDefine<"timestamp", false, false>;
11
- }, "organizationType">;
11
+ }, string>;
12
12
  //#endregion
13
13
  //#region plugins/organization/models/Organization.d.mts
14
14
  declare const Organization: _yongdall_model0.ModelTable<{
@@ -23,6 +23,6 @@ declare const Organization: _yongdall_model0.ModelTable<{
23
23
  rgt: imodel10.FieldDefine<"i32", false, false>;
24
24
  createdAt: imodel10.FieldDefine<"timestamp", false, false>;
25
25
  updatedAt: imodel10.FieldDefine<"timestamp", false, false>;
26
- }, "organization">;
26
+ }, string>;
27
27
  //#endregion
28
28
  export { Organization, OrganizationType };
package/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { n as OrganizationType, t as Organization } from "./organization-CEkzOuGJ.mjs";
1
+ import { n as OrganizationType, t as Organization } from "./organization-BxaC1aum.mjs";
2
2
 
3
3
  export { Organization, OrganizationType };
@@ -1,4 +1,4 @@
1
- import { Query, Where, createField, createModel, mergeHooks, now, uuid } from "@yongdall/model";
1
+ import { Query, Where, actions, createField, createModel, mergeHooks, now, uuid } from "@yongdall/model";
2
2
  import "@yongdall/connection";
3
3
  import { ServerError, nestedSetTree } from "@yongdall/core";
4
4
 
@@ -276,7 +276,7 @@ const Organization = createModel("organization", {
276
276
  if (!document.internal) {
277
277
  const parentId = document.parentId;
278
278
  if (!parentId) return;
279
- const parent = await conn.first(new Query(Model).where("id", parentId));
279
+ const parent = await conn.call(actions.first(new Query(Model).where("id", parentId)));
280
280
  if (!parent) {
281
281
  delete document.parentId;
282
282
  return;
@@ -285,7 +285,7 @@ const Organization = createModel("organization", {
285
285
  return;
286
286
  }
287
287
  const parentId = document.parentId;
288
- const parent = parentId ? await conn.first(new Query(Model).where("id", parentId)) : null;
288
+ const parent = parentId ? await conn.call(actions.first(new Query(Model).where("id", parentId))) : null;
289
289
  if (!parent) throw new ServerError("请选择上级组织");
290
290
  document.internal = true;
291
291
  document.mainId = parent.internal && parent.mainId || parent.id;
@@ -293,7 +293,7 @@ const Organization = createModel("organization", {
293
293
  async afterCreate(conn, Model, document) {
294
294
  if (document.mainId) return;
295
295
  const id = document.id;
296
- if (!await conn.update(Model, { mainId: id }, Where.and("id", id), true)) return;
296
+ if (!await conn.call(actions.update(Model, { mainId: id }, Where.and("id", id), true))) return;
297
297
  document.mainId = id;
298
298
  },
299
299
  async beforeUpdate(conn, Model, document, set) {
@@ -309,10 +309,10 @@ const Organization = createModel("organization", {
309
309
  return;
310
310
  }
311
311
  if (!document.internal) {
312
- if (await conn.count(new Query(Model).where("id", parentId).where("internal", false))) return;
312
+ if (await conn.call(actions.count(new Query(Model).where("id", parentId).where("internal", false)))) return;
313
313
  throw new ServerError("主体组织,只能作为其他主体组织的下级组织");
314
314
  }
315
- if (await conn.count(new Query(Model).where("id", parentId).where("mainId", document.mainId))) return;
315
+ if (await conn.call(actions.count(new Query(Model).where("id", parentId).where("mainId", document.mainId)))) return;
316
316
  throw new ServerError("非主体组织,只能调整到同一主体组织下");
317
317
  }
318
318
  })
@@ -320,4 +320,4 @@ const Organization = createModel("organization", {
320
320
 
321
321
  //#endregion
322
322
  export { OrganizationType as n, Organization as t };
323
- //# sourceMappingURL=organization-CEkzOuGJ.mjs.map
323
+ //# sourceMappingURL=organization-BxaC1aum.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"organization-BxaC1aum.mjs","names":[],"sources":["../../plugins/organization/models/OrganizationType.mjs","../../plugins/organization/models/Organization.mjs"],"sourcesContent":["import { createField, createModel, uuid, now } from '@yongdall/model';\n\nexport const OrganizationType = createModel('organizationType', {\n\tid: createField('string', { nullable: false, label: '标识', primary: 1 }),\n\tlabel: createField('string', { nullable: false, default: '', label: '名称' }),\n\tdescription: createField('string', { nullable: true, default: '', label: '描述' }),\n\tcreatedAt: createField('timestamp', { nullable: false, creating: now, label: '创建日期' }),\n\tupdatedAt: createField('timestamp', { nullable: false, updating: now, label: '最后更新日期' }),\n}, {\n\tlabel: '组织类型',\n\tlabelField: 'label',\n\tpermissions: [\n\t\t{permission: 'system:organization', fields: '*', authorizations: ['query', 'options', 'read', 'create', 'update', 'destroy', 'add', 'remove'] },\n\t\t{permission: 'organization', fields: '*', authorizations: ['query', 'options', 'read'] },\n\t],\n});\n","import { createField, createModel, uuid, now, Query, mergeHooks, Where, actions } from '@yongdall/model';\nimport { useConnection } from '@yongdall/connection';\nimport { nestedSetTree, ServerError } from '@yongdall/core';\n\n// 定义模型\nexport const Organization = createModel('organization', {\n\tid: createField('uuid', { nullable: false, default: uuid, primary: 1, uncreatable: true, layout: { hidden: true } }),\n\tlabel: createField('string', { size: 255, nullable: false, default: '', label: '名称', layout: { colSpan: 6 } }),\n\tparentId: createField('uuid', { nullable: true, label: '上级组织', model: 'organization', renderer: 'document', layout: { colSpan: 6 } }),\n\ttypeId: createField('string', { size: 255, array: true, nullable: false, label: '类型', model: 'organizationType',source: {value: 'id'}, renderer: 'select', default: [], layout: { colSpan: 6 } }),\n\tinternal: createField('bool', { nullable: false, default: false, label: '是否为子组织', layout: { colSpan: 6 } }),\n\tmainId: createField('uuid', { nullable: true, label: '所属主体组织', model: 'organization', renderer: 'document', layout: { colSpan: 6 } }),\n\tdescription: createField('text', { nullable: true, label: '描述' }),\n\n\t// Nested Set 树结构字段,通常由 hook 自动维护,但需定义以便存储\n\tlft: createField('i32', { nullable: false, default: 0, label: 'LFT', layout: { hidden: true } }),\n\trgt: createField('i32', { nullable: false, default: 0, label: 'RGT', layout: { hidden: true } }),\n\n\tcreatedAt: createField('timestamp', { nullable: false, creating: now, label: '创建日期', layout: { hidden: true } }),\n\tupdatedAt: createField('timestamp', { nullable: false, updating: now, label: '最后更新日期', layout: { hidden: true } }),\n}, {\n\tlabel: '组织',\n\tlabelField: 'label',\n\tpermissions: [\n\t\t{ permission: 'system:organization', fields: ['id', 'label', 'parentId', 'typeId', 'internal', 'description', 'createdAt', 'updatedAt'], authorizations: ['options', 'query', 'read', 'create', 'update', 'destroy', 'add', 'remove'] },\n\t\t{ permission: 'system:organization', fields: ['mainId', 'lft'], authorizations: ['options', 'query', 'read'] },\n\n\t\t{ permission: 'organization', fields: ['id', 'label', 'parentId', 'typeId', 'internal', 'description', 'createdAt', 'updatedAt'], organizationField: 'mainId', authorizations: ['options', 'query', 'read', 'create', 'update', 'destroy', 'add', 'remove'] },\n\t\t{ permission: '', fields: ['mainId', 'lft'], organizationField: 'mainId', authorizations: ['options', 'query', 'read'] },\n\t\t{ permission: 'organization', fields: ['id', 'label', 'parentId', 'typeId', 'internal', 'description', 'createdAt', 'updatedAt'], organizationField: 'parentId', authorizations: ['options', 'query', 'read', 'create', 'update', 'destroy', 'add', 'remove'] },\n\t\t{ permission: '', fields: ['mainId', 'lft'], organizationField: 'parentId', authorizations: ['options', 'query', 'read'] },\n\t\t{ permission: 'organization', fields: ['id', 'label', 'parentId', 'typeId', 'internal', 'description', 'createdAt', 'updatedAt'], organizationField: 'id', authorizations: ['options', 'query', 'read', 'create', 'update', 'add', 'remove'] },\n\t\t{ permission: '', fields: ['mainId', 'lft'], organizationField: 'id', authorizations: ['options', 'query', 'read'] },\n\t],\n\thooks: mergeHooks(nestedSetTree('parentId'), {\n\n\t\tasync beforeCreate(conn, Model, document) {\n\t\t\tdelete document.mainId;\n\n\t\t\tif (!document.internal) {\n\t\t\t\tconst parentId = document.parentId;\n\t\t\t\tif (!parentId) { return; }\n\t\t\t\tconst parent = await conn.call(actions.first(new Query(Model).where('id', parentId)));\n\t\t\t\tif (!parent) {\n\t\t\t\t\tdelete document.parentId;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (parent.internal) {\n\t\t\t\t\tthrow new ServerError('主体组织,只能作为其他主体组织的下级组织');\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst parentId = document.parentId;\n\t\t\tconst parent = parentId\n\t\t\t\t? await conn.call(actions.first(new Query(Model).where('id', parentId)))\n\t\t\t\t: null;\n\n\t\t\tif (!parent) {\n\t\t\t\tthrow new ServerError('请选择上级组织');\n\t\t\t}\n\n\t\t\tdocument.internal = true;\n\t\t\tdocument.mainId = (parent.internal && parent.mainId) || parent.id;\n\t\t},\n\t\tasync afterCreate(conn, Model, document) {\n\t\t\tif (document.mainId) { return; }\n\t\t\tconst id = document.id;\n\t\t\tif (!await conn.call(actions.update(Model, { mainId: id }, Where.and('id', id), true))) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tdocument.mainId = id;\n\t\t},\n\n\t\tasync beforeUpdate(conn, Model, document, set) {\n\t\t\tdelete set.mainId;\n\n\t\t\tconst parentId = set.parentId;\n\t\t\t// 检查 parentId 是否真的改变\n\t\t\tif ((typeof parentId !== 'number' && typeof parentId !== 'string') || parentId === document.parentId) {\n\t\t\t\tdelete set.parentId;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (!parentId) {\n\t\t\t\tif (document.internal) {\n\t\t\t\t\tdelete set.parentId;\n\t\t\t\t} else {\n\t\t\t\t\tset.parentId = null;\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (!document.internal) {\n\t\t\t\tconst parent = await conn.call(actions.count(\n\t\t\t\t\tnew Query(Model)\n\t\t\t\t\t\t.where('id', parentId)\n\t\t\t\t\t\t.where('internal', false)\n\t\t\t\t));\n\t\t\t\tif (parent) { return; }\n\t\t\t\tthrow new ServerError('主体组织,只能作为其他主体组织的下级组织');\n\t\t\t}\n\n\t\t\tconst parent = await conn.call(actions.count(\n\t\t\t\tnew Query(Model)\n\t\t\t\t.where('id', parentId)\n\t\t\t\t.where('mainId', document.mainId)\n\t\t\t));\n\t\t\t\tif (parent) { return; }\n\t\t\tthrow new ServerError('非主体组织,只能调整到同一主体组织下');\n\n\n\t\t}\n\t}),\n});\n\n// 静态方法转换为独立导出函数\n// 使用时需传入 connection 或直接使用模型引用\n/**\n * \n * @param {string?} id \n * @returns \n */\nexport async function getAncestors(id) {\n\tif (!id) return [];\n\tconst conn = useConnection('rdb');\n\tconst o = await conn.call(actions.first(\n\t\tnew Query(Organization).select('lft', 'rgt').where('id', id)\n\t));\n\tif (!o) { return []; }\n\n\tconst { lft, rgt } = o;\n\tconst list = await conn.call(actions.select(\n\t\tnew Query(Organization).select('id').where('lft', '<=', lft).where('rgt', '>=', rgt)\n\t));\n\treturn list.map(v => v.id);\n}\n\n/**\n * \n * @param {string?} id \n * @returns \n */\nexport async function getPosterity(id) {\n\tif (!id) return [];\n\tconst conn = useConnection('rdb');\n\tconst o = await conn.call(actions.first(new Query(Organization).select('lft', 'rgt').where('id', id)));\n\tif (!o) { return []; }\n\n\tconst { lft, rgt } = o;\n\tconst list = await conn.call(actions.select(\n\t\tnew Query(Organization).select('id').where('lft', '>=', lft).where('rgt', '<=', rgt)\n\t));\n\treturn list.map(v => v.id);\n}\n\nexport default Organization;\n"],"mappings":";;;;;AAEA,MAAa,mBAAmB,YAAY,oBAAoB;CAC/D,IAAI,YAAY,UAAU;EAAE,UAAU;EAAO,OAAO;EAAM,SAAS;EAAG,CAAC;CACvE,OAAO,YAAY,UAAU;EAAE,UAAU;EAAO,SAAS;EAAI,OAAO;EAAM,CAAC;CAC3E,aAAa,YAAY,UAAU;EAAE,UAAU;EAAM,SAAS;EAAI,OAAO;EAAM,CAAC;CAChF,WAAW,YAAY,aAAa;EAAE,UAAU;EAAO,UAAU;EAAK,OAAO;EAAQ,CAAC;CACtF,WAAW,YAAY,aAAa;EAAE,UAAU;EAAO,UAAU;EAAK,OAAO;EAAU,CAAC;CACxF,EAAE;CACF,OAAO;CACP,YAAY;CACZ,aAAa,CACZ;EAAC,YAAY;EAAuB,QAAQ;EAAK,gBAAgB;GAAC;G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,QAAO,IAAI;QAEX,KAAI,WAAW;AAEhB;;AAGD,OAAI,CAAC,SAAS,UAAU;AAMvB,QALe,MAAM,KAAK,KAAK,QAAQ,MACtC,IAAI,MAAM,MAAM,CACd,MAAM,MAAM,SAAS,CACrB,MAAM,YAAY,MAAM,CAC1B,CAAC,CACY;AACd,UAAM,IAAI,YAAY,uBAAuB;;AAQ7C,OALc,MAAM,KAAK,KAAK,QAAQ,MACtC,IAAI,MAAM,MAAM,CACf,MAAM,MAAM,SAAS,CACrB,MAAM,UAAU,SAAS,OAAO,CACjC,CAAC,CACa;AACf,SAAM,IAAI,YAAY,qBAAqB;;EAI5C,CAAC;CACF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yongdall/organization",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "main": "./index.mjs",
6
6
  "exports": {
@@ -1,4 +1,4 @@
1
- import { n as OrganizationType, t as Organization } from "../organization-CEkzOuGJ.mjs";
1
+ import { n as OrganizationType, t as Organization } from "../organization-BxaC1aum.mjs";
2
2
 
3
3
  //#region plugins/organization/yongdall/migration.mjs
4
4
  /** @import { Profile } from '@yongdall/migrate' */
@@ -1,4 +1,4 @@
1
- import { n as OrganizationType, t as Organization } from "../organization-CEkzOuGJ.mjs";
1
+ import { n as OrganizationType, t as Organization } from "../organization-BxaC1aum.mjs";
2
2
 
3
3
  //#region plugins/organization/yongdall/model.mjs
4
4
  /** @import { ModelProfile } from '@yongdall/core' */
@@ -1 +0,0 @@
1
- {"version":3,"file":"organization-CEkzOuGJ.mjs","names":[],"sources":["../../plugins/organization/models/OrganizationType.mjs","../../plugins/organization/models/Organization.mjs"],"sourcesContent":["import { createField, createModel, uuid, now } from '@yongdall/model';\n\nexport const OrganizationType = createModel('organizationType', {\n\tid: createField('string', { nullable: false, label: '标识', primary: 1 }),\n\tlabel: createField('string', { nullable: false, default: '', label: '名称' }),\n\tdescription: createField('string', { nullable: true, default: '', label: '描述' }),\n\tcreatedAt: createField('timestamp', { nullable: false, creating: now, label: '创建日期' }),\n\tupdatedAt: createField('timestamp', { nullable: false, updating: now, label: '最后更新日期' }),\n}, {\n\tlabel: '组织类型',\n\tlabelField: 'label',\n\tpermissions: [\n\t\t{permission: 'system:organization', fields: '*', authorizations: ['query', 'options', 'read', 'create', 'update', 'destroy', 'add', 'remove'] },\n\t\t{permission: 'organization', fields: '*', authorizations: ['query', 'options', 'read'] },\n\t],\n});\n","import { createField, createModel, uuid, now, Query, mergeHooks, Where } from '@yongdall/model';\nimport { useConnection } from '@yongdall/connection';\nimport { nestedSetTree, ServerError } from '@yongdall/core';\n\n// 定义模型\nexport const Organization = createModel('organization', {\n\tid: createField('uuid', { nullable: false, default: uuid, primary: 1, uncreatable: true, layout: { hidden: true } }),\n\tlabel: createField('string', { size: 255, nullable: false, default: '', label: '名称', layout: { colSpan: 6 } }),\n\tparentId: createField('uuid', { nullable: true, label: '上级组织', model: 'organization', renderer: 'document', layout: { colSpan: 6 } }),\n\ttypeId: createField('string', { size: 255, array: true, nullable: false, label: '类型', model: 'organizationType',source: {value: 'id'}, renderer: 'select', default: [], layout: { colSpan: 6 } }),\n\tinternal: createField('bool', { nullable: false, default: false, label: '是否为子组织', layout: { colSpan: 6 } }),\n\tmainId: createField('uuid', { nullable: true, label: '所属主体组织', model: 'organization', renderer: 'document', layout: { colSpan: 6 } }),\n\tdescription: createField('text', { nullable: true, label: '描述' }),\n\n\t// Nested Set 树结构字段,通常由 hook 自动维护,但需定义以便存储\n\tlft: createField('i32', { nullable: false, default: 0, label: 'LFT', layout: { hidden: true } }),\n\trgt: createField('i32', { nullable: false, default: 0, label: 'RGT', layout: { hidden: true } }),\n\n\tcreatedAt: createField('timestamp', { nullable: false, creating: now, label: '创建日期', layout: { hidden: true } }),\n\tupdatedAt: createField('timestamp', { nullable: false, updating: now, label: '最后更新日期', layout: { hidden: true } }),\n}, {\n\tlabel: '组织',\n\tlabelField: 'label',\n\tpermissions: [\n\t\t{ permission: 'system:organization', fields: ['id', 'label', 'parentId', 'typeId', 'internal', 'description', 'createdAt', 'updatedAt'], authorizations: ['options', 'query', 'read', 'create', 'update', 'destroy', 'add', 'remove'] },\n\t\t{ permission: 'system:organization', fields: ['mainId', 'lft'], authorizations: ['options', 'query', 'read'] },\n\n\t\t{ permission: 'organization', fields: ['id', 'label', 'parentId', 'typeId', 'internal', 'description', 'createdAt', 'updatedAt'], organizationField: 'mainId', authorizations: ['options', 'query', 'read', 'create', 'update', 'destroy', 'add', 'remove'] },\n\t\t{ permission: '', fields: ['mainId', 'lft'], organizationField: 'mainId', authorizations: ['options', 'query', 'read'] },\n\t\t{ permission: 'organization', fields: ['id', 'label', 'parentId', 'typeId', 'internal', 'description', 'createdAt', 'updatedAt'], organizationField: 'parentId', authorizations: ['options', 'query', 'read', 'create', 'update', 'destroy', 'add', 'remove'] },\n\t\t{ permission: '', fields: ['mainId', 'lft'], organizationField: 'parentId', authorizations: ['options', 'query', 'read'] },\n\t\t{ permission: 'organization', fields: ['id', 'label', 'parentId', 'typeId', 'internal', 'description', 'createdAt', 'updatedAt'], organizationField: 'id', authorizations: ['options', 'query', 'read', 'create', 'update', 'add', 'remove'] },\n\t\t{ permission: '', fields: ['mainId', 'lft'], organizationField: 'id', authorizations: ['options', 'query', 'read'] },\n\t],\n\thooks: mergeHooks(nestedSetTree('parentId'), {\n\n\t\tasync beforeCreate(conn, Model, document) {\n\t\t\tdelete document.mainId;\n\n\t\t\tif (!document.internal) {\n\t\t\t\tconst parentId = document.parentId;\n\t\t\t\tif (!parentId) { return; }\n\t\t\t\tconst parent = await conn.first(new Query(Model).where('id', parentId));\n\t\t\t\tif (!parent) {\n\t\t\t\t\tdelete document.parentId;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (parent.internal) {\n\t\t\t\t\tthrow new ServerError('主体组织,只能作为其他主体组织的下级组织');\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst parentId = document.parentId;\n\t\t\tconst parent = parentId\n\t\t\t\t? await conn.first(new Query(Model).where('id', parentId))\n\t\t\t\t: null;\n\n\t\t\tif (!parent) {\n\t\t\t\tthrow new ServerError('请选择上级组织');\n\t\t\t}\n\n\t\t\tdocument.internal = true;\n\t\t\tdocument.mainId = (parent.internal && parent.mainId) || parent.id;\n\t\t},\n\t\tasync afterCreate(conn, Model, document) {\n\t\t\tif (document.mainId) { return; }\n\t\t\tconst id = document.id;\n\t\t\tif (!await conn.update(Model, { mainId: id }, Where.and('id', id), true)) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tdocument.mainId = id;\n\t\t},\n\n\t\tasync beforeUpdate(conn, Model, document, set) {\n\t\t\tdelete set.mainId;\n\n\t\t\tconst parentId = set.parentId;\n\t\t\t// 检查 parentId 是否真的改变\n\t\t\tif ((typeof parentId !== 'number' && typeof parentId !== 'string') || parentId === document.parentId) {\n\t\t\t\tdelete set.parentId;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (!parentId) {\n\t\t\t\tif (document.internal) {\n\t\t\t\t\tdelete set.parentId;\n\t\t\t\t} else {\n\t\t\t\t\tset.parentId = null;\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (!document.internal) {\n\t\t\t\tconst parent = await conn.count(\n\t\t\t\t\tnew Query(Model)\n\t\t\t\t\t\t.where('id', parentId)\n\t\t\t\t\t\t.where('internal', false)\n\t\t\t\t);\n\t\t\t\tif (parent) { return; }\n\t\t\t\tthrow new ServerError('主体组织,只能作为其他主体组织的下级组织');\n\t\t\t}\n\n\t\t\tconst parent = await conn.count(\n\t\t\t\tnew Query(Model)\n\t\t\t\t.where('id', parentId)\n\t\t\t\t.where('mainId', document.mainId)\n\t\t\t);\n\t\t\t\tif (parent) { return; }\n\t\t\tthrow new ServerError('非主体组织,只能调整到同一主体组织下');\n\n\n\t\t}\n\t}),\n});\n\n// 静态方法转换为独立导出函数\n// 使用时需传入 connection 或直接使用模型引用\n/**\n * \n * @param {string?} id \n * @returns \n */\nexport async function getAncestors(id) {\n\tif (!id) return [];\n\tconst conn = useConnection('rdb');\n\tconst o = await conn.first(\n\t\tnew Query(Organization).select('lft', 'rgt').where('id', id)\n\t);\n\tif (!o) { return []; }\n\n\tconst { lft, rgt } = o;\n\tconst list = await conn.select(\n\t\tnew Query(Organization).select('id').where('lft', '<=', lft).where('rgt', '>=', rgt)\n\t);\n\treturn list.map(v => v.id);\n}\n\n/**\n * \n * @param {string?} id \n * @returns \n */\nexport async function getPosterity(id) {\n\tif (!id) return [];\n\tconst conn = useConnection('rdb');\n\tconst o = await conn.first(new Query(Organization).select('lft', 'rgt').where('id', id));\n\tif (!o) { return []; }\n\n\tconst { lft, rgt } = o;\n\tconst list = await conn.select(\n\t\tnew Query(Organization).select('id').where('lft', '>=', lft).where('rgt', '<=', rgt)\n\t);\n\treturn list.map(v => v.id);\n}\n\nexport default Organization;\n"],"mappings":";;;;;AAEA,MAAa,mBAAmB,YAAY,oBAAoB;CAC/D,IAAI,YAAY,UAAU;EAAE,UAAU;EAAO,OAAO;EAAM,SAAS;EAAG,CAAC;CACvE,OAAO,YAAY,UAAU;EAAE,UAAU;EAAO,SAAS;EAAI,OAAO;EAAM,CAAC;CAC3E,aAAa,YAAY,UAAU;EAAE,UAAU;EAAM,SAAS;EAAI,OAAO;EAAM,CAAC;CAChF,WAAW,YAAY,aAAa;EAAE,UAAU;EAAO,UAAU;EAAK,OAAO;EAAQ,CAAC;CACtF,WAAW,YAAY,aAAa;EAAE,UAAU;EAAO,UAAU;EAAK,OAAO;EAAU,CAAC;CACxF,EAAE;CACF,OAAO;CACP,YAAY;CACZ,aAAa,CACZ;EAAC,YAAY;EAAuB,QAAQ;EAAK,gBAAgB;GAAC;GAAS;GAAW;GAAQ;GAAU;GAAU;GAAW;GAAO;GAAS;EAAE,EAC/I;EAAC,YAAY;EAAgB,QAAQ;EAAK,gBAAgB;GAAC;GAAS;GAAW;GAAO;EAAE,CACxF;C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