Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
Fetch full assets data in query
Browse files Browse the repository at this point in the history
The original code does not query assets at all, limouren intentionally
leaving some fields blank to save a query. It is not end-user expect.

refs SkygearIO/skygear-SDK-JS#164
  • Loading branch information
rickmak committed Feb 22, 2017
2 parents 4b81eab + 0a4decf commit af87f94
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 42 deletions.
6 changes: 4 additions & 2 deletions pkg/server/handler/file_test.go
Expand Up @@ -193,7 +193,8 @@ func TestUploadFileHandler(t *testing.T) {
"result": {
"$type": "asset",
"$name": "c34e739e-ac82-44c0-b36b-28d226edb237-asset",
"$url": "c34e739e-ac82-44c0-b36b-28d226edb237-asset?signedurl=true"
"$url": "c34e739e-ac82-44c0-b36b-28d226edb237-asset?signedurl=true",
"$content_type":"plain/text"
}
}`)
})
Expand Down Expand Up @@ -230,7 +231,8 @@ func TestUploadFileHandler(t *testing.T) {
"result": {
"$type": "asset",
"$name": "78640a1a-25c7-45a1-8c1f-cf8d3b162f9e-helloworld",
"$url": "78640a1a-25c7-45a1-8c1f-cf8d3b162f9e-helloworld?signedurl=true"
"$url": "78640a1a-25c7-45a1-8c1f-cf8d3b162f9e-helloworld?signedurl=true",
"$content_type":"plain/text"
}
}`)
})
Expand Down
5 changes: 5 additions & 0 deletions pkg/server/handler/record.go
Expand Up @@ -571,6 +571,11 @@ func (h *RecordQueryHandler) Handle(payload *router.Payload, response *router.Re
return
}

// Scan does not query assets,
// it only replaces them with assets then only have name,
// so we replace them with some complete assets.
makeAssetsComplete(db, payload.DBConn, records)

eagers := eagerIDs(db, records, p.Query)
eagerRecords := doQueryEager(db, eagers)

