`npm i an-expo-sqlite-orm` or `yarn add an-expo-sqlite-orm`
npm install an-expo-sqlite-ormnpm i an-expo-sqlite-orm or yarn add an-expo-sqlite-orm
import { SQLiteModel, SQLiteModelSchema } from "an-expo-sqlite-orm";
export interface TodoFields {
title: string;
completed: boolean;
}
export interface TodoSchema {
fields: TodoFields,
children:{},
parents:{}
}
export class TodoModel extends SQLiteModel {
tableName = "todo"
getSchema(): SQLiteModelSchema<{ fields: TodoFields; children: {}; parents: {}; }> {
return {
fields:{
title: {type:'TEXT'},
completed: {type: 'BOOLEAN'},
},
children: {},
parents: {},
}
}
}
`
$3
`
import { initDatabase, resetDatabase, SQLiteModel } from "an-expo-sqlite-orm";
import { TodoModel } from "./models/todo";
export const models: SQLiteModel[] = [
new TodoModel()
];
export default (() => {
return initDatabase(models);
return resetDatabase(models);
})();
`
$3
import './db/init'
How working
$3
`
const todo = new TodoModel()
// todo.objects.[method].run() run is Promise return value depends on [method]
`
$3
`
todo.objects.create({
// todo object
}).run().then((newCreateItem)=>{
console.log('item created' , newCreateItem)
})
`
$3
`
todo.objects.filter({id:[id:number]}).update({
// todo object
}).run().then((val:SQLite.SQLiteRunResult)=>{
console.log(val)
})
`
$3
`
todo.objects.filter({id:[id:number]}).delete().run().then((val:SQLite.SQLiteRunResult)=>{
console.log(val)
})
`
$3
`
todo.objects.first().run().the(item=>{
console.log('first item', item)
}).catch(()=>{
// item is not found
})
`
$3
`
// in todo getSchema fields add useInSearch:true in any field you want to search
todo.objects.search('hello').run().the(items=>{
console.log('items', items)
})
`
$3
`
todo.objects.pagination(1,20).run().the(items=>{
console.log('items of page 1 if pageSize is 20', items)
})
``