@quantform/sqlite 0.6.7 → 0.7.0-beta.5

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/index.d.ts CHANGED
@@ -1,2 +1,6 @@
1
1
  export * from './sqlite-storage';
2
+ export declare function sqlite(directory?: string): {
3
+ provide: symbol;
4
+ useValue: import("@quantform/core").StorageFactory;
5
+ };
2
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,qBAAqB,CAAC;AAEpC,wBAAgB,MAAM,CAAC,SAAS,CAAC,EAAE,MAAM;;;EAExC"}
package/dist/index.js CHANGED
@@ -14,4 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.sqlite = void 0;
18
+ const sqlite_storage_1 = require("./sqlite-storage");
19
+ const core_1 = require("@quantform/core");
17
20
  __exportStar(require("./sqlite-storage"), exports);
21
+ function sqlite(directory) {
22
+ return (0, core_1.storage)(new sqlite_storage_1.SQLiteStorageFactory(directory));
23
+ }
24
+ exports.sqlite = sqlite;
@@ -0,0 +1,10 @@
1
+ import { InferQueryObject, Query, QueryMappingType, QueryObject, QueryObjectType, QueryWhere } from '@quantform/core';
2
+ export declare class SQLiteLanguage {
3
+ static getType(type: QueryMappingType): "INTEGER" | "TEXT";
4
+ static getValue(type: QueryMappingType, value: any): any;
5
+ static getConstraint(columnName: string, type: QueryMappingType, constraint: QueryWhere): string;
6
+ static createTable<T extends QueryObjectType<K>, K extends QueryObject>(type: T): string;
7
+ static query<T extends QueryObjectType<K>, K extends QueryObject>(type: T, query: Query<InferQueryObject<T>>): string;
8
+ static replace<T extends QueryObjectType<K>, K extends QueryObject>(type: T): string;
9
+ }
10
+ //# sourceMappingURL=sqlite-language.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-language.d.ts","sourceRoot":"","sources":["../src/sqlite-language.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,KAAK,EACL,gBAAgB,EAChB,WAAW,EACX,eAAe,EACf,UAAU,EACX,MAAM,iBAAiB,CAAC;AAEzB,qBAAa,cAAc;IACzB,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,gBAAgB;IAWrC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,gBAAgB,EAAE,KAAK,EAAE,GAAG;IAWlD,MAAM,CAAC,aAAa,CAClB,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,gBAAgB,EACtB,UAAU,EAAE,UAAU;IAiBxB,MAAM,CAAC,WAAW,CAAC,CAAC,SAAS,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,EAAE,IAAI,EAAE,CAAC;IAQ/E,MAAM,CAAC,KAAK,CAAC,CAAC,SAAS,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,EAC9D,IAAI,EAAE,CAAC,EACP,KAAK,EAAE,KAAK,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC;IAwCnC,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,EAAE,IAAI,EAAE,CAAC;CAW5E"}
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SQLiteLanguage = void 0;
4
+ class SQLiteLanguage {
5
+ static getType(type) {
6
+ switch (type) {
7
+ case 'number':
8
+ return 'INTEGER';
9
+ case 'decimal':
10
+ return 'TEXT';
11
+ case 'string':
12
+ return 'TEXT';
13
+ }
14
+ }
15
+ static getValue(type, value) {
16
+ switch (type) {
17
+ case 'number':
18
+ return value;
19
+ case 'decimal':
20
+ return `'${value.toString()}'`;
21
+ case 'string':
22
+ return `'${value}'`;
23
+ }
24
+ }
25
+ static getConstraint(columnName, type, constraint) {
26
+ switch (constraint === null || constraint === void 0 ? void 0 : constraint.type) {
27
+ case 'eq':
28
+ return `${columnName} = ${SQLiteLanguage.getValue(type, constraint.value)}`;
29
+ case 'gt':
30
+ return `${columnName} > ${SQLiteLanguage.getValue(type, constraint.value)}`;
31
+ case 'lt':
32
+ return `${columnName} < ${SQLiteLanguage.getValue(type, constraint.value)}`;
33
+ case 'between':
34
+ return `${columnName} > ${SQLiteLanguage.getValue(type, constraint.min)} AND ${columnName} < ${SQLiteLanguage.getValue(type, constraint.max)}`;
35
+ }
36
+ }
37
+ static createTable(type) {
38
+ const columns = `${Object.entries(type.type)
39
+ .map(([name, type]) => `${name} ${SQLiteLanguage.getType(type)} NOT NULL`)
40
+ .join(', ')}`;
41
+ return `CREATE TABLE IF NOT EXISTS "${type.discriminator}" (${columns}, PRIMARY KEY (timestamp))`;
42
+ }
43
+ static query(type, query) {
44
+ var _a;
45
+ const columns = `${Object.entries(type.type)
46
+ .map(([name]) => `${name}`)
47
+ .join(', ')}`;
48
+ let sql = `SELECT ${columns} FROM "${type.discriminator}"`;
49
+ if (query.where) {
50
+ const where = Array.of();
51
+ for (const columnName of Object.keys(query.where)) {
52
+ const constraint = query.where[columnName];
53
+ if (!constraint) {
54
+ continue;
55
+ }
56
+ where.push(SQLiteLanguage.getConstraint(columnName, type.type[columnName], constraint));
57
+ }
58
+ sql = `${sql} WHERE ${where.join(' AND ')}`;
59
+ }
60
+ if (query.orderBy) {
61
+ sql = `${sql} ORDER BY timestamp ${(_a = query.orderBy) !== null && _a !== void 0 ? _a : 'ASC'}`;
62
+ }
63
+ if (query.limit) {
64
+ sql = `${sql} LIMIT ${query.limit}`;
65
+ }
66
+ if (query.offset) {
67
+ sql = `${sql} OFFSET ${query.offset}`;
68
+ }
69
+ return sql;
70
+ }
71
+ static replace(type) {
72
+ const columns = `${Object.entries(type.type)
73
+ .map(([name]) => `${name}`)
74
+ .join(', ')}`;
75
+ return `REPLACE INTO "${type.discriminator}" (${columns}) VALUES(${Object.entries(type.type)
76
+ .map(() => '?')
77
+ .join(', ')})`;
78
+ }
79
+ }
80
+ exports.SQLiteLanguage = SQLiteLanguage;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=sqlite-language.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-language.spec.d.ts","sourceRoot":"","sources":["../src/sqlite-language.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const core_1 = require("@quantform/core");
13
+ const sqlite_language_1 = require("./sqlite-language");
14
+ describe(sqlite_language_1.SQLiteLanguage.name, () => {
15
+ test('create table for object', () => __awaiter(void 0, void 0, void 0, function* () {
16
+ const object = core_1.Storage.createObject('orders', {
17
+ timestamp: 'number',
18
+ price: 'decimal',
19
+ id: 'string'
20
+ });
21
+ const sql = sqlite_language_1.SQLiteLanguage.createTable(object);
22
+ expect(sql).toEqual(`CREATE TABLE IF NOT EXISTS "orders" (timestamp INTEGER NOT NULL, price TEXT NOT NULL, id TEXT NOT NULL, PRIMARY KEY (timestamp))`);
23
+ }));
24
+ test('select to query object', () => __awaiter(void 0, void 0, void 0, function* () {
25
+ const object = core_1.Storage.createObject('orders', {
26
+ timestamp: 'number',
27
+ price: 'decimal',
28
+ id: 'string'
29
+ });
30
+ const sql = sqlite_language_1.SQLiteLanguage.query(object, {
31
+ where: {
32
+ timestamp: (0, core_1.lt)(100),
33
+ id: (0, core_1.eq)('unique-id'),
34
+ price: (0, core_1.eq)((0, core_1.d)('1.123456789123456789'))
35
+ },
36
+ limit: 3,
37
+ offset: 2
38
+ });
39
+ expect(sql).toEqual(`SELECT timestamp, price, id FROM "orders" WHERE timestamp < 100 AND id = 'unique-id' AND price = '1.123456789123456789' LIMIT 3 OFFSET 2`);
40
+ }));
41
+ test('replace statement', () => __awaiter(void 0, void 0, void 0, function* () {
42
+ const object = core_1.Storage.createObject('orders', {
43
+ timestamp: 'number',
44
+ price: 'decimal',
45
+ id: 'string'
46
+ });
47
+ const sql = sqlite_language_1.SQLiteLanguage.replace(object);
48
+ expect(sql).toEqual(`REPLACE INTO "orders" (timestamp, price, id) VALUES(?, ?, ?)`);
49
+ }));
50
+ });
@@ -1,14 +1,17 @@
1
1
  import { Database } from 'better-sqlite3';
