Debian Codegraph @

types/tests/db-test.ts

raw
1 import DbCollection from "miragejs/db-collection";
2 import { Server } from "miragejs/server";
3 import { Registry } from "miragejs";
4 import { ModelDefinition } from "miragejs/-types";
5
6 const server: Server = new Server();
7
8 server.db.loadData({
9 movies: [
10 { title: "Interstellar" },
11 { title: "Inception" },
12 { title: "Dunkirk" },
13 ],
14 });
15
16 const myDb = server.db; // $ExpectType Db
17
18 interface Movie {
19 title: string;
20 }
21
22 myDb.createCollection("movies", [{ title: "Interstellar" }]); // $ExpectType void
23 myDb.dump(); // $ExpectType void
24 myDb.emptyData(); // $ExpectType void
25 myDb.loadData({}); // $ExpectType void
26
27 const dbCollection = new DbCollection("movies", [{ title: "Dunkirk" }]);
28
29 myDb.users.find(1); // $ExpectType any
30 myDb.users.find([1, 2]); // $ExpectType any
31 myDb.users.findBy({ name: "Link" }); // $ExpectType any
32 myDb.users.where({ name: "Link" }); // $ExpectType any
33 myDb.users.insert({}); // $ExpectType any
34 myDb.users.insert([]); // $ExpectType any
35 myDb.users.firstOrCreate({ name: "Link" }); // $ExpectType any
36 myDb.users.remove(); // $ExpectType void
37 myDb.users.remove(1); // $ExpectType void
38 myDb.users.remove({ name: "Zelda" }); // $ExpectType void
39 myDb.users.update({ name: "Ganon" }); // $ExpectType any
40 myDb.users.update(1, { name: "Young Link" }); // $ExpectType any
41 myDb.users.update({ name: "Link" }, { name: "Epona" }); // $ExpectType any
42
43 type TestModels = {
44 movie: ModelDefinition<Movie>;
45 };
46
47 type TestRegistry = Registry<TestModels, {}>;
48
49 const testServer = new Server<TestRegistry>();
50
51 // There's a problem with dtslint that we can't specify different results for different
52 // versions of typescript. The result here will be either `ModelInstance<{ title: string; }> | null`
53 // or `Instantiate<TestRegistry, "movie"> | null`, depending on TS version.
54 testServer.schema.findBy("movie", (instance) => instance.title.length > 0);