prisma-indexeddb
v1.0.6
Published
Prisma-like ORM for IndexedDB with React hooks and CLI tools
Maintainers
Readme
Prisma IndexedDB
A Prisma-like ORM wrapper for IndexedDB with TypeScript support. This library provides a familiar Prisma-like API for working with IndexedDB in browser environments.
Features
- Prisma-like API for IndexedDB
- Full TypeScript support
- Relationship handling (one-to-one, one-to-many, many-to-one)
- Advanced filtering and sorting
- Pagination support
- Transaction support
Installation
npm install prisma-indexeddbBasic Usage
import PrismaIndexedDB from 'prisma-indexeddb';
// Define your schema
const schema = {
User: {
id: { type: 'string', required: true, unique: true },
name: { type: 'string', required: true },
email: { type: 'string', required: true, unique: true, index: true },
posts: {
type: 'object',
relation: {
model: 'Post',
type: 'oneToMany',
foreignKey: 'authorId',
references: 'id'
}
}
},
Post: {
id: { type: 'string', required: true, unique: true },
title: { type: 'string', required: true },
content: { type: 'string' },
published: { type: 'boolean', default: false },
authorId: { type: 'string', required: true, index: true },
author: {
type: 'object',
relation: {
model: 'User',
type: 'manyToOne',
foreignKey: 'authorId',
references: 'id'
}
}
}
};
// Initialize the client
const db = new PrismaIndexedDB('my-database', schema, 1);
// Connect to the database
await db.$connect();
// Create a new user
const user = await db.User.create({
data: {
name: 'John Doe',
email: '[email protected]'
}
});
// Create a post for the user
const post = await db.Post.create({
data: {
title: 'Hello World',
content: 'This is my first post',
published: true,
authorId: user.id
}
});
// Query with relationships
const userWithPosts = await db.User.findUnique({
where: { id: user.id },
include: { posts: true }
});
// Disconnect when done
await db.$disconnect();API Reference
Client
new PrismaIndexedDB(dbName, schema, version?)- Create a new client instance$connect()- Connect to the database$disconnect()- Disconnect from the database$transaction(queries)- Execute queries in a transaction
Models
Each model defined in your schema is available as a property on the client instance with the following methods:
findMany(options?)- Find multiple recordsfindUnique(where)- Find a unique recordfindFirst(options?)- Find the first record matching the optionscreate(input)- Create a new recordupdate(input)- Update an existing recorddelete(input)- Delete a recordcount(options?)- Count recordsaggregate(options)- Perform aggregations
Query Options
where- Filter conditionsorderBy- Sorting optionstake- Limit the number of recordsskip- Skip a number of recordsinclude- Include related recordsselect- Select specific fields
License
MIT
