Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-295

Typescript realm types return RealmObject not their original type

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None

      <!---

      Questions: If you have questions about HOW TO use Realm, please ask on
      StackOverflow: http://stackoverflow.com/questions/ask?tags=realm
      We monitor the realm tag.

      Feature Request: Just fill in the first two sections below.

      Bugs: To help you as fast as possible with an issue please describe your issue
      and the steps you have taken to reproduce it in as much detail as possible.

      -->

      Goals

      Using Typescript i am attempting to use RealmJS and retrieve some objects after persisting them

      Expected Results

      when i fetch the results they are of the type i defined them as in this case 'Party'

      Actual Results

      they are RealmObject instead of my Party type

      Steps to Reproduce

      Use the format of the code below

      Code Sample

      Party type

      Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      import { NameBadge } from '../interfaces/NameBadge'
      import { WalkInConfig } from '../interfaces/WalkInConfig'
      import { Grouping } from '../interfaces/Grouping'
      import { Survey } from '../interfaces/Survey'
      import { NamecardQuota } from '../interfaces/NameCardQuota'
      import { Question } from '../interfaces/Question'
      import { Expose } from 'class-transformer'
      import getRealm from './realm'
      
      export class Party {
      	id!: number
      	name!: string
      	@Expose({ name: 'start_at' })
      	startAt!: Date
      	@Expose({ name: 'end_at' })
      	endAt!: Date
      	place?: string | null
      	details?: string | null
      	logo!: string
      	@Expose({ name: 'logo_bnw' })
      	logoBnw!: string
      	@Expose({ name: 'name_badge' })
      	nameBadge!: NameBadge
      	@Expose({ name: 'name_badges' })
      	nameBadges!: NameBadge[]
      	@Expose({ name: 'attendee_ids' })
      	attendeeIds!: number[]
      	@Expose({ name: 'grouping_ids' })
      	groupingIds!: number[]
      	@Expose({ name: 'updated_at' })
      	updatedAt!: Date
      	@Expose({ name: 'created_at' })
      	createdAt!: Date
      	@Expose({ name: 'custom_attendee_field_names' })
      	customAttendeeFieldNames!: string[]
      	@Expose({ name: 'walk_in_config' })
      	walkInConfig!: WalkInConfig
      	@Expose({ name: 'updated_attendee_ids' })
      	updatedAttendeeIds!: number[]
      	@Expose({ name: 'attendee_custom_line_1' })
      	attendeeCustomLine1?: string | null
      	@Expose({ name: 'attendee_custom_line_2' })
      	attendeeCustomLine2?: string | null
      	questions!: Question[]
      	groupings!: Grouping[]
      	surveys!: Survey[]
      	@Expose({ name: 'namecard_quota' })
      	namecardQuota!: NamecardQuota
      	static schema: Realm.ObjectSchema
      
      	static parties(): Party[] {
      		console.log(getRealm().objectForPrimaryKey(Party.schema.name, 34)) // returns RealmObject rather than 
      		return Array.from(getRealm().objects<Party>(Party.schema.name))
      	}
      
      	static persist(party: Party) {
      		console.log(Party)
      		getRealm().write(() => getRealm().create(Party.schema.name, party))
      	}
      
      	get stuff() {
      		console.log("sup")
      		return 'hey there'
      	}
      
      	set stuff(val: String) {
      		
      	}
      }
      
      Party.schema = {
      	name: 'Party',
      	primaryKey: 'id',
      	properties: {
      		id: 'int',
      		name: 'string',
      		startAt: 'date',
      		endAt: 'date',
      		place: 'string?',
      		details: 'string?',
      		logo: 'string',
      		logoBnw: 'string',
      		attendeeIds: 'int[]',
      		groupingIds: 'int[]',
      		updatedAt: 'date',
      		createdAt: 'date',
      		customAttendeeFieldNames: 'string[]',
      		updatedAttendeeIds: 'int[]',
      		attendeeCustomLine1: 'string?' ,
      		attendeeCustomLine2: 'string?'
      	}
      }
      

      realm.js file for holding shared instance

      Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      import { Party } from './Party'
      import { Test } from './test'
      import Realm from 'realm'
      
      let realmInstance: Realm | null
      
      const getRealm = (): Realm => {
      	if (realmInstance == null) realmInstance = new Realm({ schema: [ Party.schema, Test] })
      	return realmInstance!
      }
      
      export default getRealm
      

      snippet of me using RealmJS in a component

      Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      try {
      	let existingParties: Party[] = Party.parties()
      	console.log(existingParties)
      	if (existingParties.length == 0) {
      		let fetchParties = await fetch(path, {
      			headers: {
      				Authorization: `Bearer ${this.accessToken}`
      			}
      		})
      
      		let responseJson = await fetchParties.json()
      		let parties: Party[] = responseJson.parties.map( (x: any) => plainToClass(Party,x) )
      		parties.forEach(x => Party.persist(x))
      
      	} else {
      		this.partiesFetcher.next(existingParties.map(x => new Party(x))) // this function expects Party Type but get's RealmObjects and has a hissy fit.
      	}
      } catch (e) {
      	console.log(e)
      }
      

      And to confirm the behaviour i wrote a quick 'Test.js' class

      import Realm, { ObjectSchema } from 'realm'
      
      export class Test {
       
        get stuff()   {
          return "hello"
        }
      
        set stuff(val) {
      
        }
      }
      
      Test.schema = {
        name: 'Test',
          primaryKey: 'id',
          properties: {
            id: 'int',
            name: 'string'
          }
      }
      
      

      snippet attempting to retrieve Test type

      Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      let object = getRealm().objectForPrimaryKey('Test',123)
      console.log(object)
      if (object == null){
      	getRealm().write(() => {
      		getRealm().create(Test.schema.name, {id: 123, name: "steve"})
      	})
      }
      

      This prints a Test type in the console and further more has the stuff method, the Party or RealmObject does not contain the defined methods.

      Here is an repo project detailing what i am referring to

      Version of Realm and Tooling

      • Realm JS SDK Version: "^2.26.1"
      • Node or React Native: "0.59.4"
      • Client OS & Version: Mojave 10.14.5
      • Which debugger for React Native: ? React Native Debugger

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: