Git Product home page Git Product logo

cirql's Introduction




Cirql (pronounced Circle) is a simple lightweight ORM and query builder for SurrealDB, providing fully type-safe queries and Zod powered parsing & validation. Unlike most query builders, Cirql's flexible nature leaves you with complete control over your queries, while still providing powerful APIs to query your database in a concise manner.

Features

  • ๐Ÿ”— Connect to SurrealDB over stateful WebSockets or stateless HTTP requests
  • ๐Ÿ“ฆ Support for query batching & transactions
  • โš™๏ธ Zod-powered schema validation of query results
  • ๐Ÿ“ Full TypeScript support with Zod schema inference
  • ๐Ÿ’Ž Write flexible queries using the Query Writer API
  • โœ‚๏ธ Support for raw query strings and native Surreal functions
  • ๐Ÿ”ฅ Works both in browser and backend environments

Notice

Cirql is still in early developmental stages. While you can use it for production applications, it may still lack specific features and edge cases. Feel free to submit feature requests or pull requests to add additional functionality to Cirql. We do ask you to please read our Contributor Guide.

While we try to prevent making any significant API changes, we cannot guarantee this.

Installation

The first step to use Cirql is to install the package from npm, together with a supported version of zod.

npm install cirql zod

How to use

You can read our documentation for information on how to use Cirql.

Example

The following query fetches up to 5 organisations that are enabled and have the given user as a member. The result is parsed and validated using the provided Zod schema.

import { RecordSchema, select } from 'cirql';
import { z } from 'zod';

// Define your Zod schemas
const Organisation = RecordSchema.extend({
    name: z.string(),
    isEnabled: z.boolean(),
    createdAt: z.string()
});

// Execute a select query
const organisations = await cirql.execute({
    query: select()
        .from('organisation')
        .with(Organisation) // Specify the schema
        .limit(5)
        .where({
            isEnabled: true,
            members: any(userId)
        })
});

Visit our Basic Usage guide for more examples.

Contributing

We welcome any issues and PRs submitted to Cirql. Since we currently work on multiple other projects and our time is limited, we value any community help in supporting a rich future for Cirql.

Before you open an issue or PR please read our Contributor Guide.

Requirements

  • PNPM (npm i -g pnpm)

Roadmap

You can find the roadmap of intended features here.

Changelog

The changelog of previous versions can be found here.

Live Development

To run in live development mode, run pnpm dev in the project directory. This will start the Vite development server.

Maintainers

Cirql is built and maintained by Starlane Studios at no cost. If you would like to support our work feel free to donate to us โšก

License

Cirql is licensed under MIT

Copyright (c) 2023, Starlane Studios

cirql's People

Contributors

algorithmenigma avatar itsmateo avatar jackson-bean avatar macjuul avatar welpie21 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cirql's Issues

Support parameters in stateless queries

For now queries with parameters are rejected. I found this commit indicating that it should be possible, however no combination of specifying URL parameters after /sql seemed to work.

Any help on decoding the Rust code and finding the correct way would be appreciated!

[Help wanted] Documentation contributions

Maintaining documentation takes a lot of time and energy, especially for a team as small as ours. As we are often working on many Open Source projects at once, we often find little time left to write and update our docs. For this reason we kindly ask the community and those interested to help expand the Cirql documentation.

What are we looking for?

Cirql is a very dynamic library which can be used in many ways, much like SurrealDB itself. Receiving community input is essential to describe and explain the various use-cases in which Cirql can be used. Naturally we will continue to update our documentation over time and review issues and PRs as frequently as we can. Currently we are seeking to cover the following topics:

  • Installation & setup
  • Common query examples
  • Transactions & batched queries
  • Stateless requests
  • Explanation on raw() and derivatives
    • How to prevent SQL injections
    • How to prevent table spoofing
  • Effective use of zod schemas
    • RecordSchema and EdgeSchema
    • Explanation of .with(), .withAny(), and .withSchema()
  • API Reference
    • Documentation for each query writer (code)
    • Available operators (code)
    • Available functions (code)
    • Utilities e.g. parseQueries (code)

How can I help?

You can visit the documentation repository here for further instructions on how you can help. Feel free to open issues on topics you would like to see covered, and submit new pages or improvements as PR's.

Thank you for your time and your interest in Cirql

Agnostic schema validator

Describe your feature request
I would prefer to use typia over zod because it is a smaller package and performs much faster type validation.

Are you willing to contribute this feature?
Yes, with direction on how to best incorporate this into the package.

Related pull request (if applicable)
not yet

`EventTarget is not defined` error when importing module

Describe the bug
Receiving error Uncaught ReferenceError: EventTarget is not defined when importing or requiring cirql.

