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

swagger-orm-express

v1.0.1

Published

create api server and document with only orm definition.

Downloads

3

Readme

swagger-orm-express

create api server and document with only orm definition.

thanks to swagger-ui-express & jsdoc.

Install

npm install swagger-orm-express

Getting started

define your class

const { Swagger, ORM } = require('swagger-orm-express');
class Tag {
    id = 0;
    name = '';
}
class Pet extends ORM {
    id = 0;
    name = '';
    tags = Array.of(new Tag());
}

connect to your database and insert a record (remember to install better-sqlite3 first)

const db = ORM.Client();
db.connect({ driver: 'better-sqlite3', database: 'ORM.sqlite' }).then(async ()=>{
    await new Pet().set({id:1,name:'cat'}).insert(db);
})

create your server

const app = new Swagger({ Pet });
const PORT = 3000;

app.get('/pet/:petId', async (req,res)=>{
    const {petId} = req.params;
    const pet = await new Pet().eq({id:petId}).find(db);
    res.json(pet);
}).ok(Pet).tag('pet')

app.createApiDocs('/api-docs', '/swagger.json');
app.listen(PORT, () => console.log(`http://localhost:${PORT}/api-docs`));

CRUD raw query vs orm

// schema
class User { id; name; account; createdAt=new Date(); }
await new User().constraint({ id: 'INTEGER PRIMARY KEY AUTOINCREMENT' }).migrate(db);
// create
await db.write(`INSERT INTO User(name, account, createdAt) VALUES('', 'admin', '"2023-02-21T09:18:49.736Z"')`);
await new User().set({ account: 'admin' }).insert(db);
// read one
await new User().raw(`SELECT * FROM User WHERE name LIKE :name`, { name: 'ad%' }).find(db);
// read all
await db.read(`SELECT id, name, account, createdAt FROM User WHERE name LIKE 'ad%' AND id IN(0, 1, 2) ORDER BY id ASC, name DESC LIMIT 5`);
await new User().like({ account: 'ad%' }).in({ id: [0, 1, 2] }).orderBy({ id: 'ASC', name: 'DESC' }).limit(5).findAll(db).then(users => users[0].id);
// read value
await new User().raw(`SELECT COUNT(*) FROM User`).findValue(db);
// update
await db.write(`UPDATE User SET name = 'admin' WHERE account = 'admin'`);
await new User().set({ name: 'admin' }).eq({ account: 'admin' }).update(db);

Run with example

create a petscore with under 200 lines of code.

node petstore.js