@dnax/core 0.4.4 → 0.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -40,7 +40,7 @@ type optionCb = {
40
40
  useHook?: boolean;
41
41
  cleanDeep?: boolean;
42
42
  useCustomApi?: boolean;
43
- elementAt: Number | null;
43
+ elementAt?: Number | null;
44
44
  };
45
45
 
46
46
  const omitUpdate = [
package/index.ts CHANGED
@@ -6,6 +6,7 @@ import Joi from "joi";
6
6
  import define from "./define";
7
7
  import moment from "moment";
8
8
  import * as utils from "./utils";
9
+ import { searchEngine } from "./lib/orama";
9
10
 
10
11
  /**
11
12
  * v is internal data validation and based of Joi validation.
@@ -14,4 +15,4 @@ import * as utils from "./utils";
14
15
  */
15
16
  const v = Joi;
16
17
 
17
- export { runApp, define, utils, useRest, v };
18
+ export { runApp, define, utils, useRest, v, searchEngine };
package/lib/collection.ts CHANGED
@@ -123,6 +123,7 @@ async function syncCollectionDatabase() {
123
123
  .then((e) => {});
124
124
 
125
125
  // 3- suppression des indexes
126
+ //
126
127
  c.fields?.map((f) => {
127
128
  if (!f?.unique && allIndexes?.find((e) => e?.key[f.name])) {
128
129
  //@ts-expect-error
@@ -0,0 +1,39 @@
1
+ type Vector<N extends number> = `vector[${N}]`;
2
+ export type optionsType = {
3
+ store: {
4
+ path?: string;
5
+ filename: string;
6
+ };
7
+ schema: {
8
+ [key: string]:
9
+ | "string"
10
+ | "number"
11
+ | "boolean"
12
+ | Vector<number>
13
+ | "geopoint";
14
+ };
15
+ settings?: {
16
+ primaryKey?: string;
17
+ language: "french" | "english";
18
+ };
19
+ };
20
+
21
+ export type searchType = {
22
+ mode?: "fulltext" | "hybrid" | "vector";
23
+ term: string;
24
+ limit?: number;
25
+ offset?: number;
26
+ tolerance?: number;
27
+ properties?: string[];
28
+ exact?: boolean;
29
+ vector?: {
30
+ value: number[];
31
+ property: string;
32
+ };
33
+ hybridWeights?: {
34
+ text: number;
35
+ vector: number;
36
+ };
37
+ similarity?: number;
38
+ includeVectors?: boolean;
39
+ };
@@ -0,0 +1,90 @@
1
+ import type { optionsType, searchType } from "./@types";
2
+ import * as orama from "@orama/orama";
3
+ import fs from "fs-extra";
4
+ import _ from "lodash";
5
+ import { ObjectId } from "mongodb";
6
+ import path from "path";
7
+ class searchEngine {
8
+ #options: optionsType;
9
+ #db: any;
10
+ #dataPath: string;
11
+ constructor(options: optionsType) {
12
+ this.#options = options;
13
+ this.#dataPath = path.join(
14
+ this.#options.store?.path || process.cwd() + "/data",
15
+ this.#options.store?.filename || "search.db"
16
+ );
17
+ }
18
+
19
+ async initialize() {
20
+ this.#db = await orama.create({
21
+ schema: this.#options.schema,
22
+ language: this.#options.settings?.language || "english",
23
+ });
24
+
25
+ try {
26
+ let restoreData = await Bun.file(this.#dataPath).json();
27
+
28
+ if (restoreData) {
29
+ // console.log(restoreData);
30
+ await orama.load(this.#db, restoreData);
31
+ }
32
+ } catch (err) {}
33
+ }
34
+
35
+ async save() {
36
+ try {
37
+ let jsonExport = await orama.save(this.#db);
38
+ let jsonData = JSON.stringify(jsonExport);
39
+ let saveTo = Bun.write(this.#dataPath, jsonData);
40
+ } catch (error) {
41
+ console.log(error?.message);
42
+ }
43
+ }
44
+
45
+ async add(documents: Array<object>) {
46
+ try {
47
+ let ids = [];
48
+ if (Array.isArray(documents)) {
49
+ documents.map((doc) => {
50
+ doc.id = doc?._id || doc?.id || new ObjectId().toString();
51
+ doc._id = doc?.id;
52
+ });
53
+ let ids = await orama.insertMultiple(this.#db, documents);
54
+ await this.save();
55
+ return ids;
56
+ }
57
+ } catch (error) {
58
+ console.log(error?.message);
59
+ }
60
+ }
61
+
62
+ async search(search: searchType) {
63
+ return await orama.search(this.#db, search);
64
+ }
65
+
66
+ async find() {
67
+ let docs: Array<object> = [];
68
+ let data = await orama.save(this.#db);
69
+ docs = Object.values(data.docs?.docs || []);
70
+ //let docs = Object.values(data.docs[0]);
71
+ return docs;
72
+ }
73
+
74
+ async remove(ids: Array<string>) {
75
+ try {
76
+ await orama.remove(this.#db, ids);
77
+ await this.save();
78
+ } catch (error) {
79
+ console.log(error?.message);
80
+ }
81
+ }
82
+
83
+ async drop() {
84
+ fs.unlink(this.#dataPath).catch((err) => {
85
+ //console.log(err);
86
+ });
87
+ }
88
+ }
89
+
90
+ export { searchEngine };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.4.4",
3
+ "version": "0.4.6",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
@@ -17,6 +17,7 @@
17
17
  "@clack/prompts": "^0.7.0",
18
18
  "@colors/colors": "^1.6.0",
19
19
  "@lukeed/ms": "^2.0.2",
20
+ "@orama/orama": "^2.0.23",
20
21
  "@types/jsonwebtoken": "^9.0.6",
21
22
  "boxen": "^7.1.1",
22
23
  "chokidar": "^3.6.0",
@@ -26,6 +27,7 @@
26
27
  "cookie": "^0.6.0",
27
28
  "croner": "^8.1.1",
28
29
  "find-open-port": "^2.0.3",
30
+ "fs-extra": "^11.2.0",
29
31
  "generate-unique-id": "^2.0.3",
30
32
  "hono": "^4.5.4",
31
33
  "joi": "^17.13.3",