@theshelf/database 0.0.3 → 0.0.4

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.
@@ -7,12 +7,11 @@ export default class Database implements Driver {
7
7
  connect(): Promise<void>;
8
8
  disconnect(): Promise<void>;
9
9
  createRecord(type: RecordType, data: RecordData): Promise<RecordId>;
10
- readRecord(type: RecordType, id: RecordId, fields?: RecordField[]): Promise<RecordData>;
11
- findRecord(type: RecordType, query: RecordQuery, fields?: RecordField[], sort?: RecordSort): Promise<RecordData | undefined>;
10
+ readRecord(type: RecordType, query: RecordQuery, fields?: RecordField[], sort?: RecordSort): Promise<RecordData | undefined>;
12
11
  searchRecords(type: RecordType, query: RecordQuery, fields?: RecordField[], sort?: RecordSort, limit?: number, offset?: number): Promise<RecordData[]>;
13
- updateRecord(type: RecordType, id: RecordId, data: RecordData): Promise<void>;
14
- updateRecords(type: RecordType, query: RecordQuery, data: RecordData): Promise<void>;
15
- deleteRecord(type: RecordType, id: RecordId): Promise<void>;
16
- deleteRecords(type: RecordType, query: RecordQuery): Promise<void>;
12
+ updateRecord(type: RecordType, query: RecordQuery, data: RecordData): Promise<number>;
13
+ updateRecords(type: RecordType, query: RecordQuery, data: RecordData): Promise<number>;
14
+ deleteRecord(type: RecordType, query: RecordQuery): Promise<number>;
15
+ deleteRecords(type: RecordType, query: RecordQuery): Promise<number>;
17
16
  clear(): Promise<void>;
18
17
  }
package/dist/Database.js CHANGED
@@ -15,25 +15,22 @@ export default class Database {
15
15
  const cleanData = sanitize(data);
16
16
  return this.#driver.createRecord(type, cleanData);
17
17
  }
