sequelize#WhereOptions TypeScript Examples

The following examples show how to use sequelize#WhereOptions. 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: manager.ts    From server with Apache License 2.0 6 votes vote down vote up
public async initRelations(where: WhereOptions | undefined) {
        const rels = await Relation.findAll({
            where: where,
            order: [['tenantId', 'DESC']]})
        if (!rels) return

        let mng: ModelManager | null = null
        for (var i = 0; i < rels.length; i++) {
            const rel = rels[i];
            if (!mng || mng.getTenantId() !== rel.tenantId) {
                mng = this.tenantMap[rel.tenantId]
            }
            mng.getRelations().push(rel)
        }
    }
Example #2
Source File: name.service.ts    From wise-old-man with MIT License 6 votes vote down vote up
async function transferRecords(filter: WhereOptions, targetId: number, transaction: Transaction) {
  // Fetch all of the old records, and the recent new records
  const oldRecords = await Record.findAll({ where: { playerId: targetId } });
  const newRecords = await Record.findAll({ where: filter });

  const outdated: { record: Record; newValue: number }[] = [];

  newRecords.forEach(n => {
    const oldEquivalent = oldRecords.find(r => r.metric === n.metric && r.period === n.period);

    // If the new player's record is higher than the old player's,
    // add the old one  to the outdated list
    if (oldEquivalent && oldEquivalent.value < n.value) {
      outdated.push({ record: oldEquivalent, newValue: n.value });
    }
  });

  // Update all "outdated records"
  await Promise.all(
    outdated.map(async ({ record, newValue }) => {
      await record.update({ value: newValue }, { transaction });
    })
  );
}
Example #3
Source File: name.service.ts    From wise-old-man with MIT License 6 votes vote down vote up
async function transferMemberships(filter: WhereOptions, targetId: number, transaction: Transaction) {
  // Fetch all of new player's memberships (post transition date)
  const newMemberships = await Membership.findAll({ where: filter });

  // Transfer all memberships to the old player id
  const movedMemberships = newMemberships.map(ns => ({ ...ns.toJSON(), playerId: targetId }));

  // Add all these memberships, ignoring duplicates
  await Membership.bulkCreate(movedMemberships, { ignoreDuplicates: true, hooks: false, transaction });
}
Example #4
Source File: name.service.ts    From wise-old-man with MIT License 6 votes vote down vote up
async function transferParticipations(filter: WhereOptions, targetId: number, transaction: Transaction) {
  // Fetch all of new player's participations (post transition date)
  const newParticipations = await Participation.findAll({ where: filter });

  // Transfer all participations to the old player id
  const movedParticipations = newParticipations.map(ns => ({
    ...ns.toJSON(),
    playerId: targetId,
    startSnapshotId: null,
    endSnapshotId: null
  }));

  // Add all these participations, ignoring duplicates
  await Participation.bulkCreate(movedParticipations, { ignoreDuplicates: true, transaction });
}
Example #5
Source File: name.service.ts    From wise-old-man with MIT License 6 votes vote down vote up
async function transferSnapshots(filter: WhereOptions, targetId: number, transaction: Transaction) {
  // Fetch all of new player's snapshots (post transition date)
  const newSnapshots = await Snapshot.findAll({ where: filter });

  // Transfer all snapshots to the old player id
  const movedSnapshots = newSnapshots.map(s => {
    return omit({ ...s.toJSON(), playerId: targetId }, 'id');
  });

  // Add all these snapshots, ignoring duplicates
  await Snapshot.bulkCreate(movedSnapshots, { ignoreDuplicates: true, transaction });
}
Example #6
Source File: manager.ts    From server with Apache License 2.0 6 votes vote down vote up
public async initLanguages(where: WhereOptions | undefined) {
        const languages = await Language.findAll({
            where: where,
            order: [['tenantId', 'DESC'], ['id', 'ASC']]})
        if (!languages) return

        let mng: ModelManager | null = null
        for (var i = 0; i < languages.length; i++) {
            const lang = languages[i];
            if (!mng || mng.getTenantId() !== lang.tenantId) {
                mng = new ModelManager(lang.tenantId)
                this.tenantMap[lang.tenantId] = mng
            }
            mng.getLanguages().push(lang)
        }
    }
