dflourusso / expo-sqlite-orm

Expo SQLite ORM
139 stars 34 forks source link

Example request #17

Closed ermanakturk closed 4 years ago

ermanakturk commented 5 years ago

Hi dear, Where I can find codes of a running example app? Thank you.

vzpintor commented 5 years ago

I get the next error

Possible Unhandled Promise Rejection (id: 1): Object {}

this is my code

Cart.createTable();

export default class Cart extends AbstractModel {

    constructor(obj: Cart) {
        super(obj)
    }

    static get tableName() {
        return 'cart'
    }

    static findAll() {
        const sql = 'SELECT * FROM cart';
        const params = [null];
        // @ts-ignore
        return this.repository.databaseLayer.executeSql(sql, params).then(({rows}: any) => rows)
    }

    static get columnMapping() {
        return {
            id: {type: types.INTEGER, primary_key: true},
            price: {type: types.NUMERIC},
            title: {type: types.TEXT},
            rating: {type: types.FLOAT},
            complements: {type: types.JSON},
            garrisons: {type: types.JSON},
            comments: {type: types.TEXT},
            count: {type: types.NUMERIC},
            timestamp: {type: types.DATETIME, default: () => Date.now()}
        }
    }

}

const DATABASE_NAME = 'name.db';

export default class AbstractModel extends BaseModel {

    constructor(obj: AbstractModel) {
        super(obj)
    }

    static get database() {
        return async () => SQLite.openDatabase(DATABASE_NAME)
    }
}

And this is when I call my custom query

    async componentDidMount() {
        const products = await Cart.findAll();
        console.log('Todo el carrito:\n', products);

        this.setState({products});
    }
dflourusso commented 5 years ago

Hi, everything is asyncronous, try:

await Cart.createTable();

// And after
const products = await Cart.query()
werterzz commented 4 years ago

In test.js

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

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

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

  static get tableName() {
    return 'Test'
  }

  static get columnMapping() {
    return {`
      id: { type: types.INTEGER, primary_key: true }, // For while only supports id as primary key
      name: { type: types.TEXT},

    }
  }
}

In App.js

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Test from './test'

const props = {
  name: 'Bob',
}

async function main () {
  try {

    let result = await Test.create(props);
    console.log(result)
  } catch (error) {
    console.log(error)
  }
}

main()

export default function App() {
  return (
    <View style={styles.container}>
      <Text>Open up App.js to start working on your app!</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

I got undefine with Test object. How can I use it, please help thank?

werterzz commented 4 years ago

Now I use

    async function main () {

        let result = await Emojitions.findBy({emo_ID_eq: 1})
        return(result)

    }
    console.log(main())

and i got

Promise {
  "_40": 0,
  "_55": null,
  "_65": 0,
  "_72": null,
}
Possible Unhandled Promise Rejection (id: 2):
Object {}

Please help thank.