18
- readRecord(type, id, fields) {
19
- return this.#driver.readRecord(type, id, fields);
20
- }
21
- findRecord(type, query, fields, sort) {
22
- return this.#driver.findRecord(type, query, fields, sort);
18
+ readRecord(type, query, fields, sort) {
19
+ return this.#driver.readRecord(type, query, fields, sort);
23
20
  }
24
21
  searchRecords(type, query, fields, sort, limit, offset) {
25
22
  return this.#driver.searchRecords(type, query, fields, sort, limit, offset);
26
23
  }
27
- updateRecord(type, id, data) {
24
+ updateRecord(type, query, data) {
28
25
  const cleanData = sanitize(data);
29
- return this.#driver.updateRecord(type, id, cleanData);
26
+ return this.#driver.updateRecord(type, query, cleanData);
30
27
  }
31
28
  updateRecords(type, query, data) {
32
29
  const cleanData = sanitize(data);
33
30
  return this.#driver.updateRecords(type, query, cleanData);
34
31
  }
35
- deleteRecord(type, id) {
36
- return this.#driver.deleteRecord(type, id);
32
+ deleteRecord(type, query) {
33
+ return this.#driver.deleteRecord(type, query);
37
34
  }
38
35
  deleteRecords(type, query) {
39
36
  return this.#driver.deleteRecords(type, query);
@@ -4,12 +4,11 @@ export interface Driver {
4
4
  connect(): Promise<void>;
5
5
  disconnect(): Promise<void>;
6
6
  createRecord(type: RecordType, data: RecordData): Promise<RecordId>;
7
- readRecord(type: RecordType, id: RecordId, fields?: RecordField[]): Promise<RecordData>;
8
- findRecord(type: RecordType, query: RecordQuery, fields?: RecordField[], sort?: RecordSort): Promise<RecordData | undefined>;
7
+ readRecord(type: RecordType, query: RecordQuery, fields?: RecordField[], sort?: RecordSort): Promise<RecordData | undefined>;
9
8
  searchRecords(type: RecordType, query: RecordQuery, fields?: RecordField[], sort?: RecordSort, limit?: number, offset?: number): Promise<RecordData[]>;
10
- updateRecord(type: RecordType, id: RecordId, data: RecordData): Promise<void>;
11
- updateRecords(type: RecordType, query: RecordQuery, data: RecordData): Promise<void>;
12
- deleteRecord(type: RecordType, id: RecordId): Promise<void>;
13
- deleteRecords(type: RecordType, query: RecordQuery): Promise<void>;
9
+ updateRecord(type: RecordType, query: RecordQuery, data: RecordData): Promise<number>;
10
+ updateRecords(type: RecordType, query: RecordQuery, data: RecordData): Promise<number>;
11
+ deleteRecord(type: RecordType, query: RecordQuery): Promise<number>;
12
+ deleteRecords(type: RecordType, query: RecordQuery): Promise<number>;
14
13
  clear(): Promise<void>;
15
14
  }
@@ -1,5 +1,5 @@
1
1
  import type { Driver } from '../../definitions/interfaces.js';
2
- import type { QueryStatement, RecordData, RecordSort } from '../../definitions/types.js';
2
+ import type { RecordData, RecordQuery, RecordSort } from '../../definitions/types.js';
3
3
  export default class Memory implements Driver {
4
4
  #private;
5
5
  recordId: number;
@@ -7,12 +7,11 @@ export default class Memory implements Driver {
7
7
  connect(): Promise<void>;
8
8
  disconnect(): Promise<void>;
9
9
  createRecord(type: string, data: RecordData): Promise<string>;
10
- readRecord(type: string, id: string, fields?: string[]): Promise<RecordData>;
11
- findRecord(type: string, query: QueryStatement, fields?: string[], sort?: RecordSort): Promise<RecordData | undefined>;
12
- searchRecords(type: string, query: QueryStatement, fields?: string[], sort?: RecordSort, limit?: number, offset?: number): Promise<RecordData[]>;
13
- updateRecord(type: string, id: string, data: RecordData): Promise<void>;
14
- updateRecords(type: string, query: QueryStatement, data: RecordData): Promise<void>;
15
- deleteRecord(type: string, id: string): Promise<void>;
16
- deleteRecords(type: string, query: QueryStatement): Promise<void>;
10
+ readRecord(type: string, query: RecordQuery, fields?: string[], sort?: RecordSort): Promise<RecordData | undefined>;
11
+ searchRecords(type: string, query: RecordQuery, fields?: string[], sort?: RecordSort, limit?: number, offset?: number): Promise<RecordData[]>;
12
+ updateRecord(type: string, query: RecordQuery, data: RecordData): Promise<number>;
13
+ updateRecords(type: string, query: RecordQuery, data: RecordData): Promise<number>;
14
+ deleteRecord(type: string, query: RecordQuery): Promise<number>;
15
+ deleteRecords(type: string, query: RecordQuery): Promise<number>;
17
16
  clear(): Promise<void>;
18
17
  }
@@ -1,7 +1,5 @@
1
1
  import { LogicalOperators, QueryOperators, SortDirections } from '../../definitions/constants.js';
2
2
  import NotConnected from '../../errors/NotConnected.js';
3
- import RecordNotFound from '../../errors/RecordNotFound.js';
4
- import RecordNotUpdated from '../../errors/RecordNotUpdated.js';
5
3
  const OPERATORS = {
6
4
  [QueryOperators.EQUALS]: '==',
7
5
  [QueryOperators.GREATER_THAN]: '>',
@@ -33,14 +31,7 @@ export default class Memory {
33
31
  collection.push(record);
34
32
  return record.id;
35
33
  }
36
- async readRecord(type, id, fields) {
37
- const record = this.#fetchRecord(type, id);
38
- if (record === undefined) {
39
- throw new RecordNotFound();
40
- }
41
- return this.#buildRecordData(record, fields);
42
- }
43
- async findRecord(type, query, fields, sort) {
34
+ async readRecord(type, query, fields, sort) {
44
35
  const result = await this.searchRecords(type, query, fields, sort, 1, 0);
45
36
  return result[0];
46
37
  }
@@ -50,24 +41,28 @@ export default class Memory {
50
41
  const limitedRecords = this.#limitNumberOfRecords(sortedRecords, offset, limit);
51
42
  return limitedRecords.map(record => this.#buildRecordData(record, fields));
52
43
  }
53
- async updateRecord(type, id, data) {
54
- const record = this.#fetchRecord(type, id);
44
+ async updateRecord(type, query, data) {
45
+ const record = this.#fetchRecord(type, query);
55
46
  if (record === undefined) {
56
- throw new RecordNotUpdated();
47
+ return 0;
57
48
  }
58
49
  this.#updateRecordData(record, data);
50
+ return 1;
59
51
  }
60
52
  async updateRecords(type, query, data) {
61
53
  const records = this.#fetchRecords(type, query);
62
54
  records.forEach(record => this.#updateRecordData(record, data));
55
+ return records.length;
63
56
  }
64
- async deleteRecord(type, id) {
57
+ async deleteRecord(type, query) {
58
+ const filterFunction = this.#buildFilterFunction(query);
65
59
  const collection = this.#getCollection(type);
66
- const index = collection.findIndex(record => record.id === id);
60
+ const index = collection.findIndex(filterFunction);
67
61
  if (index === -1) {
68
- throw new RecordNotFound();
62
+ return 0;
69
63
  }
70
64
  collection.splice(index, 1);
65
+ return 1;
71
66
  }
72
67
  async deleteRecords(type, query) {
73
68
  const collection = this.#getCollection(type);
@@ -76,13 +71,15 @@ export default class Memory {
76
71
  .map(fetchedRecord => collection.findIndex(collectionRecord => collectionRecord.id === fetchedRecord.id))
77
72
  .sort((a, b) => b - a); // Reverse the order of indexes to delete from the end to the beginning
78
73
  indexes.forEach(index => collection.splice(index, 1));
74
+ return indexes.length;
79
75
  }
80
76
  async clear() {
81
77
  this.#memory.clear();
82
78
  }
83
- #fetchRecord(type, id) {
79
+ #fetchRecord(type, query) {
84
80
  const collection = this.#getCollection(type);
85
- return collection.find(object => object.id === id);
81
+ const filterFunction = this.#buildFilterFunction(query);
82
+ return collection.find(filterFunction);
86
83
  }
87
84
  #fetchRecords(type, query) {
88
85
  const collection = this.#getCollection(type);
@@ -7,12 +7,11 @@ export default class MongoDB implements Driver {
7
7
  connect(): Promise<void>;
8
8
  disconnect(): Promise<void>;
9
9
  createRecord(type: RecordType, data: RecordData): Promise<RecordId>;
10
- readRecord(type: RecordType, id: RecordId, fields?: RecordField[]): Promise<RecordData>;
11
- findRecord(type: RecordType, query: RecordQuery, fields?: RecordField[], sort?: RecordSort): Promise<RecordData | undefined>;
10
+ readRecord(type: RecordType, query: RecordQuery, fields?: RecordField[], sort?: RecordSort): Promise<RecordData | undefined>;
12
11
  searchRecords(type: RecordType, query: RecordQuery, fields?: RecordField[], sort?: RecordSort, limit?: number, offset?: number): Promise<RecordData[]>;
13
- updateRecord(type: RecordType, id: RecordId, data: RecordData): Promise<void>;
14
- updateRecords(type: RecordType, query: RecordQuery, data: RecordData): Promise<void>;
15
- deleteRecord(type: RecordType, id: RecordId): Promise<void>;
16
- deleteRecords(type: RecordType, query: RecordQuery): Promise<void>;
12
+ updateRecord(type: RecordType, query: RecordQuery, data: RecordData): Promise<number>;
13
+ updateRecords(type: RecordType, query: RecordQuery, data: RecordData): Promise<number>;
14
+ deleteRecord(type: RecordType, query: RecordQuery): Promise<number>;
15
+ deleteRecords(type: RecordType, query: RecordQuery): Promise<number>;
17
16
  clear(): Promise<void>;
18
17
  }
@@ -3,12 +3,6 @@ import { MongoClient } from 'mongodb';
3
3
  import { ID, LogicalOperators, QueryOperators, SortDirections } from '../../definitions/constants.js';
4
4
  import DatabaseError from '../../errors/DatabaseError.js';
5
5
  import NotConnected from '../../errors/NotConnected.js';
6
- import RecordNotCreated from '../../errors/RecordNotCreated.js';
7
- import RecordNotDeleted from '../../errors/RecordNotDeleted.js';
8
- import RecordNotFound from '../../errors/RecordNotFound.js';
9
- import RecordNotUpdated from '../../errors/RecordNotUpdated.js';
10
- import RecordsNotDeleted from '../../errors/RecordsNotDeleted.js';
11
- import RecordsNotUpdated from '../../errors/RecordsNotUpdated.js';
12
6
  const UNKNOWN_ERROR = 'Unknown error';
13
7
  const OPERATORS = {
14
8
  [QueryOperators.EQUALS]: '$eq',
@@ -73,24 +67,10 @@ export default class MongoDB {
73
67
  const dataCopy = { ...data };
74
68
  const id = dataCopy.id;
75
69
  delete dataCopy.id;
76
- try {
77
- await collection.insertOne({ _id: id, ...dataCopy });
78
- }
79
- catch (error) {
80
- const message = error instanceof Error ? error.message : UNKNOWN_ERROR;
81
- throw new RecordNotCreated(message);
82
- }
70
+ await collection.insertOne({ _id: id, ...dataCopy });
83
71
  return id;
84
72
  }
85
- async readRecord(type, id, fields) {
86
- const collection = await this.#getCollection(type);
87
- const entry = await collection.findOne({ _id: id });
88
- if (entry === null) {
89
- throw new RecordNotFound(`Record ${type} found: ${id}`);
90
- }
91
- return this.#buildRecordData(entry, fields);
92
- }
93
- async findRecord(type, query, fields, sort) {
73
+ async readRecord(type, query, fields, sort) {
94
74
  const result = await this.searchRecords(type, query, fields, sort, 1, 0);
95
75
  return result[0];
96
76
  }
@@ -102,35 +82,29 @@ export default class MongoDB {
102
82
  const result = await cursor.toArray();
103
83
  return result.map(data => this.#buildRecordData(data, fields));
104
84
  }
105
- async updateRecord(type, id, data) {
85
+ async updateRecord(type, query, data) {
86
+ const mongoQuery = this.#buildMongoQuery(query);
106
87
  const collection = await this.#getCollection(type);
107
- const entry = await collection.updateOne({ _id: id }, { $set: data });
108
- if (entry.modifiedCount === 0) {
109
- throw new RecordNotUpdated();
110
- }
88
+ const result = await collection.updateOne(mongoQuery, { $set: data });
89
+ return result.modifiedCount;
111
90
  }
112
91
  async updateRecords(type, query, data) {
113
92
  const mongoQuery = this.#buildMongoQuery(query);
114
93
  const collection = await this.#getCollection(type);
115
94
  const result = await collection.updateMany(mongoQuery, { $set: data });
116
- if (result.acknowledged === false) {
117
- throw new RecordsNotUpdated();
118
- }
95
+ return result.modifiedCount;
119
96
  }
120
- async deleteRecord(type, id) {
97
+ async deleteRecord(type, query) {
98
+ const mongoQuery = this.#buildMongoQuery(query);
121
99
  const collection = await this.#getCollection(type);
122
- const result = await collection.deleteOne({ _id: id });
123
- if (result.deletedCount !== 1) {
124
- throw new RecordNotDeleted();
125
- }
100
+ const result = await collection.deleteOne(mongoQuery);
101
+ return result.deletedCount;
126
102
  }
127
103
  async deleteRecords(type, query) {
128
104
  const mongoQuery = this.#buildMongoQuery(query);
129
105
  const collection = await this.#getCollection(type);
130
106
  const result = await collection.deleteMany(mongoQuery);
131
- if (result.acknowledged === false) {
132
- throw new RecordsNotDeleted();
133
- }
107
+ return result.deletedCount;
134
108
  }
135
109
  async clear() {
136
110
  return; // Deliberately not implemented
package/dist/index.d.ts CHANGED
@@ -4,9 +4,5 @@ export * from './definitions/constants.js';
4
4
  export * from './definitions/types.js';
5
5
  export { default as DatabaseError } from './errors/DatabaseError.js';
6
6
  export { default as NotConnected } from './errors/NotConnected.js';
7
- export { default as RecordNotCreated } from './errors/RecordNotCreated.js';
8
- export { default as RecordNotDeleted } from './errors/RecordNotDeleted.js';
9
- export { default as RecordNotFound } from './errors/RecordNotFound.js';
10
- export { default as RecordNotUpdated } from './errors/RecordNotUpdated.js';
11
7
  export type { Database };
12
8
  export default database;
package/dist/index.js CHANGED
@@ -5,8 +5,4 @@ export * from './definitions/constants.js';
5
5
  export * from './definitions/types.js';
6
6
  export { default as DatabaseError } from './errors/DatabaseError.js';
7
7
  export { default as NotConnected } from './errors/NotConnected.js';
8
- export { default as RecordNotCreated } from './errors/RecordNotCreated.js';
9
- export { default as RecordNotDeleted } from './errors/RecordNotDeleted.js';
10
- export { default as RecordNotFound } from './errors/RecordNotFound.js';
11
- export { default as RecordNotUpdated } from './errors/RecordNotUpdated.js';
12
8
  export default database;
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@theshelf/database",
3
3
  "private": false,
4
- "version": "0.0.3",
4
+ "version": "0.0.4",
5
5
  "type": "module",
6
+ "repository": {
7
+ "url": "https://github.com/MaskingTechnology/theshelf"
8
+ },
6
9
  "scripts": {
7
10
  "build": "tsc",
8
11
  "clean": "rimraf dist",
@@ -1,4 +0,0 @@
1
- import DatabaseError from './DatabaseError.js';
2
- export default class RecordNotCreated extends DatabaseError {
3
- constructor(message?: string);
4
- }
@@ -1,6 +0,0 @@
1
- import DatabaseError from './DatabaseError.js';
2
- export default class RecordNotCreated extends DatabaseError {
3
- constructor(message) {
4
- super(message ?? 'Record not created');
5
- }
6
- }
@@ -1,4 +0,0 @@
1
- import DatabaseError from './DatabaseError.js';
2
- export default class RecordNotDeleted extends DatabaseError {
3
- constructor(message?: string);
4
- }
@@ -1,6 +0,0 @@
1
- import DatabaseError from './DatabaseError.js';
2
- export default class RecordNotDeleted extends DatabaseError {
3
- constructor(message) {
4
- super(message ?? 'Record not deleted');
5
- }
6
- }
@@ -1,4 +0,0 @@
1
- import DatabaseError from './DatabaseError.js';
2
- export default class RecordNotFound extends DatabaseError {
3
- constructor(message?: string);
4
- }
@@ -1,6 +0,0 @@
1
- import DatabaseError from './DatabaseError.js';
2
- export default class RecordNotFound extends DatabaseError {
3
- constructor(message) {
4
- super(message ?? 'Record not found');
5
- }
6
- }
@@ -1,4 +0,0 @@
1
- import DatabaseError from './DatabaseError.js';
2
- export default class RecordNotUpdated extends DatabaseError {
3
- constructor(message?: string);
4
- }
@@ -1,6 +0,0 @@
1
- import DatabaseError from './DatabaseError.js';
2
- export default class RecordNotUpdated extends DatabaseError {
3
- constructor(message) {
4
- super(message ?? 'Record not updated');
5
- }
6
- }
@@ -1,4 +0,0 @@
1
- import DatabaseError from './DatabaseError.js';
2
- export default class RecordsNotDeleted extends DatabaseError {
3
- constructor(message?: string);
4
- }
@@ -1,6 +0,0 @@
1
- import DatabaseError from './DatabaseError.js';
2
- export default class RecordsNotDeleted extends DatabaseError {
3
- constructor(message) {
4
- super(message ?? 'Records not deleted');
5
- }
6
- }
@@ -1,4 +0,0 @@
1
- import DatabaseError from './DatabaseError.js';
2
- export default class RecordsNotUpdated extends DatabaseError {
3
- constructor(message?: string);
4
- }
@@ -1,6 +0,0 @@
1
- import DatabaseError from './DatabaseError.js';
2
- export default class RecordsNotUpdated extends DatabaseError {
3
- constructor(message) {
4
- super(message ?? 'Records not updated');
5
- }
6
- }