Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/soya-miruku/typed-surql

minimal surrealdb orm
https://github.com/soya-miruku/typed-surql

orm surrealdb surrealdb-client typescript

Last synced: about 1 month ago
JSON representation

minimal surrealdb orm

Awesome Lists containing this project

README

        




typed-surql


Typed-Surql


A minimal typed ORM built on top of Surrealdb.js.


Note that there are still some features that have not been completed, such as migrations and schema generation

## How To Use

```bash
# Clone this repository
$ git clone https://github.com/soya-miruku/typed-surql
```

```ts
ensure you have enabled the following in your tsconfig file:
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
```

```ts
# Or Using Deno
import {TypedSurQL} from 'https://deno.land/x/[email protected]/mod.ts';
# Or npm/bun
import { TypedSurQL } from '@soyamiruku/typed-surl';

// initialise the connection
TypedSurQL.Init(env.DB_URL, {
websocket: false, auth: {
username: env.DB_USER,
password: env.DB_PASS
},
namespace: env.DB_NAMESPACE,
database: env.DB_NAME
});

// wait until the connection is made
await TypedSurQL.Wait(5);
import { TypedSurQL, Model, Q, RelationEdge } from 'https://deno.land/x/[email protected]/mod.ts';
import { Lemons } from "./lemons";

@Q.Table({ name: "eats" })
export class Eats extends RelationEdge { }

@Q.Table({ name: "user" })
export class User extends Model {
@Q.Field({ index: { name: "username_idx", search: true } }) username!: string;
@Q.Field() something!: string;
@Q.Relation("->", Eats, "->", Lemons) readonly lemonsEaten!: Lemons[];
}

@Q.Table({ name: "session" })
export class Session extends Model {
@Q.Field() active_expires!: number;
@Q.Field() idle_expires!: number;
@Q.Field() user!: User;
}

@Q.Table({ name: "key" })
export class Account extends Model {
@Q.Field() hashed_password?: string | null;
@Q.Field() key_id!: string;
@Q.Field() user!: User;
}

// Defines the object types with only the properties
export type UserObject = Q.Static;
export type SessionObject = Q.Static;
export type AccountObject = Q.Static;

// Perform queries

const result = await User.select("*", { fetch: ["lemonsEaten"]});

// query functions

import { query } from 'https://deno.land/x/[email protected]/mod.ts';

// field param provides all surrealdb functions / operators and the table name as well allowing you to select the model properties:

query.queryModel(User, (q, field) => q`SELECT *, ${field.string.uppercase(field("username")).as("cap_username")} FROM ${field.TABLE} WHERE ${field("id")} = ....`)
// or you can do
User.query((q, { VALUE, TABLE, field }) => ....)....
```

```txt
There is also an example folder with you can check out
```

> **Note**
> There may be bugs

## License

MIT

---

> [soyamiruku](https://github.com/soya-miruku)