dflourusso / expo-sqlite-orm

Expo SQLite ORM
132 stars 34 forks source link

any operation return same weird object #58

Open taisen opened 1 year ago

taisen commented 1 year ago

Hello,

I tried to use the expo-sqlite-orm but any operation (createTable, find, findBy..) returned

{"_h": 0, "_i": 0, "_j": null, "_k": null}

can you please help?

The code

  const param = new Parameter(props);
  param.save();

  let paramInDb = Parameter.findBy({ id_eq: 1 });
  console.log("paramInDb 1");
  console.log(paramInDb);
  console.log("paramInDb 2");
  console.log(Parameter.find(1));
  console.log(Parameter.query());

The Parameter class

import * as SQLite from "expo-sqlite"
import { BaseModel, types } from 'expo-sqlite-orm'

export default class Parameter extends BaseModel {
constructor(obj) {
  super(obj)
}

static get database() {
  return async () => SQLite.openDatabase('videothequesss.db')
}

static get tableName() {
  return 'parameters'
}

static get columnMapping() {
  return {
    id: { type: types.INTEGER, primary_key: true },
    nameParam:{ type: types.TEXT },
    valueParam: { type: types.TEXT }
  }
}

}

taisen commented 1 year ago

Seems to be a promise object, so tried to use await and i have following result

The code

console.log(await Parameter.createTable());
  const props = {
    id: 1,
    nameParam: 'name parameter',
    valueParam: 'value parameter'
  }

  const param = new Parameter(props);
  param.save();
  console.log("param save",param);

  let paramInDb = await Parameter.findBy({ id_eq: 1 });
  console.log(".findBy({ id_eq: 1 })",paramInDb);
  console.log("find(1)",await Parameter.find(1));
  console.log("query",await Parameter.query());

The result

LOG true LOG true LOG param save {"id": 1, "nameParam": "name parameter", "valueParam": "value parameter"} LOG .findBy({ id_eq: 1 }) null LOG find(1) null LOG query []

Can you help? Thanks, Michaël.

taisen commented 1 year ago

For a reason i don't understand if i add autoincrement: true and remove the "forced" id from the object to save, the object is correclty saved and retrieved

the following is working

const props = {
    nameParam: 'name parameter',
    valueParam: 'value parameter'
  }//remove the "forced" id property

  const param = new Parameter(props);
  param.save();//correctly saved!

import * as SQLite from "expo-sqlite"
import { BaseModel, types } from 'expo-sqlite-orm'

export default class Parameter extends BaseModel {
constructor(obj) {
  super(obj)
}

static get database() {
  return async () => SQLite.openDatabase('videothequesss.db')
}

static get tableName() {
  return 'parameters'
}

static get columnMapping() {
  return {
    id: { type: types.INTEGER, primary_key: true, autoincrement: true},
    nameParam:{ type: types.TEXT },
    valueParam: { type: types.TEXT }
  }
}

}