Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
expo-sqlite-eloquent-orm
Advanced tools
Expo SQLite Eloquent ORM is a lightweight Object-Relational Mapping (ORM) wrapper for the expo-sqlite
module, designed to provide a fluent and intuitive API for handling database operations in React Native applications. Inspired by Laravel's Eloquent, this library simplifies the process of interacting with SQLite databases by abstracting complex SQL queries into easy-to-understand JavaScript methods.
To install Expo SQLite Eloquent ORM, you need to have an Expo or React Native project set up. Then run:
npm install expo-sqlite-eloquent-orm
# or
yarn add expo-sqlite-eloquent-orm
To get started with expo-sqlite-eloquent-orm
, you'll need to setup your initial migrations and define your models.
expo-sqlite-eloquent-orm
provides a migration system to manage your database schema and versioning. You can define migrations to create and modify tables in a structured manner.
To run migrations, you need to create migration files and then execute them. You can use the Migration
class to handle migrations. Here's an example of how to create and run migrations:
import { Migration } from 'expo-sqlite-eloquent-orm';
// Define your migration scripts
const migrations = {
'1699486848_init': `
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
email TEXT
);
`,
'1699486885_updating_users_table': `
ALTER TABLE users ADD COLUMN active BOOLEAN;
`,
};
// Run migrations
try {
await Migration.runMigrations(migrations);
} catch(error) {
console.error('Error running migrations:', error);
}
Create models by extending the Model
class. Specify your table name and any casts for attributes:
import { Model } from 'expo-sqlite-eloquent-orm';
class User extends Model {
static tableName = 'users';
static casts = {
id: 'number',
active: 'boolean',
// other attributes...
};
// Define relationships, custom methods, etc.
}
Utilize model methods to perform queries:
// Retrieve a user by ID
const user = await User.find(1);
// Get all users with a specific attribute
const activeUsers = await User.where('active', '=', true).get();
// Chain query methods for more complex queries
const specificUsers = await User.select(['id', 'name'])
.where('active', '=', true)
.orderBy('name', 'DESC')
.limit(10)
.get();
To insert new records into the database, create a new instance of your model with the desired attributes, and then call the save
method:
// Create a new user instance
const newUser = new User({
name: 'John Doe',
email: 'john@example.com',
active: true
});
// Insert the new user into the database
await newUser.save();
To update an existing record, retrieve the model instance, set the new attribute values, and then call the save
method:
// Retrieve a user by ID
const user = await User.find(1);
// Update attributes
user.name = 'Jane Doe';
user.email = 'jane.doe@example.com';
// Save the changes to the database
await user.save();
To delete a record from the database, retrieve the model instance and then call the delete
method:
// Retrieve a user by ID
const user = await User.find(1);
// Delete the user from the database
await user.delete();
// Delete all inactive users
await User.where('active', '=', false).delete();
expo-sqlite-eloquent-orm
supports defining and using relationships between models, making it easy to work with related data.
To define a one-to-one relationship between two models, you can use the hasOne
method on the model that declares the relationship. For example, if you have a User
model and a Profile
model where each user has one profile:
class User extends Model {
// ...
async profile() {
return this.hasOne(Profile, 'userId');
}
}
class Profile extends Model {
// ...
}
const user = await User.find(1);
// Automatically loaded
const userProfile = user.profile;
To define a one-to-many relationship, use the hasMany
method. For instance, if each User
can have multiple Post
records:
class User extends Model {
// ...
async posts() {
return this.hasMany(Post, 'userId');
}
}
class Post extends Model {
// ...
}
const user = await User.find(1);
// Automatically loaded
const userPosts = user.posts;
To define a many-to-one relationship, use the belongsTo
method. For example, if each Post
belongs to a single User
:
class Post extends Model {
// ...
async user() {
return this.belongsTo(User, 'userId');
}
}
class User extends Model {
// ...
}
const post = await Post.find(1);
// Automatically loaded
const postUser = await post.user;
[] Many-to-many relationships [] whereRaw [] Bulk eager loading, currently eager loading is n+1 [] Reactivity? Caching with automatic invalidation?
FAQs
An Expo SQLite ORM
The npm package expo-sqlite-eloquent-orm receives a total of 8 weekly downloads. As such, expo-sqlite-eloquent-orm popularity was classified as not popular.
We found that expo-sqlite-eloquent-orm demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.