Example #7
Source File: manager.ts    From server with Apache License 2.0 6 votes vote down vote up
public async initChannels(where: WhereOptions | undefined) {
        const items = await Channel.findAll({
            where: where,
            order: [['tenantId', 'DESC']]})
        if (!items) return

        let mng: ModelManager | null = null
        for (var i = 0; i < items.length; i++) {
            const chan = items[i];
            if (!mng || mng.getTenantId() !== chan.tenantId) {
                mng = this.tenantMap[chan.tenantId]
            }
            mng.getChannels().push(chan)
        }
    }
Example #8
Source File: manager.ts    From server with Apache License 2.0 6 votes vote down vote up
public async initDashboards(where: WhereOptions | undefined) {
        const dashboards = await Dashboard.findAll({
            where: where,
            order: [['tenantId', 'DESC']]})
        if (!dashboards) return

        let mng: ModelManager | null = null
        for (var i = 0; i < dashboards.length; i++) {
            const dashboard = dashboards[i];
            if (!mng || mng.getTenantId() !== dashboard.tenantId) {
                mng = this.tenantMap[dashboard.tenantId]
            }
            mng.getDashboards().push(dashboard)
        }
    }
Example #9
Source File: manager.ts    From server with Apache License 2.0 6 votes vote down vote up
public async initActions(where: WhereOptions | undefined) {
        const actions = await Action.findAll({
            where: where,
            order: [['tenantId', 'DESC']]})
        if (!actions) return

        let mng: ModelManager | null = null
        for (var i = 0; i < actions.length; i++) {
            const action = actions[i];
            if (!mng || mng.getTenantId() !== action.tenantId) {
                mng = this.tenantMap[action.tenantId]
            }
            mng.getActions().push(action)
        }
    }
Example #10
Source File: manager.ts    From server with Apache License 2.0 6 votes vote down vote up
public async initAttributes(where: WhereOptions | undefined) {
        // TODO optimize this to load data by 1 select with join
        const groups = await AttrGroup.findAll({
            where: where,
            include: [{model: Attribute}],
            order: [['tenantId', 'DESC']]})
        if (!groups) return

        let mng: ModelManager | null = null
        for (var i = 0; i < groups.length; i++) {
            const grp = groups[i];
            if (!mng || mng.getTenantId() !== grp.tenantId) {
                mng = this.tenantMap[grp.tenantId]
            }
            mng.getAttrGroups().push(new AttrGroupWrapper(grp, await grp.getAttributes()))
        }
    }
Example #11
Source File: manager.ts    From server with Apache License 2.0 6 votes vote down vote up
public async initRoles(where: WhereOptions | undefined) {
        // TODO optimize this to load data by 1 select with join
        const roles = await Role.findAll({
            where: where,
            order: [['tenantId', 'DESC']]})
        if (!roles) return

        let mng: ModelManager | null = null
        for (var i = 0; i < roles.length; i++) {
            const role = roles[i];
            if (!mng || mng.getTenantId() !== role.tenantId) {
                mng = this.tenantMap[role.tenantId]
            }
            (<any>role).internalId = role.id
            mng.getRoles().push(role)
        }

        const users = await User.findAll({
            where: where,
            order: [['tenantId', 'DESC']]})
        if (!users) return

        mng = null
        for (var i = 0; i < users.length; i++) {
            const user = users[i];

            if (user.tenantId === '0') continue // super user, skip it

            if (!mng || mng.getTenantId() !== user.tenantId) {
                mng = this.tenantMap[user.tenantId]
            }
            (<any>user).internalId = user.id;
            const roles = user.roles ? user.roles.map((roleId: number) => mng!.getRoles().find(role => role.id === roleId)) : []
            mng.getUsers().push(new UserWrapper(user, roles))
        }
    }
Example #12
Source File: index.ts    From server with Apache License 2.0 6 votes vote down vote up
public async initChannels(where: WhereOptions | undefined) {
        const channels = await Channel.findAll({
            where: where,
            order: [['tenantId', 'DESC']]})
        if (!channels) return

        let mng: ChannelsManager | null = null
        for (var i = 0; i < channels.length; i++) {
            const channel = channels[i];
            if (!mng || mng.getTenantId() !== channel.tenantId) {
                mng = new ChannelsManager(channel.tenantId)
                this.tenantMap[channel.tenantId] = mng
            }
            mng.addChannel(channel)
        }
    }
