sorla-db 1.0.0 → 1.2.0
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.
- package/LICENSE +21 -21
- package/README.md +274 -31
- package/dist/sorla.min.js +1 -1
- package/index.d.ts +89 -0
- package/index.mjs +5 -0
- package/package.json +66 -42
- package/src/collections/collection.js +46 -46
- package/src/dbs/database.js +71 -71
- package/src/documents/cursor.js +207 -143
- package/src/documents/document.js +83 -87
- package/src/query_language/manipulation.js +197 -160
- package/src/query_language/operators.js +242 -288
- package/src/query_language/query.js +73 -103
- package/src/sorla.js +122 -122
- package/src/utils.js +41 -40
- package/Brainstorming.md +0 -22
- package/debug-not.js +0 -24
- package/example/simple_data_example.html +0 -196
- package/index.js +0 -62
- package/src/helper.js +0 -41
- package/test/feature_collection.test.js +0 -73
- package/test/feature_collection_queries.test.js +0 -269
- package/test/unit_database.test.js +0 -36
- package/test/unit_document_test.js +0 -39
- package/test/unit_sorla.test.js +0 -42
- package/test/unit_test.test.js +0 -25
- package/webpack.config.js +0 -11
package/index.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Type declarations for sorla-db
|
|
2
|
+
|
|
3
|
+
export type Document = Record<string, unknown> & { _id?: unknown };
|
|
4
|
+
|
|
5
|
+
export type ComparisonOperators = {
|
|
6
|
+
$eq?: unknown;
|
|
7
|
+
$ne?: unknown;
|
|
8
|
+
$gt?: unknown;
|
|
9
|
+
$gte?: unknown;
|
|
10
|
+
$lt?: unknown;
|
|
11
|
+
$lte?: unknown;
|
|
12
|
+
$in?: unknown[];
|
|
13
|
+
$nin?: unknown[];
|
|
14
|
+
$not?: ComparisonOperators;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** A query: field conditions (value or operators, "a.b" paths allowed) and nested $and / $or / $nor. */
|
|
18
|
+
export type Query = {
|
|
19
|
+
$and?: Query[];
|
|
20
|
+
$or?: Query[];
|
|
21
|
+
$nor?: Query[];
|
|
22
|
+
[field: string]: unknown;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type Projection = Record<string, 0>;
|
|
26
|
+
export type SortSpec = Record<string, 1 | -1>;
|
|
27
|
+
|
|
28
|
+
export interface SortOptions {
|
|
29
|
+
/** Intl.Collator options plus the locale, e.g. { locale: 'pt-BR', sensitivity: 'base', numeric: true } */
|
|
30
|
+
collation?: Intl.CollatorOptions & { locale?: string };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface StoredDocument {
|
|
34
|
+
readonly _data: Document;
|
|
35
|
+
readonly [field: string]: unknown;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export declare class Cursor {
|
|
39
|
+
readonly length: number;
|
|
40
|
+
currIndex: number;
|
|
41
|
+
hasNext(): boolean;
|
|
42
|
+
next(): StoredDocument;
|
|
43
|
+
reset(): void;
|
|
44
|
+
/** Data of the first document, null when empty. */
|
|
45
|
+
first(): Document | null;
|
|
46
|
+
/** Data of the last document, null when empty. */
|
|
47
|
+
last(): Document | null;
|
|
48
|
+
/** Plain copies of the documents. */
|
|
49
|
+
toArray(): Document[];
|
|
50
|
+
sort(spec: SortSpec, options?: SortOptions): this;
|
|
51
|
+
projection(projection: Projection): void;
|
|
52
|
+
push(element: object): void;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export declare class Collection {
|
|
56
|
+
readonly first: Document;
|
|
57
|
+
readonly last: Document;
|
|
58
|
+
readonly all: StoredDocument[];
|
|
59
|
+
count(): number;
|
|
60
|
+
/** Inserts a copy of the document and returns its _id. */
|
|
61
|
+
insertOne(document: Document, callback?: (id: unknown) => void): unknown;
|
|
62
|
+
/** Validates every document first, then inserts them and returns their ids. */
|
|
63
|
+
insertMany(documents: Document[], callback?: (ids: unknown[]) => void): unknown[];
|
|
64
|
+
find(query: Query, projection?: Projection | null): Cursor;
|
|
65
|
+
/** Data of the first matching document, null when nothing matches. */
|
|
66
|
+
findOne(query: Query, projection?: Projection | null): Document | null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export declare class Database {
|
|
70
|
+
readonly name: string;
|
|
71
|
+
createCollection(name: string, validationSchema?: unknown): void;
|
|
72
|
+
countCollections(): number;
|
|
73
|
+
[collection: string]: unknown;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
declare class sorla {
|
|
77
|
+
constructor();
|
|
78
|
+
/** The selected database. Collections are properties of it: sorla.db.people */
|
|
79
|
+
readonly db: Database & Record<string, Collection>;
|
|
80
|
+
dbExists(name: string): boolean;
|
|
81
|
+
/** Position of the new database, or false when the name exists. */
|
|
82
|
+
createDb(name: string): number | false;
|
|
83
|
+
/** Selects a database, creating it when needed. */
|
|
84
|
+
useDb(name: string): void;
|
|
85
|
+
countDb(): number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export { sorla };
|
|
89
|
+
export default sorla;
|
package/index.mjs
ADDED
package/package.json
CHANGED
|
@@ -1,42 +1,66 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "sorla-db",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "A simple and temporary database focused to use in browser environment that will bring capabilities for other tools that will be launched as soon as possible.",
|
|
5
|
-
"main": "
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
|
|
42
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "sorla-db",
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "A simple and temporary database focused to use in browser environment that will bring capabilities for other tools that will be launched as soon as possible.",
|
|
5
|
+
"main": "src/sorla.js",
|
|
6
|
+
"module": "index.mjs",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"unpkg": "dist/sorla.min.js",
|
|
9
|
+
"jsdelivr": "dist/sorla.min.js",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./index.d.ts",
|
|
13
|
+
"import": "./index.mjs",
|
|
14
|
+
"require": "./src/sorla.js"
|
|
15
|
+
},
|
|
16
|
+
"./dist/sorla.min.js": "./dist/sorla.min.js",
|
|
17
|
+
"./src/*": "./src/*",
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"src",
|
|
22
|
+
"!src/helper.js",
|
|
23
|
+
"dist",
|
|
24
|
+
"index.mjs",
|
|
25
|
+
"index.d.ts"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "node index.js",
|
|
29
|
+
"mini": "node index.js",
|
|
30
|
+
"test": "jest"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/sysborg/sorla-db.git"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"nosql",
|
|
38
|
+
"browser",
|
|
39
|
+
"database",
|
|
40
|
+
"sorla",
|
|
41
|
+
"sysborg",
|
|
42
|
+
"anderson",
|
|
43
|
+
"arruda",
|
|
44
|
+
"andmarruda"
|
|
45
|
+
],
|
|
46
|
+
"author": "Anderson Arruda < andmarruda@gmail.com >",
|
|
47
|
+
"license": "MIT",
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/sysborg/sorla-db/issues"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/sysborg/sorla-db#readme",
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"browserify": "^17.0.0",
|
|
54
|
+
"chai": "^4.3.8",
|
|
55
|
+
"faker": "^5.5.3",
|
|
56
|
+
"fs-extra": "^11.2.0",
|
|
57
|
+
"jest": "^29.6.4",
|
|
58
|
+
"jsdom": "^22.1.0",
|
|
59
|
+
"jsdom-global": "^3.0.2",
|
|
60
|
+
"mocha": "^10.2.0",
|
|
61
|
+
"terser": "^5.27.0"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=18"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
const query = require('../query_language/query.js');
|
|
2
|
-
|
|
3
|
-
class collection extends query
|
|
4
|
-
{
|
|
5
|
-
/**
|
|
6
|
-
* Constructs class Collection to manage documents
|
|
7
|
-
* @author Anderson Arruda < anderson@sysborg.com.br >
|
|
8
|
-
* @version 1.0.0
|
|
9
|
-
* @param object validationSchema - in construction
|
|
10
|
-
* @return void
|
|
11
|
-
**/
|
|
12
|
-
constructor(validationSchema)
|
|
13
|
-
{
|
|
14
|
-
super(validationSchema);
|
|
15
|
-
this._documents = [];
|
|
16
|
-
this._indexes = [];
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Returns the first document of the collection
|
|
21
|
-
* @return object
|
|
22
|
-
*/
|
|
23
|
-
get first()
|
|
24
|
-
{
|
|
25
|
-
return structuredClone(this._documents[0]._data);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Returns the last document of the collection
|
|
30
|
-
* @return object
|
|
31
|
-
*/
|
|
32
|
-
get last()
|
|
33
|
-
{
|
|
34
|
-
return structuredClone(this._documents[this._documents.length - 1]._data);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Return all the documents of the collection
|
|
39
|
-
* @return array
|
|
40
|
-
*/
|
|
41
|
-
get all()
|
|
42
|
-
{
|
|
43
|
-
return this._documents;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
1
|
+
const query = require('../query_language/query.js');
|
|
2
|
+
|
|
3
|
+
class collection extends query
|
|
4
|
+
{
|
|
5
|
+
/**
|
|
6
|
+
* Constructs class Collection to manage documents
|
|
7
|
+
* @author Anderson Arruda < anderson@sysborg.com.br >
|
|
8
|
+
* @version 1.0.0
|
|
9
|
+
* @param object validationSchema - in construction
|
|
10
|
+
* @return void
|
|
11
|
+
**/
|
|
12
|
+
constructor(validationSchema)
|
|
13
|
+
{
|
|
14
|
+
super(validationSchema);
|
|
15
|
+
this._documents = [];
|
|
16
|
+
this._indexes = [];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Returns the first document of the collection
|
|
21
|
+
* @return object
|
|
22
|
+
*/
|
|
23
|
+
get first()
|
|
24
|
+
{
|
|
25
|
+
return structuredClone(this._documents[0]._data);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns the last document of the collection
|
|
30
|
+
* @return object
|
|
31
|
+
*/
|
|
32
|
+
get last()
|
|
33
|
+
{
|
|
34
|
+
return structuredClone(this._documents[this._documents.length - 1]._data);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Return all the documents of the collection
|
|
39
|
+
* @return array
|
|
40
|
+
*/
|
|
41
|
+
get all()
|
|
42
|
+
{
|
|
43
|
+
return this._documents;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
47
|
module.exports = collection;
|
package/src/dbs/database.js
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
const collection = require('../collections/collection.js');
|
|
2
|
-
|
|
3
|
-
class database
|
|
4
|
-
{
|
|
5
|
-
/**
|
|
6
|
-
* Constructs class Database to manage collections
|
|
7
|
-
* @author Anderson Arruda < anderson@sysborg.com.br >
|
|
8
|
-
* @version 1.0.0
|
|
9
|
-
* @param string name
|
|
10
|
-
* @return void
|
|
11
|
-
**/
|
|
12
|
-
constructor(name)
|
|
13
|
-
{
|
|
14
|
-
this._collections = {};
|
|
15
|
-
this._name = name;
|
|
16
|
-
this._collectionCount = 0;
|
|
17
|
-
|
|
18
|
-
return new Proxy(this, {
|
|
19
|
-
get: (target, prop, receiver) => {
|
|
20
|
-
if(prop in target._collections)
|
|
21
|
-
{
|
|
22
|
-
return target._collections[prop];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return target[prop];
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Returns the database name
|
|
32
|
-
* @author Anderson Arruda < anderson@sysborg.com.br >
|
|
33
|
-
* @version 1.0.0
|
|
34
|
-
* @param
|
|
35
|
-
* @return string
|
|
36
|
-
**/
|
|
37
|
-
get name()
|
|
38
|
-
{
|
|
39
|
-
return this._name;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Sets a new collection in the database
|
|
44
|
-
* @param string name
|
|
45
|
-
* @param object validationSchema - in construction
|
|
46
|
-
* @return void
|
|
47
|
-
*/
|
|
48
|
-
createCollection(name, validationSchema)
|
|
49
|
-
{
|
|
50
|
-
if(this._collections.hasOwnProperty(name) || typeof this[name] !== 'undefined')
|
|
51
|
-
{
|
|
52
|
-
throw new Error('Collection already exists');
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
this._collections[name] = new collection(validationSchema);
|
|
56
|
-
this._collectionCount++;
|
|
57
|
-
|
|
58
|
-
//creates _id as a index unique
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Returns the number of collections in the database
|
|
63
|
-
* @param
|
|
64
|
-
* @return int
|
|
65
|
-
*/
|
|
66
|
-
countCollections()
|
|
67
|
-
{
|
|
68
|
-
return this._collectionCount;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
1
|
+
const collection = require('../collections/collection.js');
|
|
2
|
+
|
|
3
|
+
class database
|
|
4
|
+
{
|
|
5
|
+
/**
|
|
6
|
+
* Constructs class Database to manage collections
|
|
7
|
+
* @author Anderson Arruda < anderson@sysborg.com.br >
|
|
8
|
+
* @version 1.0.0
|
|
9
|
+
* @param string name
|
|
10
|
+
* @return void
|
|
11
|
+
**/
|
|
12
|
+
constructor(name)
|
|
13
|
+
{
|
|
14
|
+
this._collections = {};
|
|
15
|
+
this._name = name;
|
|
16
|
+
this._collectionCount = 0;
|
|
17
|
+
|
|
18
|
+
return new Proxy(this, {
|
|
19
|
+
get: (target, prop, receiver) => {
|
|
20
|
+
if(prop in target._collections)
|
|
21
|
+
{
|
|
22
|
+
return target._collections[prop];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return target[prop];
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns the database name
|
|
32
|
+
* @author Anderson Arruda < anderson@sysborg.com.br >
|
|
33
|
+
* @version 1.0.0
|
|
34
|
+
* @param
|
|
35
|
+
* @return string
|
|
36
|
+
**/
|
|
37
|
+
get name()
|
|
38
|
+
{
|
|
39
|
+
return this._name;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Sets a new collection in the database
|
|
44
|
+
* @param string name
|
|
45
|
+
* @param object validationSchema - in construction
|
|
46
|
+
* @return void
|
|
47
|
+
*/
|
|
48
|
+
createCollection(name, validationSchema)
|
|
49
|
+
{
|
|
50
|
+
if(this._collections.hasOwnProperty(name) || typeof this[name] !== 'undefined')
|
|
51
|
+
{
|
|
52
|
+
throw new Error('Collection already exists');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this._collections[name] = new collection(validationSchema);
|
|
56
|
+
this._collectionCount++;
|
|
57
|
+
|
|
58
|
+
//creates _id as a index unique
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Returns the number of collections in the database
|
|
63
|
+
* @param
|
|
64
|
+
* @return int
|
|
65
|
+
*/
|
|
66
|
+
countCollections()
|
|
67
|
+
{
|
|
68
|
+
return this._collectionCount;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
72
|
module.exports = database;
|