2
- import { SessionFeature, Storage, StorageDocument, StorageQueryOptions } from '@quantform/core';
3
- export declare function sqlite(directory?: string): SessionFeature;
2
+ import { InferQueryObject, Query, QueryObject, QueryObjectType, Storage, StorageFactory } from '@quantform/core';
3
+ export declare class SQLiteStorageFactory implements StorageFactory {
4
+ private readonly directory?;
5
+ constructor(directory?: string | undefined);
6
+ for(key: string): Storage;
7
+ }
4
8
  export declare class SQLiteStorage implements Storage {
5
- private readonly filename;
6
- protected connection?: Database;
9
+ readonly filename: string;
10
+ protected connection: Database;
11
+ private tables?;
7
12
  constructor(filename: string);
8
- private tryConnect;
9
- private tryCreateTable;
10
13
  index(): Promise<Array<string>>;
11
- query(library: string, options: StorageQueryOptions): Promise<StorageDocument[]>;
12
- save(library: string, documents: StorageDocument[]): Promise<void>;
14
+ query<T extends QueryObjectType<K>, K extends QueryObject>(type: T, query: Query<InferQueryObject<T>>): Promise<InferQueryObject<T>[]>;
15
+ save<T extends QueryObjectType<K>, K extends QueryObject>(type: T, objects: InferQueryObject<T>[]): Promise<void>;
13
16
  }
