예제 #1
0
func (d *dbService) UpdateItemById(id string, data interface{}) (err error) {
	updateData := utils.BlacklistFields([]string{ID_FIELD}, data)

	err = d.Exec(
		d.Db().Table(DATA_TABLE).Get(id).Update(updateData),
	)
	return
}
예제 #2
0
func (d *dbService) GetItemById(id string) (item models.JSON, err error) {
	c, err := d.Run(
		d.Db().Table(DATA_TABLE).Get(id),
	)
	if err != nil {
		return
	}

	err = c.One(&item)
	if err == nil {
		item = utils.BlacklistFields(DB_FIELDS, item)
	}

	return
}
예제 #3
0
func (d *dbService) GetItems(appId, t string, filter models.JSON) (data []models.JSON, err error) {
	query := d.Db().Table(DATA_TABLE).GetAllByIndex(DATA_TABLE_APPIDTYPE_INDEX, []interface{}{appId, t})
	if filter != nil || len(filter) > 0 {
		query = query.Filter(filter)
	}

	c, err := d.Run(query)
	if err != nil {
		return
	}

	err = c.All(&data)
	if err == nil {
		for i, v := range data {
			data[i] = utils.BlacklistFields(DB_FIELDS, v)
		}
	}

	return
}
예제 #4
0
func (p WsMessageReceiver) processDatabaseUpdate(
	val map[string]interface{},
	builder *messaging.MessageBuilder,
	opts models.SubscribeOptions,
) {
	messageBuilder := *builder

	pld := models.JSON{}
	var dbOp string
	newVal := val["new_val"]
	oldVal := val["old_val"]

	if newVal != nil && oldVal == nil {
		dbOp = messaging.OP_CREATE
	} else if newVal == nil && oldVal != nil {
		dbOp = messaging.OP_DELETE
	} else {
		dbOp = messaging.OP_UPDATE
	}

	//only emit messages with the same operation as the subscriber
	if dbOp != opts.Operation {
		return
	}

	var dbVal map[string]interface{}
	if dbOp == messaging.OP_CREATE {
		if newVal == nil {
			log.Error("Invalid message:", val)
			return
		}

		dbVal = newVal.(map[string]interface{})
	} else if dbOp == messaging.OP_DELETE && oldVal != nil {
		if oldVal == nil {
			log.Error("Invalid message:", val)
			return
		}

		dbVal = oldVal.(map[string]interface{})
	} else {
		//update
		if newVal == nil {
			log.Error("Invalid message:", val)
			return
		}

		dbVal = newVal.(map[string]interface{})
	}

	pld = pld.FromMap(dbVal)
	pld = utils.BlacklistFields([]string{db.TYPE_FIELD, db.APP_ID_FIELD}, pld)

	msg := messageBuilder.Build(
		dbOp,
		messaging.ORIGIN_API,
		pld,
		models.Options{},
		opts.Type,
		opts.AppId,
		"", //TODO: token?
	)
	msg.Topic = opts.Topic

	log.Info("Publishing database feed: ", strings.ToUpper(msg.Operation), opts, val, msg)

	publishArgs := []interface{}{msg}
	p.WsClient.Publish(opts.Topic, publishArgs, nil)
}