Releases: soya-miruku/typed-surql
Releases · soya-miruku/typed-surql
version 1.1.4
Fix small bugs
Fix type issues
Extended relation functions
v1.0.25
json stringy float handler
v1.0.23
fe
v1.0.22
mapping bug fix for npm packages
release 1.0.21
v1.0.21 readme 4
v1.0.20
import "npm:reflect-metadata";
import "npm:core-js";
import { TypedSurQL, Model, Q, RelationEdge } from '../src/index.ts';
await TypedSurQL.Init("http://127.0.0.1:8000", {
auth: {
username: "root",
password: "root"
},
websocket: false,
namespace: "test",
database: "test"
})
const todo = Q.Type.Object({
title: Q.Type.String(),
completed: Q.Type.Boolean(),
});
@Q.Table({ name: "friends" })
class Friends extends RelationEdge<User, User>{ }
@Q.Table({ name: "user" })
class User extends Model {
@Q.Field() name!: string
@Q.Relation("->", Friends, "->", User) readonly friends!: User[] // so far the relational type must be readonly
@Q.Field({}, todo) todos!: Todo[] // passing the object in the second arg, will allow you to later query the object using the query func
@Q.Record(User) bestFriend?: User
}
export type UserObject = Q.Static<User>;
export type Todo = Q.Static<typeof todo>;
await User.create({ id: "user:0", name: "henry", todos: [{ title: "test", completed: false }] });
await User.create({ name: "bingo", bestFriend: "user:0", todos: [{ title: "test", completed: false }, { title: "test2", completed: true }] });
const result = await User.select(["todos", "friends", "bestFriend"], { fetch: ["friends", "bestFriend"] });
console.log(result)
/** RETURNS (AS AN EXAMPLE)
* [
{ friends: [], todos: [ { completed: false, title: "test" } ] },
{
friends: [],
todos: [
{ completed: false, title: "test" },
{ completed: true, title: "test2" }
]
},
{ friends: [], todos: [ { completed: false, title: "test" } ] }
]
*/
const anotherway = await User.query((q, f) => q`SELECT ${f("todos.completed")} FROM ${f.TABLE}`).exec<Omit<User, "id" | "friends">[]>();
console.log(anotherway)
/** RETURNS (AS AN EXAMPLE)
* [
{ todos: { completed: [ false ] } },
{ todos: { completed: [ false, true ] } },
{ todos: { completed: [ false ] } }
]
*/
type AliasReturn = { completed: boolean[] };
const alias = await User.query((q, f) => q`SELECT ${f("todos.completed").as("completed")} FROM ${f.TABLE}`).exec<AliasReturn[]>();
console.log(alias);
/** RETURNS (AS AN EXAMPLE)
* [
{ completed: [ false ] },
{ completed: [ false, true ] },
{ completed: [ false ] }
]
*/
const aliasValue = await User.query((q, { VALUE, TABLE, field }) => q`SELECT ${VALUE} ${field("todos.completed").as("completed")} FROM ${TABLE}`).exec();
console.log(aliasValue);
/** RETURNS (AS AN EXAMPLE)
* [ [ false ], [ false, true ], [ false ] ]
*/
const stringFnc = await User.query((q, { LIMIT, TABLE, field, string }) => q`SELECT ${string.uppercase(field("name")).as("upper_name")} FROM ${TABLE} ${LIMIT(2)}`).exec();
console.log(stringFnc);
/** RETURNS (AS AN EXAMPLE)
* [ { upper_name: "MILK" } ]
*/```
v1.0.18
smol fixes
release 1.0.16
v1.0.16 fire 2
v1.0.15
Typed-Surql
A minimal typed ORM Surrealdb.js.
How To Use
# Clone this repository
$ git clone https://github.com/soya-miruku/typed-surql
# Or Using Deno
import {TypedSurQL} from 'https://deno.land/x/typed_surql@v1.0.5/mod.ts'
// 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 { Model, Table, Field, OnlyFields, RelationEdge, Relation } from 'https://deno.land/x/typed_surql@v1.0.5/mod.ts';
import { Lemons } from "./lemons";
@Table({ name: "eats" })
export class Eats extends RelationEdge<User, Lemons> { }
@Table({ name: "user" })
export class User extends Model {
@Field({ index: { name: "username_idx", search: true } }) username!: string;
@Field() something!: string;
@Relation("->", Eats, "->", Lemons) readonly lemonsEaten!: Lemons[];
}
@Table({ name: "session" })
export class Session extends Model {
@Field() active_expires!: number;
@Field() idle_expires!: number;
@Field() user!: User;
}
@Table({ name: "key" })
export class Account extends Model {
@Field() hashed_password?: string | null;
@Field() key_id!: string;
@Field() user!: User;
}
// Defines the object types with only the properties
export type UserObject = OnlyFields<User>;
export type SessionObject = OnlyFields<Session>;
export type AccountObject = OnlyFields<Account>;
// Perform queries
const result = await User.select("*", { fetch: ["lemonsEaten"]});
// query functions
import { query } from 'https://deno.land/x/typed_surql@v1.0.5/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")} = ....`)
Note
There may be bugs
License
MIT