Example #13
Source File: service.ts    From expresso with MIT License 6 votes vote down vote up
/**
   *
   * @param condition
   * @param options
   * @returns
   */
  public static async findByCondition(
    condition: WhereOptions<FCMTokenAttributes>,
    options?: SqlizeOptions
  ): Promise<FCMTokenInstance> {
    const i18nOpt: string | TOptions = { lng: options?.lang }

    const data = await FCMToken.findOne({
      where: condition,
      include: options?.include,
      order: options?.order,
      paranoid: options?.paranoid,
    })

    if (!data) {
      const message = i18nConfig.t('errors.notFound', i18nOpt)
      throw new ResponseError.NotFound(`fcm token ${message}`)
    }

    return data
  }
Example #14
Source File: migration.ts    From commonwealth with GNU General Public License v3.0 5 votes vote down vote up
/**
   * Handles an event during the migration process, by creating or updating existing
   * events depending whether we've seen them before.
   */
  public async handle(event: CWEvent<IChainEventData>) {
    const chain = event.chain || this._chain;

    // case by entity type to determine what value to look for
    const createOrUpdateModel = async (
      fieldName: string,
      fieldValue: string,
      eventType: EntityEventKind
    ) => {
      const [dbEventType, created] =
        await this._models.ChainEventType.findOrCreate({
          where: {
            id: `${chain}-${event.data.kind.toString()}`,
            chain,
            event_network: event.network,
            event_name: event.data.kind.toString(),
          },
        });
      log.trace(
        `${created ? 'created' : 'found'} chain event type: ${dbEventType.id}`
      );
      const queryFieldName = `event_data.${fieldName}`;
      const queryArgs: WhereOptions<ChainEventAttributes> =
        eventType === EntityEventKind.Vote
          ? {
              chain_event_type_id: dbEventType.id,
              [queryFieldName]: fieldValue,
              // votes will be unique by data rather than by type
              event_data: event.data as any,
            }
          : {
              chain_event_type_id: dbEventType.id,
              [queryFieldName]: fieldValue,
            };
      const existingEvent = await this._models.ChainEvent.findOne({
        where: queryArgs,
      });
      if (!existingEvent) {
        log.trace('No existing event found, creating new event in db!');
        return this._models.ChainEvent.create({
          chain_event_type_id: dbEventType.id,
          block_number: event.blockNumber,
          event_data: event.data,
        });
      } else {
        existingEvent.event_data = event.data;
        await existingEvent.save();
        log.trace('Existing event found and migrated successfully!');
        return existingEvent;
      }
    };

    const entity = eventToEntity(event.network, event.data.kind);
    if (!entity) return null;
    const [entityKind, eventType] = entity;
    const fieldName = entityToFieldName(event.network, entityKind);
    if (!fieldName) return null;
    const fieldValue = event.data[fieldName];
    return createOrUpdateModel(fieldName, fieldValue, eventType);
  }
Example #15
Source File: manager.ts    From server with Apache License 2.0 5 votes vote down vote up
public async initModels(where: WhereOptions | undefined) {
        await this.initLanguages(where)

        /*
        const types: Type[] = await sequelize.query('SELECT * FROM types order by "tenantId", path', {
            model: Type,
            mapToModel: true
        }); */

        const types: Type[] = await Type.findAll({
            where: where,
            order: [['tenantId', 'DESC'],['path', 'ASC']]})

        let mng: ModelManager
        let currentNode: TreeNode<any>
        let currentLevel: number
        types.forEach( (type) => {
            // console.log('loading type-' + type.path)
            if (!mng || mng.getTenantId() !== type.tenantId) {
                mng = this.tenantMap[type.tenantId]
                currentNode = mng.getRoot()
                currentLevel = 1
            }

            const arr = type.path.split('.')
            /* console.log(currentLevel, arr.length, 
                (currentNode.getValue() ? 'cur-'+currentNode.getValue() : 'null'), 
                (currentNode.getParent() ? 'par-'+currentNode.getParent()! : "no parent")) */
            if (currentLevel > arr.length) {
                // go to one parent up
                while(currentLevel > arr.length) {
                    currentNode = currentNode.getParent()!
                    currentLevel--
                }
            }

            const node = new TreeNode<Type>(type, currentNode)
            // console.log('parent -' + JSON.stringify(currentNode.getValue()))
            currentNode.getChildren().push(node)
            currentNode = node
            currentLevel++
        })

        await this.initAttributes(where)
        await this.initRelations(where)
        await this.initRoles(where)
        await this.initActions(where)
        await this.initDashboards(where)
        await this.initChannels(where)

        logger.info('Data models were loaded')
    }