Example
In my TypeScript project, I've installed cirql, zod, and ws. However, when I attempt to import or require cirql, I get this failure:

> const cirql = require('cirql')
Uncaught ReferenceError: EventTarget is not defined
    at /home/gilbert/dev/ecmastuff/surreal/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/cirql/dist/cirql.umd.cjs:4:22
    at /home/gilbert/dev/ecmastuff/surreal/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/cirql/dist/cirql.umd.cjs:1:681
    at Object.<anonymous> (/home/gilbert/dev/ecmastuff/surreal/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/cirql/dist/cirql.umd.cjs:1:892)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)

The above was run in the node repl. I receive the same error when I run import cirql from 'cirql' in my TypeScript file and execute it using npx ts-node FILE. This also happens when I run import cirql from 'cirql' in the TS repl (via pnpm exec ts-node) after I attempt introspection on the cirql object. I do not have a tsconfig.json file, so this is the default configuration.

Expected behavior
I expect to be able to import the module.

Environment:

  • OS: Ubuntu 20.04.5 LTS
  • Version: v0.1.6
  • Node Version: v14.18.2

db.ready() throws error

Describe the bug
db.ready() throws the following error:

He {
  type: 'error',
  defaultPrevented: false,
  cancelable: false,
  timeStamp: 3265.9472999982536
}

Example

// src/lib/server/db.ts
import { Cirql } from 'cirql';

import {
    SURREALDB_NS,
    SURREALDB_DB,
    SURREALDB_URL,
    SURREALDB_USER,
    SURREALDB_PASS,
} from "$env/static/private";
import { dev } from '$app/environment';
import { log } from '$lib/logger';
//import { withTimeout } from '$lib/utils';

declare global {
    var surrealdb: Cirql
}

export let db = global.surrealdb;
export async function conectDB() {
    if (db) return;
    log.warn('creating new connection to db!');
    try {
        db = new Cirql({
            connection: {
                endpoint: SURREALDB_URL,
                namespace: SURREALDB_NS,
                database: SURREALDB_DB,
            },
            credentials: {
                user: SURREALDB_USER,
                pass: SURREALDB_PASS,
            }
        });
        await db.ready();
        // await withTimeout(db.ready(), 'db connection timed out', 5000);
        if (dev) global.surrealdb = db;
        log.info('db connected ');
    } catch (error) {
        log.error('connecting to db:', error);
    }
}

Expected behavior
for that to not err out ig hands ๐Ÿคทโ€โ™‚๏ธ

Environment:

  • OS: Windows
  • Version: 0.6.6
  • Node Version: v18.12.1

More docs would be nice!

Describe your feature request
There isn't much available in your docs.. but the library appears to work great. Would be nice to know how to use it

timeNow() is not exported from cirql

Describe the bug
Module '"cirql"' has no exported member 'timeNow'.

Example
https://cirql.starlane.studio/docs/guide/basic-usage#raw-query-values--operators

const profile = await cirql.execute({ 
    query: create('organisation').setAll({
        name: 'Example',
        isEnabled: eq('$enable'),
        createdAt: eq(timeNow()) // time::now()
    }),
    schema: Organisation,
    params: {
        enable: true
    }
});

Expected behavior
for it to work

Environment:

  • OS: Windows 10
  • Version: 0.6.6
  • Node Version: v18.12.1

Add support for next js 13

Describe the bug
after installing cirql i got error with dependencies which are nested in next js 13:

Peer dependencies that should be installed:
   @babel/core@">=7.13.0 <8.0.0" webpack@">=5.0.0 <6.0.0"
   react-is@">= 16.8.0" ws@"*"
   ts-node@>=10.0.0

I'm afraid such dependency error might seem like bugs in the future.

Environment:

  • OS: endeavouros
  • Node Version: 16.19

eq function returns escaped strings.

Describe the bug
The eq() function when passed a string returns an escaped string which prevents the usage of raw queries without using a provided method.

Example

eq('math::floor(12.133)')

output: [Symbol(Query Raw)]: "= \"math::floor(12.133)\""

Expected behavior
I expect the strings not to be escaped to allow you to use that without having to either use

eq(raw('math::floor(12.133)')

or

raw('= math::floor(12.133)')

I feel both of those options are are less clean to write. A change would also allow the use of any of the SurrealQL functions without having to rely on us having a helper method.

Environment:

  • OS: Linux
  • Version ^0.6.6
  • Node Version v16.19.0

Contribution to API documentation

Hi, I wanted to contribute to the API documentation but the comments on issue 6 are turned off. Can you sort this out? or let me know how to procceed?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.