import * as low from "lowdb"; import * as lowfp from "lowdb/lib/fp"; import * as FileSync from "lowdb/adapters/FileSync"; import * as FileAsync from "lowdb/adapters/FileAsync"; import * as LocalStorage from "lowdb/adapters/LocalStorage"; const adapterSync: low.AdapterSync<{}> = new FileSync("db.json"); const adapterAsync: low.AdapterAsync<{}> = new FileAsync("db.json"); const db = low(adapterSync); const write: DbSchema = db.defaults({ posts: [] }).write(); const result: Post[] = db .get("posts") .push({ title: "hello", views: 123 }) .value(); const teste = db.get("user").value(); const post: Post | undefined = db .get("posts") .find({ id: 123 }) .value(); // $ExpectError const postAssertWithUndefined: Post = db .get("posts") .find({ id: 123 }) .value(); // $ExpectError const postAssertWithUndefined2: Post = db .get("posts") .find({ id: 123 }) .write(); low(adapterAsync).then(dbAsync => { const writeAction: Promise = dbAsync .get("posts") .push({ title: "async hello" }) .last() .assign({ id: Date.now().toString() }) .write(); const test: Promise = dbAsync .get("posts") .find({}) .write(); const test2: Post | undefined = dbAsync .get("posts") .find({}) .value(); const tuple: Promise<[boolean, number] | undefined> = dbAsync .get("posts") .first() .get("tuple") .write(); }); async () => { const adapterSync: low.AdapterSync = new FileSync("db.json", { defaultValue: { posts: [{ name: "baz" }] } }); const adapterAsync: low.AdapterAsync = new FileAsync( "db.json", { defaultValue: { posts: [{ name: "baz" }] } } ); const dbSync = low(adapterSync); const dbAsync = await low(adapterAsync); const dbAssertTypeSync: low.Lowdb = dbSync; const dbAssertTypeAsync: low.Lowdb< ExampleSchema, typeof adapterAsync > = dbAsync; const xSync: ExampleSchema = dbSync .defaults({ posts: [{ name: "baz" }] }) .write(); const xAsync: Promise = dbAsync .defaults({ posts: [{ name: "baz" }] }) .write(); const result: Promise = dbAsync .get("posts") .push({ name: "hello" }) .write(); const resultSync: ExampleSchema["posts"] = dbSync .get("posts") .push({ name: "hello" }) .write(); const dbPromise = low(adapterAsync); const db = await dbPromise; const nested: OtherSchema["nested"] = db.get("nested").value(); const result2: Promise = db .get("nested") .get("x") .write(); }; declare const lodashChain: _.LoDashExplicitWrapper; // let's also ensure we didn't break lodash.chain through extension const weDidNotBreakLodash: ExampleSchema["posts"] = lodashChain .get("posts") .sortBy("") .value(); // tests for lowdb/lib/fp: () => { const adapterLS = new LocalStorage("test.json"); const dbFP = lowfp(adapterLS); // Get posts const postsFP: low.FpReturn> = dbFP( "posts" ); // replace posts with a new array resulting from concat // and persist database const write: Post[] = postsFP.write( concat({ title: "lowdb is awesome", views: random(0, 5) }) ); // Find post by id const post: Post = postsFP(find({ id: 1 })); // Find top 5 fives posts const popular: Post[] = postsFP([ sortBy("views") as PostsAction, take(5) as PostsAction ]); const filtered: Post[] = dbFP("posts")(filter({ published: true })); const writeAction: Post[] = dbFP("posts").write(concat({ id: "123" })); const writeAction2: string = dbFP(["user", "name"]).write(set("typicode")); async () => { const adapterAsync = new FileAsync("test.json"); const dbAsyncPromise = lowfp(adapterAsync); const dbAsync = await dbAsyncPromise; const postsWithDefault: low.FpReturn< Post[], low.AdapterAsync > = dbAsync("posts", [{ title: "baz" }] as Post[]); const func: Promise = postsWithDefault.write(post => [ ...post, { title: "another" } ]); }; type PostsAction = (posts: Post[]) => Post[]; }; declare function find(a: B): (arr: A[]) => A; declare function filter(a: B): (arr: A[]) => A[]; declare function random(a: number, b: number): number; declare function concat(a: any): (arr: A) => A; declare function sortBy(a: any): (arr: A) => A; declare function take(a: A): (arr: A) => A; declare function set(a: A): (val: A) => A; interface DbSchema { posts: Array; user: { name: string; }; } interface Post { title?: string; views?: number; id?: number; published?: boolean | undefined; tuple?: [boolean, number]; } interface ExampleSchema { posts: Array<{ name: string; }>; } interface OtherSchema extends ExampleSchema { nested: { x: { y: number; }; }; }