mongoose#DocumentQuery TypeScript Examples

The following examples show how to use mongoose#DocumentQuery. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: base.service.ts    From codeclannigeria-backend with MIT License 6 votes vote down vote up
softDeleteMany(
    filter = {}
  ): DocumentQuery<DocumentType<T>[], DocumentType<T>> {
    const update = {
      isDeleted: true,
      deletedBy: this.getUserId()
    } as any;
    return this.EntityModel.updateMany(filter, update, { multi: true })
      .where('isDeleted')
      .ne(true);
  }
Example #2
Source File: auth.service.ts    From nestjs-angular-starter with MIT License 6 votes vote down vote up
getUserFromToken(
    token: string,
  ): DocumentQuery<IUserProfileDbModel, IUserProfileDbModel, unknown> {
    // Decode the token
    const decodedUser = this.jwtService.verify(token) as IUserProfileDbModel;

    if (decodedUser) {
      // If the user has been decoded successfully, check it against the database
      return UserProfileDbModel.findById(decodedUser._id);
    }
  }
Example #3
Source File: auth.service.ts    From nestjs-angular-starter with MIT License 5 votes vote down vote up
getUserFromDB(
    email: string,
  ): DocumentQuery<IUserProfileDbModel, IUserProfileDbModel, unknown> {
    return UserProfileDbModel.findOne({ email });
  }
Example #4
Source File: mixin.spec.ts    From pebula-node with MIT License 4 votes vote down vote up
describe('goosetyped', () => {
  initMongoConnection('diff');

  describe('mixins', () => {


    class VersionMixin {
      @GtVersionKey()
      version: number;
    }

    class TimestampMixin {
      @GtTimestampCreated()
      @GtIndex({ sort: 'desc' })
      createDate: Date;
    
      @GtTimestampUpdated()
      @GtIndex({ sort: 'desc' })
      updateDate: Date;
    }

    class OwnerMixin {
      @GtColumn() owner: string;
    }

    class HookMixin {
      @GtColumn({ type: () => String })
      hookRun: string[];

      @GtSaveHook('pre')
      HookMixinPreSaveHook() {
        this.hookRun.push('HookMixinPreSaveHook');
        (this as any).name = 'Hooked';
      }
    }

    it('should apply mixins', async () => {
      @GtDocument()
      class TestClass extends GtModel(TimestampMixin, VersionMixin, OwnerMixin) {
        @GtColumn() name: string;
      }
      let t = await TestClass.create({ name: 'test', owner: 'tester' });
      t = await TestClass.findById(t.id);
      expect(t.createDate).toBeInstanceOf(Date);
      expect(t.updateDate).toBeInstanceOf(Date);
      expect(t.name).toBe('test');
      expect(t.owner).toBe('tester');
    });

    it('should apply mixins with hooks', async () => {
      @GtDocument()
      class TestClass extends GtModel(HookMixin) {
        hookRun: string[] = [];
        @GtColumn() name: string;
      }
      let t = await TestClass.create({ name: 'test' });
      t = await TestClass.findById(t.id);
      expect(t.name).toBe('Hooked');
      expect(Array.from(t.hookRun)).toEqual(['HookMixinPreSaveHook']);
    });

    it('should apply mixins with hooks in proper order', async () => {
      class HookAdHocMixin {
        hookRun: string[];

        @GtSaveHook('pre')
        HookAdHocMixinPreSaveHook() {
          this.hookRun.push('HookAdHocMixinPreSaveHook');
        }
      }

      @GtDocument()
      class BaseBaseBaseBaseTestClass extends GtModel(HookMixin, HookAdHocMixin) {
        hookRun: string[] = [];

        @GtSaveHook('pre')
        BaseBaseBaseBaseTestClassPreSaveHook() {
          this.hookRun.push('BaseBaseBaseBaseTestClassPreSaveHook');
        }
      }

      class BaseBaseBaseTestClass extends BaseBaseBaseBaseTestClass {
        @GtSaveHook('pre')
        BaseBaseBaseTestClass() {
          this.hookRun.push('BaseBaseBaseTestClass');
        }
      }

      @GtDocument()
      class BaseBaseTestClass extends BaseBaseBaseTestClass {
        @GtSaveHook('pre')
        BaseBaseTestClass() {
          this.hookRun.push('BaseBaseTestClass');
        }
      }

      @GtDocument()
      class BaseTestClass extends BaseBaseTestClass {
        @GtColumn() name: string;

        @GtSaveHook('pre')
        BaseTestClass() {
          this.hookRun.push('BaseTestClass');
        }
      }

      @GtDocument()
      class TestClass extends BaseTestClass {
        @GtSaveHook('pre')
        TestClass() {
          this.hookRun.push('TestClass');
        }
      }

      let t = await TestClass.create({ name: 'test' });
      t = await TestClass.findById(t.id);
      expect(t.name).toBe('Hooked');
      expect(Array.from(t.hookRun)).toEqual([
        'TestClass',
        'BaseTestClass',
        'BaseBaseBaseTestClass',
        'BaseBaseTestClass',
        'HookAdHocMixinPreSaveHook',
        'HookMixinPreSaveHook',
        'BaseBaseBaseBaseTestClassPreSaveHook',
      ]);
    });

    it('should apply query helper mixins', async () => {
      class QHelper {
        nameWithT<T extends Document & { name: string }>(this: DocumentQuery<T[], T, QHelper> & QHelper): DocumentQuery<T[], T, QHelper> & QHelper {
          return this.find({ name: /^t.+/ } as FilterQuery<T>);
        }

        ownerEndWithNum<T extends Document & { owner: string }>(this: DocumentQuery<T[], T, QHelper> & QHelper): DocumentQuery<T[], T, QHelper> & QHelper {
          return this.find({ owner: /\d$/ } as FilterQuery<T>);
        }
      }

      @GtDocument()
      class TestClass extends GtQuery(QHelper)(GtModel(TimestampMixin, VersionMixin, OwnerMixin)) {
        @GtColumn() name: string;
      }

      await TestClass.deleteMany({});
      await TestClass.create({ name: 'test', owner: 'a' });
      await TestClass.create({ name: 'test-a', owner: 'b' });
      await TestClass.create({ name: 'test-b', owner: 'c1' });
      await TestClass.create({ name: 'noTest', owner: 'd2' });
      await TestClass.create({ name: 'best1', owner: 'e' });
      await TestClass.create({ name: 'best2', owner: 'f' });
      let results = await TestClass.find().nameWithT();
      expect(results.length).toBe(3);

      results = await TestClass.find().ownerEndWithNum()
      expect(results.length).toBe(2);


      results = await TestClass.find().nameWithT().ownerEndWithNum();
      results = await TestClass.find().ownerEndWithNum().nameWithT();
      expect(results.length).toBe(1);
    });

  });
});