dflourusso / expo-sqlite-orm

Expo SQLite ORM
139 stars 34 forks source link

"not_null" in columnMapping, create record with empty string, query and return null instead of empty string #4

Closed YKSing closed 5 years ago

dflourusso commented 5 years ago

Could you provide your model code please?

YKSing commented 5 years ago

I created a new expo project and just copy the example of "Animal"

Animal.js

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

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

    static database() { // Here should take out "get" or exception occurred
        return Promise.resolve(SQLite.openDatabase('database.db'))
    }

    static get tableName() {
        return 'animals'
    }

    static get columnMapping() {
        return {
            id: { type: types.INTEGER, primary_key: true },
            name: { type: types.TEXT, not_null: true },
            color: { type: types.TEXT },
            age: { type: types.NUMERIC },
            another_uid: { type: types.INTEGER, unique: true },
            timestamp: { type: types.INTEGER, default: () => Date.now() }
        }
    }
}

App.js

import React from 'react';
import { View } from 'react-native';
import Animal from './Animal';

export default class App extends React.Component {
    async componentDidMount() {
        await Animal.createTable();
        await Animal.create({
            id: 1,
            name: '', // <- empty string
            color: 'Brown',
            age: 2
        });
        const createdAnimal = await Animal.find(1);
        console.log(createdAnimal);
    }

    render() {
        return ( <View /> );
    }
}

console.log Result

Animal {
  "age": 2,
  "another_uid": null, 
  "color": "Brown",
  "id": 1,
  "name": null, // not empty string
  "timestamp": 1547518269573,
}
YKSing commented 5 years ago

Here are more information

console.log(await Animal.find(1));

// return
Animal {
  "age": 2,
  "another_uid": null,
  "color": "Brown",
  "id": 1,
  "name": null, // return null here which is incorrect
  "timestamp": 1547520079013, // this value will change every time
}
console.log(await Animal.query());

// return
Array [
  Object { // maybe this should instance of Animal object?
    "age": 2,
    "another_uid": null,
    "color": "Brown",
    "id": 1,
    "name": "", // return empty string here which is correct
    "timestamp": null, // should not be null here?
  },
]

Dose the default means "SQL" default value when insert into sqlite? Or it is just a "Logically" default value for returning when SQL value is null?

dflourusso commented 5 years ago

Thanks for contribution, i will check it

dflourusso commented 5 years ago

Hi, thanks for contribution, it was fixed in v1.4.2.

Test now to check if it is as expected.