14
17
  //# sourceMappingURL=sqlite-storage.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sqlite-storage.d.ts","sourceRoot":"","sources":["../src/sqlite-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAK1C,OAAO,EAEL,cAAc,EACd,OAAO,EACP,eAAe,EACf,mBAAmB,EAEpB,MAAM,iBAAiB,CAAC;AAIzB,wBAAgB,MAAM,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,cAAc,CAOzD;AAED,qBAAa,aAAc,YAAW,OAAO;IAG/B,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAFrC,SAAS,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC;gBAEH,QAAQ,EAAE,MAAM;IAE7C,OAAO,CAAC,UAAU;IAYlB,OAAO,CAAC,cAAc;IAehB,KAAK,IAAI,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAa/B,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAwChF,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,eAAe,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAoBzE"}
1
+ {"version":3,"file":"sqlite-storage.d.ts","sourceRoot":"","sources":["../src/sqlite-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAK1C,OAAO,EAEL,gBAAgB,EAEhB,KAAK,EACL,WAAW,EACX,eAAe,EACf,OAAO,EACP,cAAc,EAEf,MAAM,iBAAiB,CAAC;AAIzB,qBACa,oBAAqB,YAAW,cAAc;IAC7C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC;gBAAV,SAAS,CAAC,oBAAQ;IAE/C,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;CAK1B;AAED,qBAAa,aAAc,YAAW,OAAO;IAI/B,QAAQ,CAAC,QAAQ,EAAE,MAAM;IAHrC,SAAS,CAAC,UAAU,EAAE,QAAQ,CAAC;IAC/B,OAAO,CAAC,MAAM,CAAC,CAAW;gBAEL,QAAQ,EAAE,MAAM;IAQ/B,KAAK,IAAI,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAQ/B,KAAK,CAAC,CAAC,SAAS,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,EAC7D,IAAI,EAAE,CAAC,EACP,KAAK,EAAE,KAAK,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,GAChC,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,CAAC;IA0B3B,IAAI,CAAC,CAAC,SAAS,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,EAC5D,IAAI,EAAE,CAAC,EACP,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,GAC7B,OAAO,CAAC,IAAI,CAAC;CAuBjB"}
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
2
11
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
12
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
13
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -9,92 +18,79 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
18
  });
10
19
  };
