Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do i manage references? #10

Closed
apppro123 opened this issue Dec 27, 2018 · 8 comments
Closed

How do i manage references? #10

apppro123 opened this issue Dec 27, 2018 · 8 comments
Labels
for FAQ Good question that can be used in FAQ section question Further information is requested

Comments

@apppro123
Copy link

Hi i have another question:
How do i manage references?
If i have two schemas and i reference one of the to the other, how to do i insert data to them?
Do i have to insert in both (and give the id of the "second" db to the first) or is it sufficient if i just insert the data into the "main" schema and insert the data of the refrenced element like the schema of the "second" db is built (as an object)?
f.e.
export class CalendarEventModel { name="CalendarEvent" props={Id: "int", Title: "string", Start: "datetime", End: "datetime", Calendar: "#Calendar"};
const CalendarSchema = {name: "Calendar", props: {Id: "int", Title:"string", Color:"string"}};
1.
Calendar.insert({Id: 1, Title:"CalendarExample", Color:"#FFFFFF"});
CalendarEvents.insert({Id: 1, Title: "example", Start: Moment(), End: Moment().add(5, "minutes"), Calendar: 1});
2.
CalendarEvent.insert({Id: 1, Title: "example", Start: Moment(), End: Moment().add(5, "minutes"), Calendar: {Id: 1, Title:"CalendarExample", Color:"#FFFFFF"}});

And which types does datetime take (only date() of js or also Moment.js objects)?

Thank you!

@hieunc229
Copy link
Contributor

hieunc229 commented Dec 27, 2018

Hi @apppro123

  1. It will need 2 inserts, for Calendar and CalendarEvents. Besides, Vasern will automatically create an id (lowercase) field.
    (P/s: Custom id has not available yet, you won't need ID in your schema)

    Here is an example, with adding reference object:

    var insertedCalendars = Calendar.insert({ Title:"CalendarExample", Color:"#FFFFFF" });
    
    // Calendar.insert will return an array of objects will be inserted into database with generated id. 
    // For example:
    // insertedCalendars = [
    //     { id: 'a_random_unique_id', Title: "CalendarExample", Color:"#FFFFFF" }
    // ]
    
    var insertedEvents = CalendarEvents.insert({ 
        Title: "example",
        Start: startTime,
        End: endTime,
        Calendar: insertedCalendars[0] // or insertedCalendars[0].id - reference by `id` or actual object
    });
  2. Property with datetime will takes either a Date object or a time value in milliseconds. If you use Momentjs, I think you can use .valueOf() to get time in milliseconds. Though, datetime is not timezone aware.

@apppro123
Copy link
Author

apppro123 commented Dec 27, 2018

You have used insertedCalendars[0] in insertedEvents under Calendar: Is it possible to insert objects? Or is it just possible to insert insertedCalendars[0].id?
And is every generated id unique for this element?

@hieunc229
Copy link
Contributor

I'm a bit confuse about the question, pls let me know if my answer isn't what you are looking for.

  1. Yes, it's possible to insert an array of reference Calendar objects to CalendarEvents. But the schema needs to be changed to:

    var calendarEventSchema = {
       name: "CalendarEvents",
       props: {
          ...,
          Calendars: "[]#Calendar" // [] means list 
       }
    }

    Then you can pass the array of inserted objects like:

    var insertedEvents = CalendarEvents.insert({ 
        Title: "example",
        Start: startTime,
        End: endTime,
        Calendar: insertedCalendars
    });
  2. id generator was inspired with MongoDB ObjectID design, which is highly unique. Though, this element is not uniqued. Currently, to make sure an element is unique, you need to check in the database first (using filter). Sounds like a necessary feature for the upcoming version, I'll take note of this.

Just a head ups, data types will be updated in the next version, so "[]#Calendar" will be deprecated or invalid soon. Also, using lowercase for property name is more preferable.

@apppro123
Copy link
Author

Thank you first.
So is it possible to insert an object into an element of the db? I just need one Calendar for one event.

And ty for the tip with lowercase characters.

@hieunc229
Copy link
Contributor

hieunc229 commented Dec 27, 2018

You're welcome. Yes, you can pass either a Calendar object or its id to CalendarEvents as

// it will automatically get and replace the `id` from `insertedCalendars[0]`
Calendar: insertedCalendars[0] 

@apppro123
Copy link
Author

Ok thanks!

@hieunc229
Copy link
Contributor

No worries!

@hieunc229 hieunc229 changed the title reference How do i manage references? Dec 29, 2018
@hieunc229 hieunc229 added question Further information is requested for FAQ Good question that can be used in FAQ section labels Dec 29, 2018
@hieunc229
Copy link
Contributor

I will close this issue since it has been resolved. Also, feel free to reopen or create a new one again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for FAQ Good question that can be used in FAQ section question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants