document-dataply 0.0.1 → 0.0.2-alpha.1

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/dist/cjs/index.js CHANGED
@@ -6776,11 +6776,11 @@ var require_cjs = __commonJS({
6776
6776
  * Retrieves metadata from the dataply.
6777
6777
  * @returns Metadata of the dataply.
6778
6778
  */
6779
- async getMetadata() {
6779
+ async getMetadata(tx) {
6780
6780
  if (!this.initialized) {
6781
6781
  throw new Error("Dataply instance is not initialized");
6782
6782
  }
6783
- return this.runWithDefault((tx) => this.rowTableEngine.getMetadata(tx));
6783
+ return this.runWithDefault((tx2) => this.rowTableEngine.getMetadata(tx2), tx);
6784
6784
  }
6785
6785
  /**
6786
6786
  * Inserts data. Returns the PK of the added row.
@@ -6932,8 +6932,8 @@ var require_cjs = __commonJS({
6932
6932
  * Retrieves metadata from the dataply.
6933
6933
  * @returns Metadata of the dataply.
6934
6934
  */
6935
- async getMetadata() {
6936
- return this.api.getMetadata();
6935
+ async getMetadata(tx) {
6936
+ return this.api.getMetadata(tx);
6937
6937
  }
6938
6938
  /**
6939
6939
  * Inserts data. Returns the PK of the added row.
@@ -7073,7 +7073,7 @@ var DocumentSerializeStrategyAsync = class extends import_dataply.SerializeStrat
7073
7073
  }
7074
7074
  async readHead() {
7075
7075
  const tx = this.txContext.get();
7076
- const metadata = await this.api.getDocumentMetadata(tx);
7076
+ const metadata = await this.api.getDocumentInnerMetadata(tx);
7077
7077
  if (metadata.treeHeads[this.treeKey]) {
7078
7078
  return metadata.treeHeads[this.treeKey];
7079
7079
  }
@@ -7081,9 +7081,9 @@ var DocumentSerializeStrategyAsync = class extends import_dataply.SerializeStrat
7081
7081
  }
7082
7082
  async writeHead(head) {
7083
7083
  const tx = this.txContext.get();
7084
- const metadata = await this.api.getDocumentMetadata(tx);
7084
+ const metadata = await this.api.getDocumentInnerMetadata(tx);
7085
7085
  metadata.treeHeads[this.treeKey] = head;
7086
- await this.api.updateDocumentMetadata(metadata, tx);
7086
+ await this.api.updateDocumentInnerMetadata(metadata, tx);
7087
7087
  }
7088
7088
  };
7089
7089
 
@@ -7124,7 +7124,7 @@ var DocumentDataplyAPI = class extends import_dataply3.DataplyAPI {
7124
7124
  if (!await this.verifyDocumentFile(tx)) {
7125
7125
  throw new Error("Document metadata verification failed");
7126
7126
  }
7127
- const metadata = await this.getDocumentMetadata(tx);
7127
+ const metadata = await this.getDocumentInnerMetadata(tx);
7128
7128
  this.treeHeads = metadata.treeHeads;
7129
7129
  return tx;
7130
7130
  });
@@ -7193,19 +7193,27 @@ var DocumentDataplyAPI = class extends import_dataply3.DataplyAPI {
7193
7193
  return result;
7194
7194
  }
7195
7195
  async getDocumentMetadata(tx) {
7196
+ const metadata = await this.getMetadata(tx);
7197
+ return {
7198
+ pageSize: metadata.pageSize,
7199
+ pageCount: metadata.pageCount,
7200
+ rowCount: metadata.rowCount
7201
+ };
7202
+ }
7203
+ async getDocumentInnerMetadata(tx) {
7196
7204
  const row = await this.select(1, false, tx);
7197
7205
  if (!row) {
7198
7206
  throw new Error("Document metadata not found");
7199
7207
  }
7200
7208
  return JSON.parse(row);
7201
7209
  }
7202
- async updateDocumentMetadata(metadata, tx) {
7210
+ async updateDocumentInnerMetadata(metadata, tx) {
7203
7211
  await this.update(1, JSON.stringify(metadata), tx);
7204
7212
  }
7205
7213
  async updateTreeHead(tx) {
7206
- const metadata = await this.getDocumentMetadata(tx);
7214
+ const metadata = await this.getDocumentInnerMetadata(tx);
7207
7215
  metadata.treeHeads = this.treeHeads;
7208
- await this.updateDocumentMetadata(metadata, tx);
7216
+ await this.updateDocumentInnerMetadata(metadata, tx);
7209
7217
  }
7210
7218
  };
7211
7219
  var DocumentDataply = class {
@@ -7217,14 +7225,27 @@ var DocumentDataply = class {
7217
7225
  lte: "primaryLte",
7218
7226
  gt: "primaryGt",
7219
7227
  gte: "primaryGte",
7228
+ or: "primaryOr",
7220
7229
  like: "like"
7221
7230
  };
7222
7231
  constructor(file, options) {
7223
7232
  this.api = new DocumentDataplyAPI(file, options ?? {});
7224
7233
  }
7234
+ /**
7235
+ * Initialize the document database
7236
+ */
7225
7237
  async init() {
7226
7238
  await this.api.init();
7227
7239
  }
7240
+ /**
7241
+ * Get the metadata of the document database
7242
+ */
7243
+ async getMetadata(tx) {
7244
+ return this.api.runWithDefault((tx2) => this.api.getDocumentMetadata(tx2), tx);
7245
+ }
7246
+ /**
7247
+ * Create a transaction
7248
+ */
7228
7249
  createTransaction() {
7229
7250
  return this.api.createTransaction();
7230
7251
  }
@@ -7250,7 +7271,7 @@ var DocumentDataply = class {
7250
7271
  return result;
7251
7272
  }
7252
7273
  /**
7253
- * Return optimized tree for query
7274
+ * Get the selectivity candidate for the given query
7254
7275
  * @param query
7255
7276
  * @returns
7256
7277
  */
@@ -7273,9 +7294,9 @@ var DocumentDataply = class {
7273
7294
  };
7274
7295
  }
7275
7296
  async insertDocument(document, tx) {
7276
- const metadata = await this.api.getDocumentMetadata(tx);
7297
+ const metadata = await this.api.getDocumentInnerMetadata(tx);
7277
7298
  const id = metadata.lastId++;
7278
- await this.api.updateDocumentMetadata(metadata, tx);
7299
+ await this.api.updateDocumentInnerMetadata(metadata, tx);
7279
7300
  const dataplyDocument = Object.assign({
7280
7301
  _id: id
7281
7302
  }, document);
@@ -7286,6 +7307,12 @@ var DocumentDataply = class {
7286
7307
  document: dataplyDocument
7287
7308
  };
7288
7309
  }
7310
+ /**
7311
+ * Insert a document into the database
7312
+ * @param document The document to insert
7313
+ * @param tx The transaction to use
7314
+ * @returns True if the document was inserted successfully
7315
+ */
7289
7316
  async insert(document, tx) {
7290
7317
  return this.api.runWithDefault(async (tx2) => {
7291
7318
  const { k, document: dataplyDocument } = await this.insertDocument(document, tx2);
@@ -7298,6 +7325,13 @@ var DocumentDataply = class {
7298
7325
  return true;
7299
7326
  }, tx);
7300
7327
  }
7328
+ /**
7329
+ * Select documents from the database
7330
+ * @param query The query to use
7331
+ * @param limit The maximum number of documents to return
7332
+ * @param tx The transaction to use
7333
+ * @returns The documents that match the query
7334
+ */
7301
7335
  async select(query, limit = Infinity, tx) {
7302
7336
  return this.api.runWithDefault(async (tx2) => {
7303
7337
  const verbose = this.verboseQuery(query);
@@ -7331,6 +7365,9 @@ var DocumentDataply = class {
7331
7365
  return documents;
7332
7366
  }, tx);
7333
7367
  }
7368
+ /**
7369
+ * Close the document database
7370
+ */
7334
7371
  async close() {
7335
7372
  await this.api.close();
7336
7373
  }
@@ -1,14 +1,14 @@
1
- import type { DataplyTreeValue, DocumentDataplyMetadata, DocumentDataplyOptions, DocumentJSON, FlattenedDocumentJSON, Primitive, DocumentDataplyQuery, FinalFlatten, DocumentDataplyCondition, DataplyDocument } from '../types';
1
+ import type { DataplyTreeValue, DocumentDataplyInnerMetadata, DocumentDataplyOptions, DocumentJSON, FlattenedDocumentJSON, Primitive, DocumentDataplyQuery, FinalFlatten, DocumentDataplyCondition, DataplyDocument, DocumentDataplyMetadata } from '../types';
2
2
  import { DataplyAPI, Transaction, BPTreeAsync } from 'dataply';
3
3
  import { DocumentValueComparator } from './bptree/documentComparator';
4
4
  export declare class DocumentDataplyAPI<T extends DocumentJSON> extends DataplyAPI {
5
5
  runWithDefault: <T_1>(callback: (tx: Transaction) => Promise<T_1>, tx?: Transaction) => Promise<T_1>;
6
- treeHeads: DocumentDataplyMetadata['treeHeads'];
6
+ treeHeads: DocumentDataplyInnerMetadata['treeHeads'];
7
7
  readonly trees: Map<string, BPTreeAsync<number, DataplyTreeValue<Primitive>>>;
8
8
  readonly comparator: DocumentValueComparator<DataplyTreeValue<Primitive>, Primitive>;
9
9
  constructor(file: string, options: DocumentDataplyOptions);
10
10
  ensureTree<T extends Primitive>(field: string): Promise<BPTreeAsync<number, DataplyTreeValue<T>>>;
11
- createDocumentMetadata(): DocumentDataplyMetadata;
11
+ createDocumentMetadata(): DocumentDataplyInnerMetadata;
12
12
  initializeDocumentFile(tx: Transaction): Promise<void>;
13
13
  verifyDocumentFile(tx: Transaction): Promise<boolean>;
14
14
  /**
@@ -18,18 +18,29 @@ export declare class DocumentDataplyAPI<T extends DocumentJSON> extends DataplyA
18
18
  */
19
19
  flattenDocument(document: T): FlattenedDocumentJSON;
20
20
  getDocumentMetadata(tx: Transaction): Promise<DocumentDataplyMetadata>;
21
- updateDocumentMetadata(metadata: DocumentDataplyMetadata, tx: Transaction): Promise<void>;
21
+ getDocumentInnerMetadata(tx: Transaction): Promise<DocumentDataplyInnerMetadata>;
22
+ updateDocumentInnerMetadata(metadata: DocumentDataplyInnerMetadata, tx: Transaction): Promise<void>;
22
23
  updateTreeHead(tx: Transaction): Promise<void>;
23
24
  }
24
25
  export declare class DocumentDataply<T extends DocumentJSON> {
25
26
  protected readonly api: DocumentDataplyAPI<T>;
26
27
  private readonly operatorConverters;
27
28
  constructor(file: string, options?: DocumentDataplyOptions);
29
+ /**
30
+ * Initialize the document database
31
+ */
28
32
  init(): Promise<void>;
33
+ /**
34
+ * Get the metadata of the document database
35
+ */
36
+ getMetadata(tx?: Transaction): Promise<DocumentDataplyMetadata>;
37
+ /**
38
+ * Create a transaction
39
+ */
29
40
  createTransaction(): Transaction;
30
41
  private verboseQuery;
31
42
  /**
32
- * Return optimized tree for query
43
+ * Get the selectivity candidate for the given query
33
44
  * @param query
34
45
  * @returns
35
46
  */
@@ -44,7 +55,23 @@ export declare class DocumentDataply<T extends DocumentJSON> {
44
55
  }[];
45
56
  } | null>;
46
57
  private insertDocument;
58
+ /**
59
+ * Insert a document into the database
60
+ * @param document The document to insert
61
+ * @param tx The transaction to use
62
+ * @returns True if the document was inserted successfully
63
+ */
47
64
  insert(document: T, tx?: Transaction): Promise<boolean>;
65
+ /**
66
+ * Select documents from the database
67
+ * @param query The query to use
68
+ * @param limit The maximum number of documents to return
69
+ * @param tx The transaction to use
70
+ * @returns The documents that match the query
71
+ */
48
72
  select(query: Partial<DocumentDataplyQuery<FinalFlatten<DataplyDocument<T>>>>, limit?: number, tx?: Transaction): Promise<DataplyDocument<T>[]>;
73
+ /**
74
+ * Close the document database
75
+ */
49
76
  close(): Promise<void>;
50
77
  }
@@ -9,7 +9,7 @@ export type DocumentJSON = {
9
9
  export type FlattenedDocumentJSON = {
10
10
  [key: string]: Primitive;
11
11
  };
12
- export interface DocumentDataplyMetadata {
12
+ export interface DocumentDataplyInnerMetadata {
13
13
  magicString: string;
14
14
  version: number;
15
15
  createdAt: number;
@@ -17,6 +17,20 @@ export interface DocumentDataplyMetadata {
17
17
  lastId: number;
18
18
  treeHeads: Record<string, SerializeStrategyHead>;
19
19
  }
20
+ export interface DocumentDataplyMetadata {
21
+ /**
22
+ * The size of a page in bytes.
23
+ */
24
+ pageSize: number;
25
+ /**
26
+ * The total number of pages in the dataply.
27
+ */
28
+ pageCount: number;
29
+ /**
30
+ * The total number of data rows in the dataply.
31
+ */
32
+ rowCount: number;
33
+ }
20
34
  export type DataplyDocumentBase = {
21
35
  _id: number;
22
36
  };
@@ -29,6 +43,7 @@ export type DocumentDataplyCondition<V> = {
29
43
  equal?: Partial<V>;
30
44
  notEqual?: Partial<V>;
31
45
  like?: Partial<V>;
46
+ or?: Partial<V>[];
32
47
  };
33
48
  export type DocumentDataplyQuery<T> = {
34
49
  [key in keyof T]: T[key] | DocumentDataplyCondition<T[key]>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document-dataply",
3
- "version": "0.0.1",
3
+ "version": "0.0.2-alpha.1",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "author": "izure <admin@izure.org>",
@@ -26,7 +26,7 @@
26
26
  "build": "node build/index.js && tsc"
27
27
  },
28
28
  "dependencies": {
29
- "dataply": "^0.0.14"
29
+ "dataply": "^0.0.16-alpha.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/jest": "^30.0.0",