You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lio cdf7557fcf Update 'deps.ts' 7 months ago
.github/workflows Remove unstable keyword in test suites 10 months ago
design Initial commit 1 year ago
docs/v1.0.21-migration Update plugin approach comments and documentation 9 months ago
lib add overloads to `where` method 9 months ago
tests Remove unused await for database.link call 10 months ago
.gitattributes style: force developers to use this project EOL (LF) 11 months ago
.gitignore Better commit 10 months ago
LICENSE Add license 1 year ago
README.md docs(migration): :memo: clean readme and move migration to own folder 10 months ago
connector-mod.ts style(format): format using deno fmt 10 months ago
deps.ts Update 'deps.ts' 7 months ago
mod.ts fix(): fix some exporting complain 10 months ago

README.md

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