proto.io 0.0.165 → 0.0.166

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.
Files changed (31) hide show
  1. package/dist/adapters/file/database.d.ts +2 -2
  2. package/dist/adapters/file/filesystem.d.ts +2 -2
  3. package/dist/adapters/file/google-cloud-storage.d.ts +2 -2
  4. package/dist/adapters/storage/progres.d.ts +1 -1
  5. package/dist/adapters/storage/progres.mjs +1 -1
  6. package/dist/client.d.ts +3 -3
  7. package/dist/client.js +1 -1
  8. package/dist/client.mjs +2 -2
  9. package/dist/index.d.ts +3 -3
  10. package/dist/index.js +60 -24
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +62 -26
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/internals/{index-Dz3jvqxZ.js → index-B-pq8xkP.js} +5 -21
  15. package/dist/internals/index-B-pq8xkP.js.map +1 -0
  16. package/dist/internals/{index-bCACA0cS.d.ts → index-BQggoDNX.d.ts} +2 -2
  17. package/dist/internals/index-BQggoDNX.d.ts.map +1 -0
  18. package/dist/internals/{index-BibByOcU.mjs → index-CUJMde7V.mjs} +5 -21
  19. package/dist/internals/index-CUJMde7V.mjs.map +1 -0
  20. package/dist/internals/{index-DaDfXlay.d.ts → index-rkqvel7o.d.ts} +2 -2
  21. package/dist/internals/index-rkqvel7o.d.ts.map +1 -0
  22. package/dist/internals/{index-RPh4TX0T.d.ts → index-y8EePsDY.d.ts} +8 -5
  23. package/dist/internals/index-y8EePsDY.d.ts.map +1 -0
  24. package/dist/internals/{random-B1P0EZO5.mjs → random-DrURPPxr.mjs} +2 -2
  25. package/dist/internals/{random-B1P0EZO5.mjs.map → random-DrURPPxr.mjs.map} +1 -1
  26. package/package.json +2 -2
  27. package/dist/internals/index-BibByOcU.mjs.map +0 -1
  28. package/dist/internals/index-DaDfXlay.d.ts.map +0 -1
  29. package/dist/internals/index-Dz3jvqxZ.js.map +0 -1
  30. package/dist/internals/index-RPh4TX0T.d.ts.map +0 -1
  31. package/dist/internals/index-bCACA0cS.d.ts.map +0 -1
package/dist/index.mjs CHANGED
@@ -1,10 +1,10 @@
1
1
  import _ from 'lodash';
2
2
  import { Server } from '@o2ter/server-js';
3
- import { Q as QueryValidator, r as resolveColumn, g as generateId, a as resolveDataType } from './internals/random-B1P0EZO5.mjs';
3
+ import { Q as QueryValidator, r as resolveColumn, a as resolveDataType, g as generateId } from './internals/random-DrURPPxr.mjs';
4
4
  import { P as PVK } from './internals/private-BUpLAMZi.mjs';
5
5
  import { asyncStream, asyncIterableToArray, isBinaryData, base64ToBuffer } from '@o2ter/utils-js';
6
- import { T as TQuery, M as MASTER_USER_HEADER_NAME, a as MASTER_PASS_HEADER_NAME, A as AUTH_COOKIE_KEY, b as TUser, P as ProtoType, s as serialize, d as deserialize, U as UPLOAD_TOKEN_HEADER_NAME } from './internals/index-BibByOcU.mjs';
7
- export { c as ProtoClient, e as classExtends, j as isFile, f as isObject, i as isQuery, h as isRole, g as isUser } from './internals/index-BibByOcU.mjs';
6
+ import { T as TQuery, M as MASTER_USER_HEADER_NAME, a as MASTER_PASS_HEADER_NAME, A as AUTH_COOKIE_KEY, b as TUser, P as ProtoType, s as serialize, d as deserialize, U as UPLOAD_TOKEN_HEADER_NAME } from './internals/index-CUJMde7V.mjs';
7
+ export { c as ProtoClient, e as classExtends, j as isFile, f as isObject, i as isQuery, h as isRole, g as isUser } from './internals/index-CUJMde7V.mjs';
8
8
  import { i as isPointer, a as isRelation, d as decodeUpdateOp, T as TObject, b as isShape, c as defaultObjectKeyTypes, e as isPrimitive } from './internals/index-BYbMU-Ao.mjs';
9
9
  import jwt from 'jsonwebtoken';
10
10
  import { Blob } from 'node:buffer';
