@primate/core 0.6.1 → 0.6.2

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.
@@ -1,6 +1,6 @@
1
1
  import E from "#db/errors";
2
2
  import bundle from "#db/migrate/bundle";
3
- import store from "#db/migrate/store";
3
+ import toMigrationStore from "#db/migrate/store";
4
4
  import c from "@rcompat/cli/color";
5
5
  import print from "@rcompat/cli/print";
6
6
  import is_cancel from "@rcompat/cli/prompts/is-cancel";
@@ -47,7 +47,7 @@ export default async function create_migration(desc) {
47
47
  throw E.store_directory_empty();
48
48
  let next = 1;
49
49
  if (await migrations.exists()) {
50
- const Migration = await store();
50
+ const Migration = await toMigrationStore();
51
51
  await Migration.table.create();
52
52
  const last = await Migration.find({ limit: 1, sort: { id: "desc" } });
53
53
  const last_id = last.length === 0 ? 0 : last[0].id;
@@ -62,17 +62,22 @@ export default async function create_migration(desc) {
62
62
  ${store_files.map((_, i) => `s${i}`).join(",\n ")}\n];`;
63
63
  const stores = await bundle(entry);
64
64
  const diffs = [];
65
- const migration_stores = stores.filter(s => s.migrate);
65
+ const seen = new Set();
66
+ const migration_stores = [];
66
67
  const names = new Map();
67
68
  for (let i = 0; i < store_files.length; i++) {
68
69
  const file = store_files[i];
69
70
  const store = stores[i];
70
71
  if (!store.migrate)
71
72
  continue;
73
+ if (seen.has(store.id))
74
+ continue;
75
+ seen.add(store.id);
76
+ migration_stores.push(store);
72
77
  const existing = names.get(store.name) ?? [];
73
78
  names.set(store.name, [...existing, file.name]);
74
79
  }
75
- const conflicts = [...names.entries()].filter(([, files]) => files.length > 1);
80
+ const conflicts = [...names].filter(([, files]) => files.length > 1);
76
81
  if (conflicts.length > 0) {
77
82
  const messages = conflicts
78
83
  .map(([name, files]) => `table "${name}" claimed by: ${files.join(", ")}`)
@@ -80,6 +80,7 @@ type WithRelations<Base, Relations extends Dict<Relation>, W extends With<Relati
80
80
  type Init<S extends StoreInput, R extends Dict<Relation>> = {
81
81
  name: string;
82
82
  db: DB;
83
+ id?: symbol;
83
84
  schema: S;
84
85
  relations?: R;
85
86
  migrate?: boolean;
@@ -104,6 +105,7 @@ export declare class Store<T extends StoreInput, R extends Dict<Relation> = Empt
104
105
  get schema(): Dict<Storable<keyof import("pema").DataType, unknown>>;
105
106
  get type(): StoreType<ExtractSchema<T>>;
106
107
  get migrate(): boolean;
108
+ get id(): symbol;
107
109
  get name(): string;
108
110
  get pk(): PK;
109
111
  get db(): DB;
@@ -64,6 +64,7 @@ const DATE_OPS = ["$before", "$after", "$ne"];
64
64
  * CRUD/query API.
65
65
  */
66
66
  export class Store {
67
+ #input;
67
68
  #schema;
68
69
  #type;
69
70
  #types;
@@ -75,9 +76,10 @@ export class Store {
75
76
  #fks;
76
77
  #relations;
77
78
  #migrate;
79
+ #id;
78
80
  constructor(init) {
81
+ const { name, db, migrate, id } = init;
79
82
  const { pk, generate_pk, fks, schema } = parse(init.schema);
80
- const { name, db, migrate } = init;
81
83
  if (name === undefined)
82
84
  throw E.store_name_required();
83
85
  assert.string(name);
@@ -87,6 +89,8 @@ export class Store {
87
89
  assert.dict(schema);
88
90
  assert.maybe.boolean(migrate);
89
91
  assert.maybe.dict(init.extend);
92
+ assert.maybe.symbol(id);
93
+ this.#id = id ?? Symbol();
90
94
  this.#schema = schema;
91
95
  this.#type = new StoreType(schema, pk);
92
96
  this.#types = Object.fromEntries(Object.entries(schema).map(([key, value]) => [key, value.datatype]));
@@ -95,6 +99,7 @@ export class Store {
95
99
  this.#pk = pk;
96
100
  this.#generate_pk = generate_pk;
97
101
  this.#fks = fks;
102
+ this.#input = init.schema;
98
103
  this.#relations = init.relations ?? {};
99
104
  this.#migrate = migrate ?? true;
100
105
  for (const relation of Object.keys(this.#relations)) {
@@ -144,6 +149,9 @@ export class Store {
144
149
  get migrate() {
145
150
  return this.#migrate;
146
151
  }
152
+ get id() {
153
+ return this.#id;
154
+ }
147
155
  get name() {
148
156
  return this.#name;
149
157
  }
@@ -528,9 +536,10 @@ export class Store {
528
536
  return new Store({
529
537
  name: this.name,
530
538
  db: this.db,
531
- schema: this.#schema,
539
+ schema: this.#input,
532
540
  relations: this.#relations,
533
- migrate: false,
541
+ migrate: this.#migrate,
542
+ id: this.#id,
534
543
  extend: extensor(this),
535
544
  });
536
545
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primate/core",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "The universal web framework",
5
5
  "homepage": "https://primate.run",
6
6
  "bugs": "https://github.com/primate-run/primate/issues",