11
20
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.SQLiteStorage = exports.sqlite = void 0;
21
+ exports.SQLiteStorage = exports.SQLiteStorageFactory = void 0;
13
22
  const bettersqlite3 = require("better-sqlite3");
14
23
  const fs_1 = require("fs");
15
24
  const path_1 = require("path");
16
25
  const core_1 = require("@quantform/core");
17
- const error_1 = require("./error");
18
- function sqlite(directory) {
19
- return (builder) => {
20
- builder.useStorage((type) => new SQLiteStorage((0, path_1.join)(directory !== null && directory !== void 0 ? directory : (0, core_1.workingDirectory)(), `/${type}.sqlite`)));
21
- };
22
- }
23
- exports.sqlite = sqlite;
26
+ const sqlite_language_1 = require("./sqlite-language");
27
+ let SQLiteStorageFactory = class SQLiteStorageFactory {
28
+ constructor(directory) {
29
+ this.directory = directory;
30
+ }
31
+ for(key) {
32
+ var _a;
33
+ return new SQLiteStorage((0, path_1.join)((_a = this.directory) !== null && _a !== void 0 ? _a : (0, core_1.workingDirectory)(), `/${key}.sqlite`));
34
+ }
35
+ };
36
+ SQLiteStorageFactory = __decorate([
37
+ (0, core_1.provider)(),
38
+ __metadata("design:paramtypes", [String])
39
+ ], SQLiteStorageFactory);
40
+ exports.SQLiteStorageFactory = SQLiteStorageFactory;
24
41
  class SQLiteStorage {
25
42
  constructor(filename) {
26
43
  this.filename = filename;
27
- }
28
- tryConnect() {
29
- if (this.connection) {
30
- return;
31
- }
32
44
  if (!(0, fs_1.existsSync)((0, path_1.dirname)(this.filename))) {
33
45
  (0, fs_1.mkdirSync)((0, path_1.dirname)(this.filename), { recursive: true });
34
46
  }
35
47
  this.connection = bettersqlite3(this.filename);
36
48
  }
37
- tryCreateTable(table) {
38
- if (!this.connection) {
39
- throw new error_1.NoConnectionError();
40
- }
41
- this.connection.exec(`CREATE TABLE IF NOT EXISTS "${table}" (
42
- timestamp INTEGER NOT NULL,
43
- kind TEXT NOT NULL,
44
- json TEXT NOT NULL,
45
- PRIMARY KEY (timestamp, kind)
46
- )`);
47
- }
48
49
  index() {
49
50
  return __awaiter(this, void 0, void 0, function* () {
50
- this.tryConnect();
51
- if (!this.connection) {
52
- throw new error_1.NoConnectionError();
53
- }
54
51
  return this.connection
55
52
  .prepare("SELECT name FROM sqlite_master WHERE type='table'")
56
53
  .all()
57
54
  .map(it => it.name);
58
55
  });
59
56
  }
60
- query(library, options) {
61
- var _a, _b;
57
+ // eslint-disable-next-line complexity
58
+ query(type, query) {
62
59
  return __awaiter(this, void 0, void 0, function* () {
63
- this.tryConnect();
64
- if (!this.connection) {
65
- throw new error_1.NoConnectionError();
60
+ if (!this.tables) {
61
+ this.tables = yield this.index();
66
62
  }
67
- if (!this.connection
68
- .prepare(`SELECT name FROM sqlite_master WHERE type='table' AND name='${library}'`)
69
- .all().length) {
63
+ if (!this.tables.includes(type.discriminator)) {
70
64
  return [];
71
65
  }
72
- const isBackward = options.from == undefined;
73
- let rows = this.connection
74
- .prepare(`SELECT * FROM "${library}"
75
- WHERE timestamp > ? AND timestamp < ? ${options.kind ? `AND kind = '${options.kind}'` : ''}
76
- ORDER BY timestamp ${isBackward ? 'DESC' : ''}
77
- LIMIT ?`)
78
- .all([(_a = options.from) !== null && _a !== void 0 ? _a : 0, (_b = options.to) !== null && _b !== void 0 ? _b : Number.MAX_VALUE], Math.min(options.count, 50000));
79
- if (isBackward) {
80
- rows = rows.reverse();
81
- }
82
- return rows;
66
+ const objects = yield this.connection
67
+ .prepare(sqlite_language_1.SQLiteLanguage.query(type, query))
68
+ .all();
69
+ const types = Object.keys(type.type);
70
+ objects.forEach(it => {
71
+ for (const prop of types) {
72
+ if (type.type[prop] == 'decimal') {
73
+ it[prop] = (0, core_1.d)(it[prop]);
74
+ }
75
+ }
76
+ });
77
+ return objects;
83
78
  });
84
79
  }
85
- save(library, documents) {
80
+ save(type, objects) {
86
81
  return __awaiter(this, void 0, void 0, function* () {
87
- this.tryConnect();
88
- if (!this.connection) {
89
- throw new error_1.NoConnectionError();
82
+ if (!this.tables) {
83
+ this.tables = yield this.index();
84
+ }
85
+ if (!this.tables.includes(type.discriminator)) {
86
+ this.connection.exec(sqlite_language_1.SQLiteLanguage.createTable(type));
87
+ this.tables = undefined;
90
88
  }
91
- this.tryCreateTable(library);
92
- const statement = this.connection.prepare(`
93
- REPLACE INTO "${library}" (timestamp, kind, json)
94
- VALUES(?, ?, ?);
95
- `);
96
- const insertMany = this.connection.transaction(rows => rows.forEach((it) => statement.run(it.timestamp, it.kind, it.json)));
97
- insertMany(documents);
89
+ const statement = this.connection.prepare(sqlite_language_1.SQLiteLanguage.replace(type));
90
+ const types = Object.keys(type.type);
91
+ const mapper = (it) => types.map(type => it[type].toString());
92
+ const insertMany = this.connection.transaction(rows => rows.forEach((it) => statement.run(mapper(it))));
93
+ insertMany(objects);
98
94
  });
99
95
  }
100
96
  }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=sqlite-storage.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite-storage.spec.d.ts","sourceRoot":"","sources":["../src/sqlite-storage.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,204 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const fs_1 = require("fs");
13
+ const core_1 = require("@quantform/core");
14
+ const sqlite_storage_1 = require("./sqlite-storage");
15
+ describe(sqlite_storage_1.SQLiteStorage.name, () => {
16
+ let fixtures;
17
+ beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
18
+ fixtures = yield getFixtures();
19
+ }));
20
+ afterEach(() => {
21
+ fixtures.dispose();
22
+ });
23
+ /*
24
+ test('index return the names of discriminators', async () => {
25
+ const { sut } = fixtures;
26
+
27
+ await sut.save({ discriminator: 'pricing' }, [{ timestamp: 1, message: 'test-1' }]);
28
+ await sut.save({ discriminator: 'ordering' }, [{ timestamp: 1, message: 'test-1' }]);
29
+
30
+ const index = await sut.index();
31
+
32
+ expect(index).toEqual(['pricing', 'ordering']);
33
+ });
34
+ */
35
+ test('write and read single object', () => __awaiter(void 0, void 0, void 0, function* () {
36
+ const { sut, object } = fixtures;
37
+ yield sut.save(object, [
38
+ { timestamp: 1, id: '123 123', price: (0, core_1.d)('1.123456789123456789'), quantity: 5 }
39
+ ]);
40
+ const set = yield sut.query(object, {
41
+ where: {
42
+ id: (0, core_1.eq)('123 123')
43
+ }
44
+ });
45
+ expect(set).toEqual([
46
+ { timestamp: 1, id: '123 123', price: (0, core_1.d)('1.123456789123456789'), quantity: 5 }
47
+ ]);
48
+ }));
49
+ test('save and read full data', () => __awaiter(void 0, void 0, void 0, function* () {
50
+ const { sut } = fixtures;
51
+ const pricing = core_1.Storage.createObject('pricing', {
52
+ timestamp: 'number',
53
+ rate: 'decimal'
54
+ });
55
+ yield sut.save(pricing, [
56
+ { timestamp: 1, rate: (0, core_1.d)(1) },
57
+ { timestamp: 2, rate: (0, core_1.d)(2) },
58
+ { timestamp: 3, rate: (0, core_1.d)(3) },
59
+ { timestamp: 4, rate: (0, core_1.d)(4) },
60
+ { timestamp: 5, rate: (0, core_1.d)(5) }
61
+ ]);
62
+ const set = yield sut.query(pricing, {});
63
+ expect(set).toEqual([
64
+ { timestamp: 1, rate: (0, core_1.d)(1) },
65
+ { timestamp: 2, rate: (0, core_1.d)(2) },
66
+ { timestamp: 3, rate: (0, core_1.d)(3) },
67
+ { timestamp: 4, rate: (0, core_1.d)(4) },
68
+ { timestamp: 5, rate: (0, core_1.d)(5) }
69
+ ]);
70
+ }));
71
+ test('save and read limited data', () => __awaiter(void 0, void 0, void 0, function* () {
72
+ const { sut } = fixtures;
73
+ const pricing = core_1.Storage.createObject('pricing', {
74
+ timestamp: 'number',
75
+ rate: 'decimal'
76
+ });
77
+ yield sut.save(pricing, [
78
+ { timestamp: 1, rate: (0, core_1.d)(1) },
79
+ { timestamp: 2, rate: (0, core_1.d)(2) },
80
+ { timestamp: 3, rate: (0, core_1.d)(3) },
81
+ { timestamp: 4, rate: (0, core_1.d)(4) },
82
+ { timestamp: 5, rate: (0, core_1.d)(5) }
83
+ ]);
84
+ const set = yield sut.query(pricing, { limit: 3 });
85
+ expect(set).toEqual([
86
+ { timestamp: 1, rate: (0, core_1.d)(1) },
87
+ { timestamp: 2, rate: (0, core_1.d)(2) },
88
+ { timestamp: 3, rate: (0, core_1.d)(3) }
89
+ ]);
90
+ }));
91
+ test('save and read desc ordered data', () => __awaiter(void 0, void 0, void 0, function* () {
92
+ const { sut } = fixtures;
93
+ const pricing = core_1.Storage.createObject('pricing', {
94
+ timestamp: 'number',
95
+ rate: 'decimal'
96
+ });
97
+ yield sut.save(pricing, [
98
+ { timestamp: 1, rate: (0, core_1.d)(1) },
99
+ { timestamp: 2, rate: (0, core_1.d)(2) },
100
+ { timestamp: 3, rate: (0, core_1.d)(3) },
101
+ { timestamp: 4, rate: (0, core_1.d)(4) },
102
+ { timestamp: 5, rate: (0, core_1.d)(5) }
103
+ ]);
104
+ const set = yield sut.query(pricing, { orderBy: 'DESC' });
105
+ expect(set).toEqual([
106
+ { timestamp: 5, rate: (0, core_1.d)(5) },
107
+ { timestamp: 4, rate: (0, core_1.d)(4) },
108
+ { timestamp: 3, rate: (0, core_1.d)(3) },
109
+ { timestamp: 2, rate: (0, core_1.d)(2) },
110
+ { timestamp: 1, rate: (0, core_1.d)(1) }
111
+ ]);
112
+ }));
113
+ test('save and read filtered eq data', () => __awaiter(void 0, void 0, void 0, function* () {
114
+ const { sut } = fixtures;
115
+ const pricing = core_1.Storage.createObject('pricing', {
116
+ timestamp: 'number',
117
+ rate: 'decimal'
118
+ });
119
+ yield sut.save(pricing, [
120
+ { timestamp: 1, rate: (0, core_1.d)(1) },
121
+ { timestamp: 2, rate: (0, core_1.d)(2) },
122
+ { timestamp: 3, rate: (0, core_1.d)(3) },
123
+ { timestamp: 4, rate: (0, core_1.d)(4) },
124
+ { timestamp: 5, rate: (0, core_1.d)(5) }
125
+ ]);
126
+ const set = yield sut.query(pricing, {
127
+ where: {
128
+ timestamp: (0, core_1.eq)(4)
129
+ }
130
+ });
131
+ expect(set).toEqual([{ timestamp: 4, rate: (0, core_1.d)(4) }]);
132
+ }));
133
+ test('save and read filtered lt data', () => __awaiter(void 0, void 0, void 0, function* () {
134
+ const { sut } = fixtures;
135
+ const pricing = core_1.Storage.createObject('pricing', {
136
+ timestamp: 'number',
137
+ rate: 'decimal'
138
+ });
139
+ yield sut.save(pricing, [
140
+ { timestamp: 1, rate: (0, core_1.d)(1) },
141
+ { timestamp: 2, rate: (0, core_1.d)(2) },
142
+ { timestamp: 3, rate: (0, core_1.d)(3) },
143
+ { timestamp: 4, rate: (0, core_1.d)(4) },
144
+ { timestamp: 5, rate: (0, core_1.d)(5) }
145
+ ]);
146
+ const set = yield sut.query(pricing, {
147
+ where: {
148
+ timestamp: (0, core_1.lt)(3)
149
+ }
150
+ });
151
+ expect(set).toEqual([
152
+ { timestamp: 1, rate: (0, core_1.d)(1) },
153
+ { timestamp: 2, rate: (0, core_1.d)(2) }
154
+ ]);
155
+ }));
156
+ test('save and read filtered gt data', () => __awaiter(void 0, void 0, void 0, function* () {
157
+ const { sut } = fixtures;
158
+ const pricing = core_1.Storage.createObject('pricing', {
159
+ timestamp: 'number',
160
+ rate: 'decimal'
161
+ });
162
+ yield sut.save(pricing, [
163
+ { timestamp: 1, rate: (0, core_1.d)(1) },
164
+ { timestamp: 2, rate: (0, core_1.d)(2) },
165
+ { timestamp: 3, rate: (0, core_1.d)(3) },
166
+ { timestamp: 4, rate: (0, core_1.d)(4) },
167
+ { timestamp: 5, rate: (0, core_1.d)(5) }
168
+ ]);
169
+ const set = yield sut.query(pricing, {
170
+ where: {
171
+ timestamp: (0, core_1.gt)(3)
172
+ }
173
+ });
174
+ expect(set).toEqual([
175
+ { timestamp: 4, rate: (0, core_1.d)(4) },
176
+ { timestamp: 5, rate: (0, core_1.d)(5) }
177
+ ]);
178
+ }));
179
+ });
180
+ function getFixtures() {
181
+ return __awaiter(this, void 0, void 0, function* () {
182
+ const { get } = yield (0, core_1.makeTestModule)([
183
+ {
184
+ provide: 'storage',
185
+ useValue: new sqlite_storage_1.SQLiteStorage('test.db')
186
+ }
187
+ ]);
188
+ const sut = get('storage');
189
+ return {
190
+ sut,
191
+ object: core_1.Storage.createObject('test', {
192
+ timestamp: 'number',
193
+ price: 'decimal',
194
+ quantity: 'number',
195
+ id: 'string'
196
+ }),
197
+ dispose() {
198
+ if ((0, fs_1.existsSync)(sut.filename)) {
199
+ (0, fs_1.unlinkSync)(sut.filename);
200
+ }
201
+ }
202
+ };
203
+ });
204
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quantform/sqlite",
3
- "version": "0.6.7",
3
+ "version": "0.7.0-beta.5",
4
4
  "license": "MIT",
5
5
  "author": "Mateusz Majchrzak",
6
6
  "description": "Node.js library for building systematic trading strategies in reactive way.",
@@ -18,10 +18,10 @@
18
18
  },
19
19
  "devDependencies": {
20
20
  "@types/better-sqlite3": "^7.6.0",
21
- "@quantform/core": "0.6.7"
21
+ "@quantform/core": "0.7.0-beta.5"
22
22
  },
23
23
  "peerDependencies": {
24
- "@quantform/core": "0.6.7"
24
+ "@quantform/core": "0.7.0-beta.5"
25
25
  },
26
26
  "scripts": {
27
27
  "build": "tsc && tsc-alias",
package/src/index.ts CHANGED
@@ -1 +1,8 @@
1
+ import { SQLiteStorageFactory } from '@lib/sqlite-storage';
2
+ import { storage } from '@quantform/core';
3
+
1
4
  export * from '@lib/sqlite-storage';
5
+
6
+ export function sqlite(directory?: string) {
7
+ return storage(new SQLiteStorageFactory(directory));
8
+ }