Expand Down
74 changes: 65 additions & 9 deletions pkg/server/handler/record_test.go
Expand Up @@ -421,7 +421,7 @@ func TestRecordSaveDataType(t *testing.T) {
resp := r.POST(`{
"records": [{
"_id": "type1/id1",
"asset": {"$type": "asset", "$name": "asset-name"}
"asset": {"$type": "asset", "$name": "asset-name", "$content_type":"plain/text"}
}]
}`)

Expand All @@ -430,7 +430,7 @@ func TestRecordSaveDataType(t *testing.T) {
"_id": "type1/id1",
"_type": "record",
"_access": null,
"asset": {"$type": "asset", "$name": "asset-name"},
"asset": {"$type": "asset", "$name": "asset-name", "$content_type":"plain/text"},
"_created_by":"user0",
"_updated_by":"user0",
"_ownerID": "user0"
Expand All @@ -442,7 +442,10 @@ func TestRecordSaveDataType(t *testing.T) {
So(record, ShouldResemble, skydb.Record{
ID: skydb.NewRecordID("type1", "id1"),
Data: map[string]interface{}{
"asset": &skydb.Asset{Name: "asset-name"},
"asset": &skydb.Asset{
Name: "asset-name",
ContentType: "plain/text",
},
},
OwnerID: "user0",
CreatorID: "user0",
Expand Down Expand Up @@ -708,6 +711,7 @@ func (db *queryDatabase) Query(query *skydb.Query) (*skydb.Rows, error) {
type queryResultsDatabase struct {
records []skydb.Record
databaseID string
typemap map[string]skydb.RecordSchema
skydb.Database
}

Expand All @@ -728,6 +732,10 @@ func (db *queryResultsDatabase) Query(query *skydb.Query) (*skydb.Rows, error) {
return skydb.NewRows(skydb.NewMemoryRows(db.records)), nil
}

func (db *queryResultsDatabase) GetSchema(recordType string) (skydb.RecordSchema, error) {
return db.typemap[recordType], nil
}

func TestRecordQueryResults(t *testing.T) {
Convey("Given a Database with records", t, func() {
record0 := skydb.Record{
Expand Down Expand Up @@ -1219,8 +1227,9 @@ func TestRecordQuery(t *testing.T) {

// a very naive Database that alway returns the single record set onto it
type singleRecordDatabase struct {
record skydb.Record
databaseID string
record skydb.Record
databaseID string
recordSchema skydb.RecordSchema
skydb.Database
}

Expand Down Expand Up @@ -1255,6 +1264,10 @@ func (db *singleRecordDatabase) Extend(recordType string, schema skydb.RecordSch
return false, nil
}

func (db *singleRecordDatabase) GetSchema(recordType string) (skydb.RecordSchema, error) {
return db.recordSchema, nil
}

func TestRecordOwnerIDSerialization(t *testing.T) {
timeNow = func() time.Time { return ZeroTime }
defer func() {
Expand All @@ -1268,6 +1281,7 @@ func TestRecordOwnerIDSerialization(t *testing.T) {
}
db := &singleRecordDatabase{
record: record,
recordSchema: skydb.RecordSchema{},
}

injectDBFunc := func(payload *router.Payload) {
Expand Down Expand Up @@ -1445,7 +1459,10 @@ func TestRecordAssetSerialization(t *testing.T) {
db.Save(&skydb.Record{
ID: skydb.NewRecordID("record", "id"),
Data: map[string]interface{}{
"asset": &skydb.Asset{Name: "asset-name"},
"asset": &skydb.Asset{
Name: "asset-name",
ContentType: "plain/text",
},
},
})

Expand All @@ -1469,7 +1486,8 @@ func TestRecordAssetSerialization(t *testing.T) {
"asset": {
"$type": "asset",
"$name": "asset-name",
"$url": "http://skygear.test/asset/asset-name?expiredAt=1997-07-01T00:00:00"
"$url": "http://skygear.test/asset/asset-name?expiredAt=1997-07-01T00:00:00",
"$content_type":"plain/text"
}
}]
}`)
Expand All @@ -1480,7 +1498,10 @@ func TestRecordAssetSerialization(t *testing.T) {
record0 := skydb.Record{
ID: skydb.NewRecordID("record", "id"),
Data: map[string]interface{}{
"asset": &skydb.Asset{Name: "asset-name"},
"asset": &skydb.Asset{
Name: "asset-name",
ContentType: "plain/text",
},
},
}

Expand All @@ -1507,7 +1528,8 @@ func TestRecordAssetSerialization(t *testing.T) {
"asset": {
"$type": "asset",
"$name": "asset-name",
"$url": "http://skygear.test/asset/asset-name?expiredAt=1997-07-01T00:00:00"
"$url": "http://skygear.test/asset/asset-name?expiredAt=1997-07-01T00:00:00",
"$content_type":"plain/text"
}
}]
}`)
Expand Down Expand Up @@ -1583,6 +1605,37 @@ func (db *referencedRecordDatabase) Extend(recordType string, schema skydb.Recor
return false, nil
}

func (db *referencedRecordDatabase) GetSchema(recordType string) (skydb.RecordSchema, error) {
typemap := map[string]skydb.RecordSchema{
"note": skydb.RecordSchema{
"category": skydb.FieldType{
Type: skydb.TypeReference,
ReferenceType: "category",
},
"city": skydb.FieldType{
Type: skydb.TypeReference,
ReferenceType: "city",
},
},
"category": skydb.RecordSchema{
"title": skydb.FieldType{
Type: skydb.TypeString,
},
},
"city": skydb.RecordSchema{
"name": skydb.FieldType{
Type: skydb.TypeString,
},
},
"user": skydb.RecordSchema{
"name": skydb.FieldType{
Type: skydb.TypeString,
},
},
}
return typemap[recordType], nil
}

func TestRecordQueryWithEagerLoad(t *testing.T) {
Convey("Given a referenced record in DB", t, func() {
db := &referencedRecordDatabase{
Expand Down Expand Up @@ -1757,6 +1810,9 @@ func TestRecordQueryWithCount(t *testing.T) {

db := &queryResultsDatabase{}
db.records = []skydb.Record{record1, record0, record2}
db.typemap = map[string]skydb.RecordSchema{
"note": skydb.RecordSchema{},
}

r := handlertest.NewSingleRouteRouter(&RecordQueryHandler{}, func(p *router.Payload) {
p.Database = db
Expand Down
48 changes: 48 additions & 0 deletions pkg/server/handler/recordutil.go
Expand Up @@ -661,3 +661,51 @@ func queryResultInfo(db skydb.Database, query *skydb.Query, results *skydb.Rows)
}
return resultInfo, nil
}

func makeAssetsComplete(db skydb.Database, conn skydb.Conn, records []skydb.Record) error {
if len(records) == 0 {
return nil
}

recordType := records[0].ID.Type
typemap, _ := db.GetSchema(recordType)
assetColumns := []string{}
assetNames := []string{}

for column, schema := range typemap {
if schema.Type == skydb.TypeAsset {
assetColumns = append(assetColumns, column)
}
}

for _, record := range records {
for _, assetColumn := range assetColumns {
if thisAsset, ok := record.Get(assetColumn).(*skydb.Asset); ok {
assetNames = append(assetNames, thisAsset.Name)
}
}
}

if len(assetNames) == 0 {
return nil
}

assets, err := conn.GetAssets(assetNames)
if err != nil {
return err
}

assetsByName := map[string]skydb.Asset{}
for _, asset := range assets {
assetsByName[asset.Name] = asset
}
for _, record := range records {
for _, assetColumn := range assetColumns {
if thisAsset, ok := record.Get(assetColumn).(*skydb.Asset); ok {
completeAsset := assetsByName[thisAsset.Name]
record.Set(assetColumn, &completeAsset)
}
}
}
return nil
}
37 changes: 28 additions & 9 deletions pkg/server/plugin/exec/process_test.go
Expand Up @@ -219,7 +219,10 @@ func TestRun(t *testing.T) {
"date": time.Date(2017, 7, 23, 19, 30, 24, 0, time.UTC),
"ref": skydb.NewReference("category", "1"),
"auto_increment": skydb.Sequence{},
"asset": &skydb.Asset{Name: "asset-name"},
"asset": &skydb.Asset{
Name: "asset-name",
ContentType: "plain/text",
},
},
}

Expand Down Expand Up @@ -262,7 +265,8 @@ func TestRun(t *testing.T) {
},
"asset": {
"$type": "asset",
"$name": "asset-name"
"$name": "asset-name",
"$content_type":"plain/text"
},
"auto_increment": {
"$type": "seq"
Expand Down Expand Up @@ -316,7 +320,8 @@ func TestRun(t *testing.T) {
},
"asset": {
"$type": "asset",
"$name": "asset-name"
"$name": "asset-name",
"$content_type":"plain/text"
},
"auto_increment": {
"$type": "seq"
Expand Down Expand Up @@ -352,7 +357,10 @@ func TestRun(t *testing.T) {
"tags": []interface{}{"test", "unimportant"},
"ref": skydb.NewReference("category", "1"),
"auto_increment": skydb.Sequence{},
"asset": &skydb.Asset{Name: "asset-name"},
"asset": &skydb.Asset{
Name: "asset-name",
ContentType: "plain/text",
},
},
})
// GoConvey's bug, ShouldEqual and ShouldResemble doesn't work on time.Time
Expand All @@ -373,7 +381,10 @@ func TestRun(t *testing.T) {
"tags": []interface{}{"test", "unimportant"},
"ref": skydb.NewReference("category", "1"),
"auto_increment": skydb.Sequence{},
"asset": &skydb.Asset{Name: "asset-name"},
"asset": &skydb.Asset{
Name: "asset-name",
ContentType: "plain/text",
},
},
})
So(dateout == time.Date(2017, 7, 23, 19, 30, 24, 0, time.UTC), ShouldBeTrue)
Expand Down Expand Up @@ -403,7 +414,8 @@ func TestRun(t *testing.T) {
},
"asset": {
"$type": "asset",
"$name": "asset-name"
"$name": "asset-name",
"$content_type":"plain/text"
},
"auto_increment": {
"$type": "seq"
Expand Down Expand Up @@ -438,7 +450,8 @@ func TestRun(t *testing.T) {
},
"asset":{
"$type": "asset",
"$name": "asset-name"
"$name": "asset-name",
"$content_type":"plain/text"
},
"auto_increment": {
"$type": "seq"
Expand Down Expand Up @@ -474,7 +487,10 @@ func TestRun(t *testing.T) {
"tags": []interface{}{"test", "unimportant"},
"ref": skydb.NewReference("category", "1"),
"auto_increment": skydb.Sequence{},
"asset": &skydb.Asset{Name: "asset-name"},
"asset": &skydb.Asset{
Name: "asset-name",
ContentType: "plain/text",
},
},
})
// GoConvey's bug, ShouldEqual and ShouldResemble doesn't work on time.Time
Expand All @@ -495,7 +511,10 @@ func TestRun(t *testing.T) {
"tags": []interface{}{"test", "unimportant"},
"ref": skydb.NewReference("category", "1"),
"auto_increment": skydb.Sequence{},
"asset": &skydb.Asset{Name: "asset-name"},
"asset": &skydb.Asset{
Name: "asset-name",
ContentType: "plain/text",
},
},
})
So(dateout == time.Date(2017, 7, 23, 19, 30, 24, 0, time.UTC), ShouldBeTrue)
Expand Down

0 comments on commit af87f94

Please sign in to comment.