Socket
Socket
Sign inDemoInstall

react-native-sqlite-orm

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-native-sqlite-orm

Rails-style sqlite ORM for React Native


Version published
Weekly downloads
6
increased by50%
Maintainers
1
Install size
18.4 kB
Created
Weekly downloads
 

Readme

Source

React Native SQLite ORM

It is a simple ORM utility to use with React Native sqlite storage

Warn: it works only on iOS and Android. Web is not supported (SEE)

Install

yarn add react-native-sqlite-orm

Creating a model

You need to provide 3 things:

  • database: Instance of expo SQLite or promise with that instance
  • tableName: The name of the table
  • columnMapping: The columns for the model and their types
    • Supported options: type, primary_key, not_null, unique, default
import SQLite from 'react-native-sqlite-storage'
import { BaseModel, types } from 'expo-sqlite-orm'

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

  static get database() {
    return async () => SQLite.openDatabase({ name: '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() }
    }
  }
}

Database operations

Drop table

Animal.dropTable()

Create table

Create a new table if it doesn't exist

Animal.createTable()

Create a record

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(new Animal(props))

Find a record

const id = 1
Animal.find(id).then(animal => animal.name)

or

Animal.findBy({ age_eq: 12345, color_cont: '%Brown%' }).then(animal => animal.name)

Update a record

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)

Destroy a record

const id = 1
Animal.destroy(id)

or

const id = 1
const animal = await Animal.find(id)
animal.destroy()

Destroy all records

Animal.destroyAll()

Query

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

Data types

  • INTEGER
  • FLOAT
  • TEXT
  • NUMERIC
  • DATE
  • DATETIME
  • BOOLEAN
  • JSON

How to exec a sql manually?

import { BaseModel } from 'expo-sqlite-orm'

export default class Example extends BaseModel {
  //...another model methods...
  static myCustomMethod() {
    const sql = 'SELECT * FROM table_name WHERE status = ?'
    const params = ['active']
    return this.repository.databaseLayer.executeSql(sql, params).then(({ rows }) => rows)
  }
}

or

import SQLite from 'react-native-sqlite-storage'
import DatabaseLayer from 'expo-sqlite-orm/src/DatabaseLayer'

const databaseLayer = new DatabaseLayer(async () => SQLite.openDatabase({ name: 'database_name' }))
databaseLayer.executeSql('SELECT * from table_name;').then(response => {
  console.log(response)
})

Bulk insert or replace?

import SQLite from 'react-native-sqlite-storage'
import DatabaseLayer from 'expo-sqlite-orm/src/DatabaseLayer'

const databaseLayer = new DatabaseLayer(async () => SQLite.openDatabase('database_name'), 'table_name')
const itens = [{id: 1, color: 'green'}, {id: 2, color: 'red'}]
databaseLayer.bulkInsertOrReplace(itens).then(response => {
  console.log(response)
})

Changelog

  • 1.0.0 - initial version

Development

docker-compose run --rm bump         # patch
docker-compose run --rm bump --minor # minor

git push
git push --tags

Test

docker-compose run --rm app install
docker-compose run --rm app test

Working examples

TBA

Authors

Inspired by

Upgraded by

License

This project is licensed under MIT License

Keywords

FAQs

Last updated on 25 Feb 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc