Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
user8628 committed Jun 7, 2021
1 parent 8b8620a commit 0c82a34
Show file tree
Hide file tree
Showing 16 changed files with 491 additions and 387 deletions.
12 changes: 12 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
kind: pipeline
name: default

steps:
- name: build_packages
image: node
commands:
- npm i -g pnpm
- pnpm install
- pnpm run -r build
- pnpm run -r --filter @rtdb2/embed test
- pnpm run -r publish
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@rtdb2/core",
"version": "3.0.0-beta.0",
"version": "3.0.0-beta.1",
"description": "",
"main": "lib/index.js",
"private": true,
"scripts": {
"build": "tsc",
"dev": "nodemon -e ts --exec ts-node src/test.ts",
"prepublishOnly": "tsc"
"prepublishOnly": "tsc" ,
"publish": "pnpm publish"
},
"author": "Fabian Stamm <dev@fabianstamm.de>",
"license": "ISC",
Expand Down
19 changes: 10 additions & 9 deletions packages/core/src/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ITypedQuery,
IQuery,
} from "./query";
import Logging from "@hibas123/logging";
import { LoggingBase, ILoggingInterface } from "@hibas123/logging";
import Session from "./session";
import nanoid = require("nanoid");
import { Observable } from "@hibas123/utils";
Expand Down Expand Up @@ -38,6 +38,7 @@ const DummyRuleEngine = {
} as IRuleEngine;

export class Database {
logging: ILoggingInterface = new LoggingBase({ console: false });
public static getKey(collectionid: string, documentid?: string) {
return `${collectionid || ""}/${documentid || ""}`;
}
Expand Down Expand Up @@ -205,7 +206,7 @@ export class Database {

const isBatch = queries.length > 1;
let parsed = queries.map((rawQuery) => {
Logging.debug("Running query:", rawQuery.type);
this.logging.debug("Running query:", rawQuery.type);
this.validate(rawQuery);
const isCollection = rawQuery.path.length % 2 === 1;

Expand Down Expand Up @@ -269,7 +270,7 @@ export class Database {
session: Session,
onchange: (change: any) => void
) {
Logging.debug("Snaphot request:", rawQuery.path);
this.logging.debug("Snaphot request:", rawQuery.path);
this.validate(rawQuery);

if (rawQuery.type !== "snapshot") throw new Error("Invalid query type!");
Expand Down Expand Up @@ -316,8 +317,8 @@ export class Database {
const collections = new Set<string>();
const onValue = (err: Error, key: string, value: string) => {
if (err) {
Logging.error(err);
stream.end((err) => Logging.error(err));
this.logging.error(err);
stream.end((err) => this.logging.error(err));
no(err);
}

Expand All @@ -341,8 +342,8 @@ export class Database {
const collections = new Set<string>();
const onValue = (err: Error, key: string, value: Buffer) => {
if (err) {
Logging.error(err);
stream.end((err) => Logging.error(err));
this.logging.error(err);
stream.end((err) => this.logging.error(err));
no(err);
}

Expand Down Expand Up @@ -383,8 +384,8 @@ export class Database {

const onValue = (err: Error, key: string, value: Buffer) => {
if (err) {
Logging.error(err);
stream.end((err) => Logging.error(err));
this.logging.error(err);
stream.end((err) => this.logging.error(err));
no(err);
}

Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/database/query.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Database, Change, ChangeTypes } from "./database";
import { resNull } from "../storage";
import * as nanoid from "nanoid";
import Logging from "@hibas123/logging";
import { LoggingBase } from "@hibas123/logging";
import * as MSGPack from "msgpack5";
import Session from "./session";
import { LevelUpChain } from "levelup";
Expand Down Expand Up @@ -130,7 +130,7 @@ export abstract class Query {
sender: this.session.id,
};

Logging.debug("Sending change:", change);
this.database.logging.debug("Sending change:", change);

this.changes.push(change);
}
Expand Down Expand Up @@ -498,7 +498,7 @@ export class CollectionQuery extends Query {
if (!Array.isArray(value)) throw invalidWhere;
let c = [];
this._where = value.map((cond) => {
Logging.debug("Query Condition", cond);
this.database.logging.debug("Query Condition", cond);
if (Array.isArray(cond)) {
if (cond.length !== 3) throw invalidWhere;
return cond;
Expand Down Expand Up @@ -631,11 +631,11 @@ export class CollectionQuery extends Query {
const onValue = (err: Error, key: string, value: Buffer) => {
if (err) {
no(err);
stream.end((err) => Logging.error(err));
stream.end((err) => this.database.logging.error(err));
} else {
if (!key && !value) {
// END
Logging.debug("Checked all!");
this.database.logging.debug("Checked all!");
yes(values);
} else {
let s = key.split("/", 2);
Expand Down
11 changes: 7 additions & 4 deletions packages/embed/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{
"name": "@rtdb2/embed",
"version": "3.0.0-beta.0",
"version": "3.0.0-beta.1",
"description": "",
"main": "lib/index.js",
"private": true,
"scripts": {
"build": "tsc",
"dev": "nodemon -e ts --exec ts-node src/test.ts",
"prepublishOnly": "tsc"
"prepublishOnly": "tsc",
"test": "mocha --no-timeout --require ts-node/register src/test.ts",
"publish": "pnpm publish"
},
"author": "Fabian Stamm <dev@fabianstamm.de>",
"license": "ISC",
"devDependencies": {
"@types/mocha": "^8.2.2",
"@types/node": "^14.17.2",
"mocha": "^8.4.0",
"nodemon": "^2.0.7",
"ts-node": "^10.0.0",
"typescript": "^4.3.2"
Expand All @@ -22,6 +24,7 @@
"@hibas123/logging": "^3.1.2",
"@hibas123/utils": "^2.2.18",
"nanoid": "^3.1.23",
"@rtdb2/core": "workspace:../core"
"@rtdb2/core": "workspace:../core",
"@rtdb2/sdk": "workspace:../sdk"
}
}
45 changes: 45 additions & 0 deletions packages/embed/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as RTDB from "@rtdb2/core";
import { AwaitStore } from "@hibas123/utils";
import { Database, IConnector, IQueryRequest, Snapshot } from "@rtdb2/sdk";

export class EmbeddedConnector implements IConnector {
#db: RTDB.Database;
#session: RTDB.Session;

snapshots = new Map<string, Snapshot<any>>();
offline = new AwaitStore(false);

constructor(path: string) {
this.#db = new RTDB.Database(path);
this.#session = new RTDB.Session("session");
this.#session.root = true; // root is always true on the local session
}

async queryRequest<T = any>(
query: IQueryRequest,
id?: string,
ns?: string
): Promise<T> {
if (Array.isArray(query)) {
return this.#db.run(query as RTDB.IQuery[], this.#session);
} else {
if (query.type === "snapshot") {
return (await this.#db.snapshot(
query as RTDB.ITypedQuery<"snapshot">,
this.#session,
(change) => {
this.snapshots.get(id).receivedData(change);
}
)) as any;
} else if (query.type === "unsubscribe") {
return this.#db.unsubscribe(id, this.#session) as any;
} else {
return this.#db.run([query as RTDB.IQuery], this.#session);
}
}
}

async close() {
await this.#db.stop();
}
}
49 changes: 5 additions & 44 deletions packages/embed/src/test.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,7 @@
import { Database, Session } from "@rtdb2/core";
import Database from "@rtdb2/sdk";
import { testDB } from "@rtdb2/sdk/lib/tests";
import { EmbeddedConnector } from "./main";

const db = new Database("./test");
const db = new Database(new EmbeddedConnector("./test"));

// db.run()

const session = new Session("134");

async function test() {
const res = await db.run(
[
{
path: ["coll1", "doc1"],
type: "get",
},
],
session
);
console.log(res);

await db.run(
[
{
path: ["coll1", "doc1"],
type: "set",
data: {
hi: "hallo" + Date.now(),
},
},
],
session
);

const res2 = await db.run(
[
{
path: ["coll1", "doc1"],
type: "get",
},
],
session
);
console.log(res2);
}

test();
testDB(db);
9 changes: 5 additions & 4 deletions packages/rules/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
{
"name": "@rtdb2/rules",
"version": "3.0.0-beta.0",
"version": "3.0.0-beta.1",
"description": "",
"main": "lib/index.js",
"private": true,
"scripts": {
"build": "tsc",
"dev": "nodemon -e ts --exec ts-node src/test.ts",
"prepublishOnly": "tsc"
"prepublishOnly": "tsc",
"publish": "pnpm publish"
},
"author": "Fabian Stamm <dev@fabianstamm.de>",
"license": "ISC",
"devDependencies": {
"@types/node": "^14.17.2",
"nodemon": "^2.0.7",
"ts-node": "^10.0.0",
"typescript": "^4.3.2"
"typescript": "^4.3.2",
"@rtdb2/core": "workspace:../core"
},
"dependencies": {
"@hibas123/logging": "^3.1.2"
Expand Down
10 changes: 5 additions & 5 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@rtdb2/sdk",
"version": "3.0.0-beta.0",
"version": "3.0.0-beta.1",
"description": "",
"main": "lib/index.js",
"private": true,
"scripts": {
"build": "tsc",
"dev": "nodemon -e ts --exec ts-node src/test.ts",
"prepublishOnly": "tsc",
"test": "mocha --no-timeout --require ts-node/register src/test.ts"
"test": "mocha --no-timeout --require ts-node/register src/test.ts",
"publish": "pnpm publish"
},
"author": "Fabian Stamm <dev@fabianstamm.de>",
"license": "ISC",
Expand All @@ -17,6 +17,8 @@
"@types/chai-as-promised": "^7.1.4",
"@types/node": "^14.17.2",
"@types/lodash.isequal": "^4.5.5",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"mocha": "^8.4.0",
"nodemon": "^2.0.7",
"ts-node": "^10.0.0",
Expand All @@ -25,8 +27,6 @@
"dependencies": {
"@hibas123/logging": "^3.1.2",
"@hibas123/utils": "^2.2.18",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"lodash.isequal": "^4.5.0",
"nanoid": "^3.1.23",
"ws": "^7.4.6"
Expand Down
Loading

0 comments on commit 0c82a34

Please sign in to comment.