Expo SQLite ORM
It is a simple ORM utility to use with expo sqlite
Install
yarn add expo-sqlite-orm
Creating a model
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:
{ superobj } static { return async SQLite } static { return 'animals' } static { return id: type: typesINTEGER primary_key: true // For while only supports id as primary key name: type: typesTEXT not_null: true color: type: typesTEXT age: type: typesNUMERIC another_uid: type: typesINTEGER unique: true timestamp: type: typesINTEGER Date }
Database operations
Drop table
Animal.dropTable()
Create table
Animal.createTable()
Create a record
const props = name: 'Bob' color: 'Brown' age: 2 const animal = propsanimal
or
const props = name: 'Bob' color: 'Brown' age: 2 Animal
Find a record
const id = 1Animal
or
Animal
Update a record
const id = 1const animal = await Animalanimalage = 3animal
or
const props = id: 1 // required age: 3 Animal
Destroy a record
const id = 1Animal
or
const id = 1const animal = await Animalanimal
Destroy all records
Animal
Query
const options = columns: 'id, name' where: age_gt: 2 page: 2 limit: 30 order: 'name ASC' Animal
Where operations
- eq:
=
, - neq:
<>
, - lt:
<
, - lteq:
<=
, - gt:
>
, - gteq:
>=
, - cont:
LIKE
Data types
- INTEGER
- FLOAT
- TEXT
- NUMERIC
- DATE
- DATETIME
- BOOLEAN
- JSON
How to exec a sql manually?
//...another model methods... static { const sql = 'SELECT * FROM table_name WHERE status = ?' const params = 'active' return thisrepositorydatabaseLayer }
or
const databaseLayer = async SQLitedatabaseLayer
Bulk insert or replace?
const databaseLayer = async SQLite 'table_name'const itens = id: 1 color: 'green' id: 2 color: 'red'databaseLayer
Changelog
- 1.5.0 - Return unlimited rows if
page
is not specified in thequery
params
Development
docker-compose run --rm bump # patch docker-compose run --rm bump --minor # minor git pushgit push --tags
Test
docker-compose run --rm app installdocker-compose run --rm app test
Author
License
This project is licensed under MIT License