-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Offline mode and document paging, also move to ESM
- Loading branch information
Showing
13 changed files
with
462 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
node_modules/ | ||
.env | ||
dev.ts | ||
dev/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as DetaOrm from '../src/index' // import * as DetaOrm from 'deta-base-orm' | ||
|
||
const run = async () => { | ||
|
||
// ✨ Define a schema for the kittens | ||
type KittenSchema = { | ||
name: string, | ||
cuteness?: number | ||
} | ||
|
||
const schema = new DetaOrm.Schema<KittenSchema>({ | ||
name: 'string', | ||
cuteness: { | ||
type: 'number', | ||
default: 0 | ||
} | ||
}) | ||
|
||
// 🛌 Create our Kitten base | ||
const Kitten = new DetaOrm.Base('Kitten', schema, { | ||
offline: true // Enable offline mode | ||
}) | ||
|
||
// 🐱 Create a new kitten | ||
const line = Kitten.create({ | ||
name: 'Line', | ||
cuteness: 8 | ||
}) | ||
|
||
// 🔖 Access the kittens name | ||
console.log(line.name) // 'Line' | ||
|
||
// 💾 Save our kitten to the Deta Base | ||
await line.save() | ||
|
||
// 🔍 Find all kittens | ||
const kittens = await Kitten.find() | ||
console.log(kittens) // [{name: 'Line', cuteness: 8}, ...] | ||
|
||
// 🔑 Find a kitten by its key | ||
const sameKitten = await Kitten.findByKey(line.key) | ||
console.log(sameKitten) // {name: 'Line', cuteness: 8} | ||
|
||
// 🧵 Find a kitten by its cuteness | ||
const cutest = await Kitten.find({ cuteness: 8 }) | ||
console.log(cutest) // [{name: 'Line', cuteness: 8}] | ||
|
||
// 💘 Delete a kitten | ||
await sameKitten?.delete() | ||
} | ||
|
||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Low, Adapter } from 'lowdb'; | ||
import lodash from 'lodash'; | ||
declare class LowDash<T> extends Low<T> { | ||
chain?: lodash.CollectionChain<any> & lodash.FunctionChain<any> & lodash.ObjectChain<any> & lodash.PrimitiveChain<any> & lodash.StringChain; | ||
constructor(adapter: Adapter<T>); | ||
} | ||
export declare class OfflineDB { | ||
db: LowDash<any>; | ||
constructor(storagePath?: string); | ||
static create(storagePath?: string): Promise<OfflineDB>; | ||
init(): Promise<void>; | ||
put(data: any): void; | ||
list(): any[] | undefined; | ||
get(key: string): any; | ||
fetch(query: any): any; | ||
delete(key: string): void; | ||
} | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
import { join } from 'path'; | ||
import { Low, JSONFile } from 'lowdb'; | ||
import lodash from 'lodash'; | ||
class LowDash extends Low { | ||
constructor(adapter) { | ||
super(adapter); | ||
} | ||
} | ||
export class OfflineDB { | ||
constructor(storagePath = '') { | ||
// const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
const file = join(storagePath, 'db.json'); | ||
const adapter = new JSONFile(file); | ||
this.db = new LowDash(adapter); | ||
} | ||
static create(storagePath = '') { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const db = new OfflineDB(storagePath); | ||
yield db.init(); | ||
return db; | ||
}); | ||
} | ||
init() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.db.read(); | ||
if (this.db.data === null) { | ||
this.db.data = []; | ||
} | ||
this.db.chain = lodash.chain(this.db.data); | ||
}); | ||
} | ||
put(data) { | ||
this.db.data.push(data); | ||
this.db.write(); | ||
} | ||
list() { | ||
var _a; | ||
return (_a = this.db.chain) === null || _a === void 0 ? void 0 : _a.value(); | ||
} | ||
get(key) { | ||
var _a; | ||
return (_a = this.db.chain) === null || _a === void 0 ? void 0 : _a.find({ key }).value(); | ||
} | ||
fetch(query) { | ||
var _a; | ||
return (_a = this.db.chain) === null || _a === void 0 ? void 0 : _a.find(query).value(); | ||
} | ||
delete(key) { | ||
var _a; | ||
(_a = this.db.chain) === null || _a === void 0 ? void 0 : _a.remove({ key }).value(); | ||
this.db.write(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.