Example #16
Source File: manager.ts    From server with Apache License 2.0 5 votes vote down vote up
public async init(channelTypes?: number[]) {
        if (channelTypes) this.channelTypes = channelTypes
        let where: WhereOptions | undefined = undefined
        if (process.argv.length > 3) {
            where = {tenantId: process.argv.splice(3)}
        }
        await this.initModels(where)
    }
Example #17
Source File: index.ts    From server with Apache License 2.0 5 votes vote down vote up
public async init() {
        let where: WhereOptions | undefined = undefined
        if (process.argv.length > 3) {
            where = {tenantId: process.argv.splice(3)}
        }
        await this.initChannels(where)
    }
Example #18
Source File: setupChainEventListeners.ts    From commonwealth with GNU General Public License v3.0 4 votes vote down vote up
setupChainEventListeners = async (
  wss: WebSocket.Server,
  chains: string[] | 'all' | 'none',
  skipCatchup?: boolean
): Promise<[ChainInstance, IEventSubscriber<any, any>][]> => {
  await sequelize.authenticate();
  log.info('Fetching node urls...');
  if (chains === 'none') {
    log.info('No event listeners configured.');
    return [];
  }
  const whereOptions: WhereOptions<ChainAttributes> =
    chains === 'all'
      ? {
          active: true,
          has_chain_events_listener: true,
        }
      : {
          active: true,
          has_chain_events_listener: true,
          id: { [Op.in]: chains },
        };
  const nodes = await models.Chain.findAll({
    where: whereOptions,
    include: [
      {
        model: models.ChainNode,
        required: true,
      },
    ],
  });

  if (nodes.length === 0) {
    log.info('No event listeners found.');
    return [];
  }

  log.info('Setting up event listeners...');
  const subscribers = await Promise.all(
    nodes.map(
      async (
        node,
      ): Promise<[ChainInstance, IEventSubscriber<any, any>]> => {
        let subscriber: IEventSubscriber<any, any>;
        if (node.base === ChainBase.Substrate) {
          const nodeUrl = constructSubstrateUrl(node.ChainNode.url);
          const api = await SubstrateEvents.createApi(
            nodeUrl,
            node.substrate_spec
          );
          const excludedEvents = [
            SubstrateTypes.EventKind.Reward,
            SubstrateTypes.EventKind.TreasuryRewardMinting,
            SubstrateTypes.EventKind.TreasuryRewardMintingV2,
            SubstrateTypes.EventKind.HeartbeatReceived,
          ];

          const handlers = generateHandlers(node, wss, { excludedEvents });
          subscriber = await SubstrateEvents.subscribeEvents({
            chain: node.id,
            handlers,
            skipCatchup,
            discoverReconnectRange: () => discoverReconnectRange(node.id),
            api,
            enricherConfig: {
              balanceTransferThresholdPermill:
                BALANCE_TRANSFER_THRESHOLD_PERMILL,
            },
          });
        } else if (node.network === ChainNetwork.Moloch) {
          const contractVersion = 1;
          const api = await MolochEvents.createApi(
            node.ChainNode.url,
            contractVersion,
            node.address
          );
          const handlers = generateHandlers(node, wss);
          subscriber = await MolochEvents.subscribeEvents({
            chain: node.id,
            handlers,
            skipCatchup,
            discoverReconnectRange: () => discoverReconnectRange(node.id),
            api,
            contractVersion,
          });
        } else if (node.network === ChainNetwork.Compound) {
          const api = await CompoundEvents.createApi(node.ChainNode.url, node.address);
          const handlers = generateHandlers(node, wss);
          subscriber = await CompoundEvents.subscribeEvents({
            chain: node.id,
            handlers,
            skipCatchup,
            discoverReconnectRange: () => discoverReconnectRange(node.id),
            api,
          });
        } else if (node.network === ChainNetwork.Aave) {
          const api = await AaveEvents.createApi(node.ChainNode.url, node.address);
          const handlers = generateHandlers(node, wss);
          subscriber = await AaveEvents.subscribeEvents({
            chain: node.id,
            handlers,
            skipCatchup,
            discoverReconnectRange: () => discoverReconnectRange(node.id),
            api,
            verbose: true,
          });
        }

        // hook for clean exit
        process.on('SIGTERM', () => {
          if (subscriber) {
            subscriber.unsubscribe();
          }
        });
        return [node, subscriber];
      }
    )
  );
  return subscribers;
}