Product
Socket Now Supports uv.lock Files
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
expo-sqlite-orm
Advanced tools
It is a simple ORM utility to use with expo sqlite
Warn: it works only on iOS and Android. Web is not supported (SEE)
yarn add expo-sqlite-orm
You need to provide 3 things:
databaseName
: Name of the database to be created/used by expo SQLitetableName
: The name of the tablecolumnMapping
: The columns for the model and their types
type
, primary_key
, autoincrement
, not_null
, unique
, default
import { Text } from '@components'
import { ColumnMapping, columnTypes, IStatement, Migrations, Repository, sql } from 'expo-sqlite-orm'
import React, { useMemo, useState } from 'react'
import { ScrollView } from 'react-native'
import { RootTabScreenProps } from '../../navigation/types'
/**
* Expo Sqlite ORM V2 - Usage example
*/
interface Animal {
id: number
name: string
color: string
age: number
another_uid?: number
timestamp?: number
}
const columMapping: ColumnMapping<Animal> = {
id: { type: columnTypes.INTEGER },
name: { type: columnTypes.TEXT },
color: { type: columnTypes.TEXT },
age: { type: columnTypes.NUMERIC },
another_uid: { type: columnTypes.INTEGER },
timestamp: { type: columnTypes.INTEGER, default: () => Date.now() },
}
const statements: IStatement = {
'1662689376195_create_animals': sql`
CREATE TABLE animals (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
color TEXT,
age NUMERIC,
another_uid TEXT UNIQUE,
timestamp INTEGER
);`,
}
const databaseName = 'dbName'
export function MeusServicosScreen({ navigation }: RootTabScreenProps<'MeusServicos'>) {
const [animals, setAnimals] = useState<Animal[]>([])
const migrations = useMemo(() => new Migrations(databaseName, statements), [])
const animalRepository = useMemo(() => {
return new Repository(databaseName, 'animals', columMapping)
}, [])
const onPressRunMigrations = async () => {
await migrations.migrate()
}
const onPressReset = async () => {
await migrations.reset()
setAnimals([])
}
const onPressInsert = () => {
animalRepository.insert({ name: 'Bob', color: 'Brown', age: 2 }).then((createdAnimal) => {
console.log(createdAnimal)
})
}
const onPressQuery = () => {
animalRepository.query({ where: { age: { gte: 1 } } }).then((foundAnimals) => {
console.log(foundAnimals)
setAnimals(foundAnimals)
})
}
return (
<ScrollView>
<Text type="text2" onPress={onPressRunMigrations}>
Migrate
</Text>
<Text type="text2" onPress={onPressReset}>
Reset Database
</Text>
<Text type="text2" onPress={onPressInsert}>
Insert Animal
</Text>
<Text type="text2" onPress={onPressQuery}>
List Animals
</Text>
<Text type="text2">{JSON.stringify(animals, null, 1)}</Text>
</ScrollView>
)
}
const props: Animal = {
name: 'Bob',
color: 'Brown',
age: 2
}
animalRepository.insert(props)
const id = 1
animalRepository.find(id)
or
animalRepository.findBy({ age: { equals: 12345 }, color: { contains: '%Brown%' } })
const props = {
id: 1 // required
age: 3
}
animalRepository.update(props)
const id = 1
animalRepository.destroy(id)
animalRepository.destroyAll()
const options = {
columns: 'id, name',
where: {
id: { in: [1, 2, 3, 4] },
age: { gt: 2, lt: 10 }
},
page: 2,
limit: 30,
order: { name: 'ASC' }
}
animalRepository.query(options)
The property
page
is applied only if you pass thelimit
as well
Where operations
=
,<>
,<
,<=
,>
,>=
,LIKE
IN (?)
NOT IN (?)
myCustomMethod() {
const sql = 'SELECT * FROM table_name WHERE status = ?'
const params = ['active']
return animalRepository.databaseLayer.executeSql(sql, params).then(({ rows }) => rows)
}
const itens = [{id: 1, color: 'green'}, {id: 2, color: 'red'}]
animalRepository.databaseLayer.bulkInsertOrReplace(itens).then(response => {
console.log(response)
})
import * as SQLite from 'expo-sqlite/legacy'
import { Migrations, sql } from 'expo-sqlite-orm'
const statements: IStatement = {
'1662689376195_init': sql`CREATE TABLE animals (id TEXT, name TEXT);`,
'1662689376196_add_age_column': sql`ALTER TABLE animals ADD age NUMERIC;`,
'1662689376197_add_color_column': sql`ALTER TABLE animals ADD color TEXT;`
}
const migrations = new Migrations('databaseName', statements)
await migrations.migrate()
const migrations = new Migrations('databaseName', statements)
await migrations.reset()
page
is not specified in the query
paramsautoincrement
property to be optionaldocker-compose run --rm bump # patch
docker-compose run --rm bump --minor # minor
git push
git push --tags
docker-compose run --rm app install
docker-compose run --rm app test
This project is licensed under MIT License
FAQs
Simple orm for expo
The npm package expo-sqlite-orm receives a total of 171 weekly downloads. As such, expo-sqlite-orm popularity was classified as not popular.
We found that expo-sqlite-orm demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
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.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.