It is a simple ORM utility to use with expo sqlite
yarn add expo-sqlite-orm
You need to provide 3 things:
database
: Instance of expo SQLite or promise with that instancetableName
: The name of the tablecolumnMapping
: The columns for the model and their types- Supported options:
type
,primary_key
,not_null
,unique
,default
- Supported options:
import { SQLite } from 'expo'
import { BaseModel, types } from 'expo-sqlite-orm'
export default class Animal extends BaseModel {
constructor(obj) {
super(obj)
}
static get database() {
return Promise.resolve(SQLite.openDatabase('database.db'))
}
static get tableName() {
return 'animals'
}
static get columnMapping() {
return {
id: { type: types.INTEGER, primary_key: true }, // For while only supports id as primary key
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() }
}
}
}
Animal.dropTable()
Animal.createTable()
const props = {
name: 'Bob',
color: 'Brown',
age: 2
}
const animal = new Animal(props)
animal.save()
or
const props = {
name: 'Bob',
color: 'Brown',
age: 2
}
Animal.create(props)
const id = 1
Animal.find(id)
or
Animal.findBy({ age_eq: 12345, color_cont: '%Brown%' })
const id = 1
const animal = await Animal.find(id)
animal.age = 3
animal.save()
or
const props = {
id: 1 // required
age: 3
}
Animal.update(props)
const id = 1
Animal.destroy(id)
or
const id = 1
const animal = await Animal.find(id)
animal.destroy()
Animal.destroyAll()
const options = {
columns: 'id, name',
where: {
age_gt: 2
},
page: 2,
limit: 30,
order: 'name ASC'
}
Animal.query(options)
Where operations
- eq:
=
, - neq:
<>
, - lt:
<
, - lteq:
<=
, - gt:
>
, - gteq:
>=
, - cont:
LIKE
- INTEGER
- FLOAT
- TEXT
- NUMERIC
- DATE
- DATETIME
- BOOLEAN
- JSON
This project is licensed under MIT License