npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

expo-orm-sqlite

v1.5.3

Published

Simple orm for expo

Downloads

6

Readme

Expo SQLite ORM

Build Status

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 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 'expo-sqlite'
import { BaseModel, types } from 'expo-sqlite-orm'

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

  static get database() {
    return async () => 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() }
    }
  }
}

Database operations

Drop table

Animal.dropTable()

Create table

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

Find a record

const id = 1
Animal.find(id)

or

Animal.findBy({ age_eq: 12345, color_cont: '%Brown%' })

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 'expo-sqlite'
import DatabaseLayer from 'expo-sqlite-orm/src/DatabaseLayer'

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

Bulk insert or replace?

import { SQLite } from 'expo-sqlite'
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.5.0 - Return unlimited rows if page is not specified in the query params

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

Author

License

This project is licensed under MIT License