@@ -855,6 +855,12 @@ class ProtoInternal {
855
855
  validateSchemaName(options.schema);
856
856
  const schema = mergeSchema(defaultSchema, options.fileStorage.schema, options.schema);
857
857
  validateSchema(schema);
858
+ if (!_.every(options.roleInheritKeys, k => {
859
+ const type = resolveDataType(schema, 'Role', k);
860
+ return type && isRelation(type) && _.includes(['User', 'Role'], type.target);
861
+ })) {
862
+ throw Error(`Invalid role keys`);
863
+ }
858
864
  this.options = {
859
865
  ...options,
860
866
  schema,
@@ -1293,29 +1299,7 @@ const signUser = async (proto, res, user, options) => {
1293
1299
  //
1294
1300
  const scheduleOp = {
1295
1301
  expireDocument: async (proto) => {
1296
- for (const className of proto.classes()) {
1297
- if (className === 'File') {
1298
- const found = proto.storage.find({
1299
- className: 'File',
1300
- filter: QuerySelector.decode({ _expired_at: { $lt: new Date() } }),
1301
- matches: {},
1302
- includes: ['_id', '_expired_at', 'token'],
1303
- objectIdSize: 0
1304
- });
1305
- for await (const item of found) {
1306
- const token = item.get('token');
1307
- if (!_.isEmpty(token))
1308
- await proto.fileStorage.destroy(proto, token);
1309
- }
1310
- }
1311
- await proto.storage.deleteMany({
1312
- className,
1313
- filter: QuerySelector.decode({ _expired_at: { $lt: new Date() } }),
1314
- includes: ['_id', '_expired_at'],
1315
- matches: {},
1316
- objectIdSize: 0
1317
- });
1318
- }
1302
+ await proto.gc();
1319
1303
  }
1320
1304
  };
1321
1305
  const schedule = (proto) => {
@@ -1377,6 +1361,7 @@ class ProtoService extends ProtoType {
1377
1361
  constructor(options) {
1378
1362
  super();
1379
1363
  this[PVK] = new ProtoInternal({
1364
+ roleInheritKeys: [],
1380
1365
  objectIdSize: 10,
1381
1366
  maxFetchLimit: 1000,
1382
1367
  maxUploadSize: 20 * 1024 * 1024,
@@ -1462,6 +1447,32 @@ class ProtoService extends ProtoType {
1462
1447
  const payload = _.create(this, { session });
1463
1448
  return _.assign(payload, _.isFunction(attrs) ? attrs(payload) : attrs);
1464
1449
  }
1450
+ async userRoles(user) {
1451
+ const roleInheritKeys = this[PVK].options.roleInheritKeys;
1452
+ const schema = this.schema;
1453
+ const userKeys = _.filter(roleInheritKeys, k => {
1454
+ const type = resolveDataType(schema, 'Role', k);
1455
+ return !!type && isRelation(type) && type.target === 'User';
1456
+ });
1457
+ const roleKeys = _.filter(roleInheritKeys, k => {
1458
+ const type = resolveDataType(schema, 'Role', k);
1459
+ return !!type && isRelation(type) && type.target === 'Role';
1460
+ });
1461
+ let queue = await this.Query('Role')
1462
+ .or(_.map(_.uniq(['users', ...userKeys]), k => q => q.isIntersect(k, [user])))
1463
+ .includes('name')
1464
+ .find({ master: true });
1465
+ let roles = queue;
1466
+ while (!_.isEmpty(queue)) {
1467
+ queue = await this.Query('Role')
1468
+ .or(_.map(_.uniq(['roles', ...roleKeys]), k => q => q.isIntersect(k, queue)))
1469
+ .notContainsIn('_id', _.compact(_.map(roles, x => x.objectId)))
1470
+ .includes('name')
1471
+ .find({ master: true });
1472
+ roles = _.uniqBy([...roles, ...queue], x => x.objectId);
1473
+ }
1474
+ return roles;
1475
+ }
1465
1476
  async becomeUser(req, user, options) {
1466
1477
  if (!user.objectId)
1467
1478
  throw Error('Invalid user object');
@@ -1566,6 +1577,31 @@ class ProtoService extends ProtoType {
1566
1577
  yield self.rebind(object);
1567
1578
  });
1568
1579
  }
1580
+ async gc(classNames) {
1581
+ for (const className of _.castArray(classNames ?? this.classes())) {
1582
+ if (className === 'File') {
1583
+ const found = this.storage.find({
1584
+ className: 'File',
1585
+ filter: QuerySelector.decode({ _expired_at: { $lt: new Date() } }),
1586
+ matches: {},
1587
+ includes: ['_id', '_expired_at', 'token'],
1588
+ objectIdSize: 0
1589
+ });
1590
+ for await (const item of found) {
1591
+ const token = item.get('token');
1592
+ if (!_.isEmpty(token))
1593
+ await this.fileStorage.destroy(this, token);
1594
+ }
1595
+ }
1596
+ await this.storage.deleteMany({
1597
+ className,
1598
+ filter: QuerySelector.decode({ _expired_at: { $lt: new Date() } }),
1599
+ includes: ['_id', '_expired_at'],
1600
+ matches: {},
1601
+ objectIdSize: 0
1602
+ });
1603
+ }
1604
+ }
1569
1605
  }
1570
1606
 
1571
1607
  //