Skip to content

Commit ca34608

Browse files
committed
♻️ Move back to cjs and use lowdb v1
1 parent 34fba22 commit ca34608

File tree

16 files changed

+170
-198
lines changed

16 files changed

+170
-198
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ Then import it into your project:
3636
import * as DetaOrm from 'deta-base-orm'
3737
```
3838

39+
Or:
40+
41+
```ts
42+
const DetaOrm = require('deta-base-orm')
43+
```
44+
3945
### Example
4046

4147
Here's a quick example to help you get started!

example/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as DetaOrm from '../lib/index'
1+
// eslint-disable-next-line @typescript-eslint/no-var-requires
2+
const DetaOrm = require('../lib/index') // const DetaOrm = require('deta-base-orm')
23

34
const run = async () => {
45
// Create the model

lib/Base.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use strict";
12
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
23
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
34
return new (P || (P = Promise))(function (resolve, reject) {
@@ -7,28 +8,30 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
78
step((generator = generator.apply(thisArg, _arguments || [])).next());
89
});
910
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
exports.Base = void 0;
1013
/* eslint-disable valid-jsdoc */
11-
import { Deta } from 'deta';
12-
import { generateKey } from './random';
13-
import { OfflineDB } from './Offline';
14-
import { Schema } from './Schema';
15-
import { Document } from './Document';
14+
const deta_1 = require("deta");
15+
const random_1 = require("./random");
16+
const Offline_1 = require("./Offline");
17+
const Schema_1 = require("./Schema");
18+
const Document_1 = require("./Document");
1619
/**
1720
* Create and interact with a Deta Base
1821
*/
19-
export class Base {
22+
class Base {
2023
/**
2124
* Create a new Base with the provided name, schema and options
2225
* @param {string} name Name of the Base
2326
* @param {BaseOptions} opts Options object
2427
*/
2528
constructor(name, schema, opts) {
2629
this._baseName = name;
27-
if (schema instanceof Schema) {
30+
if (schema instanceof Schema_1.Schema) {
2831
this._baseSchema = schema;
2932
}
3033
else {
31-
this._baseSchema = new Schema(schema);
34+
this._baseSchema = new Schema_1.Schema(schema);
3235
}
3336
// Parse options
3437
const ascending = (opts === null || opts === void 0 ? void 0 : opts.descending) !== true;
@@ -37,7 +40,7 @@ export class Base {
3740
const storagePath = (opts === null || opts === void 0 ? void 0 : opts.storagePath) || '.deta-base-orm';
3841
this._opts = { ascending, timestamp, offline, storagePath };
3942
if (this._opts.offline) {
40-
this._db = new OfflineDB(this._opts.storagePath, this._baseName);
43+
this._db = new Offline_1.OfflineDB(this._opts.storagePath, this._baseName);
4144
return;
4245
}
4346
// Reuse Deta Base
@@ -47,7 +50,7 @@ export class Base {
4750
return;
4851
}
4952
// Create new Deta Base instance
50-
const deta = Deta();
53+
const deta = deta_1.Deta();
5154
this._db = deta.Base(name);
5255
}
5356
/**
@@ -57,12 +60,12 @@ export class Base {
5760
*/
5861
create(rawData) {
5962
// Set configs
60-
Document._baseName = this._baseName;
61-
Document._db = this._db;
62-
Document._opts = this._opts;
63+
Document_1.Document._baseName = this._baseName;
64+
Document_1.Document._db = this._db;
65+
Document_1.Document._opts = this._opts;
6366
// Auto generate a key if is missing
6467
if (!rawData.key) {
65-
rawData.key = generateKey(this._opts.ascending);
68+
rawData.key = random_1.generateKey(this._opts.ascending);
6669
}
6770
// Add timestamp to document
6871
if (this._opts.timestamp && rawData.createdAt === undefined) {
@@ -76,7 +79,7 @@ export class Base {
7679
}
7780
const data = validated.result;
7881
// Create new document with data
79-
return new Document(data, this._baseSchema);
82+
return new Document_1.Document(data, this._baseSchema);
8083
}
8184
/**
8285
* Helper function to create and immediately save a new document
@@ -258,3 +261,4 @@ export class Base {
258261
});
259262
}
260263
}
264+
exports.Base = Base;

lib/Document.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use strict";
12
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
23
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
34
return new (P || (P = Promise))(function (resolve, reject) {
@@ -7,15 +8,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
78
step((generator = generator.apply(thisArg, _arguments || [])).next());
89
});
910
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
exports.Document = void 0;
1013
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
1114
/* eslint-disable valid-jsdoc */
12-
import { Deta } from 'deta';
13-
import { generateKey } from './random';
15+
const deta_1 = require("deta");
16+
const random_1 = require("./random");
1417
/**
1518
* Represents a Document with all of its data and methods
1619
* @internal
1720
*/
18-
export class Document {
21+
class Document {
1922
/**
2023
* Create a new Document instance with the provided data.
2124
*
@@ -24,7 +27,7 @@ export class Document {
2427
*/
2528
constructor(data, _baseSchema) {
2629
Object.assign(this, data);
27-
this.key = this.key || generateKey(Document._opts.ascending);
30+
this.key = this.key || random_1.generateKey(Document._opts.ascending);
2831
// Add timestamp to document
2932
if (Document._opts.timestamp && this.createdAt === undefined) {
3033
this.createdAt = Date.now();
@@ -65,7 +68,7 @@ export class Document {
6568
if (!baseName)
6669
throw new Error(`Can't populate this path because it doesn't have a baseName defined`);
6770
// Create new Deta Base instance
68-
const deta = Deta();
71+
const deta = deta_1.Deta();
6972
const db = deta.Base(baseName);
7073
const key = this[path];
7174
const rawData = yield db.get(key);
@@ -104,3 +107,4 @@ export class Document {
104107
});
105108
}
106109
}
110+
exports.Document = Document;

lib/Offline.d.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { Low, Adapter } from 'lowdb';
2-
import lodash from 'lodash';
3-
declare class LowDash<T> extends Low<T> {
4-
chain?: lodash.CollectionChain<any> & lodash.FunctionChain<any> & lodash.ObjectChain<any> & lodash.PrimitiveChain<any> & lodash.StringChain;
5-
constructor(adapter: Adapter<T>);
1+
import low from 'lowdb';
2+
interface DbSchema {
3+
[k: string]: [any];
64
}
75
/**
86
* Mocks the Deta Base SDK with a local database
@@ -12,8 +10,11 @@ declare class LowDash<T> extends Low<T> {
1210
* Note: Not all methods are implemented, only the ones need by deta-base-orm
1311
*/
1412
export declare class OfflineDB {
15-
db: LowDash<any>;
13+
_db?: low.LowdbAsync<DbSchema>;
14+
_adapter: any;
1615
_didLoad: boolean;
16+
_filePath: string;
17+
_baseName: string;
1718
constructor(storagePath?: string, fileName?: string);
1819
/**
1920
* Create a new instance of the OfflineDB with the file loaded automatically
@@ -40,7 +41,7 @@ export declare class OfflineDB {
4041
* Lists all items in the database
4142
* @returns All items in the database
4243
*/
43-
list(): Promise<any[] | undefined>;
44+
list(): Promise<DbSchema | undefined>;
4445
/**
4546
* Implements the get API
4647
* @param key The key of the item to be retrieved

lib/Offline.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use strict";
12
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
23
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
34
return new (P || (P = Promise))(function (resolve, reject) {
@@ -7,36 +8,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
78
step((generator = generator.apply(thisArg, _arguments || [])).next());
89
});
910
};
11+
var __importDefault = (this && this.__importDefault) || function (mod) {
12+
return (mod && mod.__esModule) ? mod : { "default": mod };
13+
};
14+
Object.defineProperty(exports, "__esModule", { value: true });
15+
exports.OfflineDB = void 0;
1016
/* eslint-disable valid-jsdoc */
1117
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
12-
import { join } from 'path';
13-
import fs from 'fs';
14-
import { Low, JSONFile } from 'lowdb';
15-
import lodash from 'lodash';
16-
class LowDash extends Low {
17-
constructor(adapter) {
18-
super(adapter);
19-
}
20-
}
18+
const path_1 = require("path");
19+
const fs_1 = __importDefault(require("fs"));
20+
const lowdb_1 = __importDefault(require("lowdb"));
21+
const FileAsync_1 = __importDefault(require("lowdb/adapters/FileAsync"));
22+
const lodash_1 = require("lodash");
2123
/**
2224
* Mocks the Deta Base SDK with a local database
2325
*
2426
* It uses a local JSON file for each Base.
2527
*
2628
* Note: Not all methods are implemented, only the ones need by deta-base-orm
2729
*/
28-
export class OfflineDB {
30+
class OfflineDB {
2931
constructor(storagePath = '.deta-base-orm', fileName = 'base') {
30-
const folderExists = fs.existsSync(storagePath);
32+
const folderExists = fs_1.default.existsSync(storagePath);
3133
if (!folderExists) {
3234
if (storagePath !== '.deta-base-orm') {
3335
throw new Error(`The folder "${storagePath}" does not exist, please create it manually`);
3436
}
35-
fs.mkdirSync(storagePath);
37+
fs_1.default.mkdirSync(storagePath);
3638
}
37-
const file = join(storagePath, `${fileName}.json`);
38-
const adapter = new JSONFile(file);
39-
this.db = new LowDash(adapter);
39+
const file = path_1.join(storagePath, `${fileName}.json`);
40+
this._baseName = fileName;
41+
this._filePath = file;
42+
this._adapter = new FileAsync_1.default(file);
4043
this._didLoad = false;
4144
}
4245
/**
@@ -57,11 +60,11 @@ export class OfflineDB {
5760
*/
5861
init() {
5962
return __awaiter(this, void 0, void 0, function* () {
60-
yield this.db.read();
61-
if (this.db.data === null) {
62-
this.db.data = [];
63+
this._db = yield lowdb_1.default(this._adapter);
64+
yield this._db.read();
65+
if (this._db.get(this._baseName).value() === undefined) {
66+
yield this._db.set(this._baseName, []).write();
6367
}
64-
this.db.chain = lodash.chain(this.db.data);
6568
this._didLoad = true;
6669
});
6770
}
@@ -81,10 +84,10 @@ export class OfflineDB {
8184
* @returns The inserted data
8285
*/
8386
put(data) {
87+
var _a;
8488
return __awaiter(this, void 0, void 0, function* () {
8589
yield this.check();
86-
this.db.data.push(data);
87-
yield this.db.write();
90+
yield ((_a = this._db) === null || _a === void 0 ? void 0 : _a.get(this._baseName).push(data).write());
8891
return data;
8992
});
9093
}
@@ -96,7 +99,7 @@ export class OfflineDB {
9699
var _a;
97100
return __awaiter(this, void 0, void 0, function* () {
98101
yield this.check();
99-
return (_a = this.db.chain) === null || _a === void 0 ? void 0 : _a.value();
102+
return (_a = this._db) === null || _a === void 0 ? void 0 : _a.value();
100103
});
101104
}
102105
/**
@@ -108,7 +111,7 @@ export class OfflineDB {
108111
var _a;
109112
return __awaiter(this, void 0, void 0, function* () {
110113
yield this.check();
111-
return ((_a = this.db.chain) === null || _a === void 0 ? void 0 : _a.find({ key }).value()) || null;
114+
return ((_a = this._db) === null || _a === void 0 ? void 0 : _a.get(this._baseName).find({ key }).value()) || null;
112115
});
113116
}
114117
/**
@@ -124,8 +127,8 @@ export class OfflineDB {
124127
let results = [];
125128
query.forEach((query) => {
126129
var _a;
127-
const items = (_a = this.db.chain) === null || _a === void 0 ? void 0 : _a.filter(query).value();
128-
results = lodash.unionBy(results, items, 'key');
130+
const items = (_a = this._db) === null || _a === void 0 ? void 0 : _a.get(this._baseName).filter(query).value();
131+
results = lodash_1.unionBy(results, items, 'key');
129132
});
130133
if (!results) {
131134
return { items: [], count: 0, last: undefined };
@@ -143,8 +146,7 @@ export class OfflineDB {
143146
var _a;
144147
return __awaiter(this, void 0, void 0, function* () {
145148
yield this.check();
146-
(_a = this.db.chain) === null || _a === void 0 ? void 0 : _a.remove({ key }).value();
147-
yield this.db.write();
149+
yield ((_a = this._db) === null || _a === void 0 ? void 0 : _a.get(this._baseName).remove({ key }).write());
148150
return null;
149151
});
150152
}
@@ -158,9 +160,9 @@ export class OfflineDB {
158160
var _a;
159161
return __awaiter(this, void 0, void 0, function* () {
160162
yield this.check();
161-
(_a = this.db.chain) === null || _a === void 0 ? void 0 : _a.update(key, updates).value();
162-
yield this.db.write();
163+
yield ((_a = this._db) === null || _a === void 0 ? void 0 : _a.get(this._baseName).update(key, updates).write());
163164
return null;
164165
});
165166
}
166167
}
168+
exports.OfflineDB = OfflineDB;

lib/Schema.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { Base } from './Base';
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Schema = void 0;
4+
const Base_1 = require("./Base");
25
/**
36
* Create a schema for your Base
47
*/
5-
export class Schema {
8+
class Schema {
69
constructor(schema) {
710
this.schema = this.parse(schema);
811
}
@@ -20,7 +23,7 @@ export class Schema {
2023
};
2124
// Parse Base shorthand notation
2225
}
23-
else if (value instanceof Base) {
26+
else if (value instanceof Base_1.Base) {
2427
parsedSchema[key] = {
2528
__end: true,
2629
type: 'base',
@@ -127,3 +130,4 @@ export class Schema {
127130
return { errors, result: result };
128131
}
129132
}
133+
exports.Schema = Schema;

lib/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
exports.Schema = exports.Base = void 0;
17
/* eslint-disable valid-jsdoc */
2-
import dotenv from 'dotenv';
3-
import { Base } from './Base';
4-
import { Schema } from './Schema';
5-
dotenv.config();
6-
export { Base, Schema };
8+
const dotenv_1 = __importDefault(require("dotenv"));
9+
const Base_1 = require("./Base");
10+
Object.defineProperty(exports, "Base", { enumerable: true, get: function () { return Base_1.Base; } });
11+
const Schema_1 = require("./Schema");
12+
Object.defineProperty(exports, "Schema", { enumerable: true, get: function () { return Schema_1.Schema; } });
13+
dotenv_1.default.config();

0 commit comments

Comments
 (0)