Latest Threat ResearchGlassWorm Loader Hits Open VSX via Developer Account Compromise.Details
Socket
Book a DemoInstallSign in
Socket

l12.xyz/x/dal

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

l12.xyz/x/dal

Go Modules
Version
v0.0.0-20240902231117-e62a155e0492
Version published
Created
Source

DAL

Data Access Layer

DAL is a proxy layer for SQL databases with a MongoDB inspired query interface. It can be used as a Go or NodeJS package (requires compiler). It is modular and allows to create your own proxy and apply custom middlewares.

Notes:

  • This project is still in early alpha. You need to build it yourself and use at your own risk.
  • At the time only SQLite is implemented, however, other drivers might work.

Use cases:

  • For IOT networks when MySQL/PG are too heavy.
  • If you need a layer between your application and the database (i.e. for caching).
  • If you want a MongoDB-like query interface for your SQL db.
  • When you need a SQLite proxy (useful to share datasets with services)

Usage

Server

The most efficient way to use DAL is to run the server as a standalone service.

Build:

go build -o server

Run:

export SQLITE_DIRECTORY=/opt/data

./server

2024/08/21 22:01:54 Starting server on port 8118
2024/08/21 22:01:54 Using directory: /opt/data

Client

Install:

pnpm add git+git@github.com:nesterow/dal.git

Query Interface

MethodDescriptionSQL
In(table: string)Select tableSELECT * FROM table
Find(filter: object)Filter rowsSELECT * FROM table WHERE filter
Fields(fields: string[])Select fieldsSELECT fields, FROM table
Sort(sort)Sort rowsSELECT * FROM table ORDER BY sort
Limit(limit: number)Limit rowsSELECT * FROM table LIMIT limit
Offset(offset: number)Offset rowsSELECT * FROM table OFFSET offset
Join({ $for: "t_2", $do: { "t.a": "b" } })Join tablesSELECT * FROM table t JOIN t_2 ON t.a = b
Insert({name: "J"}, {name: "B"})Insert rowINSERT INTO table (name,) VALUES ('J', 'B')
Set({name: "Julian"})Update row (Find(filter).Set({}))UPDATE table SET name = 'Julian' WHERE filter
Delete()Delete row (Find(filter).Delete())DELETE FROM table WHERE filter
As(DTO)Map rows to a DTOSELECT * FROM table
Rows()Get rows iteratorSELECT * FROM table
Exec()Execute query (update, insert, delete)SQL RESULT
Query()Query databaseDTO array
Tx()Run in trasaction

Filters

FilterDescriptionSQL
{id: 1, num: 2}Equals, default filterWHERE id = 1 AND num = 2
{id: { $eq: 1 }}Equals, explicitWHERE id = 1
{id: { $gt: 1 }}Greater thanWHERE id > 1
{id: { $gte: 1 }}Greater than or equalWHERE id >= 1
{id: { $lt: 1 }}Less thanWHERE id < 1
{id: { $lte: 1 }}Less than or equalWHERE id <= 1
{id: { $ne: 1 }}Not equalWHERE id != 1
{id: { $in: [1, 2] }}InWHERE id IN (1, 2)
{id: { $nin: [1, 2] }}Not inWHERE id NOT IN (1, 2)
{id: { $like: "a" }}LikeWHERE id LIKE '%a%'
{id: { $nlike: "a" }}Not likeWHERE id NOT LIKE '%a%'
{id: { $between: [1, 2] }}BetweenWHERE id BETWEEN 1 AND 2
{id: { $nbetween: [1, 2] }}Not betweenWHERE id NOT BETWEEN 1 AND 2
{id: { $glob: "\*son" }}GlobWHERE id GLOB '*son'

Example

import { DAL } from "@nesterow/dal";

class UserDTO {
  id: number = 0;
  name: string = "";
  data: string = "";
  age: number | undefined;
}

const db = new DAL({
  database: "test.sqlite",
  url: "http://localhost:8111",
});

// SELECT * FROM test t WHERE name GLOB '*son' AND age >= 18
const rows = db
  .In("test t")
  .Find({
    name: { $glob: "*son" },
    age: { $gte: 18 },
  })
  .As(UserDTO) // Map every row to DTO
  .Rows();

for await (const row of rows) {
  console.log(row); // Jason, Jackson
}

Internals

The client uses a light builder and messagepack over http. It is relatively easy to implement a client in any language see the docs

License

While in alpha stage the project is free for research purposes. Later it will be released under MIT-like license with AI/dataset exclusion terms.

FAQs

Package last updated on 02 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts