npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-cloudflare-orm

v1.0.9

Published

Simple ORM for working with DB on Cloudflare

Downloads

47

Readme

Simple ORM / Query Builder for Cloudflare D1.

npm i simple-cloudflare-orm

Features

  • [x] Finding records by query
  • [x] Getting record by it's ID
  • [x] Creating a new record
  • [x] Updating a record
  • [x] Deleting a single record
  • [x] Binding with other related tables
  • [x] Selecting specific columns
  • [x] Getting the number of records
  • [x] Searching for a specified pattern in a column
  • [x] Tranforming plain result to nested object
  • [x] Searching in nested json data
  • [ ] Case insensitive searching
  • [ ] Supporting OR/NOT operation
  • [ ] Bulk creating records
  • [ ] Prepare a demo for testing

Usage

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
}

Pass DB instance of Cloudflare to the class

const simpleORM = new SimpleORM(env.DB, tables)

Examples

Get active users

const options = {
    where: [
        ['status', '=', 'active']
    ]    
}
cosnt activeUsers = await simpleORM.findAll("users", options)

Searching by column name

const options = {
    where: [
        ['status', '=', 'active'],
        ['email', 'LIKE', 'serik']
    ]    
}
cosnt result = await simpleORM.findAll("users", options)

Get new users

const options = {
    where: [
        ['status', '=', 'active'],        
    ],
    orderBy: {
        createdAt: 'DESC'
    },
}
cosnt newUsers = await simpleORM.findAll("users", options)

Get only first 100 users

const options = {
    where: [
        ['status', '=', 'active'],        
    ],
    orderBy: {
        createdAt: 'DESC'
    },
    limit: 100
}
cosnt limitedUsers = await simpleORM.findAll("users", options)

Also you can join with other tables

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
    }
]

Inserting data

    const input = {
        name: 'Berik Shaikamalov',
        phone: 77075757091,
        status: "active"
    }
    const newUser = await simpleORM.create('users', input)

Updating data

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)

Deleting data

To delete user just pass his identifier

await simpleORM.delete('users', input.id)

or delete all records of given table

await simpleORM.deleteAll('users')