only-cliches / Nano-SQL

Universal database layer for the client, server & mobile devices. It's like Lego for databases.
https://nanosql.io
MIT License
782 stars 49 forks source link

Problems structuring a model in which data follows indexing pattern #185

Closed ChrisMichaelPerezSantiago closed 5 years ago

ChrisMichaelPerezSantiago commented 5 years ago

Hello everyone,

I'm having trouble developing the property structure model episodes.

"episodes": {
  "1": {
      "episode": 1,
      "id": "53099 / psychopass-3-1"
   },
   "nextEpisodeDate": "2019-10-31"
 }

The episodes property is structured by indexing by the property episode. If episode is 1 then the structure should be: "1": {}, if the episode is 2 it should be "2": {}, and so on with the others

 const data =   {
      "title": "Psycho-Pass 3",
      "poster": "https://animeflv.net/uploads/animes/covers/3225.jpg",
      "synopsis": "Anime 4.8En un futuro próximo, es posible medir de forma instantánea el estado mental de una persona, la personalidad y la probabilidad de que dicha persona vaya a cometer delitos gracias a un escáner psico-somático que realiza un escaneo de las funciones del cerebro y demás funciones biológicas y quí...",
      "debut": "estreno",
      "type": "Anime",
      "rating": "4.8",
      "episodes": {
        "1": {
          "episode": 1,
          "id": "53099/psychopass-3-1"
        },
        "nextEpisodeDate": "2019-10-31"
      }
    },

createDatabase From the latestAnimeAdded table model, the only data that does not show me is of the episodes property.

const nSQL = require('@nano-sql/core').nSQL;

nSQL().createDatabase({
  id: 'animeflydb',
  model: 'PERM',
  tables: [
    {
      name: 'latestAnimeAdded',
      model:{
        "title:string": {},
        "poster:string": {},
        "synopsis:string": {},
        "debut:string": {},
        "type:string": {},
        "rating:string": {},
        "episodes:array": {
          "number":{
            "episode:string": {},
            "id:string": {}
          },
          "nextEpisodeDate:string": {}
        },
      }
    }
  ],
  version: 1,
  onVersionUpdate: (prevVersion) =>{
    return new Promise((resolve, reject) =>{
      switch(prevVersion){
        case 1:
          resolve(2);
          break;
        case 2:
          resolve(3);
          break;
      }
    })
  }
});

upsert


   nSQL().useDatabase('animeflydb');
   nSQL('latestAnimeAdded');

   nSQL()
    .query('upsert' , [{...data}])
    .exec()
    .then(d =>{
      console.log(d)
    })
ChrisMichaelPerezSantiago commented 5 years ago

Problem solved

The episodes property did not show me the data because it was defined as an array, now, I made the change to episodes:object and yes, it worked.

tables: [
    {
      name: 'latestAnimeAdded',
      model:{
        "title:string": {},
        "poster:string": {},
        "synopsis:string": {},
        "debut:string": {},
        "type:string": {},
        "rating:string": {},
        "episodes:object": {
          "eps:string": {
            "episode:number": {},
            "id:string": {}
          },
          "nextEpisodeDate:string": {}
        },
      }
    }
  ],