Simple ORM / Query Builder for Cloudflare D1.
npm i simple-cloudflare-orm
- Finding records by query
- Getting record by it's ID
- Creating a new record
- Updating a record
- Deleting a single record
- Binding with other related tables
- Selecting specific columns
- Getting the number of records
- Searching for a specified pattern in a column
- Tranforming plain result to nested object
- Searching in nested json data
- Case insensitive searching
- Supporting OR/NOT operation
- Bulk creating records
- Prepare a demo for testing
Before starting make sure you have D1 Cloudflare database and defined tables with their column definitions
const tables = {
users: {
id: 'text primary key',
name: 'text',
email: 'text',
password: 'text',
status: 'text',
phone: 'text',
birthdate: 'text',
address: 'text',
photo: 'text',
comment: 'text',
IDCard: 'json',
passport: 'json',
data: 'json',
createdAt: 'DATETIME DEFAULT CURRENT_TIMESTAMP',
updatedAt: 'DATETIME DEFAULT CURRENT_TIMESTAMP',
roleID: 'text'
},
roles: {
id: 'text primary key',
name: 'text',
orgID: 'text',
systemName: 'text'
},
// Other table definitions
}
const simpleORM = new SimpleORM(env.DB, tables)
const options = {
where: [
['status', '=', 'active']
]
}
cosnt activeUsers = await simpleORM.findAll("users", options)
const options = {
where: [
['status', '=', 'active'],
['email', 'LIKE', 'serik']
]
}
cosnt result = await simpleORM.findAll("users", options)
const options = {
where: [
['status', '=', 'active'],
],
orderBy: {
createdAt: 'DESC'
},
}
cosnt newUsers = await simpleORM.findAll("users", options)
const options = {
where: [
['status', '=', 'active'],
],
orderBy: {
createdAt: 'DESC'
},
limit: 100
}
cosnt limitedUsers = await simpleORM.findAll("users", options)
const options = {
include: [
['roles', 'roles.id', 'users.roleID'],
],
where: [
['users.status', '=', 'active'],
],
orderBy: {
createdAt: 'DESC'
},
limit: 100
}
cosnt users = await simpleORM.findAll("users", options)
The response is:
[
{
"id": "serik",
"email": "[email protected]",
"status": "active",
"phone": 77772001991,
"photo": "http://localhost:8788/storage/users/serik/3DBZ7G4YdTCL4UQtx5fnM-square_1200-2.jpg",
"roles": {
"id": "admin",
"name": "admin",
},
"createdAt": 1726353128482,
"updatedAt": 1726353128482
}
]
const input = {
name: 'Berik Shaikamalov',
phone: 77075757091,
status: "active"
}
const newUser = await simpleORM.create('users', input)
To update data pass id and data itself
const input = {
id: 1,
name: 'Berik Shaikamalov',
phone: 77075757091,
status: "active"
}
const updatedUser = await simpleORM.update('users', input.id, input)
To delete user just pass his identifier
await simpleORM.delete('users', input.id)
or delete all records of given table
await simpleORM.deleteAll('users')