@0stv0/databases
v1.0.0
Published
```bash npm install @0stv0/databases ```
Readme
Installation
npm install @0stv0/databasesMySQL
import { MySQL } from "@0stv0/databases";
type TestUser = {
id: number;
name: string;
lastname: string;
};
await MySQL.connect({
host: 'localhost',
port: 3306, // Optional, default 3306
user: 'root',
password: '', // Optional, default empty
database: 'database',
keepAlive: true // Optional, default false
});
let users: TestUser[] | null = await MySQL.query<TestUser[]>("SELECT * FROM users WHERE id > ?", [10]);
await MySQL.execute("INSERT INTO users (id) VALUES (?)", [123]);
await MySQL.transaction(async(con) =>
{
await con.execute("somesql", [1, 2, 3]);
await con.execute("somesql2", [3, 2, 1]);
// You can return something here if needed
});
await MySQL.close();PostgreSQL
import { Postgres } from "@0stv0/databases";
type TestUser = {
id: number;
name: string;
lastname: string;
};
await Postgres.connect({
host: 'localhost',
port: 5432, // Optional, default 5432
user: 'root',
password: '', // Optional, default empty
database: 'database',
keepAlive: true, // Optional, default false
ssl: true // Optional, default false
});
let data: TestUser[] | null = await Postgres.query<TestUser[]>("SELECT * FROM users WHERE id > $1", [10]);
await Postgres.query("INSERT INTO users (id) VALUES ($1)", [123]);
await Postgres.transaction(async(con) =>
{
await con.query("somesql", [1, 2, 3]);
await con.query("somesql2", [3, 2, 1]);
// You can return something here if needed
});
await Postgres.close();MongoDB
import { Collection } from "mongodb";
import { Mongo } from "@0stv0/databases";
type TestUser = {
id: number;
name: string;
lastname: string;
};
await Mongo.connect({
uri: 'your-db-uri',
db: 'your-db'
});
let col: Collection<TestUser> | null = Mongo.getCollection<TestUser>('users');
await Mongo.transaction(async(session, db) =>
{
db.collection("somecoll").insertOne({somekey: 'someval'}, { session });
db.collection("somecoll").deleteOne({somekey2: 'someval2'}, { session });
// YOu can return something here if needed
});
await Mongo.close();Redis
import { Redis } from "@0stv0/databases";
await Redis.connect({
url: 'your-db-url'
});
await Redis.set('somekey', 'someval');
let val: string | null = await Redis.get('somekey');
let exists: boolean = await Redis.exists('somekey');
await Redis.del('somekey');
await Redis.close();