Coder Social home page Coder Social logo

kt3k / denodb Goto Github PK

View Code? Open in Web Editor NEW

This project forked from eveningkid/denodb

0.0 0.0 0.0 628 KB

MySQL, SQLite, MariaDB, PostgreSQL and MongoDB ORM for Deno

Home Page: https://eveningkid.com/denodb-docs

License: MIT License

TypeScript 100.00%

denodb's Introduction

DenoDB

  • 🗣Supports PostgreSQL, MySQL, MariaDB, SQLite and MongoDB
  • 🔥Simple, typed API
  • 🦕Deno-ready
  • Read the documentation
import { DataTypes, Database, Model, PostgresConnector } from 'https://deno.land/x/denodb/mod.ts';

const connection = new PostgresConnector({
  host: '...',
  username: 'user',
  password: 'password',
  database: 'airlines',
});

const db = new Database(connection);

class Flight extends Model {
  static table = 'flights';
  static timestamps = true;

  static fields = {
    id: { primaryKey: true, autoIncrement: true },
    departure: DataTypes.STRING,
    destination: DataTypes.STRING,
    flightDuration: DataTypes.FLOAT,
  };

  static defaults = {
    flightDuration: 2.5,
  };
}

db.link([Flight]);

await db.sync({ drop: true });

await Flight.create({
  departure: 'Paris',
  destination: 'Tokyo',
});

// or

const flight = new Flight();
flight.departure = 'London';
flight.destination = 'San Francisco';
await flight.save();

await Flight.select('destination').all();
// [ { destination: "Tokyo" }, { destination: "San Francisco" } ]

await Flight.where('destination', 'Tokyo').delete();

const sfFlight = await Flight.select('destination').find(2);
// { destination: "San Francisco" }

await Flight.count();
// 1

await Flight.select('id', 'destination').orderBy('id').get();
// [ { id: "2", destination: "San Francisco" } ]

await sfFlight.delete();

await db.close();

First steps

Setting up your database with DenoDB is a four-step process:

  • Create a database, using Database (learn more about clients):

    const connection = new PostgresConnector({
      host: '...',
      username: 'user',
      password: 'password',
      database: 'airlines',
    });
    
    const db = new Database(connection);
  • Create models, extending Model. table and fields are both required static attributes:

    class User extends Model {
      static table = 'users';
    
      static timestamps = true;
    
      static fields = {
        id: {
          primaryKey: true,
          autoIncrement: true,
        },
        name: DataTypes.STRING,
        email: {
          type: DataTypes.STRING,
          unique: true,
          allowNull: false,
          length: 50,
        },
      };
    }
  • Link your models, to add them to your database instance:

    db.link([User]);
  • Optional: Create tables in your database, by using sync(...):

    await db.sync();
  • Query your models!

    await User.create({ name: 'Amelia' });
    await User.all();
    await User.deleteById('1');

Migrate from previous versions

License

MIT License — eveningkid

denodb's People

Contributors

0xtlt avatar aiko-suzuki avatar alfierichou avatar borsemayur2 avatar coteh avatar cs-pedro-abreu-azevedo avatar dani-beltran avatar eveningkid avatar fzn0x avatar g-plane avatar georgzepf avatar leonardofreitass avatar michaelgaultjr avatar rluvaton avatar sntg-p avatar stillalivx avatar u-ways avatar vmasdani avatar youngyou avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.